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