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