3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
18 static struct swit switches[] = {
38 { "rmmproc program", 0 },
48 static char maildir[BUFSIZ];
58 static void opnfolds (struct st_fold *, int);
59 static void clsfolds (struct st_fold *, int);
60 static void remove_files (int, char **);
61 static int m_file (char *, struct st_fold *, int, int, int);
65 main (int argc, char **argv)
67 int linkf = 0, preserve = 0, filep = 0;
68 int foldp = 0, isdf = 0, unlink_msgs = 0;
70 char *cp, *folder = NULL, buf[BUFSIZ];
71 char **argp, **arguments;
72 char *filevec[NFOLDERS + 2];
73 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
74 struct st_fold folders[NFOLDERS + 1];
75 struct msgs_array msgs = { 0, 0, NULL };
79 setlocale(LC_ALL, "");
81 invo_name = r1bindex (argv[0], '/');
83 /* read user profile/context */
86 arguments = getarguments (invo_name, argc, argv, 1);
92 while ((cp = *argp++)) {
94 switch (smatch (++cp, switches)) {
96 ambigsw (cp, switches);
99 adios (NULL, "-%s unknown\n", cp);
102 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
104 print_help (buf, switches, 1);
107 print_version(invo_name);
133 adios (NULL, "only one source folder at a time!");
134 if (!(cp = *argp++) || *cp == '-')
135 adios (NULL, "missing argument to %s", argp[-2]);
136 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
137 *cp != '@' ? TFOLDER : TSUBCWF);
140 if (filep > NFOLDERS)
141 adios (NULL, "only %d files allowed!", NFOLDERS);
143 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
146 if (filep > NFOLDERS)
147 adios (NULL, "only %d files allowed!", NFOLDERS);
148 if (!(cp = *argp++) || *cp == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
150 files[filep++] = path (cp, TFILE);
154 if (!(rmmproc = *argp++) || *rmmproc == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
162 if (*cp == '+' || *cp == '@') {
163 if (foldp > NFOLDERS)
164 adios (NULL, "only %d folders allowed!", NFOLDERS);
165 folders[foldp++].f_name =
168 app_msgarg(&msgs, cp);
171 if (!context_find ("path"))
172 free (path ("./", TFOLDER));
174 adios (NULL, "no folder specified");
177 if (!msgs.size && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
182 * We are refiling a file to the folders
185 if (folder || msgs.size)
186 adios (NULL, "use -file or some messages, not both");
187 opnfolds (folders, foldp);
188 for (i = 0; i < filep; i++)
189 if (m_file (files[i], folders, foldp, preserve, 0))
191 /* If -nolink, then "remove" files */
193 remove_files (filep, filevec);
198 app_msgarg(&msgs, "cur");
200 folder = getfolder (1);
201 strncpy (maildir, m_maildir (folder), sizeof(maildir));
203 if (chdir (maildir) == NOTOK)
204 adios (maildir, "unable to change directory to");
206 /* read source folder and create message structure */
207 if (!(mp = folder_read (folder)))
208 adios (NULL, "unable to read folder %s", folder);
210 /* check for empty folder */
212 adios (NULL, "no messages in %s", folder);
214 /* parse the message range/sequence/name and set SELECTED */
215 for (msgnum = 0; msgnum < msgs.size; msgnum++)
216 if (!m_convert (mp, msgs.msgs[msgnum]))
218 seq_setprev (mp); /* set the previous-sequence */
220 /* create folder structures for each destination folder */
221 opnfolds (folders, foldp);
223 /* Link all the selected messages into destination folders.
225 * This causes the add hook to be run for messages that are
226 * linked into another folder. The refile hook is run for
227 * messages that are moved to another folder.
229 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
230 if (is_selected (mp, msgnum)) {
231 cp = getcpy (m_name (msgnum));
232 if (m_file (cp, folders, foldp, preserve, !linkf))
239 * This is a hack. If we are using an external rmmproc,
240 * then save the current folder to the context file,
241 * so the external rmmproc will remove files from the correct
242 * directory. This should be moved to folder_delmsgs().
245 context_replace (pfolder, folder);
250 /* If -nolink, then "remove" messages from source folder.
252 * Note that folder_delmsgs does not call the delete hook
253 * because the message has already been handled above.
256 folder_delmsgs (mp, unlink_msgs, 1);
259 clsfolds (folders, foldp);
261 if (mp->hghsel != mp->curmsg
262 && (mp->numsel != mp->nummsg || linkf))
263 seq_setcur (mp, mp->hghsel);
264 seq_save (mp); /* synchronize message sequences */
266 context_replace (pfolder, folder); /* update current folder */
267 context_save (); /* save the context file */
268 folder_free (mp); /* free folder structure */
275 * Read all the destination folders and
276 * create folder structures for all of them.
280 opnfolds (struct st_fold *folders, int nfolders)
282 char nmaildir[BUFSIZ];
283 register struct st_fold *fp, *ep;
284 register struct msgs *mp;
286 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
287 chdir (m_maildir (""));
288 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
290 create_folder (nmaildir, 0, done);
292 if (chdir (nmaildir) == NOTOK)
293 adios (nmaildir, "unable to change directory to");
294 if (!(mp = folder_read (fp->f_name)))
295 adios (NULL, "unable to read folder %s", fp->f_name);
306 * Set the Previous-Sequence and then sychronize the
307 * sequence file, for each destination folder.
311 clsfolds (struct st_fold *folders, int nfolders)
313 register struct st_fold *fp, *ep;
314 register struct msgs *mp;
316 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
325 * If you have a "rmmproc" defined, we called that
326 * to remove all the specified files. If "rmmproc"
327 * is not defined, then just unlink the files.
331 remove_files (int filep, char **files)
336 /* If rmmproc is defined, we use that */
338 vec = files++; /* vec[0] = filevec[0] */
339 files[filep] = NULL; /* NULL terminate list */
342 vec[0] = r1bindex (rmmproc, '/');
343 execvp (rmmproc, vec);
344 adios (rmmproc, "unable to exec");
347 /* Else just unlink the files */
348 files++; /* advance past filevec[0] */
349 for (i = 0; i < filep; i++) {
350 if (unlink (files[i]) == NOTOK)
351 admonish (files[i], "unable to unlink");
357 * Link (or copy) the message into each of
358 * the destination folders.
362 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve, int refile)
365 struct st_fold *fp, *ep;
367 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
368 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve, nfolders == 1 && refile, maildir)) == -1)