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