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