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