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