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