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