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