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[] = {
33 { "rmmproc program", 0 },
43 static char maildir[BUFSIZ];
53 static void opnfolds(struct st_fold *, int);
54 static void clsfolds(struct st_fold *, int);
55 static void remove_files(int, char **);
56 static int m_file(char *, struct st_fold *, int, int, int);
60 main(int argc, char **argv)
62 int linkf = 0, preserve = 0, filep = 0;
63 int foldp = 0, unlink_msgs = 0;
65 char *cp, *folder = NULL, buf[BUFSIZ];
66 char **argp, **arguments;
67 char *filevec[NFOLDERS + 2];
68 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
69 struct st_fold folders[NFOLDERS + 1];
70 struct msgs_array msgs = { 0, 0, NULL };
74 setlocale(LC_ALL, "");
76 invo_name = mhbasename(argv[0]);
78 /* read user profile/context */
81 arguments = getarguments(invo_name, argc, argv, 1);
87 while ((cp = *argp++)) {
89 switch (smatch(++cp, switches)) {
91 ambigsw(cp, switches);
94 adios(NULL, "-%s unknown\n", cp);
97 snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
98 print_help(buf, switches, 1);
101 print_version(invo_name);
127 adios(NULL, "only one source folder at a time!");
128 if (!(cp = *argp++) || *cp == '-')
129 adios(NULL, "missing argument to %s",
131 folder = getcpy(expandfol(cp));
134 if (filep > NFOLDERS)
135 adios(NULL, "only %d files allowed!",
137 if (!(cp = *argp++) || *cp == '-')
138 adios(NULL, "missing argument to %s",
140 files[filep++] = getcpy(expanddir(cp));
144 if (!(rmmproc = *argp++) || *rmmproc == '-')
145 adios(NULL, "missing argument to %s",
153 if (*cp == '+' || *cp == '@') {
154 if (foldp > NFOLDERS)
155 adios(NULL, "only %d folders allowed!",
157 folders[foldp++].f_name = getcpy(expandfol(cp));
159 app_msgarg(&msgs, cp);
163 adios(NULL, "no folder specified");
166 if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
171 ** We are refiling a file to the folders
174 if (folder || msgs.size)
175 adios(NULL, "use -file or some messages, not both");
176 opnfolds(folders, foldp);
177 for (i = 0; i < filep; i++)
178 if (m_file(files[i], folders, foldp, preserve, 0))
180 /* If -nolink, then "remove" files */
182 remove_files(filep, filevec);
187 app_msgarg(&msgs, seq_cur);
189 folder = getcurfol();
190 strncpy(maildir, toabsdir(folder), sizeof(maildir));
192 if (chdir(maildir) == NOTOK)
193 adios(maildir, "unable to change directory to");
195 /* read source folder and create message structure */
196 if (!(mp = folder_read(folder)))
197 adios(NULL, "unable to read folder %s", folder);
199 /* check for empty folder */
201 adios(NULL, "no messages in %s", folder);
203 /* parse the message range/sequence/name and set SELECTED */
204 for (msgnum = 0; msgnum < msgs.size; msgnum++)
205 if (!m_convert(mp, msgs.msgs[msgnum]))
207 seq_setprev(mp); /* set the previous-sequence */
209 /* create folder structures for each destination folder */
210 opnfolds(folders, foldp);
212 /* Link all the selected messages into destination folders.
214 ** This causes the add hook to be run for messages that are
215 ** linked into another folder. The refile hook is run for
216 ** messages that are moved to another folder.
218 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
219 if (is_selected(mp, msgnum)) {
220 cp = getcpy(m_name(msgnum));
221 if (m_file(cp, folders, foldp, preserve, !linkf))
228 ** This is a hack. If we are using an external rmmproc,
229 ** then save the current folder to the context file,
230 ** so the external rmmproc will remove files from the correct
231 ** directory. This should be moved to folder_delmsgs().
234 context_replace(curfolder, folder);
240 ** If -nolink, then "remove" messages from source folder.
242 ** Note that folder_delmsgs does not call the delete hook
243 ** because the message has already been handled above.
246 folder_delmsgs(mp, unlink_msgs, 1);
249 clsfolds(folders, foldp);
251 if (mp->hghsel != mp->curmsg &&
252 (mp->numsel != mp->nummsg || linkf))
253 seq_setcur(mp, mp->hghsel);
254 seq_save(mp); /* synchronize message sequences */
256 context_replace(curfolder, folder); /* update current folder */
257 context_save(); /* save the context file */
258 folder_free(mp); /* free folder structure */
265 ** Read all the destination folders and
266 ** create folder structures for all of them.
270 opnfolds(struct st_fold *folders, int nfolders)
272 char nmaildir[BUFSIZ];
273 register struct st_fold *fp, *ep;
274 register struct msgs *mp;
276 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
277 chdir(toabsdir("+"));
278 strncpy(nmaildir, toabsdir(fp->f_name), sizeof(nmaildir));
280 create_folder(nmaildir, 0, done);
282 if (chdir(nmaildir) == NOTOK)
283 adios(nmaildir, "unable to change directory to");
284 if (!(mp = folder_read(fp->f_name)))
285 adios(NULL, "unable to read folder %s", fp->f_name);
296 ** Set the Previous-Sequence and then sychronize the
297 ** sequence file, for each destination folder.
301 clsfolds(struct st_fold *folders, int nfolders)
303 register struct st_fold *fp, *ep;
304 register struct msgs *mp;
306 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
315 ** If you have a "rmmproc" defined, we called that
316 ** to remove all the specified files. If "rmmproc"
317 ** is not defined, then just unlink the files.
321 remove_files(int filep, char **files)
326 /* If rmmproc is defined, we use that */
328 vec = files++; /* vec[0] = filevec[0] */
329 files[filep] = NULL; /* NULL terminate list */
332 vec[0] = mhbasename(rmmproc);
333 execvp(rmmproc, vec);
334 adios(rmmproc, "unable to exec");
337 /* Else just unlink the files */
338 files++; /* advance past filevec[0] */
339 for (i = 0; i < filep; i++) {
340 if (unlink(files[i]) == NOTOK)
341 admonish(files[i], "unable to unlink");
347 ** Link (or copy) the message into each of
348 ** the destination folders.
352 m_file(char *msgfile, struct st_fold *folders, int nfolders, int preserve,
356 struct st_fold *fp, *ep;
358 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
359 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
360 preserve, nfolders == 1 && refile, maildir))