]> diplodocus.org Git - nmh/blob - uip/burst.c
Fixed three LC_ALL settings:
[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 setlocale(LC_ALL, "");
79 invo_name = r1bindex (argv[0], '/');
80
81 /* read user profile/context */
82 context_read();
83
84 arguments = getarguments (invo_name, argc, argv, 1);
85 argp = arguments;
86
87 while ((cp = *argp++)) {
88 if (*cp == '-') {
89 switch (smatch (++cp, switches)) {
90 case AMBIGSW:
91 ambigsw (cp, switches);
92 done (1);
93 case UNKWNSW:
94 adios (NULL, "-%s unknown\n", cp);
95
96 case HELPSW:
97 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
98 invo_name);
99 print_help (buf, switches, 1);
100 done (0);
101 case VERSIONSW:
102 print_version(invo_name);
103 done (0);
104
105 case INPLSW:
106 inplace++;
107 continue;
108 case NINPLSW:
109 inplace = 0;
110 continue;
111
112 case MIMESW:
113 mimesw = 2;
114 continue;
115 case NMIMESW:
116 mimesw = 0;
117 continue;
118 case AUTOMIMESW:
119 mimesw = 1;
120 continue;
121
122 case QIETSW:
123 quietsw++;
124 continue;
125 case NQIETSW:
126 quietsw = 0;
127 continue;
128
129 case VERBSW:
130 verbosw++;
131 continue;
132 case NVERBSW:
133 verbosw = 0;
134 continue;
135 }
136 }
137 if (*cp == '+' || *cp == '@') {
138 if (folder)
139 adios (NULL, "only one folder at a time!");
140 else
141 folder = pluspath (cp);
142 } else {
143 app_msgarg(&msgs, cp);
144 }
145 }
146
147 if (!context_find ("path"))
148 free (path ("./", TFOLDER));
149 if (!msgs.size)
150 app_msgarg(&msgs, "cur");
151 if (!folder)
152 folder = getfolder (1);
153 maildir = m_maildir (folder);
154
155 if (chdir (maildir) == NOTOK)
156 adios (maildir, "unable to change directory to");
157
158 /* read folder and create message structure */
159 if (!(mp = folder_read (folder, 1)))
160 adios (NULL, "unable to read folder %s", folder);
161
162 /* check for empty folder */
163 if (mp->nummsg == 0)
164 adios (NULL, "no messages in %s", folder);
165
166 /* parse all the message ranges/sequences and set SELECTED */
167 for (msgnum = 0; msgnum < msgs.size; msgnum++)
168 if (!m_convert (mp, msgs.msgs[msgnum]))
169 done (1);
170 seq_setprev (mp); /* set the previous-sequence */
171
172 smsgs = (struct smsg *)
173 calloc ((size_t) (MAXFOLDER + 2), sizeof(*smsgs));
174 if (smsgs == NULL)
175 adios (NULL, "unable to allocate burst storage");
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, numburst > 1 ? "s" : "", msgnum);
186 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw,
187 maildir, mimesw);
188 } else {
189 if (numburst == 0) {
190 if (!quietsw)
191 admonish (NULL, "message %d not in digest format",
192 msgnum);
193 } /* this pair of braces was missing before 1999-07-15 */
194 else
195 adios (NULL, "burst() botch -- you lose big");
196 }
197 }
198 }
199
200 free ((char *) 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 else 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 } else 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 preceeding 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 else
304 pos += (long) strlen (buffer);
305 }
306
307 /*
308 * Only count as a new message if we got the message delimiter.
309 * Swallow a blank line if it was right before the message delimiter.
310 */
311 if (smsgs[msgp].s_start != pos && wasdlm)
312 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
313
314 if (feof (in)) {
315 #if 0
316 if (wasdlm) {
317 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
318 msgp++; /* fake "End of XXX Digest" */
319 }
320 #endif
321 break;
322 }
323 pos += (long) strlen (buffer);
324 }
325
326 fclose (in);
327 return (msgp - 1); /* return the number of messages burst */
328 }
329
330
331 /*
332 * Find any MIME content in the message that is a message/rfc822 and add
333 * it to the list of messages to burst.
334 */
335
336 static void
337 find_mime_parts (CT content, struct smsg *smsgs, int *msgp)
338 {
339 struct multipart *m;
340 struct part *part;
341
342 /*
343 * If we have a message/rfc822, then it's easy.
344 */
345
346 if (content->c_type == CT_MESSAGE &&
347 content->c_subtype == MESSAGE_RFC822) {
348 smsgs[*msgp].s_start = content->c_begin;
349 smsgs[*msgp].s_stop = content->c_end;
350 (*msgp)++;
351 return;
352 }
353
354 /*
355 * Otherwise, if we do have multiparts, try all of the sub-parts.
356 */
357
358 if (content->c_type == CT_MULTIPART) {
359 m = (struct multipart *) content->c_ctparams;
360
361 for (part = m->mp_parts; part; part = part->mp_next)
362 find_mime_parts(part->mp_part, smsgs, msgp);
363 }
364
365 return;
366 }
367
368
369 /*
370 * Burst out the messages in the digest into the folder
371 */
372
373 static void
374 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
375 int inplace, int verbosw, char *maildir, int mimesw)
376 {
377 int i, j, mode;
378 char *msgnam;
379 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
380 FILE *in, *out;
381 struct stat st;
382 struct msgs *mp;
383
384 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
385 adios (msgnam, "unable to read message");
386
387 mode =
388 fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot();
389 mp = *mpp;
390
391 /*
392 * See if we have enough space in the folder
393 * structure for all the new messages.
394 */
395 if ((mp->hghmsg + numburst > mp->hghoff) &&
396 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
397 adios (NULL, "unable to allocate folder storage");
398 *mpp = mp;
399
400 j = mp->hghmsg; /* old value */
401 mp->hghmsg += numburst;
402 mp->nummsg += numburst;
403
404 /*
405 * If this is not the highest SELECTED message, then
406 * increment mp->hghsel by numburst, since the highest
407 * SELECTED is about to be slid down by that amount.
408 */
409 if (msgnum < mp->hghsel)
410 mp->hghsel += numburst;
411
412 /*
413 * If -inplace is given, renumber the messages after the
414 * source message, to make room for each of the messages
415 * contained within the digest.
416 *
417 * This is equivalent to refiling a message from the point
418 * of view of the external hooks.
419 */
420 if (inplace) {
421 for (i = mp->hghmsg; j > msgnum; i--, j--) {
422 strncpy (f1, m_name (i), sizeof(f1));
423 strncpy (f2, m_name (j), sizeof(f2));
424 if (does_exist (mp, j)) {
425 if (verbosw)
426 printf ("message %d becomes message %d\n", j, i);
427
428 if (rename (f2, f1) == NOTOK)
429 admonish (f1, "unable to rename %s to", f2);
430
431 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
432 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
433 ext_hook("ref-hook", f1, f2);
434
435 copy_msg_flags (mp, i, j);
436 clear_msg_flags (mp, j);
437 mp->msgflags |= SEQMOD;
438 }
439 }
440 }
441
442 unset_selected (mp, msgnum);
443
444 /* new hghmsg is hghmsg + numburst
445 *
446 * At this point, there is an array of numburst smsgs, each element of
447 * which contains the starting and stopping offsets (seeks) of the message
448 * in the digest. The inplace flag is set if the original digest is replaced
449 * by a message containing the table of contents. smsgs[0] is that table of
450 * contents. Go through the message numbers in reverse order (high to low).
451 *
452 * Set f1 to the name of the destination message, f2 to the name of a scratch
453 * file. Extract a message from the digest to the scratch file. Move the
454 * original message to a backup file if the destination message number is the
455 * same as the number of the original message, which only happens if the
456 * inplace flag is set. Then move the scratch file to the destination message.
457 *
458 * Moving the original message to the backup file is equivalent to deleting the
459 * message from the point of view of the external hooks. And bursting each
460 * message is equivalent to adding a new message.
461 */
462
463 i = inplace ? msgnum + numburst : mp->hghmsg;
464 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
465 strncpy (f1, m_name (i), sizeof(f1));
466 strncpy (f2, m_mktemp(invo_name, NULL, &out), sizeof(f2));
467
468 if (verbosw && i != msgnum)
469 printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
470
471 chmod (f2, mode);
472 fseeko (in, smsgs[j].s_start, SEEK_SET);
473 cpybrst (in, out, msgnam, f2,
474 (int) (smsgs[j].s_stop - smsgs[j].s_start), mimesw);
475 fclose (out);
476
477 if (i == msgnum) {
478 strncpy (f3, m_backup (f1), sizeof(f3));
479 if (rename (f1, f3) == NOTOK)
480 admonish (f3, "unable to rename %s to", f1);
481
482 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
483 ext_hook("del-hook", f3, (char *)0);
484 }
485 if (rename (f2, f1) == NOTOK)
486 admonish (f1, "unable to rename %s to", f2);
487
488 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
489 ext_hook("add-hook", f3, (char *)0);
490
491 copy_msg_flags (mp, i, msgnum);
492 mp->msgflags |= SEQMOD;
493 }
494
495 fclose (in);
496 }
497
498
499 #define S1 0
500 #define S2 1
501 #define S3 2
502 #define S4 3
503
504 /*
505 * Copy a mesage which is being burst out of a digest.
506 * It will remove any "dashstuffing" in the message.
507 */
508
509 static void
510 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len, int mime)
511 {
512 register int c, state;
513
514 for (state = mime ? S4 : S1; (c = fgetc (in)) != EOF && len > 0; len--) {
515 if (c == 0)
516 continue;
517 switch (state) {
518 case S1:
519 switch (c) {
520 case '-':
521 state = S3;
522 break;
523
524 default:
525 state = S2;
526 case '\n':
527 fputc (c, out);
528 break;
529 }
530 break;
531
532 case S2:
533 switch (c) {
534 case '\n':
535 state = S1;
536 default:
537 fputc (c, out);
538 break;
539 }
540 break;
541
542 case S3:
543 switch (c) {
544 case ' ':
545 state = S2;
546 break;
547
548 default:
549 state = (c == '\n') ? S1 : S2;
550 fputc ('-', out);
551 fputc (c, out);
552 break;
553 }
554 break;
555
556 case S4:
557 fputc (c, out);
558 break;
559 }
560 }
561
562 if (ferror (in) && !feof (in))
563 adios (ifile, "error reading");
564 if (ferror (out))
565 adios (ofile, "error writing");
566 }