]> diplodocus.org Git - nmh/blob - uip/burst.c
Use pref_encoding() to select the default encoding for all headers.
[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 pid_t xpid = 0;
46
47 /*
48 * static prototypes
49 */
50 static int find_delim (int, struct smsg *, int *);
51 static void find_mime_parts (CT, struct smsg *, int *);
52 static void burst (struct msgs **, int, struct smsg *, int, int, int,
53 char *, int);
54 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
55
56 /*
57 * A macro to check to see if we have reached a message delimiter
58 * (an encapsulation boundary, EB, in RFC 934 parlance).
59 *
60 * According to RFC 934, an EB is simply a line which starts with
61 * a "-" and is NOT followed by a space. So even a single "-" on a line
62 * by itself would be an EB.
63 */
64
65 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
66
67 int
68 main (int argc, char **argv)
69 {
70 int inplace = 0, quietsw = 0, verbosw = 0, mimesw = 1;
71 int hi, msgnum, numburst;
72 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
73 char **argp, **arguments;
74 struct msgs_array msgs = { 0, 0, NULL };
75 struct smsg *smsgs;
76 struct msgs *mp;
77
78 #ifdef LOCALE
79 setlocale(LC_ALL, "");
80 #endif
81 invo_name = r1bindex (argv[0], '/');
82
83 /* read user profile/context */
84 context_read();
85
86 arguments = getarguments (invo_name, argc, argv, 1);
87 argp = arguments;
88
89 while ((cp = *argp++)) {
90 if (*cp == '-') {
91 switch (smatch (++cp, switches)) {
92 case AMBIGSW:
93 ambigsw (cp, switches);
94 done (1);
95 case UNKWNSW:
96 adios (NULL, "-%s unknown\n", cp);
97
98 case HELPSW:
99 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
100 invo_name);
101 print_help (buf, switches, 1);
102 done (0);
103 case VERSIONSW:
104 print_version(invo_name);
105 done (0);
106
107 case INPLSW:
108 inplace++;
109 continue;
110 case NINPLSW:
111 inplace = 0;
112 continue;
113
114 case MIMESW:
115 mimesw = 2;
116 continue;
117 case NMIMESW:
118 mimesw = 0;
119 continue;
120 case AUTOMIMESW:
121 mimesw = 1;
122 continue;
123
124 case QIETSW:
125 quietsw++;
126 continue;
127 case NQIETSW:
128 quietsw = 0;
129 continue;
130
131 case VERBSW:
132 verbosw++;
133 continue;
134 case NVERBSW:
135 verbosw = 0;
136 continue;
137 }
138 }
139 if (*cp == '+' || *cp == '@') {
140 if (folder)
141 adios (NULL, "only one folder at a time!");
142 else
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 adios (NULL, "unable to read folder %s", folder);
163
164 /* check for empty folder */
165 if (mp->nummsg == 0)
166 adios (NULL, "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 = (struct smsg *)
175 calloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
176 if (smsgs == NULL)
177 adios (NULL, "unable to allocate burst storage");
178
179 hi = mp->hghmsg + 1;
180
181 /* burst all the SELECTED messages */
182 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
183 if (is_selected (mp, msgnum)) {
184 if ((numburst = find_delim (msgnum, smsgs, &mimesw)) >= 1) {
185 if (verbosw)
186 printf ("%d message%s exploded from digest %d\n",
187 numburst, numburst > 1 ? "s" : "", msgnum);
188 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw,
189 maildir, mimesw);
190 } else {
191 if (numburst == 0) {
192 if (!quietsw)
193 admonish (NULL, "message %d not in digest format",
194 msgnum);
195 } /* this pair of braces was missing before 1999-07-15 */
196 else
197 adios (NULL, "burst() botch -- you lose big");
198 }
199 }
200 }
201
202 free ((char *) smsgs);
203 context_replace (pfolder, folder); /* update current folder */
204
205 /*
206 * If -inplace is given, then the first message burst becomes
207 * the current message (which will now show a table of contents).
208 * Otherwise, the first message extracted from the first digest
209 * becomes the current message.
210 */
211 if (inplace) {
212 if (mp->lowsel != mp->curmsg)
213 seq_setcur (mp, mp->lowsel);
214 } else {
215 if (hi <= mp->hghmsg)
216 seq_setcur (mp, hi);
217 }
218
219 seq_save (mp); /* synchronize message sequences */
220 context_save (); /* save the context file */
221 folder_free (mp); /* free folder/message structure */
222 done (0);
223 return 1;
224 }
225
226
227 /*
228 * Scan the message and find the beginning and
229 * end of all the messages in the digest.
230 *
231 * If requested, see if the message is MIME-formatted and contains any
232 * message/rfc822 parts; if so, burst those parts.
233 */
234
235 static int
236 find_delim (int msgnum, struct smsg *smsgs, int *mimesw)
237 {
238 int wasdlm = 0, msgp;
239 off_t pos;
240 char c, *msgnam;
241 char buffer[BUFSIZ];
242 FILE *in;
243 CT content;
244
245 msgnam = m_name (msgnum);
246
247 /*
248 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
249 */
250
251 if (*mimesw > 0) {
252 content = parse_mime(msgnam);
253 if (! content && *mimesw == 2)
254 return 0;
255 else if (content) {
256 smsgs[0].s_start = 0;
257 smsgs[0].s_stop = content->c_begin - 1;
258 msgp = 1;
259 find_mime_parts(content, smsgs, &msgp);
260 free_content(content);
261 if (msgp == 1 && *mimesw == 2) {
262 adios (msgnam, "does not have any message/rfc822 parts");
263 } else if (msgp > 1) {
264 *mimesw = 1;
265 return (msgp - 1);
266 }
267 }
268 }
269
270 *mimesw = 0;
271
272 if ((in = fopen (msgnam, "r")) == NULL)
273 adios (msgnam, "unable to read message");
274
275 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
276 /*
277 * We're either at the beginning of the whole message, or
278 * we're just past the delimiter of the last message.
279 * Swallow lines until we get to something that's not a newline
280 */
281 while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
282 pos += (long) strlen (buffer);
283 if (feof (in))
284 break;
285
286 /*
287 * Reset to the beginning of the last non-blank line, and save our
288 * starting position. This is where the encapsulated message
289 * starts.
290 */
291 fseeko (in, pos, SEEK_SET);
292 smsgs[msgp].s_start = pos;
293
294 /*
295 * Read in lines until we get to a message delimiter.
296 *
297 * Previously we checked to make sure the preceeding line and
298 * next line was a newline. That actually does not comply with
299 * RFC 934, so make sure we break on a message delimiter even
300 * if the previous character was NOT a newline.
301 */
302 for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
303 if ((wasdlm = CHECKDELIM(buffer)))
304 break;
305 else
306 pos += (long) strlen (buffer);
307 }
308
309 /*
310 * Only count as a new message if we got the message delimiter.
311 * Swallow a blank line if it was right before the message delimiter.
312 */
313 if (smsgs[msgp].s_start != pos && wasdlm)
314 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
315
316 if (feof (in)) {
317 #if 0
318 if (wasdlm) {
319 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
320 msgp++; /* fake "End of XXX Digest" */
321 }
322 #endif
323 break;
324 }
325 pos += (long) strlen (buffer);
326 }
327
328 fclose (in);
329 return (msgp - 1); /* return the number of messages burst */
330 }
331
332
333 /*
334 * Find any MIME content in the message that is a message/rfc822 and add
335 * it to the list of messages to burst.
336 */
337
338 static void
339 find_mime_parts (CT content, struct smsg *smsgs, int *msgp)
340 {
341 struct multipart *m;
342 struct part *part;
343
344 /*
345 * If we have a message/rfc822, then it's easy.
346 */
347
348 if (content->c_type == CT_MESSAGE &&
349 content->c_subtype == MESSAGE_RFC822) {
350 smsgs[*msgp].s_start = content->c_begin;
351 smsgs[*msgp].s_stop = content->c_end;
352 (*msgp)++;
353 return;
354 }
355
356 /*
357 * Otherwise, if we do have multiparts, try all of the sub-parts.
358 */
359
360 if (content->c_type == CT_MULTIPART) {
361 m = (struct multipart *) content->c_ctparams;
362
363 for (part = m->mp_parts; part; part = part->mp_next)
364 find_mime_parts(part->mp_part, smsgs, msgp);
365 }
366
367 return;
368 }
369
370
371 /*
372 * Burst out the messages in the digest into the folder
373 */
374
375 static void
376 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
377 int inplace, int verbosw, char *maildir, int mimesw)
378 {
379 int i, j, mode;
380 char *msgnam;
381 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
382 FILE *in, *out;
383 struct stat st;
384 struct msgs *mp;
385
386 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
387 adios (msgnam, "unable to read message");
388
389 mode =
390 fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot();
391 mp = *mpp;
392
393 /*
394 * See if we have enough space in the folder
395 * structure for all the new messages.
396 */
397 if ((mp->hghmsg + numburst > mp->hghoff) &&
398 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
399 adios (NULL, "unable to allocate folder storage");
400 *mpp = mp;
401
402 j = mp->hghmsg; /* old value */
403 mp->hghmsg += numburst;
404 mp->nummsg += numburst;
405
406 /*
407 * If this is not the highest SELECTED message, then
408 * increment mp->hghsel by numburst, since the highest
409 * SELECTED is about to be slid down by that amount.
410 */
411 if (msgnum < mp->hghsel)
412 mp->hghsel += numburst;
413
414 /*
415 * If -inplace is given, renumber the messages after the
416 * source message, to make room for each of the messages
417 * contained within the digest.
418 *
419 * This is equivalent to refiling a message from the point
420 * of view of the external hooks.
421 */
422 if (inplace) {
423 for (i = mp->hghmsg; j > msgnum; i--, j--) {
424 strncpy (f1, m_name (i), sizeof(f1));
425 strncpy (f2, m_name (j), sizeof(f2));
426 if (does_exist (mp, j)) {
427 if (verbosw)
428 printf ("message %d becomes message %d\n", j, i);
429
430 if (rename (f2, f1) == NOTOK)
431 admonish (f1, "unable to rename %s to", f2);
432
433 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
434 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
435 ext_hook("ref-hook", f1, f2);
436
437 copy_msg_flags (mp, i, j);
438 clear_msg_flags (mp, j);
439 mp->msgflags |= SEQMOD;
440 }
441 }
442 }
443
444 unset_selected (mp, msgnum);
445
446 /* new hghmsg is hghmsg + numburst
447 *
448 * At this point, there is an array of numburst smsgs, each element of
449 * which contains the starting and stopping offsets (seeks) of the message
450 * in the digest. The inplace flag is set if the original digest is replaced
451 * by a message containing the table of contents. smsgs[0] is that table of
452 * contents. Go through the message numbers in reverse order (high to low).
453 *
454 * Set f1 to the name of the destination message, f2 to the name of a scratch
455 * file. Extract a message from the digest to the scratch file. Move the
456 * original message to a backup file if the destination message number is the
457 * same as the number of the original message, which only happens if the
458 * inplace flag is set. Then move the scratch file to the destination message.
459 *
460 * Moving the original message to the backup file is equivalent to deleting the
461 * message from the point of view of the external hooks. And bursting each
462 * message is equivalent to adding a new message.
463 */
464
465 i = inplace ? msgnum + numburst : mp->hghmsg;
466 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
467 strncpy (f1, m_name (i), sizeof(f1));
468 strncpy (f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
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, (char *)0);
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, (char *)0);
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 mesage 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 register 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 case '\n':
529 fputc (c, out);
530 break;
531 }
532 break;
533
534 case S2:
535 switch (c) {
536 case '\n':
537 state = S1;
538 default:
539 fputc (c, out);
540 break;
541 }
542 break;
543
544 case S3:
545 switch (c) {
546 case ' ':
547 state = S2;
548 break;
549
550 default:
551 state = (c == '\n') ? S1 : S2;
552 fputc ('-', out);
553 fputc (c, out);
554 break;
555 }
556 break;
557
558 case S4:
559 fputc (c, out);
560 break;
561 }
562 }
563
564 if (ferror (in) && !feof (in))
565 adios (ifile, "error reading");
566 if (ferror (out))
567 adios (ofile, "error writing");
568 }