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