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