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 = path(*cp == '+' || *cp == '@' ?
137 if (filep > NFOLDERS)
138 adios(NULL, "only %d files allowed!",
140 if (!(cp = *argp++) || *cp == '-')
141 adios(NULL, "missing argument to %s",
143 files[filep++] = path(cp, TFILE);
147 if (!(rmmproc = *argp++) || *rmmproc == '-')
148 adios(NULL, "missing argument to %s",
156 if (*cp == '+' || *cp == '@') {
157 if (foldp > NFOLDERS)
158 adios(NULL, "only %d folders allowed!",
160 folders[foldp++].f_name =
163 app_msgarg(&msgs, cp);
166 if (!context_find("path"))
167 free(path("./", TFOLDER));
169 adios(NULL, "no folder specified");
172 if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
177 ** We are refiling a file to the folders
180 if (folder || msgs.size)
181 adios(NULL, "use -file or some messages, not both");
182 opnfolds(folders, foldp);
183 for (i = 0; i < filep; i++)
184 if (m_file(files[i], folders, foldp, preserve, 0))
186 /* If -nolink, then "remove" files */
188 remove_files(filep, filevec);
193 app_msgarg(&msgs, "cur");
195 folder = getfolder(1);
196 strncpy(maildir, m_maildir(folder), sizeof(maildir));
198 if (chdir(maildir) == NOTOK)
199 adios(maildir, "unable to change directory to");
201 /* read source folder and create message structure */
202 if (!(mp = folder_read(folder)))
203 adios(NULL, "unable to read folder %s", folder);
205 /* check for empty folder */
207 adios(NULL, "no messages in %s", folder);
209 /* parse the message range/sequence/name and set SELECTED */
210 for (msgnum = 0; msgnum < msgs.size; msgnum++)
211 if (!m_convert(mp, msgs.msgs[msgnum]))
213 seq_setprev(mp); /* set the previous-sequence */
215 /* create folder structures for each destination folder */
216 opnfolds(folders, foldp);
218 /* Link all the selected messages into destination folders.
220 ** This causes the add hook to be run for messages that are
221 ** linked into another folder. The refile hook is run for
222 ** messages that are moved to another folder.
224 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
225 if (is_selected(mp, msgnum)) {
226 cp = getcpy(m_name(msgnum));
227 if (m_file(cp, folders, foldp, preserve, !linkf))
234 ** This is a hack. If we are using an external rmmproc,
235 ** then save the current folder to the context file,
236 ** so the external rmmproc will remove files from the correct
237 ** directory. This should be moved to folder_delmsgs().
240 context_replace(pfolder, folder);
246 ** If -nolink, then "remove" messages from source folder.
248 ** Note that folder_delmsgs does not call the delete hook
249 ** because the message has already been handled above.
252 folder_delmsgs(mp, unlink_msgs, 1);
255 clsfolds(folders, foldp);
257 if (mp->hghsel != mp->curmsg &&
258 (mp->numsel != mp->nummsg || linkf))
259 seq_setcur(mp, mp->hghsel);
260 seq_save(mp); /* synchronize message sequences */
262 context_replace(pfolder, folder); /* update current folder */
263 context_save(); /* save the context file */
264 folder_free(mp); /* free folder structure */
271 ** Read all the destination folders and
272 ** create folder structures for all of them.
276 opnfolds(struct st_fold *folders, int nfolders)
278 char nmaildir[BUFSIZ];
279 register struct st_fold *fp, *ep;
280 register struct msgs *mp;
282 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
283 chdir(m_maildir(""));
284 strncpy(nmaildir, m_maildir(fp->f_name), sizeof(nmaildir));
286 create_folder(nmaildir, 0, done);
288 if (chdir(nmaildir) == NOTOK)
289 adios(nmaildir, "unable to change directory to");
290 if (!(mp = folder_read(fp->f_name)))
291 adios(NULL, "unable to read folder %s", fp->f_name);
302 ** Set the Previous-Sequence and then sychronize the
303 ** sequence file, for each destination folder.
307 clsfolds(struct st_fold *folders, int nfolders)
309 register struct st_fold *fp, *ep;
310 register struct msgs *mp;
312 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
321 ** If you have a "rmmproc" defined, we called that
322 ** to remove all the specified files. If "rmmproc"
323 ** is not defined, then just unlink the files.
327 remove_files(int filep, char **files)
332 /* If rmmproc is defined, we use that */
334 vec = files++; /* vec[0] = filevec[0] */
335 files[filep] = NULL; /* NULL terminate list */
338 vec[0] = mhbasename(rmmproc);
339 execvp(rmmproc, vec);
340 adios(rmmproc, "unable to exec");
343 /* Else just unlink the files */
344 files++; /* advance past filevec[0] */
345 for (i = 0; i < filep; i++) {
346 if (unlink(files[i]) == NOTOK)
347 admonish(files[i], "unable to unlink");
353 ** Link (or copy) the message into each of
354 ** the destination folders.
358 m_file(char *msgfile, struct st_fold *folders, int nfolders, int preserve,
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,
366 preserve, nfolders == 1 && refile, maildir))