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