3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
16 static struct swit switches[] = {
36 { "rmmproc program", 0 },
46 static char maildir[BUFSIZ];
56 static void opnfolds (struct st_fold *, int);
57 static void clsfolds (struct st_fold *, int);
58 static void remove_files (int, char **);
59 static int m_file (char *, struct st_fold *, int, int, int);
63 main (int argc, char **argv)
65 int linkf = 0, preserve = 0, filep = 0;
66 int foldp = 0, isdf = 0, unlink_msgs = 0;
68 char *cp, *folder = NULL, buf[BUFSIZ];
69 char **argp, **arguments;
70 char *filevec[NFOLDERS + 2];
71 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
72 struct st_fold folders[NFOLDERS + 1];
73 struct msgs_array msgs = { 0, 0, NULL };
77 setlocale(LC_ALL, "");
79 invo_name = r1bindex (argv[0], '/');
81 /* read user profile/context */
84 arguments = getarguments (invo_name, argc, argv, 1);
90 while ((cp = *argp++)) {
92 switch (smatch (++cp, switches)) {
94 ambigsw (cp, switches);
97 adios (NULL, "-%s unknown\n", cp);
100 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
102 print_help (buf, switches, 1);
105 print_version(invo_name);
131 adios (NULL, "only one source folder at a time!");
132 if (!(cp = *argp++) || *cp == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
134 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
135 *cp != '@' ? TFOLDER : TSUBCWF);
138 if (filep > NFOLDERS)
139 adios (NULL, "only %d files allowed!", NFOLDERS);
141 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
144 if (filep > NFOLDERS)
145 adios (NULL, "only %d files allowed!", NFOLDERS);
146 if (!(cp = *argp++) || *cp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
148 files[filep++] = path (cp, TFILE);
152 if (!(rmmproc = *argp++) || *rmmproc == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
160 if (*cp == '+' || *cp == '@') {
161 if (foldp > NFOLDERS)
162 adios (NULL, "only %d folders allowed!", NFOLDERS);
163 folders[foldp++].f_name =
166 app_msgarg(&msgs, cp);
169 if (!context_find ("path"))
170 free (path ("./", TFOLDER));
172 adios (NULL, "no folder specified");
175 if (!msgs.size && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
180 * We are refiling a file to the folders
183 if (folder || msgs.size)
184 adios (NULL, "use -file or some messages, not both");
185 opnfolds (folders, foldp);
186 for (i = 0; i < filep; i++)
187 if (m_file (files[i], folders, foldp, preserve, 0))
189 /* If -nolink, then "remove" files */
191 remove_files (filep, filevec);
196 app_msgarg(&msgs, "cur");
198 folder = getfolder (1);
199 strncpy (maildir, m_maildir (folder), sizeof(maildir));
201 if (chdir (maildir) == NOTOK)
202 adios (maildir, "unable to change directory to");
204 /* read source folder and create message structure */
205 if (!(mp = folder_read (folder)))
206 adios (NULL, "unable to read folder %s", folder);
208 /* check for empty folder */
210 adios (NULL, "no messages in %s", folder);
212 /* parse the message range/sequence/name and set SELECTED */
213 for (msgnum = 0; msgnum < msgs.size; msgnum++)
214 if (!m_convert (mp, msgs.msgs[msgnum]))
216 seq_setprev (mp); /* set the previous-sequence */
218 /* create folder structures for each destination folder */
219 opnfolds (folders, foldp);
221 /* Link all the selected messages into destination folders.
223 * This causes the add hook to be run for messages that are
224 * linked into another folder. The refile hook is run for
225 * messages that are moved to another folder.
227 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
228 if (is_selected (mp, msgnum)) {
229 cp = getcpy (m_name (msgnum));
230 if (m_file (cp, folders, foldp, preserve, !linkf))
237 * This is a hack. If we are using an external rmmproc,
238 * then save the current folder to the context file,
239 * so the external rmmproc will remove files from the correct
240 * directory. This should be moved to folder_delmsgs().
243 context_replace (pfolder, folder);
248 /* If -nolink, then "remove" messages from source folder.
250 * Note that folder_delmsgs does not call the delete hook
251 * because the message has already been handled above.
254 folder_delmsgs (mp, unlink_msgs, 1);
257 clsfolds (folders, foldp);
259 if (mp->hghsel != mp->curmsg
260 && (mp->numsel != mp->nummsg || linkf))
261 seq_setcur (mp, mp->hghsel);
262 seq_save (mp); /* synchronize message sequences */
264 context_replace (pfolder, folder); /* update current folder */
265 context_save (); /* save the context file */
266 folder_free (mp); /* free folder structure */
273 * Read all the destination folders and
274 * create folder structures for all of them.
278 opnfolds (struct st_fold *folders, int nfolders)
280 char nmaildir[BUFSIZ];
281 register struct st_fold *fp, *ep;
282 register struct msgs *mp;
284 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
285 chdir (m_maildir (""));
286 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
288 create_folder (nmaildir, 0, done);
290 if (chdir (nmaildir) == NOTOK)
291 adios (nmaildir, "unable to change directory to");
292 if (!(mp = folder_read (fp->f_name)))
293 adios (NULL, "unable to read folder %s", fp->f_name);
304 * Set the Previous-Sequence and then sychronize the
305 * sequence file, for each destination folder.
309 clsfolds (struct st_fold *folders, int nfolders)
311 register struct st_fold *fp, *ep;
312 register struct msgs *mp;
314 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
323 * If you have a "rmmproc" defined, we called that
324 * to remove all the specified files. If "rmmproc"
325 * is not defined, then just unlink the files.
329 remove_files (int filep, char **files)
334 /* If rmmproc is defined, we use that */
336 vec = files++; /* vec[0] = filevec[0] */
337 files[filep] = NULL; /* NULL terminate list */
340 vec[0] = r1bindex (rmmproc, '/');
341 execvp (rmmproc, vec);
342 adios (rmmproc, "unable to exec");
345 /* Else just unlink the files */
346 files++; /* advance past filevec[0] */
347 for (i = 0; i < filep; i++) {
348 if (unlink (files[i]) == NOTOK)
349 admonish (files[i], "unable to unlink");
355 * Link (or copy) the message into each of
356 * the destination folders.
360 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve, int refile)
363 struct st_fold *fp, *ep;
365 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
366 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve, nfolders == 1 && refile, maildir)) == -1)