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