]> diplodocus.org Git - nmh/blob - uip/burst.c
Alter HasSuffixC()'s char * to be const.
[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
46 /*
47 * static prototypes
48 */
49 static int find_delim (int, struct smsg *, int *);
50 static void find_mime_parts (CT, struct smsg *, int *);
51 static void burst (struct msgs **, int, struct smsg *, int, int, int,
52 char *, int);
53 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
54
55 /*
56 * A macro to check to see if we have reached a message delimiter
57 * (an encapsulation boundary, EB, in RFC 934 parlance).
58 *
59 * According to RFC 934, an EB is simply a line which starts with
60 * a "-" and is NOT followed by a space. So even a single "-" on a line
61 * by itself would be an EB.
62 */
63
64 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
65
66 int
67 main (int argc, char **argv)
68 {
69 int inplace = 0, quietsw = 0, verbosw = 0, mimesw = 1;
70 int hi, msgnum, numburst;
71 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
72 char **argp, **arguments;
73 struct msgs_array msgs = { 0, 0, NULL };
74 struct smsg *smsgs;
75 struct msgs *mp;
76
77 if (nmh_init(argv[0], 1)) { return 1; }
78
79 arguments = getarguments (invo_name, argc, argv, 1);
80 argp = arguments;
81
82 while ((cp = *argp++)) {
83 if (*cp == '-') {
84 switch (smatch (++cp, switches)) {
85 case AMBIGSW:
86 ambigsw (cp, switches);
87 done (1);
88 case UNKWNSW:
89 adios (NULL, "-%s unknown\n", cp);
90
91 case HELPSW:
92 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
93 invo_name);
94 print_help (buf, switches, 1);
95 done (0);
96 case VERSIONSW:
97 print_version(invo_name);
98 done (0);
99
100 case INPLSW:
101 inplace++;
102 continue;
103 case NINPLSW:
104 inplace = 0;
105 continue;
106
107 case MIMESW:
108 mimesw = 2;
109 continue;
110 case NMIMESW:
111 mimesw = 0;
112 continue;
113 case AUTOMIMESW:
114 mimesw = 1;
115 continue;
116
117 case QIETSW:
118 quietsw++;
119 continue;
120 case NQIETSW:
121 quietsw = 0;
122 continue;
123
124 case VERBSW:
125 verbosw++;
126 continue;
127 case NVERBSW:
128 verbosw = 0;
129 continue;
130 }
131 }
132 if (*cp == '+' || *cp == '@') {
133 if (folder)
134 adios (NULL, "only one folder at a time!");
135 else
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, numburst > 1 ? "s" : "", msgnum);
178 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw,
179 maildir, mimesw);
180 } else {
181 if (numburst == 0) {
182 if (!quietsw)
183 admonish (NULL, "message %d not in digest format",
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 } else 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 else
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 return;
358 }
359
360
361 /*
362 * Burst out the messages in the digest into the folder
363 */
364
365 static void
366 burst (struct msgs **mpp, int msgnum, struct smsg *smsgs, int numburst,
367 int inplace, int verbosw, char *maildir, int mimesw)
368 {
369 int i, j, mode;
370 char *msgnam;
371 char f1[BUFSIZ], f2[BUFSIZ], f3[BUFSIZ];
372 FILE *in, *out;
373 struct stat st;
374 struct msgs *mp;
375
376 if ((in = fopen (msgnam = m_name (msgnum), "r")) == NULL)
377 adios (msgnam, "unable to read message");
378
379 mode =
380 fstat (fileno(in), &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot();
381 mp = *mpp;
382
383 /*
384 * See if we have enough space in the folder
385 * structure for all the new messages.
386 */
387 if ((mp->hghmsg + numburst > mp->hghoff) &&
388 !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + numburst)))
389 adios (NULL, "unable to allocate folder storage");
390 *mpp = mp;
391
392 j = mp->hghmsg; /* old value */
393 mp->hghmsg += numburst;
394 mp->nummsg += numburst;
395
396 /*
397 * If this is not the highest SELECTED message, then
398 * increment mp->hghsel by numburst, since the highest
399 * SELECTED is about to be slid down by that amount.
400 */
401 if (msgnum < mp->hghsel)
402 mp->hghsel += numburst;
403
404 /*
405 * If -inplace is given, renumber the messages after the
406 * source message, to make room for each of the messages
407 * contained within the digest.
408 *
409 * This is equivalent to refiling a message from the point
410 * of view of the external hooks.
411 */
412 if (inplace) {
413 for (i = mp->hghmsg; j > msgnum; i--, j--) {
414 strncpy (f1, m_name (i), sizeof(f1));
415 strncpy (f2, m_name (j), sizeof(f2));
416 if (does_exist (mp, j)) {
417 if (verbosw)
418 printf ("message %d becomes message %d\n", j, i);
419
420 if (rename (f2, f1) == NOTOK)
421 admonish (f1, "unable to rename %s to", f2);
422
423 (void)snprintf(f1, sizeof (f1), "%s/%d", maildir, i);
424 (void)snprintf(f2, sizeof (f2), "%s/%d", maildir, j);
425 ext_hook("ref-hook", f1, f2);
426
427 copy_msg_flags (mp, i, j);
428 clear_msg_flags (mp, j);
429 mp->msgflags |= SEQMOD;
430 }
431 }
432 }
433
434 unset_selected (mp, msgnum);
435
436 /* new hghmsg is hghmsg + numburst
437 *
438 * At this point, there is an array of numburst smsgs, each element of
439 * which contains the starting and stopping offsets (seeks) of the message
440 * in the digest. The inplace flag is set if the original digest is replaced
441 * by a message containing the table of contents. smsgs[0] is that table of
442 * contents. Go through the message numbers in reverse order (high to low).
443 *
444 * Set f1 to the name of the destination message, f2 to the name of a scratch
445 * file. Extract a message from the digest to the scratch file. Move the
446 * original message to a backup file if the destination message number is the
447 * same as the number of the original message, which only happens if the
448 * inplace flag is set. Then move the scratch file to the destination message.
449 *
450 * Moving the original message to the backup file is equivalent to deleting the
451 * message from the point of view of the external hooks. And bursting each
452 * message is equivalent to adding a new message.
453 */
454
455 i = inplace ? msgnum + numburst : mp->hghmsg;
456 for (j = numburst; j >= (inplace ? 0 : 1); i--, j--) {
457 char *tempfile;
458
459 if ((tempfile = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
460 adios(NULL, "unable to create temporary file in %s",
461 get_temp_dir());
462 }
463 strncpy (f2, tempfile, sizeof(f2));
464 strncpy (f1, m_name (i), sizeof(f1));
465
466 if (verbosw && i != msgnum)
467 printf ("message %d of digest %d becomes message %d\n", j, msgnum, i);
468
469 chmod (f2, mode);
470 fseeko (in, smsgs[j].s_start, SEEK_SET);
471 cpybrst (in, out, msgnam, f2,
472 (int) (smsgs[j].s_stop - smsgs[j].s_start), mimesw);
473 fclose (out);
474
475 if (i == msgnum) {
476 strncpy (f3, m_backup (f1), sizeof(f3));
477 if (rename (f1, f3) == NOTOK)
478 admonish (f3, "unable to rename %s to", f1);
479
480 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
481 ext_hook("del-hook", f3, NULL);
482 }
483 if (rename (f2, f1) == NOTOK)
484 admonish (f1, "unable to rename %s to", f2);
485
486 (void)snprintf(f3, sizeof (f3), "%s/%d", maildir, i);
487 ext_hook("add-hook", f3, NULL);
488
489 copy_msg_flags (mp, i, msgnum);
490 mp->msgflags |= SEQMOD;
491 }
492
493 fclose (in);
494 }
495
496
497 #define S1 0
498 #define S2 1
499 #define S3 2
500 #define S4 3
501
502 /*
503 * Copy a message which is being burst out of a digest.
504 * It will remove any "dashstuffing" in the message.
505 */
506
507 static void
508 cpybrst (FILE *in, FILE *out, char *ifile, char *ofile, int len, int mime)
509 {
510 int c, state;
511
512 for (state = mime ? S4 : S1; (c = fgetc (in)) != EOF && len > 0; len--) {
513 if (c == 0)
514 continue;
515 switch (state) {
516 case S1:
517 switch (c) {
518 case '-':
519 state = S3;
520 break;
521
522 default:
523 state = S2;
524 case '\n':
525 fputc (c, out);
526 break;
527 }
528 break;
529
530 case S2:
531 switch (c) {
532 case '\n':
533 state = S1;
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 }