]> diplodocus.org Git - nmh/blob - uip/refile.c
mhshowsbr.c: Delete single-use global int `nolist'.
[nmh] / uip / refile.c
1 /* refile.c -- move or link message(s) from a source folder
2 * -- into one or more destination folders
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include "h/mh.h"
10 #include "sbr/m_name.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_draft.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/folder_free.h"
21 #include "sbr/folder_delmsgs.h"
22 #include "sbr/folder_addmsg.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_find.h"
26 #include "sbr/ambigsw.h"
27 #include "sbr/path.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/seq_getnum.h"
31 #include "sbr/seq_add.h"
32 #include "sbr/arglist.h"
33 #include "sbr/error.h"
34 #include "h/done.h"
35 #include "h/utils.h"
36 #include "sbr/m_maildir.h"
37 #include "sbr/m_mktemp.h"
38 #include <fcntl.h>
39
40 #define REFILE_SWITCHES \
41 X("draft", 0, DRAFTSW) \
42 X("link", 0, LINKSW) \
43 X("nolink", 0, NLINKSW) \
44 X("preserve", 0, PRESSW) \
45 X("nopreserve", 0, NPRESSW) \
46 X("retainsequences", 0, RETAINSEQSSW) \
47 X("noretainsequences", 0, NRETAINSEQSSW) \
48 X("unlink", 0, UNLINKSW) \
49 X("nounlink", 0, NUNLINKSW) \
50 X("src +folder", 0, SRCSW) \
51 X("file file", 0, FILESW) \
52 X("rmmproc program", 0, RPROCSW) \
53 X("normmproc", 0, NRPRCSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(REFILE);
59 #undef X
60
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(REFILE, switches);
63 #undef X
64
65 static char maildir[BUFSIZ];
66
67 struct st_fold {
68 char *f_name;
69 struct msgs *f_mp;
70 };
71
72 /*
73 * static prototypes
74 */
75 static void opnfolds (struct msgs *, struct st_fold *, int);
76 static void clsfolds (struct st_fold *, int);
77 static void remove_files (int, char **);
78 static int m_file (struct msgs *, char *, int, struct st_fold *, int, int, int);
79 static void copy_seqs (struct msgs *, int, struct msgs *, int);
80
81
82 int
83 main (int argc, char **argv)
84 {
85 bool linkf = false;
86 bool preserve = false;
87 bool retainseqs = false;
88 int filep = 0;
89 int foldp = 0, isdf = 0;
90 bool unlink_msgs = false;
91 int i, msgnum;
92 char *cp, *folder = NULL, buf[BUFSIZ];
93 char **argp, **arguments;
94 char *filevec[NFOLDERS + 2];
95 char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
96 struct st_fold folders[NFOLDERS + 1];
97 struct msgs_array msgs = { 0, 0, NULL };
98 struct msgs *mp;
99
100 if (nmh_init(argv[0], true, true)) { return 1; }
101
102 arguments = getarguments (invo_name, argc, argv, 1);
103 argp = arguments;
104
105 /*
106 * Parse arguments
107 */
108 while ((cp = *argp++)) {
109 if (*cp == '-') {
110 switch (smatch (++cp, switches)) {
111 case AMBIGSW:
112 ambigsw (cp, switches);
113 done (1);
114 case UNKWNSW:
115 die("-%s unknown\n", cp);
116
117 case HELPSW:
118 snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
119 invo_name);
120 print_help (buf, switches, 1);
121 done (0);
122 case VERSIONSW:
123 print_version(invo_name);
124 done (0);
125
126 case LINKSW:
127 linkf = true;
128 continue;
129 case NLINKSW:
130 linkf = false;
131 continue;
132
133 case PRESSW:
134 preserve = true;
135 continue;
136 case NPRESSW:
137 preserve = false;
138 continue;
139
140 case RETAINSEQSSW:
141 retainseqs = true;
142 continue;
143 case NRETAINSEQSSW:
144 retainseqs = false;
145 continue;
146
147 case UNLINKSW:
148 unlink_msgs = true;
149 continue;
150 case NUNLINKSW:
151 unlink_msgs = false;
152 continue;
153
154 case SRCSW:
155 if (folder)
156 die("only one source folder at a time!");
157 if (!(cp = *argp++) || *cp == '-')
158 die("missing argument to %s", argp[-2]);
159 folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
160 *cp != '@' ? TFOLDER : TSUBCWF);
161 continue;
162 case DRAFTSW:
163 if (filep > NFOLDERS)
164 die("only %d files allowed!", NFOLDERS);
165 isdf = 0;
166 files[filep++] = mh_xstrdup(m_draft(NULL, NULL, 1, &isdf));
167 continue;
168 case FILESW:
169 if (filep > NFOLDERS)
170 die("only %d files allowed!", NFOLDERS);
171 if (!(cp = *argp++) || *cp == '-')
172 die("missing argument to %s", argp[-2]);
173 files[filep++] = path (cp, TFILE);
174 continue;
175
176 case RPROCSW:
177 if (!(rmmproc = *argp++) || *rmmproc == '-')
178 die("missing argument to %s", argp[-2]);
179 continue;
180 case NRPRCSW:
181 rmmproc = NULL;
182 continue;
183 }
184 }
185 if (*cp == '+' || *cp == '@') {
186 if (foldp > NFOLDERS)
187 die("only %d folders allowed!", NFOLDERS);
188 folders[foldp++].f_name =
189 pluspath (cp);
190 } else
191 app_msgarg(&msgs, cp);
192 }
193
194 if (!context_find ("path"))
195 free (path ("./", TFOLDER));
196 if (foldp == 0)
197 die("no folder specified");
198
199 /*
200 * We are refiling a file to the folders
201 */
202 if (filep > 0) {
203 if (folder || msgs.size)
204 die("use -file or some messages, not both");
205 opnfolds (NULL, folders, foldp);
206 for (i = 0; i < filep; i++)
207 if (m_file (0, files[i], 0, folders, foldp, preserve, 0))
208 done (1);
209 /* If -nolink, then "remove" files */
210 if (!linkf)
211 remove_files (filep, filevec);
212 done (0);
213 }
214
215 if (!msgs.size)
216 app_msgarg(&msgs, "cur");
217 if (!folder)
218 folder = getfolder (1);
219 strncpy (maildir, m_maildir (folder), sizeof(maildir));
220
221 if (chdir (maildir) == NOTOK)
222 adios (maildir, "unable to change directory to");
223
224 /* read source folder and create message structure */
225 if (!(mp = folder_read (folder, 1)))
226 die("unable to read folder %s", folder);
227
228 /* check for empty folder */
229 if (mp->nummsg == 0)
230 die("no messages in %s", folder);
231
232 /* parse the message range/sequence/name and set SELECTED */
233 for (msgnum = 0; msgnum < msgs.size; msgnum++)
234 if (!m_convert (mp, msgs.msgs[msgnum]))
235 done (1);
236 seq_setprev (mp); /* set the previous-sequence */
237
238 /* create folder structures for each destination folder */
239 opnfolds (mp, folders, foldp);
240
241 /* Link all the selected messages into destination folders.
242 *
243 * This causes the add hook to be run for messages that are
244 * linked into another folder. The refile hook is run for
245 * messages that are moved to another folder.
246 */
247 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
248 if (is_selected (mp, msgnum)) {
249 cp = mh_xstrdup(m_name (msgnum));
250 if (m_file (mp, cp, retainseqs ? msgnum : 0, folders, foldp,
251 preserve, !linkf))
252 done (1);
253 free (cp);
254 }
255 }
256
257 /*
258 * Update this now, since folder_delmsgs() will save the context
259 * but doesn't have access to the folder name.
260 */
261
262 context_replace (pfolder, folder); /* update current folder */
263
264 /*
265 * Adjust "cur" if necessary
266 */
267
268 if (mp->hghsel != mp->curmsg
269 && (mp->numsel != mp->nummsg || linkf))
270 seq_setcur (mp, mp->hghsel);
271
272 /*
273 * Close destination folders now; if we are using private sequences
274 * we need to have all of our calls to seq_save() complete before we
275 * call context_save().
276 */
277
278 clsfolds (folders, foldp);
279
280 /* If -nolink, then "remove" messages from source folder.
281 *
282 * Note that folder_delmsgs does not call the delete hook
283 * because the message has already been handled above.
284 */
285 if (!linkf) {
286 folder_delmsgs (mp, unlink_msgs, 1);
287 } else {
288 seq_save (mp); /* synchronize message sequences */
289 context_save (); /* save the context file */
290 }
291
292 folder_free (mp); /* free folder structure */
293 done (0);
294 return 1;
295 }
296
297
298 /*
299 * Read all the destination folders and
300 * create folder structures for all of them.
301 */
302
303 static void
304 opnfolds (struct msgs *src_folder, struct st_fold *folders, int nfolders)
305 {
306 char nmaildir[BUFSIZ];
307 struct st_fold *fp, *ep;
308 struct msgs *mp;
309
310 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
311 if (chdir (m_maildir ("")) < 0) {
312 advise (m_maildir (""), "chdir");
313 }
314 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
315
316 /*
317 * Null src_folder indicates that we are refiling a file to
318 * the folders, in which case we don't want to short-circuit
319 * fp->f_mp to any "source folder".
320 */
321 if (! src_folder || strcmp (src_folder->foldpath, nmaildir)) {
322 create_folder (nmaildir, 0, done);
323
324 if (chdir (nmaildir) == NOTOK)
325 adios (nmaildir, "unable to change directory to");
326 if (!(mp = folder_read (fp->f_name, 1)))
327 die("unable to read folder %s", fp->f_name);
328 mp->curmsg = 0;
329
330 fp->f_mp = mp;
331 } else {
332 /* Source and destination folders are the same. */
333 fp->f_mp = src_folder;
334 }
335
336 if (maildir[0] != '\0' && chdir (maildir) < 0) {
337 advise (maildir, "chdir");
338 }
339 }
340 }
341
342
343 /*
344 * Set the Previous-Sequence and then synchronize the
345 * sequence file, for each destination folder.
346 */
347
348 static void
349 clsfolds (struct st_fold *folders, int nfolders)
350 {
351 struct st_fold *fp, *ep;
352 struct msgs *mp;
353
354 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
355 mp = fp->f_mp;
356 seq_setprev (mp);
357 seq_save (mp);
358 }
359 }
360
361
362 /*
363 * If you have a "rmmproc" defined, we called that
364 * to remove all the specified files. If "rmmproc"
365 * is not defined, then just unlink the files.
366 */
367
368 static void
369 remove_files (int filep, char **files)
370 {
371 int i, vecp;
372 char **vec, *program;
373
374 /* If rmmproc is defined, we use that */
375 if (rmmproc) {
376 vec = argsplit(rmmproc, &program, &vecp);
377 files++; /* Yes, we need to do this */
378 for (i = 0; i < filep; i++)
379 vec[vecp++] = files[i];
380 vec[vecp] = NULL; /* NULL terminate list */
381
382 fflush (stdout);
383 execvp (program, vec);
384 adios (rmmproc, "unable to exec");
385 }
386
387 /* Else just unlink the files */
388 files++; /* advance past filevec[0] */
389 for (i = 0; i < filep; i++) {
390 if (m_unlink (files[i]) == NOTOK)
391 admonish (files[i], "unable to unlink");
392 }
393 }
394
395
396 /*
397 * Link (or copy) the message into each of the destination folders.
398 * If oldmsgnum is not 0, call copy_seqs().
399 */
400
401 static int
402 m_file (struct msgs *mp, char *msgfile, int oldmsgnum,
403 struct st_fold *folders, int nfolders, int preserve, int refile)
404 {
405 int msgnum;
406 struct st_fold *fp, *ep;
407
408 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
409 /*
410 * With same source and destination folder, don't indicate that
411 * the new message is selected so that 1) folder_delmsgs() doesn't
412 * delete it later and 2) it is not reflected in mp->hghsel, and
413 * therefore won't be assigned to be the current message.
414 */
415 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile,
416 mp != fp->f_mp,
417 0, preserve, nfolders == 1 && refile,
418 maildir)) == -1)
419 return 1;
420 if (oldmsgnum) copy_seqs (mp, oldmsgnum, fp->f_mp, msgnum);
421 }
422 return 0;
423 }
424
425
426 /*
427 * Copy sequence information for a refiled message to its
428 * new folder. Skip the cur sequence.
429 */
430 static void
431 copy_seqs (struct msgs *oldmp, int oldmsgnum, struct msgs *newmp, int newmsgnum)
432 {
433 char **seq;
434 size_t seqnum;
435
436 for (seq = svector_strs (oldmp->msgattrs), seqnum = 0;
437 *seq && seqnum < svector_size (oldmp->msgattrs);
438 ++seq, ++seqnum) {
439 if (strcmp (current, *seq)) {
440 assert ((int) seqnum == seq_getnum (oldmp, *seq));
441 if (in_sequence (oldmp, seqnum, oldmsgnum)) {
442 seq_addmsg (newmp, *seq, newmsgnum,
443 !is_seq_private (oldmp, seqnum), 0);
444 }
445 }
446 }
447 }