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 = pluspath(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++] = path(cp, TFILE);
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 =
160 app_msgarg(&msgs, cp);
164 adios(NULL, "no folder specified");
167 if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
172 ** We are refiling a file to the folders
175 if (folder || msgs.size)
176 adios(NULL, "use -file or some messages, not both");
177 opnfolds(folders, foldp);
178 for (i = 0; i < filep; i++)
179 if (m_file(files[i], folders, foldp, preserve, 0))
181 /* If -nolink, then "remove" files */
183 remove_files(filep, filevec);
188 app_msgarg(&msgs, "cur");
190 folder = getfolder(FCUR);
191 strncpy(maildir, m_maildir(folder), sizeof(maildir));
193 if (chdir(maildir) == NOTOK)
194 adios(maildir, "unable to change directory to");
196 /* read source folder and create message structure */
197 if (!(mp = folder_read(folder)))
198 adios(NULL, "unable to read folder %s", folder);
200 /* check for empty folder */
202 adios(NULL, "no messages in %s", folder);
204 /* parse the message range/sequence/name and set SELECTED */
205 for (msgnum = 0; msgnum < msgs.size; msgnum++)
206 if (!m_convert(mp, msgs.msgs[msgnum]))
208 seq_setprev(mp); /* set the previous-sequence */
210 /* create folder structures for each destination folder */
211 opnfolds(folders, foldp);
213 /* Link all the selected messages into destination folders.
215 ** This causes the add hook to be run for messages that are
216 ** linked into another folder. The refile hook is run for
217 ** messages that are moved to another folder.
219 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
220 if (is_selected(mp, msgnum)) {
221 cp = getcpy(m_name(msgnum));
222 if (m_file(cp, folders, foldp, preserve, !linkf))
229 ** This is a hack. If we are using an external rmmproc,
230 ** then save the current folder to the context file,
231 ** so the external rmmproc will remove files from the correct
232 ** directory. This should be moved to folder_delmsgs().
235 context_replace(pfolder, folder);
241 ** If -nolink, then "remove" messages from source folder.
243 ** Note that folder_delmsgs does not call the delete hook
244 ** because the message has already been handled above.
247 folder_delmsgs(mp, unlink_msgs, 1);
250 clsfolds(folders, foldp);
252 if (mp->hghsel != mp->curmsg &&
253 (mp->numsel != mp->nummsg || linkf))
254 seq_setcur(mp, mp->hghsel);
255 seq_save(mp); /* synchronize message sequences */
257 context_replace(pfolder, folder); /* update current folder */
258 context_save(); /* save the context file */
259 folder_free(mp); /* free folder structure */
266 ** Read all the destination folders and
267 ** create folder structures for all of them.
271 opnfolds(struct st_fold *folders, int nfolders)
273 char nmaildir[BUFSIZ];
274 register struct st_fold *fp, *ep;
275 register struct msgs *mp;
277 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
278 chdir(m_maildir(""));
279 strncpy(nmaildir, m_maildir(fp->f_name), sizeof(nmaildir));
281 create_folder(nmaildir, 0, done);
283 if (chdir(nmaildir) == NOTOK)
284 adios(nmaildir, "unable to change directory to");
285 if (!(mp = folder_read(fp->f_name)))
286 adios(NULL, "unable to read folder %s", fp->f_name);
297 ** Set the Previous-Sequence and then sychronize the
298 ** sequence file, for each destination folder.
302 clsfolds(struct st_fold *folders, int nfolders)
304 register struct st_fold *fp, *ep;
305 register struct msgs *mp;
307 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
316 ** If you have a "rmmproc" defined, we called that
317 ** to remove all the specified files. If "rmmproc"
318 ** is not defined, then just unlink the files.
322 remove_files(int filep, char **files)
327 /* If rmmproc is defined, we use that */
329 vec = files++; /* vec[0] = filevec[0] */
330 files[filep] = NULL; /* NULL terminate list */
333 vec[0] = mhbasename(rmmproc);
334 execvp(rmmproc, vec);
335 adios(rmmproc, "unable to exec");
338 /* Else just unlink the files */
339 files++; /* advance past filevec[0] */
340 for (i = 0; i < filep; i++) {
341 if (unlink(files[i]) == NOTOK)
342 admonish(files[i], "unable to unlink");
348 ** Link (or copy) the message into each of
349 ** the destination folders.
353 m_file(char *msgfile, struct st_fold *folders, int nfolders, int preserve,
357 struct st_fold *fp, *ep;
359 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
360 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
361 preserve, nfolders == 1 && refile, maildir))