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