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);
163 if (!context_find("path"))
164 free(path("./", TFOLDER));
166 adios(NULL, "no folder specified");
169 if (!msgs.size && !foldp && !filep && (cp = getenv("mhdraft")) && *cp)
174 ** We are refiling a file to the folders
177 if (folder || msgs.size)
178 adios(NULL, "use -file or some messages, not both");
179 opnfolds(folders, foldp);
180 for (i = 0; i < filep; i++)
181 if (m_file(files[i], folders, foldp, preserve, 0))
183 /* If -nolink, then "remove" files */
185 remove_files(filep, filevec);
190 app_msgarg(&msgs, "cur");
192 folder = getfolder(FCUR);
193 strncpy(maildir, m_maildir(folder), sizeof(maildir));
195 if (chdir(maildir) == NOTOK)
196 adios(maildir, "unable to change directory to");
198 /* read source folder and create message structure */
199 if (!(mp = folder_read(folder)))
200 adios(NULL, "unable to read folder %s", folder);
202 /* check for empty folder */
204 adios(NULL, "no messages in %s", folder);
206 /* parse the message range/sequence/name and set SELECTED */
207 for (msgnum = 0; msgnum < msgs.size; msgnum++)
208 if (!m_convert(mp, msgs.msgs[msgnum]))
210 seq_setprev(mp); /* set the previous-sequence */
212 /* create folder structures for each destination folder */
213 opnfolds(folders, foldp);
215 /* Link all the selected messages into destination folders.
217 ** This causes the add hook to be run for messages that are
218 ** linked into another folder. The refile hook is run for
219 ** messages that are moved to another folder.
221 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
222 if (is_selected(mp, msgnum)) {
223 cp = getcpy(m_name(msgnum));
224 if (m_file(cp, folders, foldp, preserve, !linkf))
231 ** This is a hack. If we are using an external rmmproc,
232 ** then save the current folder to the context file,
233 ** so the external rmmproc will remove files from the correct
234 ** directory. This should be moved to folder_delmsgs().
237 context_replace(pfolder, folder);
243 ** If -nolink, then "remove" messages from source folder.
245 ** Note that folder_delmsgs does not call the delete hook
246 ** because the message has already been handled above.
249 folder_delmsgs(mp, unlink_msgs, 1);
252 clsfolds(folders, foldp);
254 if (mp->hghsel != mp->curmsg &&
255 (mp->numsel != mp->nummsg || linkf))
256 seq_setcur(mp, mp->hghsel);
257 seq_save(mp); /* synchronize message sequences */
259 context_replace(pfolder, folder); /* update current folder */
260 context_save(); /* save the context file */
261 folder_free(mp); /* free folder structure */
268 ** Read all the destination folders and
269 ** create folder structures for all of them.
273 opnfolds(struct st_fold *folders, int nfolders)
275 char nmaildir[BUFSIZ];
276 register struct st_fold *fp, *ep;
277 register struct msgs *mp;
279 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
280 chdir(m_maildir(""));
281 strncpy(nmaildir, m_maildir(fp->f_name), sizeof(nmaildir));
283 create_folder(nmaildir, 0, done);
285 if (chdir(nmaildir) == NOTOK)
286 adios(nmaildir, "unable to change directory to");
287 if (!(mp = folder_read(fp->f_name)))
288 adios(NULL, "unable to read folder %s", fp->f_name);
299 ** Set the Previous-Sequence and then sychronize the
300 ** sequence file, for each destination folder.
304 clsfolds(struct st_fold *folders, int nfolders)
306 register struct st_fold *fp, *ep;
307 register struct msgs *mp;
309 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
318 ** If you have a "rmmproc" defined, we called that
319 ** to remove all the specified files. If "rmmproc"
320 ** is not defined, then just unlink the files.
324 remove_files(int filep, char **files)
329 /* If rmmproc is defined, we use that */
331 vec = files++; /* vec[0] = filevec[0] */
332 files[filep] = NULL; /* NULL terminate list */
335 vec[0] = mhbasename(rmmproc);
336 execvp(rmmproc, vec);
337 adios(rmmproc, "unable to exec");
340 /* Else just unlink the files */
341 files++; /* advance past filevec[0] */
342 for (i = 0; i < filep; i++) {
343 if (unlink(files[i]) == NOTOK)
344 admonish(files[i], "unable to unlink");
350 ** Link (or copy) the message into each of
351 ** the destination folders.
355 m_file(char *msgfile, struct st_fold *folders, int nfolders, int preserve,
359 struct st_fold *fp, *ep;
361 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
362 if ((msgnum = folder_addmsg(&fp->f_mp, msgfile, 1, 0,
363 preserve, nfolders == 1 && refile, maildir))