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