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