2 * refile.c -- move or link message(s) from a source folder
3 * -- into one or more destination folders
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
15 static struct swit switches[] = {
35 { "rmmproc program", 0 },
45 static char maildir[BUFSIZ];
55 static void opnfolds (struct st_fold *, int);
56 static void clsfolds (struct st_fold *, int);
57 static void remove_files (int, char **);
58 static int m_file (char *, struct st_fold *, int, int, int);
62 main (int argc, char **argv)
64 int linkf = 0, preserve = 0, filep = 0;
65 int foldp = 0, isdf = 0, unlink_msgs = 0;
67 char *cp, *folder = NULL, buf[BUFSIZ];
68 char **argp, **arguments;
69 char *filevec[NFOLDERS + 2];
70 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
71 struct st_fold folders[NFOLDERS + 1];
72 struct msgs_array msgs = { 0, 0, NULL };
76 setlocale(LC_ALL, "");
78 invo_name = r1bindex (argv[0], '/');
80 /* read user profile/context */
83 arguments = getarguments (invo_name, argc, argv, 1);
89 while ((cp = *argp++)) {
91 switch (smatch (++cp, switches)) {
93 ambigsw (cp, switches);
96 adios (NULL, "-%s unknown\n", cp);
99 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
101 print_help (buf, switches, 1);
104 print_version(invo_name);
130 adios (NULL, "only one source folder at a time!");
131 if (!(cp = *argp++) || *cp == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
134 *cp != '@' ? TFOLDER : TSUBCWF);
137 if (filep > NFOLDERS)
138 adios (NULL, "only %d files allowed!", NFOLDERS);
140 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
143 if (filep > NFOLDERS)
144 adios (NULL, "only %d files allowed!", NFOLDERS);
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
147 files[filep++] = path (cp, TFILE);
151 if (!(rmmproc = *argp++) || *rmmproc == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
159 if (*cp == '+' || *cp == '@') {
160 if (foldp > NFOLDERS)
161 adios (NULL, "only %d folders allowed!", NFOLDERS);
162 folders[foldp++].f_name =
165 app_msgarg(&msgs, cp);
168 if (!context_find ("path"))
169 free (path ("./", TFOLDER));
171 adios (NULL, "no folder specified");
174 if (!msgs.size && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
179 * We are refiling a file to the folders
182 if (folder || msgs.size)
183 adios (NULL, "use -file or some messages, not both");
184 opnfolds (folders, foldp);
185 for (i = 0; i < filep; i++)
186 if (m_file (files[i], folders, foldp, preserve, 0))
188 /* If -nolink, then "remove" files */
190 remove_files (filep, filevec);
195 app_msgarg(&msgs, "cur");
197 folder = getfolder (1);
198 strncpy (maildir, m_maildir (folder), sizeof(maildir));
200 if (chdir (maildir) == NOTOK)
201 adios (maildir, "unable to change directory to");
203 /* read source folder and create message structure */
204 if (!(mp = folder_read (folder)))
205 adios (NULL, "unable to read folder %s", folder);
207 /* check for empty folder */
209 adios (NULL, "no messages in %s", folder);
211 /* parse the message range/sequence/name and set SELECTED */
212 for (msgnum = 0; msgnum < msgs.size; msgnum++)
213 if (!m_convert (mp, msgs.msgs[msgnum]))
215 seq_setprev (mp); /* set the previous-sequence */
217 /* create folder structures for each destination folder */
218 opnfolds (folders, foldp);
220 /* Link all the selected messages into destination folders.
222 * This causes the add hook to be run for messages that are
223 * linked into another folder. The refile hook is run for
224 * messages that are moved to another folder.
226 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
227 if (is_selected (mp, msgnum)) {
228 cp = getcpy (m_name (msgnum));
229 if (m_file (cp, folders, foldp, preserve, !linkf))
236 * This is a hack. If we are using an external rmmproc,
237 * then save the current folder to the context file,
238 * so the external rmmproc will remove files from the correct
239 * directory. This should be moved to folder_delmsgs().
242 context_replace (pfolder, folder);
247 /* If -nolink, then "remove" messages from source folder.
249 * Note that folder_delmsgs does not call the delete hook
250 * because the message has already been handled above.
253 folder_delmsgs (mp, unlink_msgs, 1);
256 clsfolds (folders, foldp);
258 if (mp->hghsel != mp->curmsg
259 && (mp->numsel != mp->nummsg || linkf))
260 seq_setcur (mp, mp->hghsel);
261 seq_save (mp); /* synchronize message sequences */
263 context_replace (pfolder, folder); /* update current folder */
264 context_save (); /* save the context file */
265 folder_free (mp); /* free folder structure */
272 * Read all the destination folders and
273 * create folder structures for all of them.
277 opnfolds (struct st_fold *folders, int nfolders)
279 char nmaildir[BUFSIZ];
280 register struct st_fold *fp, *ep;
281 register struct msgs *mp;
283 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
284 chdir (m_maildir (""));
285 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
287 create_folder (nmaildir, 0, done);
289 if (chdir (nmaildir) == NOTOK)
290 adios (nmaildir, "unable to change directory to");
291 if (!(mp = folder_read (fp->f_name)))
292 adios (NULL, "unable to read folder %s", fp->f_name);
303 * Set the Previous-Sequence and then sychronize the
304 * sequence file, for each destination folder.
308 clsfolds (struct st_fold *folders, int nfolders)
310 register struct st_fold *fp, *ep;
311 register struct msgs *mp;
313 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
322 * If you have a "rmmproc" defined, we called that
323 * to remove all the specified files. If "rmmproc"
324 * is not defined, then just unlink the files.
328 remove_files (int filep, char **files)
333 /* If rmmproc is defined, we use that */
335 vec = files++; /* vec[0] = filevec[0] */
336 files[filep] = NULL; /* NULL terminate list */
339 vec[0] = r1bindex (rmmproc, '/');
340 execvp (rmmproc, vec);
341 adios (rmmproc, "unable to exec");
344 /* Else just unlink the files */
345 files++; /* advance past filevec[0] */
346 for (i = 0; i < filep; i++) {
347 if (unlink (files[i]) == NOTOK)
348 admonish (files[i], "unable to unlink");
354 * Link (or copy) the message into each of
355 * the destination folders.
359 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve, int refile)
362 struct st_fold *fp, *ep;
364 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
365 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve, nfolders == 1 && refile, maildir)) == -1)