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