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