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 * We allocate spaces for messages (msgs array)
19 * this number of elements at a time.
24 static struct swit switches[] = {
44 { "rmmproc program", 0 },
56 static char maildir[BUFSIZ];
66 static void opnfolds (struct st_fold *, int);
67 static void clsfolds (struct st_fold *, int);
68 static void remove_files (int, char **);
69 static int m_file (char *, struct st_fold *, int, int, int);
73 main (int argc, char **argv)
75 int linkf = 0, preserve = 0, filep = 0;
76 int foldp = 0, isdf = 0, unlink_msgs = 0;
77 int i, msgnum, nummsgs, maxmsgs;
78 char *cp, *folder = NULL, buf[BUFSIZ];
79 char **argp, **arguments, **msgs;
80 char *filevec[NFOLDERS + 2];
81 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
82 struct st_fold folders[NFOLDERS + 1];
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
93 arguments = getarguments (invo_name, argc, argv, 1);
97 * Allocate the initial space to record message
98 * names, ranges, and sequences.
102 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
103 adios (NULL, "unable to allocate storage");
108 while ((cp = *argp++)) {
110 switch (smatch (++cp, switches)) {
112 ambigsw (cp, switches);
115 adios (NULL, "-%s unknown\n", cp);
118 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
120 print_help (buf, switches, 1);
123 print_version(invo_name);
149 adios (NULL, "only one source folder at a time!");
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
152 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
153 *cp != '@' ? TFOLDER : TSUBCWF);
156 if (filep > NFOLDERS)
157 adios (NULL, "only %d files allowed!", NFOLDERS);
159 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
162 if (filep > NFOLDERS)
163 adios (NULL, "only %d files allowed!", NFOLDERS);
164 if (!(cp = *argp++) || *cp == '-')
165 adios (NULL, "missing argument to %s", argp[-2]);
166 files[filep++] = path (cp, TFILE);
170 if (!(rmmproc = *argp++) || *rmmproc == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
178 if (*cp == '+' || *cp == '@') {
179 if (foldp > NFOLDERS)
180 adios (NULL, "only %d folders allowed!", NFOLDERS);
181 folders[foldp++].f_name =
182 path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
185 * Check if we need to allocate more space
186 * for message names, ranges, and sequences.
188 if (nummsgs >= maxmsgs) {
190 if (!(msgs = (char **) realloc (msgs,
191 (size_t) (maxmsgs * sizeof(*msgs)))))
192 adios (NULL, "unable to reallocate msgs storage");
194 msgs[nummsgs++] = cp;
198 if (!context_find ("path"))
199 free (path ("./", TFOLDER));
201 adios (NULL, "no folder specified");
204 if (!nummsgs && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
209 * We are refiling a file to the folders
212 if (folder || nummsgs)
213 adios (NULL, "use -file or some messages, not both");
214 opnfolds (folders, foldp);
215 for (i = 0; i < filep; i++)
216 if (m_file (files[i], folders, foldp, preserve, 0))
218 /* If -nolink, then "remove" files */
220 remove_files (filep, filevec);
225 msgs[nummsgs++] = "cur";
227 folder = getfolder (1);
228 strncpy (maildir, m_maildir (folder), sizeof(maildir));
230 if (chdir (maildir) == NOTOK)
231 adios (maildir, "unable to change directory to");
233 /* read source folder and create message structure */
234 if (!(mp = folder_read (folder)))
235 adios (NULL, "unable to read folder %s", folder);
237 /* check for empty folder */
239 adios (NULL, "no messages in %s", folder);
241 /* parse the message range/sequence/name and set SELECTED */
242 for (msgnum = 0; msgnum < nummsgs; msgnum++)
243 if (!m_convert (mp, msgs[msgnum]))
245 seq_setprev (mp); /* set the previous-sequence */
247 /* create folder structures for each destination folder */
248 opnfolds (folders, foldp);
250 /* Link all the selected messages into destination folders.
252 * This causes the add hook to be run for messages that are
253 * linked into another folder. The refile hook is run for
254 * messages that are moved to another folder.
256 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
257 if (is_selected (mp, msgnum)) {
258 cp = getcpy (m_name (msgnum));
259 if (m_file (cp, folders, foldp, preserve, !linkf))
266 * This is a hack. If we are using an external rmmproc,
267 * then save the current folder to the context file,
268 * so the external rmmproc will remove files from the correct
269 * directory. This should be moved to folder_delmsgs().
272 context_replace (pfolder, folder);
277 /* If -nolink, then "remove" messages from source folder.
279 * Note that folder_delmsgs does not call the delete hook
280 * because the message has already been handled above.
283 folder_delmsgs (mp, unlink_msgs, 1);
286 clsfolds (folders, foldp);
288 if (mp->hghsel != mp->curmsg
289 && (mp->numsel != mp->nummsg || linkf))
290 seq_setcur (mp, mp->hghsel);
291 seq_save (mp); /* synchronize message sequences */
293 context_replace (pfolder, folder); /* update current folder */
294 context_save (); /* save the context file */
295 folder_free (mp); /* free folder structure */
301 * Read all the destination folders and
302 * create folder structures for all of them.
306 opnfolds (struct st_fold *folders, int nfolders)
309 char nmaildir[BUFSIZ];
310 register struct st_fold *fp, *ep;
311 register struct msgs *mp;
314 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
315 chdir (m_maildir (""));
316 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
318 if (stat (nmaildir, &st) == NOTOK) {
320 adios (nmaildir, "error on folder");
321 cp = concat ("Create folder \"", nmaildir, "\"? ", NULL);
325 if (!makedir (nmaildir))
326 adios (NULL, "unable to create folder %s", nmaildir);
329 if (chdir (nmaildir) == NOTOK)
330 adios (nmaildir, "unable to change directory to");
331 if (!(mp = folder_read (fp->f_name)))
332 adios (NULL, "unable to read folder %s", fp->f_name);
343 * Set the Previous-Sequence and then sychronize the
344 * sequence file, for each destination folder.
348 clsfolds (struct st_fold *folders, int nfolders)
350 register struct st_fold *fp, *ep;
351 register struct msgs *mp;
353 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
362 * If you have a "rmmproc" defined, we called that
363 * to remove all the specified files. If "rmmproc"
364 * is not defined, then just unlink the files.
368 remove_files (int filep, char **files)
373 /* If rmmproc is defined, we use that */
375 vec = files++; /* vec[0] = filevec[0] */
376 files[filep] = NULL; /* NULL terminate list */
379 vec[0] = r1bindex (rmmproc, '/');
380 execvp (rmmproc, vec);
381 adios (rmmproc, "unable to exec");
384 /* Else just unlink the files */
385 files++; /* advance past filevec[0] */
386 for (i = 0; i < filep; i++) {
387 if (unlink (files[i]) == NOTOK)
388 admonish (files[i], "unable to unlink");
394 * Link (or copy) the message into each of
395 * the destination folders.
399 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve, int refile)
402 struct st_fold *fp, *ep;
404 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
405 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve, nfolders == 1 && refile)) == -1)