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