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