]> diplodocus.org Git - nmh/blob - uip/burst.c
sbr/dtime.c: Remove struct-assigning twscopy().
[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
12 #define BURST_SWITCHES \
13 X("inplace", 0, INPLSW) \
14 X("noinplace", 0, NINPLSW) \
15 X("mime", 0, MIMESW) \
16 X("nomime", 0, NMIMESW) \
17 X("automime", 0, AUTOMIMESW) \
18 X("quiet", 0, QIETSW) \
19 X("noquiet", 0, NQIETSW) \
20 X("verbose", 0, VERBSW) \
21 X("noverbose", 0, NVERBSW) \
22 X("version", 0, VERSIONSW) \
23 X("help", 0, HELPSW) \
24
25 #define X(sw, minchars, id) id,
26 DEFINE_SWITCH_ENUM(BURST);
27 #undef X
28
29 #define X(sw, minchars, id) { sw, minchars, id },
30 DEFINE_SWITCH_ARRAY(BURST, switches);
31 #undef X
32
33 struct smsg {
34 off_t s_start;
35 off_t s_stop;
36 };
37
38 /*
39 * For the MIME parsing routines
40 */
41
42 int debugsw = 0;
43
44 /*
45 * static prototypes
46 */
47 static int find_delim (int, struct smsg *, int *);
48 static void find_mime_parts (CT, struct smsg *, int *);
49 static void burst (struct msgs **, int, struct smsg *, int, int, int,
50 char *, int);
51 static void cpybrst (FILE *, FILE *, char *, char *, int, int);
52
53 /*
54 * A macro to check to see if we have reached a message delimiter
55 * (an encapsulation boundary, EB, in RFC 934 parlance).
56 *
57 * According to RFC 934, an EB is simply a line which starts with
58 * a "-" and is NOT followed by a space. So even a single "-" on a line
59 * by itself would be an EB.
60 */
61
62 #define CHECKDELIM(buffer) (buffer[0] == '-' && buffer[1] != ' ')
63
64 int
65 main (int argc, char **argv)
66 {
67 int inplace = 0, quietsw = 0, verbosw = 0, mimesw = 1;
68 int hi, msgnum, numburst;
69 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
70 char **argp, **arguments;
71 struct msgs_array msgs = { 0, 0, NULL };
72 struct smsg *smsgs;
73 struct msgs *mp;
74
75 if (nmh_init(argv[0], 1)) { return 1; }
76
77 arguments = getarguments (invo_name, argc, argv, 1);
78 argp = arguments;
79
80 while ((cp = *argp++)) {
81 if (*cp == '-') {
82 switch (smatch (++cp, switches)) {
83 case AMBIGSW:
84 ambigsw (cp, switches);
85 done (1);
86 case UNKWNSW:
87 adios (NULL, "-%s unknown\n", cp);
88
89 case HELPSW:
90 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
91 invo_name);
92 print_help (buf, switches, 1);
93 done (0);
94 case VERSIONSW:
95 print_version(invo_name);
96 done (0);
97
98 case INPLSW:
99 inplace++;
100 continue;
101 case NINPLSW:
102 inplace = 0;
103 continue;
104
105 case MIMESW:
106 mimesw = 2;
107 continue;
108 case NMIMESW:
109 mimesw = 0;
110 continue;
111 case AUTOMIMESW:
112 mimesw = 1;
113 continue;
114
115 case QIETSW:
116 quietsw++;
117 continue;
118 case NQIETSW:
119 quietsw = 0;
120 continue;
121
122 case VERBSW:
123 verbosw++;
124 continue;
125 case NVERBSW:
126 verbosw = 0;
127 continue;
128 }
129 }
130 if (*cp == '+' || *cp == '@') {
131 if (folder)
132 adios (NULL, "only one folder at a time!");
133 else
134 folder = pluspath (cp);
135 } else {
136 app_msgarg(&msgs, cp);
137 }
138 }
139
140 if (!context_find ("path"))
141 free (path ("./", TFOLDER));
142 if (!msgs.size)
143 app_msgarg(&msgs, "cur");
144 if (!folder)
145 folder = getfolder (1);
146 maildir = m_maildir (folder);
147
148 if (chdir (maildir) == NOTOK)
149 adios (maildir, "unable to change directory to");
150
151 /* read folder and create message structure */
152 if (!(mp = folder_read (folder, 1)))
153 adios (NULL, "unable to read folder %s", folder);
154
155 /* check for empty folder */
156 if (mp->nummsg == 0)
157 adios (NULL, "no messages in %s", folder);
158
159 /* parse all the message ranges/sequences and set SELECTED */
160 for (msgnum = 0; msgnum < msgs.size; msgnum++)
161 if (!m_convert (mp, msgs.msgs[msgnum]))
162 done (1);
163 seq_setprev (mp); /* set the previous-sequence */
164
165 smsgs = mh_xcalloc(MAXFOLDER + 2, sizeof *smsgs);
166
167 hi = mp->hghmsg + 1;
168
169 /* burst all the SELECTED messages */
170 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
171 if (is_selected (mp, msgnum)) {
172 if ((numburst = find_delim (msgnum, smsgs, &mimesw)) >= 1) {
173 if (verbosw)
174 printf ("%d message%s exploded from digest %d\n",
175 numburst, numburst > 1 ? "s" : "", msgnum);
176 burst (&mp, msgnum, smsgs, numburst, inplace, verbosw,
177 maildir, mimesw);
178 } else {
179 if (numburst == 0) {
180 if (!quietsw)
181 inform("message %d not in digest format, continuing...",
182 msgnum);
183 } /* this pair of braces was missing before 1999-07-15 */
184 else
185 adios (NULL, "burst() botch -- you lose big");
186 }
187 }
188 }
189
190 free(smsgs);
191 context_replace (pfolder, folder); /* update current folder */
192
193 /*
194 * If -inplace is given, then the first message burst becomes
195 * the current message (which will now show a table of contents).
196 * Otherwise, the first message extracted from the first digest
197 * becomes the current message.
198 */
199 if (inplace) {
200 if (mp->lowsel != mp->curmsg)
201 seq_setcur (mp, mp->lowsel);
202 } else {
203 if (hi <= mp->hghmsg)
204 seq_setcur (mp, hi);
205 }
206
207 seq_save (mp); /* synchronize message sequences */
208 context_save (); /* save the context file */
209 folder_free (mp); /* free folder/message structure */
210 done (0);
211 return 1;
212 }
213
214
215 /*
216 * Scan the message and find the beginning and
217 * end of all the messages in the digest.
218 *
219 * If requested, see if the message is MIME-formatted and contains any
220 * message/rfc822 parts; if so, burst those parts.
221 */
222
223 static int
224 find_delim (int msgnum, struct smsg *smsgs, int *mimesw)
225 {
226 int wasdlm = 0, msgp;
227 off_t pos;
228 char c, *msgnam;
229 char buffer[BUFSIZ];
230 FILE *in;
231 CT content;
232
233 msgnam = m_name (msgnum);
234
235 /*
236 * If mimesw is 1 or 2, try to see if it's got proper MIME formatting.
237 */
238
239 if (*mimesw > 0) {
240 content = parse_mime(msgnam);
241 if (! content && *mimesw == 2)
242 return 0;
243 if (content) {
244 smsgs[0].s_start = 0;
245 smsgs[0].s_stop = content->c_begin - 1;
246 msgp = 1;
247 find_mime_parts(content, smsgs, &msgp);
248 free_content(content);
249 if (msgp == 1 && *mimesw == 2) {
250 adios (msgnam, "does not have any message/rfc822 parts");
251 } else if (msgp > 1) {
252 *mimesw = 1;
253 return (msgp - 1);
254 }
255 }
256 }
257
258 *mimesw = 0;
259
260 if ((in = fopen (msgnam, "r")) == NULL)
261 adios (msgnam, "unable to read message");
262
263 for (msgp = 0, pos = 0L; msgp <= MAXFOLDER;) {
264 /*
265 * We're either at the beginning of the whole message, or
266 * we're just past the delimiter of the last message.
267 * Swallow lines until we get to something that's not a newline
268 */
269 while (fgets (buffer, sizeof(buffer), in) && buffer[0] == '\n')
270 pos += (long) strlen (buffer);
271 if (feof (in))
272 break;
273
274 /*
275 * Reset to the beginning of the last non-blank line, and save our
276 * starting position. This is where the encapsulated message
277 * starts.
278 */
279 fseeko (in, pos, SEEK_SET);
280 smsgs[msgp].s_start = pos;
281
282 /*
283 * Read in lines until we get to a message delimiter.
284 *
285 * Previously we checked to make sure the preceding line and
286 * next line was a newline. That actually does not comply with
287 * RFC 934, so make sure we break on a message delimiter even
288 * if the previous character was NOT a newline.
289 */
290 for (c = 0; fgets (buffer, sizeof(buffer), in); c = buffer[0]) {
291 if ((wasdlm = CHECKDELIM(buffer)))
292 break;
293 else
294 pos += (long) strlen (buffer);
295 }
296
297 /*
298 * Only count as a new message if we got the message delimiter.
299 * Swallow a blank line if it was right before the message delimiter.
300 */
301 if (smsgs[msgp].s_start != pos && wasdlm)
302 smsgs[msgp++].s_stop = (c == '\n' && wasdlm) ? pos - 1 : pos;
303
304 if (feof (in)) {
305 #if 0
306 if (wasdlm) {
307 smsgs[msgp - 1].s_stop -= ((long) strlen (buffer) + 1);
308 msgp++; /* fake "End of XXX Digest" */
309 }
310 #endif
311 break;
312 }
313 pos += (long) strlen (buffer);
314 }
315
316 fclose (in);
317 return (msgp - 1); /* return the number of messages burst */
318 }
319
320
321 /*
322 * Find any MIME content in the message that is a message/rfc822 and add
323 * it to the list of messages to burst.
324 */
325
326 static void
327 find_mime_parts (CT content, struct smsg *smsgs, int *msgp)
328 {
329 struct multipart *m;
330 struct part *part;
331
332 /*
333 * If we have a message/rfc822, then it's easy.
334 */
335
336 if (content->c_type == CT_MESSAGE &&
337 content->c_subtype == MESSAGE_RFC822) {
338 smsgs[*msgp].s_start = content->c_begin;
339 smsgs[*msgp].s_stop = content->c_end;
340 (*msgp)++;
341 return;
342 }
343
344 /*
345 * Otherwise, if we do have multiparts, try all of the sub-parts.
346 */
347
348 if (content->c_type == CT_MULTIPART) {
349 m = (struct multipart *) content->c_ctparams;
350
351 for (part = m->mp_parts; part; part = part->mp_next)
352 find_mime_parts(part->mp_part, smsgs, msgp);
353 }
354
355 return;
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 }