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