3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
14 * We allocate spaces for messages (msgs array)
15 * this number of elements at a time.
20 static struct swit switches[] = {
40 { "rmmproc program", 0 },
52 static char maildir[BUFSIZ];
62 static void opnfolds (struct st_fold *, int);
63 static void clsfolds (struct st_fold *, int);
64 static void remove_files (int, char **);
65 static int m_file (char *, struct st_fold *, int, int);
69 main (int argc, char **argv)
71 int linkf = 0, preserve = 0, filep = 0;
72 int foldp = 0, isdf = 0, unlink_msgs = 0;
73 int i, msgnum, nummsgs, maxmsgs;
74 char *cp, *folder = NULL, buf[BUFSIZ];
75 char **argp, **arguments, **msgs;
76 char *filevec[NFOLDERS + 2];
77 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
78 struct st_fold folders[NFOLDERS + 1];
82 setlocale(LC_ALL, "");
84 invo_name = r1bindex (argv[0], '/');
86 /* read user profile/context */
89 arguments = getarguments (invo_name, argc, argv, 1);
93 * Allocate the initial space to record message
94 * names, ranges, and sequences.
98 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
99 adios (NULL, "unable to allocate storage");
104 while ((cp = *argp++)) {
106 switch (smatch (++cp, switches)) {
108 ambigsw (cp, switches);
111 adios (NULL, "-%s unknown\n", cp);
114 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
116 print_help (buf, switches, 1);
119 print_version(invo_name);
145 adios (NULL, "only one source folder at a time!");
146 if (!(cp = *argp++) || *cp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
148 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
149 *cp != '@' ? TFOLDER : TSUBCWF);
152 if (filep > NFOLDERS)
153 adios (NULL, "only %d files allowed!", NFOLDERS);
155 files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
158 if (filep > NFOLDERS)
159 adios (NULL, "only %d files allowed!", NFOLDERS);
160 if (!(cp = *argp++) || *cp == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
162 files[filep++] = path (cp, TFILE);
166 if (!(rmmproc = *argp++) || *rmmproc == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
174 if (*cp == '+' || *cp == '@') {
175 if (foldp > NFOLDERS)
176 adios (NULL, "only %d folders allowed!", NFOLDERS);
177 folders[foldp++].f_name =
178 path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
181 * Check if we need to allocate more space
182 * for message names, ranges, and sequences.
184 if (nummsgs >= maxmsgs) {
186 if (!(msgs = (char **) realloc (msgs,
187 (size_t) (maxmsgs * sizeof(*msgs)))))
188 adios (NULL, "unable to reallocate msgs storage");
190 msgs[nummsgs++] = cp;
194 if (!context_find ("path"))
195 free (path ("./", TFOLDER));
197 adios (NULL, "no folder specified");
200 if (!nummsgs && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp)
205 * We are refiling a file to the folders
208 if (folder || nummsgs)
209 adios (NULL, "use -file or some messages, not both");
210 opnfolds (folders, foldp);
211 for (i = 0; i < filep; i++)
212 if (m_file (files[i], folders, foldp, preserve))
214 /* If -nolink, then "remove" files */
216 remove_files (filep, filevec);
221 msgs[nummsgs++] = "cur";
223 folder = getfolder (1);
224 strncpy (maildir, m_maildir (folder), sizeof(maildir));
226 if (chdir (maildir) == NOTOK)
227 adios (maildir, "unable to change directory to");
229 /* read source folder and create message structure */
230 if (!(mp = folder_read (folder)))
231 adios (NULL, "unable to read folder %s", folder);
233 /* check for empty folder */
235 adios (NULL, "no messages in %s", folder);
237 /* parse the message range/sequence/name and set SELECTED */
238 for (msgnum = 0; msgnum < nummsgs; msgnum++)
239 if (!m_convert (mp, msgs[msgnum]))
241 seq_setprev (mp); /* set the previous-sequence */
243 /* create folder structures for each destination folder */
244 opnfolds (folders, foldp);
246 /* Link all the selected messages into destination folders */
247 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
248 if (is_selected (mp, msgnum)) {
249 cp = getcpy (m_name (msgnum));
250 if (m_file (cp, folders, foldp, preserve))
257 * This is a hack. If we are using an external rmmproc,
258 * then save the current folder to the context file,
259 * so the external rmmproc will remove files from the correct
260 * directory. This should be moved to folder_delmsgs().
263 context_replace (pfolder, folder);
268 /* If -nolink, then "remove" messages from source folder */
270 folder_delmsgs (mp, unlink_msgs);
273 clsfolds (folders, foldp);
275 if (mp->hghsel != mp->curmsg
276 && (mp->numsel != mp->nummsg || linkf))
277 seq_setcur (mp, mp->hghsel);
278 seq_save (mp); /* synchronize message sequences */
280 context_replace (pfolder, folder); /* update current folder */
281 context_save (); /* save the context file */
282 folder_free (mp); /* free folder structure */
288 * Read all the destination folders and
289 * create folder structures for all of them.
293 opnfolds (struct st_fold *folders, int nfolders)
296 char nmaildir[BUFSIZ];
297 register struct st_fold *fp, *ep;
298 register struct msgs *mp;
301 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
302 chdir (m_maildir (""));
303 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
305 if (stat (nmaildir, &st) == NOTOK) {
307 adios (nmaildir, "error on folder");
308 cp = concat ("Create folder \"", nmaildir, "\"? ", NULL);
312 if (!makedir (nmaildir))
313 adios (NULL, "unable to create folder %s", nmaildir);
316 if (chdir (nmaildir) == NOTOK)
317 adios (nmaildir, "unable to change directory to");
318 if (!(mp = folder_read (fp->f_name)))
319 adios (NULL, "unable to read folder %s", fp->f_name);
330 * Set the Previous-Sequence and then sychronize the
331 * sequence file, for each destination folder.
335 clsfolds (struct st_fold *folders, int nfolders)
337 register struct st_fold *fp, *ep;
338 register struct msgs *mp;
340 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
349 * If you have a "rmmproc" defined, we called that
350 * to remove all the specified files. If "rmmproc"
351 * is not defined, then just unlink the files.
355 remove_files (int filep, char **files)
360 /* If rmmproc is defined, we use that */
362 vec = files++; /* vec[0] = filevec[0] */
363 files[filep] = NULL; /* NULL terminate list */
366 vec[0] = r1bindex (rmmproc, '/');
367 execvp (rmmproc, vec);
368 adios (rmmproc, "unable to exec");
371 /* Else just unlink the files */
372 files++; /* advance past filevec[0] */
373 for (i = 0; i < filep; i++) {
374 if (unlink (files[i]) == NOTOK)
375 admonish (files[i], "unable to unlink");
381 * Link (or copy) the message into each of
382 * the destination folders.
386 m_file (char *msgfile, struct st_fold *folders, int nfolders, int preserve)
389 struct st_fold *fp, *ep;
391 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
392 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve)) == -1)