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