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