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