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