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