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