3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
18 * We allocate spaces for messages (msgs array)
19 * this number of elements at a time.
24 static struct swit switches[] = {
44 { "rmmproc program", 0 },
56 static char maildir[BUFSIZ];
66 static void opnfolds (struct st_fold *, int);
67 static void clsfolds (struct st_fold *, int);
68 static void remove_files (int, char **);
69 static int m_file (char *, struct st_fold *, int, int);
73 main (int argc, char **argv)
75 int linkf = 0, preserve = 0, filep = 0;
76 int foldp = 0, isdf = 0, unlink_msgs = 0;
77 int i, msgnum, nummsgs, maxmsgs;
78 char *cp, *folder = NULL, buf[BUFSIZ];
79 char **argp, **arguments, **msgs;
80 char *filevec[NFOLDERS + 2];
81 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
82 struct st_fold folders[NFOLDERS + 1];
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
93 arguments = getarguments (invo_name, argc, argv, 1);
97 * Allocate the initial space to record message
98 * names, ranges, and sequences.
102 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
103 adios (NULL, "unable to allocate storage");
108 while ((cp = *argp++)) {
110 switch (smatch (++cp, switches)) {
112 ambigsw (cp, switches);
115 adios (NULL, "-%s unknown\n", cp);
118 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
120 print_help (buf, switches, 1);
123 print_version(invo_name);
149 adios (NULL, "only one source folder at a time!");
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
152 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
153 *cp != '@' ? TFOLDER : TSUBCWF);
156 if (filep > NFOLDERS)
157 adios (NULL, "only %d files allowed!", NFOLDERS);
159 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
162 if (filep > NFOLDERS)
163 adios (NULL, "only %d files allowed!", NFOLDERS);
164 if (!(cp = *argp++) || *cp == '-')
165 adios (NULL, "missing argument to %s", argp[-2]);
166 files[filep++] = path (cp, TFILE);
170 if (!(rmmproc = *argp++) || *rmmproc == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
178 if (*cp == '+' || *cp == '@') {
179 if (foldp > NFOLDERS)
180 adios (NULL, "only %d folders allowed!", NFOLDERS);
181 folders[foldp++].f_name =
182 path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
185 * Check if we need to allocate more space
186 * for message names, ranges, and sequences.
188 if (nummsgs >= maxmsgs) {
190 if (!(msgs = (char **) realloc (msgs,
191 (size_t) (maxmsgs * sizeof(*msgs)))))
192 adios (NULL, "unable to reallocate msgs storage");
194 msgs[nummsgs++] = cp;
198 if (!context_find ("path"))
199 free (path ("./", TFOLDER));
201 adios (NULL, "no folder specified");
204 if (!nummsgs && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
209 * We are refiling a file to the folders
212 if (folder || nummsgs)
213 adios (NULL, "use -file or some messages, not both");
214 opnfolds (folders, foldp);
215 for (i = 0; i < filep; i++)
216 if (m_file (files[i], folders, foldp, preserve))
218 /* If -nolink, then "remove" files */
220 remove_files (filep, filevec);
225 msgs[nummsgs++] = "cur";
227 folder = getfolder (1);
228 strncpy (maildir, m_maildir (folder), sizeof(maildir));
230 if (chdir (maildir) == NOTOK)
231 adios (maildir, "unable to change directory to");
233 /* read source folder and create message structure */
234 if (!(mp = folder_read (folder)))
235 adios (NULL, "unable to read folder %s", folder);
237 /* check for empty folder */
239 adios (NULL, "no messages in %s", folder);
241 /* parse the message range/sequence/name and set SELECTED */
242 for (msgnum = 0; msgnum < nummsgs; msgnum++)
243 if (!m_convert (mp, msgs[msgnum]))
245 seq_setprev (mp); /* set the previous-sequence */
247 /* create folder structures for each destination folder */
248 opnfolds (folders, foldp);
250 /* Link all the selected messages into destination folders */
251 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
252 if (is_selected (mp, msgnum)) {
253 cp = getcpy (m_name (msgnum));
254 if (m_file (cp, folders, foldp, preserve))
261 * This is a hack. If we are using an external rmmproc,
262 * then save the current folder to the context file,
263 * so the external rmmproc will remove files from the correct
264 * directory. This should be moved to folder_delmsgs().
267 context_replace (pfolder, folder);
272 /* If -nolink, then "remove" messages from source folder */
274 folder_delmsgs (mp, unlink_msgs);
277 clsfolds (folders, foldp);
279 if (mp->hghsel != mp->curmsg
280 && (mp->numsel != mp->nummsg || linkf))
281 seq_setcur (mp, mp->hghsel);
282 seq_save (mp); /* synchronize message sequences */
284 context_replace (pfolder, folder); /* update current folder */
285 context_save (); /* save the context file */
286 folder_free (mp); /* free folder structure */
292 * Read all the destination folders and
293 * create folder structures for all of them.
297 opnfolds (struct st_fold *folders, int nfolders)
300 char nmaildir[BUFSIZ];
301 register struct st_fold *fp, *ep;
302 register struct msgs *mp;
305 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
306 chdir (m_maildir (""));
307 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
309 if (stat (nmaildir, &st) == NOTOK) {
311 adios (nmaildir, "error on folder");
312 cp = concat ("Create folder \"", nmaildir, "\"? ", NULL);
316 if (!makedir (nmaildir))
317 adios (NULL, "unable to create folder %s", nmaildir);
320 if (chdir (nmaildir) == NOTOK)
321 adios (nmaildir, "unable to change directory to");
322 if (!(mp = folder_read (fp->f_name)))
323 adios (NULL, "unable to read folder %s", fp->f_name);
334 * Set the Previous-Sequence and then sychronize the
335 * sequence file, for each destination folder.
339 clsfolds (struct st_fold *folders, int nfolders)
341 register struct st_fold *fp, *ep;
342 register struct msgs *mp;
344 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
353 * If you have a "rmmproc" defined, we called that
354 * to remove all the specified files. If "rmmproc"
355 * is not defined, then just unlink the files.
359 remove_files (int filep, char **files)
364 /* If rmmproc is defined, we use that */
366 vec = files++; /* vec[0] = filevec[0] */
367 files[filep] = NULL; /* NULL terminate list */
370 vec[0] = r1bindex (rmmproc, '/');
371 execvp (rmmproc, vec);
372 adios (rmmproc, "unable to exec");
375 /* Else just unlink the files */
376 files++; /* advance past filevec[0] */
377 for (i = 0; i < filep; i++) {
378 if (unlink (files[i]) == NOTOK)
379 admonish (files[i], "unable to unlink");
385 * Link (or copy) the message into each of
386 * the destination folders.
390 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve)
393 struct st_fold *fp, *ep;
395 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
396 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve)) == -1)