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