]> diplodocus.org Git - nmh/blob - uip/refile.c
Finished replacing mh_strcasecmp() with strcasecmp(). Removed
[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 * Update this now, since folder_delmsgs() will save the context
243 * but doesn't have access to the folder name.
244 */
245
246 context_replace (pfolder, folder); /* update current folder */
247
248 /*
249 * Adjust "cur" if necessary
250 */
251
252 if (mp->hghsel != mp->curmsg
253 && (mp->numsel != mp->nummsg || linkf))
254 seq_setcur (mp, mp->hghsel);
255
256 /* If -nolink, then "remove" messages from source folder.
257 *
258 * Note that folder_delmsgs does not call the delete hook
259 * because the message has already been handled above.
260 */
261 if (!linkf) {
262 folder_delmsgs (mp, unlink_msgs, 1);
263 } else {
264 seq_save (mp); /* synchronize message sequences */
265 context_save (); /* save the context file */
266 }
267
268 clsfolds (folders, foldp);
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 st_fold *folders, int nfolders)
283 {
284 char nmaildir[BUFSIZ];
285 register struct st_fold *fp, *ep;
286 register struct msgs *mp;
287
288 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
289 chdir (m_maildir (""));
290 strncpy (nmaildir, m_maildir (fp->f_name), sizeof(nmaildir));
291
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
302 chdir (maildir);
303 }
304 }
305
306
307 /*
308 * Set the Previous-Sequence and then sychronize the
309 * sequence file, for each destination folder.
310 */
311
312 static void
313 clsfolds (struct st_fold *folders, int nfolders)
314 {
315 register struct st_fold *fp, *ep;
316 register struct msgs *mp;
317
318 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
319 mp = fp->f_mp;
320 seq_setprev (mp);
321 seq_save (mp);
322 }
323 }
324
325
326 /*
327 * If you have a "rmmproc" defined, we called that
328 * to remove all the specified files. If "rmmproc"
329 * is not defined, then just unlink the files.
330 */
331
332 static void
333 remove_files (int filep, char **files)
334 {
335 int i, vecp;
336 char **vec, *program;
337
338 /* If rmmproc is defined, we use that */
339 if (rmmproc) {
340 vec = argsplit(rmmproc, &program, &vecp);
341 files++; /* Yes, we need to do this */
342 for (i = 0; i < filep; i++)
343 vec[vecp++] = files[i];
344 vec[vecp] = NULL; /* NULL terminate list */
345
346 fflush (stdout);
347 execvp (program, vec);
348 adios (rmmproc, "unable to exec");
349 }
350
351 /* Else just unlink the files */
352 files++; /* advance past filevec[0] */
353 for (i = 0; i < filep; i++) {
354 if (unlink (files[i]) == NOTOK)
355 admonish (files[i], "unable to unlink");
356 }
357 }
358
359
360 /*
361 * Link (or copy) the message into each of the destination folders.
362 * If oldmsgnum is not 0, call copy_seqs().
363 */
364
365 static int
366 m_file (struct msgs *mp, char *msgfile, int oldmsgnum,
367 struct st_fold *folders, int nfolders, int preserve, int refile)
368 {
369 int msgnum;
370 struct st_fold *fp, *ep;
371
372 for (fp = folders, ep = folders + nfolders; fp < ep; fp++) {
373 if ((msgnum = folder_addmsg (&fp->f_mp, msgfile, 1, 0, preserve, nfolders == 1 && refile, maildir)) == -1)
374 return 1;
375 if (oldmsgnum) copy_seqs (mp, oldmsgnum, fp->f_mp, msgnum);
376 }
377 return 0;
378 }
379
380
381 /*
382 * Copy sequence information for a refiled message to its
383 * new folder. Skip the cur sequence.
384 */
385 static void
386 copy_seqs (struct msgs *oldmp, int oldmsgnum, struct msgs *newmp, int newmsgnum)
387 {
388 char **seq;
389 int seqnum;
390
391 for (seq = oldmp->msgattrs, seqnum = 0; *seq; ++seq, ++seqnum) {
392 if (strcmp (current, *seq)) {
393 assert (seqnum == seq_getnum (oldmp, *seq));
394 if (in_sequence (oldmp, seqnum, oldmsgnum)) {
395 seq_addmsg (newmp, *seq, newmsgnum,
396 is_seq_private (oldmp, seqnum) ? 0 : 1, 0);
397 }
398 }
399 }
400 }