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