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