]> diplodocus.org Git - nmh/blob - uip/burst.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / burst.c
1 /* burst.c -- explode digests into individual messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include <h/mhparse.h>
11 #include "h/done.h"
12 #include "sbr/m_maildir.h"
13 #include "sbr/m_mktemp.h"
14 #include "mhfree.h"
15
16 #define BURST_SWITCHES \
17 X("inplace", 0, INPLSW) \
18 X("noinplace", 0, NINPLSW) \
19 X("mime", 0, MIMESW) \
20 X("nomime", 0, NMIMESW) \
21 X("automime", 0, AUTOMIMESW) \
22 X("quiet", 0, QIETSW) \
23 X("noquiet", 0, NQIETSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(BURST);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(BURST, switches);
35 #undef X
36
37 struct smsg {
38 off_t s_start;
39 off_t s_stop;
40 };
41
42 /*
43 * For the MIME parsing routines
44 */
45
46 int debugsw = 0;
47
48 /*
49 * static prototypes
50 */
51 static int find_delim (int, struct smsg *, int *);
52 static void find_mime_parts (CT, struct smsg *, int *);
53 static void burst(struct msgs **, int, struct smsg *, int, bool, bool,
54 char *, int);
55 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
56
57 /*
58 * A macro to check to see if we have reached a message delimiter
59 * (an encapsulation boundary, EB, in RFC 934 parlance).
60 *
61 * According to RFC 934, an EB is simply a line which starts with
62 * a "-" and is NOT followed by a space. So even a single "-" on a line
63 * by itself would be an EB.
64 */
65
66 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
67
68 int
69 main (int argc, char **argv)
70 {
71 bool inplace = false;
72 bool quietsw = false;
73 bool verbosw = false;
74 int mimesw = 1;
75 int hi, msgnum, numburst;
76 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
77 char **argp, **arguments;
78 struct msgs_array msgs = { 0, 0, NULL };
79 struct smsg *smsgs;
80 struct msgs *mp;
81
82 if (nmh_init(argv[0], true, true)) { return 1; }
83
84 arguments = getarguments (invo_name, argc, argv, 1);
85 argp = 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 die("-%s unknown\n", cp);
95
96 case HELPSW:
97 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
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 INPLSW:
106 inplace = true;
107 continue;
108 case NINPLSW:
109 inplace = false;
110 continue;
111
112 case MIMESW:
113 mimesw = 2;
114 continue;
115 case NMIMESW:
116 mimesw = 0;
117 continue;
118 case AUTOMIMESW:
119 mimesw = 1;
120 continue;
121
122 case QIETSW:
123 quietsw = true;
124 continue;
125 case NQIETSW:
126 quietsw = false;
127 continue;
128
129 case VERBSW:
130 verbosw = true;
131 continue;
132 case NVERBSW:
133 verbosw = false;
134 continue;
135 }
136 }
137 if (*cp == '+' || *cp == '@') {
138 if (folder)
139 die("only one folder at a time!");
140 folder = pluspath (cp);
141 } else {
142 app_msgarg(&msgs, cp);
143 }
144 }
145
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER));
148 if (!msgs.size)
149 app_msgarg(&msgs, "cur");
150 if (!folder)
151 folder = getfolder (1);
152 maildir = m_maildir (folder);
153
154 if (chdir (maildir) == NOTOK)
155 adios (maildir, "unable to change directory to");
156
157 /* read folder and create message structure */
158 if (!(mp = folder_read (folder, 1)))
159 die("unable to read folder %s", folder);
160
161 /* check for empty folder */
162 if (mp->nummsg == 0)
163 die("no messages in %s", folder);
164
165 /* parse all the message ranges/sequences and set SELECTED */
166 for (msgnum = 0; msgnum < msgs.size; msgnum++)
167 if (!m_convert (mp, msgs.msgs[msgnum]))
168 done (1);
169 seq_setprev (mp); /* set the previous-sequence */
170
171 smsgs = mh_xcalloc(MAXFOLDER + 2, sizeof *smsgs);
172
173 hi = mp->hghmsg + 1;
174
175 /* burst all the SELECTED messages */
176 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
177 if (is_selected (mp, msgnum)) {
178 if ((numburst = find_delim (msgnum, smsgs, &mimesw)) >= 1) {
179 if (verbosw)
180 printf ("%d message%s exploded from digest %d\n",
181 numburst, PLURALS(numburst), msgnum);
182 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw,
183 maildir, mimesw);
184 } else {
185 if (numburst == 0) {
186 if (!quietsw)
187 inform("message %d not in digest format, continuing...",
188 msgnum);
189 } /* this pair of braces was missing before 1999-07-15 */
190 else
191 die("burst() botch -- you lose big");
192 }
193 }
194 }
195
196 free(smsgs);
197 context_replace (pfolder, folder); /* update current folder */
198
199 /*
200 * If -inplace is given, then the first message burst becomes
201 * the current message (which will now show a table of contents).
202 * Otherwise, the first message extracted from the first digest
203 * becomes the current message.
204 */
205 if (inplace) {
206 if (mp->lowsel != mp->curmsg)
207 seq_setcur (mp, mp->lowsel);
208 } else {
209 if (hi <= mp->hghmsg)
210 seq_setcur (mp, hi);
211 }
212
213 seq_save (mp); /* synchronize message sequences */
214 context_save (); /* save the context file */
215 folder_free (mp); /* free folder/message structure */
216 done (0);
217 return 1;
218 }
219
220
221 /*
222 * Scan the message and find the beginning and
223 * end of all the messages in the digest.
224 *
225 * If requested, see if the message is MIME-formatted and contains any
226 * message/rfc822 parts; if so, burst those parts.
227 */
228
229 static int
230 find_delim (int msgnum, struct smsg *smsgs, int *mimesw)
231 {
232 int wasdlm = 0, msgp;
233 off_t pos;
234 char c, *msgnam;
235 char buffer[BUFSIZ];
236 FILE *in;
237 CT content;
238
239 msgnam = m_name (msgnum);
240
241 /*
242 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
243 */
244
245 if (*mimesw > 0) {
246 content = parse_mime(msgnam);
247 if (! content && *mimesw == 2)
248 return 0;
249 if (content) {
250 smsgs[0].s_start = 0;
251 smsgs[0].s_stop = content->c_begin - 1;
252 msgp = 1;
253 find_mime_parts(content, smsgs, &msgp);
254 free_content(content);
255 if (msgp == 1 && *mimesw == 2)
256 adios (msgnam, "does not have any message/rfc822 parts");
257 if (msgp > 1) {
258 *mimesw = 1;
259 return msgp - 1;
260 }
261 }
262 }
263
264 *mimesw = 0;
265
266 if ((in = fopen (msgnam, "r")) == NULL)
267 adios (msgnam, "unable to read message");
268
269 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
270 /*
271 * We're either at the beginning of the whole message, or
272 * we're just past the delimiter of the last message.
273 * Swallow lines until we get to something that's not a newline
274 */
275 while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
276 pos += (long) strlen (buffer);
277 if (feof (in))
278 break;
279
280 /*
281 * Reset to the beginning of the last non-blank line, and save our
282 * starting position. This is where the encapsulated message
283 * starts.
284 */
285 fseeko (in, pos, SEEK_SET);
286 smsgs[msgp].s_start = pos;
287
288 /*
289 * Read in lines until we get to a message delimiter.
290 *
291 * Previously we checked to make sure the preceding line and
292 * next line was a newline. That actually does not comply with
293 * RFC 934, so make sure we break on a message delimiter even
294 * if the previous character was NOT a newline.
295 */
296 for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
297 if ((wasdlm = CHECKDELIM(buffer)))
298 break;
299 pos += (long) strlen (buffer);
300 }
301
302 /*
303 * Only count as a new message if we got the message delimiter.
304 * Swallow a blank line if it was right before the message delimiter.
305 */
306 if (smsgs[msgp].s_start != pos && wasdlm)
307 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
308
309 if (feof (in)) {
310 #if 0
311 if (wasdlm) {
312 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
313 msgp++; /* fake "End of XXX Digest" */
314 }
315 #endif
316 break;
317 }
318 pos += (long) strlen (buffer);
319 }
320
321 fclose (in);
322 return msgp - 1; /* return the number of messages burst */
323 }
324
325
326 /*
327 * Find any MIME content in the message that is a message/rfc822 and add
328 * it to the list of messages to burst.
329 */
330
331 static void
332 find_mime_parts (CT content, struct smsg *smsgs, int *msgp)
333 {
334 struct multipart *m;
335 struct part *part;
336
337 /*
338 * If we have a message/rfc822, then it's easy.
339 */
340
341 if (content->c_type == CT_MESSAGE &&
342 content->c_subtype == MESSAGE_RFC822) {
343 smsgs[*msgp].s_start = content->c_begin;
344 smsgs[*msgp].s_stop = content->c_end;
345 (*msgp)++;
346 return;
347 }
348
349 /*
350 * Otherwise, if we do have multiparts, try all of the sub-parts.
351 */
352
353 if (content->c_type == CT_MULTIPART) {
354 m = (struct multipart *) content->c_ctparams;
355
356 for (part = m->mp_parts; part; part = part->mp_next)
357 find_mime_parts(part->mp_part, smsgs, msgp);
358 }
359 }
360
361
362 /*
363 * Burst out the messages in the digest into the folder
364 */
365
366 static void
367 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
368 bool inplace, bool verbosw, char *maildir, int mimesw)
369 {
370 int i, j, mode;
371 char *msgnam;
372 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
373 FILE *in, *out;
374 struct stat st;
375 struct msgs *mp;
376
377 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
378 adios (msgnam, "unable to read message");
379
380 mode =
381 fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot();
382 mp = *mpp;
383
384 /*
385 * See if we have enough space in the folder
386 * structure for all the new messages.
387 */
388 if ((mp->hghmsg + numburst > mp->hghoff) &&
389 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
390 die("unable to allocate folder storage");
391 *mpp = mp;
392
393 j = mp->hghmsg; /* old value */
394 mp->hghmsg += numburst;
395 mp->nummsg += numburst;
396
397 /*
398 * If this is not the highest SELECTED message, then
399 * increment mp->hghsel by numburst, since the highest
400 * SELECTED is about to be slid down by that amount.
401 */
402 if (msgnum < mp->hghsel)
403 mp->hghsel += numburst;
404
405 /*
406 * If -inplace is given, renumber the messages after the
407 * source message, to make room for each of the messages
408 * contained within the digest.
409 *
410 * This is equivalent to refiling a message from the point
411 * of view of the external hooks.
412 */
413 if (inplace) {
414 for (i = mp->hghmsg; j > msgnum; i--, j--) {
415 strncpy (f1, m_name (i), sizeof(f1));
416 strncpy (f2, m_name (j), sizeof(f2));
417 if (does_exist (mp, j)) {
418 if (verbosw)
419 printf ("message %d becomes message %d\n", j, i);
420
421 if (rename (f2, f1) == NOTOK)
422 admonish (f1, "unable to rename %s to", f2);
423
424 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
425 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
426 ext_hook("ref-hook", f1, f2);
427
428 copy_msg_flags (mp, i, j);
429 clear_msg_flags (mp, j);
430 mp->msgflags |= SEQMOD;
431 }
432 }
433 }
434
435 unset_selected (mp, msgnum);
436
437 /* new hghmsg is hghmsg + numburst
438 *
439 * At this point, there is an array of numburst smsgs, each element of
440 * which contains the starting and stopping offsets (seeks) of the message
441 * in the digest. The inplace flag is set if the original digest is replaced
442 * by a message containing the table of contents. smsgs[0] is that table of
443 * contents. Go through the message numbers in reverse order (high to low).
444 *
445 * Set f1 to the name of the destination message, f2 to the name of a scratch
446 * file. Extract a message from the digest to the scratch file. Move the
447 * original message to a backup file if the destination message number is the
448 * same as the number of the original message, which only happens if the
449 * inplace flag is set. Then move the scratch file to the destination message.
450 *
451 * Moving the original message to the backup file is equivalent to deleting the
452 * message from the point of view of the external hooks. And bursting each
453 * message is equivalent to adding a new message.
454 */
455
456 i = inplace ? msgnum + numburst : mp->hghmsg;
457 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
458 char *tempfile;
459
460 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
461 die("unable to create temporary file in %s",
462 get_temp_dir());
463 }
464 strncpy (f2, tempfile, sizeof(f2));
465 strncpy (f1, m_name (i), sizeof(f1));
466
467 if (verbosw && i != msgnum)
468 printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
469
470 chmod (f2, mode);
471 fseeko (in, smsgs[j].s_start, SEEK_SET);
472 cpybrst (in, out, msgnam, f2,
473 (int) (smsgs[j].s_stop - smsgs[j].s_start), mimesw);
474 fclose (out);
475
476 if (i == msgnum) {
477 strncpy (f3, m_backup (f1), sizeof(f3));
478 if (rename (f1, f3) == NOTOK)
479 admonish (f3, "unable to rename %s to", f1);
480
481 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
482 ext_hook("del-hook", f3, NULL);
483 }
484 if (rename (f2, f1) == NOTOK)
485 admonish (f1, "unable to rename %s to", f2);
486
487 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
488 ext_hook("add-hook", f3, NULL);
489
490 copy_msg_flags (mp, i, msgnum);
491 mp->msgflags |= SEQMOD;
492 }
493
494 fclose (in);
495 }
496
497
498 #define S1 0
499 #define S2 1
500 #define S3 2
501 #define S4 3
502
503 /*
504 * Copy a message which is being burst out of a digest.
505 * It will remove any "dashstuffing" in the message.
506 */
507
508 static void
509 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len, int mime)
510 {
511 int c, state;
512
513 for (state = mime ? S4 : S1; (c = fgetc (in)) != EOF && len > 0; len--) {
514 if (c == 0)
515 continue;
516 switch (state) {
517 case S1:
518 switch (c) {
519 case '-':
520 state = S3;
521 break;
522
523 default:
524 state = S2;
525 /* FALLTHRU */
526 case '\n':
527 fputc (c, out);
528 break;
529 }
530 break;
531
532 case S2:
533 switch (c) {
534 case '\n':
535 state = S1;
536 /* FALLTHRU */
537 default:
538 fputc (c, out);
539 break;
540 }
541 break;
542
543 case S3:
544 switch (c) {
545 case ' ':
546 state = S2;
547 break;
548
549 default:
550 state = (c == '\n') ? S1 : S2;
551 fputc ('-', out);
552 fputc (c, out);
553 break;
554 }
555 break;
556
557 case S4:
558 fputc (c, out);
559 break;
560 }
561 }
562
563 if (ferror (in) && !feof (in))
564 adios (ifile, "error reading");
565 if (ferror (out))
566 adios (ofile, "error writing");
567 }