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