]> diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
7b1dbd519b5c3655eed7d734b5ed4b5ac94b6d0b
[nmh] / uip / mhstoresbr.c
1
2 /*
3 * mhstoresbr.c -- routines to save/store the contents of MIME 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 <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/utils.h>
19
20
21 /*
22 * The list of top-level contents to display
23 */
24 extern CT *cts;
25
26 int autosw = 0;
27
28 /*
29 * Cache of current directory. This must be
30 * set before these routines are called.
31 */
32 char *cwd;
33
34 /*
35 * The directory in which to store the contents.
36 */
37 static char *dir;
38
39 /*
40 * Type for a compare function for qsort. This keeps
41 * the compiler happy.
42 */
43 typedef int (*qsort_comp) (const void *, const void *);
44
45
46 /* mhmisc.c */
47 int part_ok (CT, int);
48 int type_ok (CT, int);
49 void flush_errors (void);
50
51 /* mhshowsbr.c */
52 int show_content_aux (CT, int, int, char *, char *);
53
54 /*
55 * prototypes
56 */
57 void store_all_messages (CT *);
58
59 /*
60 * static prototypes
61 */
62 static void store_single_message (CT);
63 static int store_switch (CT);
64 static int store_generic (CT);
65 static int store_application (CT);
66 static int store_multi (CT);
67 static int store_partial (CT);
68 static int store_external (CT);
69 static int ct_compar (CT *, CT *);
70 static int store_content (CT, CT);
71 static int output_content_file (CT, int);
72 static int output_content_folder (char *, char *);
73 static int parse_format_string (CT, char *, char *, int, char *);
74 static void get_storeproc (CT);
75 static int copy_some_headers (FILE *, CT);
76 static char *clobber_check (char *);
77
78 /*
79 * Main entry point to store content
80 * from a collection of messages.
81 */
82
83 void
84 store_all_messages (CT *cts)
85 {
86 CT ct, *ctp;
87 char *cp;
88
89 /*
90 * Check for the directory in which to
91 * store any contents.
92 */
93 if ((cp = context_find (nmhstorage)) && *cp)
94 dir = getcpy (cp);
95 else
96 dir = getcpy (cwd);
97
98 for (ctp = cts; *ctp; ctp++) {
99 ct = *ctp;
100 store_single_message (ct);
101 }
102
103 flush_errors ();
104 }
105
106
107 /*
108 * Entry point to store the content
109 * in a (single) message
110 */
111
112 static void
113 store_single_message (CT ct)
114 {
115 if (type_ok (ct, 1)) {
116 umask (ct->c_umask);
117 store_switch (ct);
118 if (ct->c_fp) {
119 fclose (ct->c_fp);
120 ct->c_fp = NULL;
121 }
122 if (ct->c_ceclosefnx)
123 (*ct->c_ceclosefnx) (ct);
124 }
125 }
126
127
128 /*
129 * Switching routine to store different content types
130 */
131
132 static int
133 store_switch (CT ct)
134 {
135 switch (ct->c_type) {
136 case CT_MULTIPART:
137 return store_multi (ct);
138
139 case CT_MESSAGE:
140 switch (ct->c_subtype) {
141 case MESSAGE_PARTIAL:
142 return store_partial (ct);
143
144 case MESSAGE_EXTERNAL:
145 return store_external (ct);
146
147 case MESSAGE_RFC822:
148 default:
149 return store_generic (ct);
150 }
151
152 case CT_APPLICATION:
153 return store_application (ct);
154
155 case CT_TEXT:
156 case CT_AUDIO:
157 case CT_IMAGE:
158 case CT_VIDEO:
159 return store_generic (ct);
160
161 default:
162 adios (NULL, "unknown content type %d", ct->c_type);
163 }
164
165 return OK; /* NOT REACHED */
166 }
167
168
169 /*
170 * Generic routine to store a MIME content.
171 * (audio, video, image, text, message/rfc822)
172 */
173
174 static int
175 store_generic (CT ct)
176 {
177 /*
178 * Check if the content specifies a filename.
179 * Don't bother with this for type "message"
180 * (only "message/rfc822" will use store_generic).
181 */
182 if (autosw && ct->c_type != CT_MESSAGE)
183 get_storeproc (ct);
184
185 return store_content (ct, NULL);
186 }
187
188
189 /*
190 * Store content of type "application"
191 */
192
193 static int
194 store_application (CT ct)
195 {
196 char **ap, **ep;
197 CI ci = &ct->c_ctinfo;
198
199 /* Check if the content specifies a filename */
200 if (autosw)
201 get_storeproc (ct);
202
203 /*
204 * If storeproc is not defined, and the content is type
205 * "application/octet-stream", we also check for various
206 * attribute/value pairs which specify if this a tar file.
207 */
208 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
209 int tarP = 0, zP = 0, gzP = 0;
210
211 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
212 /* check for "type=tar" attribute */
213 if (!strcasecmp (*ap, "type")) {
214 if (strcasecmp (*ep, "tar"))
215 break;
216
217 tarP = 1;
218 continue;
219 }
220
221 /* check for "conversions=compress" attribute */
222 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
223 && (!strcasecmp (*ep, "compress") || !strcasecmp (*ep, "x-compress"))) {
224 zP = 1;
225 continue;
226 }
227 /* check for "conversions=gzip" attribute */
228 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
229 && (!strcasecmp (*ep, "gzip") || !strcasecmp (*ep, "x-gzip"))) {
230 gzP = 1;
231 continue;
232 }
233 }
234
235 if (tarP) {
236 ct->c_showproc = add (zP ? "%euncompress | tar tvf -"
237 : (gzP ? "%egzip -dc | tar tvf -"
238 : "%etar tvf -"), NULL);
239 if (!ct->c_storeproc) {
240 if (autosw) {
241 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -"
242 : (gzP ? "| gzip -dc | tar xvpf -"
243 : "| tar xvpf -"), NULL);
244 ct->c_umask = 0022;
245 } else {
246 ct->c_storeproc= add (zP ? "%m%P.tar.Z"
247 : (gzP ? "%m%P.tar.gz"
248 : "%m%P.tar"), NULL);
249 }
250 }
251 }
252 }
253
254 return store_content (ct, NULL);
255 }
256
257
258 /*
259 * Store the content of a multipart message
260 */
261
262 static int
263 store_multi (CT ct)
264 {
265 int result;
266 struct multipart *m = (struct multipart *) ct->c_ctparams;
267 struct part *part;
268
269 result = NOTOK;
270 for (part = m->mp_parts; part; part = part->mp_next) {
271 CT p = part->mp_part;
272
273 if (part_ok (p, 1) && type_ok (p, 1)) {
274 if (ct->c_storage) {
275 /* Support mhstore -outfile. The MIME parser doesn't
276 load c_storage, so we know that p->c_storage is
277 NULL here. */
278 p->c_storage = ct->c_storage;
279 }
280 result = store_switch (p);
281 p->c_storage = NULL;
282
283 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
284 break;
285 }
286 }
287
288 return result;
289 }
290
291
292 /*
293 * Reassemble and store the contents of a collection
294 * of messages of type "message/partial".
295 */
296
297 static int
298 store_partial (CT ct)
299 {
300 int cur, hi, i;
301 CT p, *ctp, *ctq;
302 CT *base;
303 struct partial *pm, *qm;
304
305 qm = (struct partial *) ct->c_ctparams;
306 if (qm->pm_stored)
307 return OK;
308
309 hi = i = 0;
310 for (ctp = cts; *ctp; ctp++) {
311 p = *ctp;
312 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
313 pm = (struct partial *) p->c_ctparams;
314 if (!pm->pm_stored
315 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
316 pm->pm_marked = pm->pm_partno;
317 if (pm->pm_maxno)
318 hi = pm->pm_maxno;
319 pm->pm_stored = 1;
320 i++;
321 }
322 else
323 pm->pm_marked = 0;
324 }
325 }
326
327 if (hi == 0) {
328 advise (NULL, "missing (at least) last part of multipart message");
329 return NOTOK;
330 }
331
332 if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
333 adios (NULL, "out of memory");
334
335 ctq = base;
336 for (ctp = cts; *ctp; ctp++) {
337 p = *ctp;
338 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
339 pm = (struct partial *) p->c_ctparams;
340 if (pm->pm_marked)
341 *ctq++ = p;
342 }
343 }
344 *ctq = NULL;
345
346 if (i > 1)
347 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
348
349 cur = 1;
350 for (ctq = base; *ctq; ctq++) {
351 p = *ctq;
352 pm = (struct partial *) p->c_ctparams;
353 if (pm->pm_marked != cur) {
354 if (pm->pm_marked == cur - 1) {
355 admonish (NULL,
356 "duplicate part %d of %d part multipart message",
357 pm->pm_marked, hi);
358 continue;
359 }
360
361 missing_part:
362 advise (NULL,
363 "missing %spart %d of %d part multipart message",
364 cur != hi ? "(at least) " : "", cur, hi);
365 goto losing;
366 }
367 else
368 cur++;
369 }
370 if (hi != --cur) {
371 cur = hi;
372 goto missing_part;
373 }
374
375 /*
376 * Now cycle through the sorted list of messages of type
377 * "message/partial" and save/append them to a file.
378 */
379
380 ctq = base;
381 ct = *ctq++;
382 if (store_content (ct, NULL) == NOTOK) {
383 losing:
384 free ((char *) base);
385 return NOTOK;
386 }
387
388 for (; *ctq; ctq++) {
389 p = *ctq;
390 if (store_content (p, ct) == NOTOK)
391 goto losing;
392 }
393
394 free ((char *) base);
395 return OK;
396 }
397
398
399 /*
400 * Store content from a message of type "message/external".
401 */
402
403 static int
404 store_external (CT ct)
405 {
406 int result = NOTOK;
407 struct exbody *e = (struct exbody *) ct->c_ctparams;
408 CT p = e->eb_content;
409
410 if (!type_ok (p, 1))
411 return OK;
412
413 /*
414 * Check if the parameters for the external body
415 * specified a filename.
416 */
417 if (autosw) {
418 char *cp;
419
420 if ((cp = e->eb_name)
421 && *cp != '/'
422 && *cp != '.'
423 && *cp != '|'
424 && *cp != '!'
425 && !strchr (cp, '%')) {
426 if (!ct->c_storeproc)
427 ct->c_storeproc = add (cp, NULL);
428 if (!p->c_storeproc)
429 p->c_storeproc = add (cp, NULL);
430 }
431 }
432
433 /*
434 * Since we will let the Content structure for the
435 * external body substitute for the current content,
436 * we temporarily change its partno (number inside
437 * multipart), so everything looks right.
438 */
439 p->c_partno = ct->c_partno;
440
441 /* we probably need to check if content is really there */
442 if (ct->c_storage) {
443 /* Support mhstore -outfile. The MIME parser doesn't load
444 c_storage, so we know that p->c_storage is NULL here. */
445 p->c_storage = ct->c_storage;
446 }
447 result = store_switch (p);
448 p->c_storage = NULL;
449
450 p->c_partno = NULL;
451 return result;
452 }
453
454
455 /*
456 * Compare the numbering from two different
457 * message/partials (needed for sorting).
458 */
459
460 static int
461 ct_compar (CT *a, CT *b)
462 {
463 struct partial *am = (struct partial *) ((*a)->c_ctparams);
464 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
465
466 return (am->pm_marked - bm->pm_marked);
467 }
468
469
470 /*
471 * Store contents of a message or message part to
472 * a folder, a file, the standard output, or pass
473 * the contents to a command.
474 *
475 * If the current content to be saved is a followup part
476 * to a collection of messages of type "message/partial",
477 * then field "p" is a pointer to the Content structure
478 * to the first message/partial in the group.
479 */
480
481 static int
482 store_content (CT ct, CT p)
483 {
484 int appending = 0, msgnum = 0;
485 int is_partial = 0, first_partial = 0;
486 int last_partial = 0;
487 char *cp, buffer[BUFSIZ];
488
489 /*
490 * Do special processing for messages of
491 * type "message/partial".
492 *
493 * We first check if this content is of type
494 * "message/partial". If it is, then we need to check
495 * whether it is the first and/or last in the group.
496 *
497 * Then if "p" is a valid pointer, it points to the Content
498 * structure of the first partial in the group. So we copy
499 * the file name and/or folder name from that message. In
500 * this case, we also note that we will be appending.
501 */
502 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
503 struct partial *pm = (struct partial *) ct->c_ctparams;
504
505 /* Yep, it's a message/partial */
506 is_partial = 1;
507
508 /* But is it the first and/or last in the collection? */
509 if (pm->pm_partno == 1)
510 first_partial = 1;
511 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
512 last_partial = 1;
513
514 /*
515 * If "p" is a valid pointer, then it points to the
516 * Content structure for the first message in the group.
517 * So we just copy the filename or foldername information
518 * from the previous iteration of this function.
519 */
520 if (p) {
521 appending = 1;
522 if (! ct->c_storage) {
523 ct->c_storage = add (p->c_storage, NULL);
524
525 /* record the folder name */
526 if (p->c_folder) {
527 ct->c_folder = add (p->c_folder, NULL);
528 }
529 }
530 goto got_filename;
531 }
532 }
533
534 /*
535 * Get storage formatting string.
536 *
537 * 1) If we have storeproc defined, then use that
538 * 2) Else check for a mhn-store-<type>/<subtype> entry
539 * 3) Else check for a mhn-store-<type> entry
540 * 4) Else if content is "message", use "+" (current folder)
541 * 5) Else use string "%m%P.%s".
542 */
543 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
544 CI ci = &ct->c_ctinfo;
545
546 snprintf (buffer, sizeof(buffer), "%s-store-%s/%s",
547 invo_name, ci->ci_type, ci->ci_subtype);
548 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
549 snprintf (buffer, sizeof(buffer), "%s-store-%s", invo_name, ci->ci_type);
550 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
551 cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
552 }
553 }
554 }
555
556 if (! ct->c_storage) {
557 /*
558 * Check the beginning of storage formatting string
559 * to see if we are saving content to a folder.
560 */
561 if (*cp == '+' || *cp == '@') {
562 char *tmpfilenam, *folder;
563
564 /* Store content in temporary file for now */
565 if ((tmpfilenam = m_mktemp(invo_name, NULL, NULL)) == NULL) {
566 adios(NULL, "unable to create temporary file in %s",
567 get_temp_dir());
568 }
569 ct->c_storage = add (tmpfilenam, NULL);
570
571 /* Get the folder name */
572 if (cp[1])
573 folder = pluspath (cp);
574 else
575 folder = getfolder (1);
576
577 /* Check if folder exists */
578 create_folder(m_mailpath(folder), 0, exit);
579
580 /* Record the folder name */
581 ct->c_folder = add (folder, NULL);
582
583 if (cp[1])
584 free (folder);
585
586 goto got_filename;
587 }
588
589 /*
590 * Parse and expand the storage formatting string
591 * in `cp' into `buffer'.
592 */
593 parse_format_string (ct, cp, buffer, sizeof(buffer), dir);
594
595 /*
596 * If formatting begins with '|' or '!', then pass
597 * content to standard input of a command and return.
598 */
599 if (buffer[0] == '|' || buffer[0] == '!')
600 return show_content_aux (ct, 1, 0, buffer + 1, dir);
601
602 /* record the filename */
603 if ((ct->c_storage = clobber_check (add (buffer, NULL))) == NULL) {
604 return NOTOK;
605 }
606 } else {
607 /* The output filename was explicitly specified, so use it. */
608 if ((ct->c_storage = clobber_check (add (ct->c_storage, NULL))) ==
609 NULL) {
610 return NOTOK;
611 }
612 }
613
614 got_filename:
615 /* flush the output stream */
616 fflush (stdout);
617
618 /* Now save or append the content to a file */
619 if (output_content_file (ct, appending) == NOTOK)
620 return NOTOK;
621
622 /*
623 * If necessary, link the file into a folder and remove
624 * the temporary file. If this message is a partial,
625 * then only do this if it is the last one in the group.
626 */
627 if (ct->c_folder && (!is_partial || last_partial)) {
628 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
629 (void) m_unlink (ct->c_storage);
630 if (msgnum == NOTOK)
631 return NOTOK;
632 }
633
634 /*
635 * Now print out the name/number of the message
636 * that we are storing.
637 */
638 if (is_partial) {
639 if (first_partial)
640 fprintf (stderr, "reassembling partials ");
641 if (last_partial)
642 fprintf (stderr, "%s", ct->c_file);
643 else
644 fprintf (stderr, "%s,", ct->c_file);
645 } else {
646 fprintf (stderr, "storing message %s", ct->c_file);
647 if (ct->c_partno)
648 fprintf (stderr, " part %s", ct->c_partno);
649 }
650
651 /*
652 * Unless we are in the "middle" of group of message/partials,
653 * we now print the name of the file, folder, and/or message
654 * to which we are storing the content.
655 */
656 if (!is_partial || last_partial) {
657 if (ct->c_folder) {
658 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder, msgnum);
659 } else if (!strcmp(ct->c_storage, "-")) {
660 fprintf (stderr, " to stdout\n");
661 } else {
662 int cwdlen;
663
664 cwdlen = strlen (cwd);
665 fprintf (stderr, " as file %s\n",
666 strncmp (ct->c_storage, cwd, cwdlen)
667 || ct->c_storage[cwdlen] != '/'
668 ? ct->c_storage : ct->c_storage + cwdlen + 1);
669 }
670 }
671
672 return OK;
673 }
674
675
676 /*
677 * Output content to a file
678 */
679
680 static int
681 output_content_file (CT ct, int appending)
682 {
683 int filterstate;
684 char *file, buffer[BUFSIZ];
685 long pos, last;
686 FILE *fp;
687
688 /*
689 * If the pathname is absolute, make sure
690 * all the relevant directories exist.
691 */
692 if (strchr(ct->c_storage, '/')
693 && make_intermediates (ct->c_storage) == NOTOK)
694 return NOTOK;
695
696 if (ct->c_encoding != CE_7BIT) {
697 int cc, fd;
698
699 if (!ct->c_ceopenfnx) {
700 advise (NULL, "don't know how to decode part %s of message %s",
701 ct->c_partno, ct->c_file);
702 return NOTOK;
703 }
704
705 file = appending || !strcmp (ct->c_storage, "-") ? NULL
706 : ct->c_storage;
707 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
708 return NOTOK;
709 if (!strcmp (file, ct->c_storage)) {
710 (*ct->c_ceclosefnx) (ct);
711 return OK;
712 }
713
714 /*
715 * Send to standard output
716 */
717 if (!strcmp (ct->c_storage, "-")) {
718 int gd;
719
720 if ((gd = dup (fileno (stdout))) == NOTOK) {
721 advise ("stdout", "unable to dup");
722 losing:
723 (*ct->c_ceclosefnx) (ct);
724 return NOTOK;
725 }
726 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
727 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
728 appending ? "a" : "w");
729 close (gd);
730 goto losing;
731 }
732 } else {
733 /*
734 * Open output file
735 */
736 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
737 advise (ct->c_storage, "unable to fopen for %s",
738 appending ? "appending" : "writing");
739 goto losing;
740 }
741 }
742
743 /*
744 * Filter the header fields of the initial enclosing
745 * message/partial into the file.
746 */
747 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
748 struct partial *pm = (struct partial *) ct->c_ctparams;
749
750 if (pm->pm_partno == 1)
751 copy_some_headers (fp, ct);
752 }
753
754 for (;;) {
755 switch (cc = read (fd, buffer, sizeof(buffer))) {
756 case NOTOK:
757 advise (file, "error reading content from");
758 break;
759
760 case OK:
761 break;
762
763 default:
764 fwrite (buffer, sizeof(*buffer), cc, fp);
765 continue;
766 }
767 break;
768 }
769
770 (*ct->c_ceclosefnx) (ct);
771
772 if (cc != NOTOK && fflush (fp))
773 advise (ct->c_storage, "error writing to");
774
775 fclose (fp);
776
777 return (cc != NOTOK ? OK : NOTOK);
778 }
779
780 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
781 advise (ct->c_file, "unable to open for reading");
782 return NOTOK;
783 }
784
785 pos = ct->c_begin;
786 last = ct->c_end;
787 fseek (ct->c_fp, pos, SEEK_SET);
788
789 if (!strcmp (ct->c_storage, "-")) {
790 int gd;
791
792 if ((gd = dup (fileno (stdout))) == NOTOK) {
793 advise ("stdout", "unable to dup");
794 return NOTOK;
795 }
796 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
797 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
798 appending ? "a" : "w");
799 close (gd);
800 return NOTOK;
801 }
802 } else {
803 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
804 advise (ct->c_storage, "unable to fopen for %s",
805 appending ? "appending" : "writing");
806 return NOTOK;
807 }
808 }
809
810 /*
811 * Copy a few of the header fields of the initial
812 * enclosing message/partial into the file.
813 */
814 filterstate = 0;
815 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
816 struct partial *pm = (struct partial *) ct->c_ctparams;
817
818 if (pm->pm_partno == 1) {
819 copy_some_headers (fp, ct);
820 filterstate = 1;
821 }
822 }
823
824 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
825 if ((pos += strlen (buffer)) > last) {
826 int diff;
827
828 diff = strlen (buffer) - (pos - last);
829 if (diff >= 0)
830 buffer[diff] = '\0';
831 }
832 /*
833 * If this is the first content of a group of
834 * message/partial contents, then we only copy a few
835 * of the header fields of the enclosed message.
836 */
837 if (filterstate) {
838 switch (buffer[0]) {
839 case ' ':
840 case '\t':
841 if (filterstate < 0)
842 buffer[0] = 0;
843 break;
844
845 case '\n':
846 filterstate = 0;
847 break;
848
849 default:
850 if (!uprf (buffer, XXX_FIELD_PRF)
851 && !uprf (buffer, VRSN_FIELD)
852 && !uprf (buffer, "Subject:")
853 && !uprf (buffer, "Encrypted:")
854 && !uprf (buffer, "Message-ID:")) {
855 filterstate = -1;
856 buffer[0] = 0;
857 break;
858 }
859 filterstate = 1;
860 break;
861 }
862 }
863 fputs (buffer, fp);
864 if (pos >= last)
865 break;
866 }
867
868 if (fflush (fp))
869 advise (ct->c_storage, "error writing to");
870
871 fclose (fp);
872 fclose (ct->c_fp);
873 ct->c_fp = NULL;
874 return OK;
875 }
876
877
878 /*
879 * Add a file to a folder.
880 *
881 * Return the new message number of the file
882 * when added to the folder. Return -1, if
883 * there is an error.
884 */
885
886 static int
887 output_content_folder (char *folder, char *filename)
888 {
889 int msgnum;
890 struct msgs *mp;
891
892 /* Read the folder. */
893 if ((mp = folder_read (folder, 0))) {
894 /* Link file into folder */
895 msgnum = folder_addmsg (&mp, filename, 0, 0, 0, 0, (char *)0);
896 } else {
897 advise (NULL, "unable to read folder %s", folder);
898 return NOTOK;
899 }
900
901 /* free folder structure */
902 folder_free (mp);
903
904 /*
905 * Return msgnum. We are relying on the fact that
906 * msgnum will be -1, if folder_addmsg() had an error.
907 */
908 return msgnum;
909 }
910
911
912 /*
913 * Parse and expand the storage formatting string
914 * pointed to by "cp" into "buffer".
915 */
916
917 static int
918 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
919 {
920 int len;
921 char *bp;
922 CI ci = &ct->c_ctinfo;
923
924 /*
925 * If storage string is "-", just copy it, and
926 * return (send content to standard output).
927 */
928 if (cp[0] == '-' && cp[1] == '\0') {
929 strncpy (buffer, cp, buflen);
930 return 0;
931 }
932
933 bp = buffer;
934 bp[0] = '\0';
935
936 /*
937 * If formatting string is a pathname that doesn't
938 * begin with '/', then preface the path with the
939 * appropriate directory.
940 */
941 if (*cp != '/' && *cp != '|' && *cp != '!') {
942 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
943 len = strlen (bp);
944 bp += len;
945 buflen -= len;
946 }
947
948 for (; *cp; cp++) {
949
950 /* We are processing a storage escape */
951 if (*cp == '%') {
952 switch (*++cp) {
953 case 'a':
954 /*
955 * Insert parameters from Content-Type.
956 * This is only valid for '|' commands.
957 */
958 if (buffer[0] != '|' && buffer[0] != '!') {
959 *bp++ = *--cp;
960 *bp = '\0';
961 buflen--;
962 continue;
963 } else {
964 char **ap, **ep;
965 char *s = "";
966
967 for (ap = ci->ci_attrs, ep = ci->ci_values;
968 *ap; ap++, ep++) {
969 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
970 len = strlen (bp);
971 bp += len;
972 buflen -= len;
973 s = " ";
974 }
975 }
976 break;
977
978 case 'm':
979 /* insert message number */
980 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
981 break;
982
983 case 'P':
984 /* insert part number with leading dot */
985 if (ct->c_partno)
986 snprintf (bp, buflen, ".%s", ct->c_partno);
987 break;
988
989 case 'p':
990 /* insert part number withouth leading dot */
991 if (ct->c_partno)
992 strncpy (bp, ct->c_partno, buflen);
993 break;
994
995 case 't':
996 /* insert content type */
997 strncpy (bp, ci->ci_type, buflen);
998 break;
999
1000 case 's':
1001 /* insert content subtype */
1002 strncpy (bp, ci->ci_subtype, buflen);
1003 break;
1004
1005 case '%':
1006 /* insert the character % */
1007 goto raw;
1008
1009 default:
1010 *bp++ = *--cp;
1011 *bp = '\0';
1012 buflen--;
1013 continue;
1014 }
1015
1016 /* Advance bp and decrement buflen */
1017 len = strlen (bp);
1018 bp += len;
1019 buflen -= len;
1020
1021 } else {
1022 raw:
1023 *bp++ = *cp;
1024 *bp = '\0';
1025 buflen--;
1026 }
1027 }
1028
1029 return 0;
1030 }
1031
1032
1033 /*
1034 * Check if the content specifies a filename
1035 * in its MIME parameters.
1036 */
1037
1038 static void
1039 get_storeproc (CT ct)
1040 {
1041 char **ap, **ep, *cp;
1042 CI ci;
1043
1044 /*
1045 * If the storeproc has already been defined,
1046 * we just return (for instance, if this content
1047 * is part of a "message/external".
1048 */
1049 if (ct->c_storeproc)
1050 return;
1051
1052 /*
1053 * If there's a Content-Disposition header and it has a filename,
1054 * use that (RFC-2183).
1055 */
1056 if (ct->c_dispo) {
1057 char *cp = strchr (ct->c_dispo, ';');
1058 CI ci = calloc (1, sizeof *ci);
1059 int status;
1060 int found_filename = 0;
1061
1062 if (cp && parse_header_attrs (ct->c_file, strlen (invo_name) + 2, &cp,
1063 ci, &status) == OK) {
1064 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1065 if (! strcasecmp (*ap, "filename")
1066 && *(cp = *ep) != '/'
1067 && *cp != '.'
1068 && *cp != '|'
1069 && *cp != '!'
1070 && !strchr (cp, '%')) {
1071 ct->c_storeproc = add (cp, NULL);
1072 found_filename = 1;
1073 }
1074 free (*ap);
1075 }
1076 }
1077
1078 free (ci);
1079 if (found_filename) return;
1080 }
1081
1082 /*
1083 * Check the attribute/value pairs, for the attribute "name".
1084 * If found, do a few sanity checks and copy the value into
1085 * the storeproc.
1086 */
1087 ci = &ct->c_ctinfo;
1088 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1089 if (! strcasecmp (*ap, "name")
1090 && *(cp = *ep) != '/'
1091 && *cp != '.'
1092 && *cp != '|'
1093 && *cp != '!'
1094 && !strchr (cp, '%')) {
1095 ct->c_storeproc = add (cp, NULL);
1096 return;
1097 }
1098 }
1099 }
1100
1101
1102 /*
1103 * Copy some of the header fields of the initial message/partial
1104 * message into the header of the reassembled message.
1105 */
1106
1107 static int
1108 copy_some_headers (FILE *out, CT ct)
1109 {
1110 HF hp;
1111
1112 hp = ct->c_first_hf; /* start at first header field */
1113
1114 while (hp) {
1115 /*
1116 * A few of the header fields of the enclosing
1117 * messages are not copied.
1118 */
1119 if (!uprf (hp->name, XXX_FIELD_PRF)
1120 && strcasecmp (hp->name, VRSN_FIELD)
1121 && strcasecmp (hp->name, "Subject")
1122 && strcasecmp (hp->name, "Encrypted")
1123 && strcasecmp (hp->name, "Message-ID"))
1124 fprintf (out, "%s:%s", hp->name, hp->value);
1125 hp = hp->next; /* next header field */
1126 }
1127
1128 return OK;
1129 }
1130
1131 /******************************************************************************/
1132 /* -clobber support */
1133
1134 enum clobber_policy_t {
1135 NMH_CLOBBER_ALWAYS,
1136 NMH_CLOBBER_AUTO,
1137 NMH_CLOBBER_SUFFIX,
1138 NMH_CLOBBER_ASK,
1139 NMH_CLOBBER_NEVER
1140 };
1141
1142 static enum clobber_policy_t clobber_policy = NMH_CLOBBER_ALWAYS;
1143
1144 int files_not_clobbered = 0;
1145
1146 int
1147 save_clobber_policy (const char *value) {
1148 if (! strcasecmp (value, "always")) {
1149 clobber_policy = NMH_CLOBBER_ALWAYS;
1150 } else if (! strcasecmp (value, "auto")) {
1151 clobber_policy = NMH_CLOBBER_AUTO;
1152 } else if (! strcasecmp (value, "suffix")) {
1153 clobber_policy = NMH_CLOBBER_SUFFIX;
1154 } else if (! strcasecmp (value, "ask")) {
1155 clobber_policy = NMH_CLOBBER_ASK;
1156 } else if (! strcasecmp (value, "never")) {
1157 clobber_policy = NMH_CLOBBER_NEVER;
1158 } else {
1159 return 1;
1160 }
1161
1162 return 0;
1163 }
1164
1165
1166 static char *
1167 next_version (char *file, enum clobber_policy_t clobber_policy) {
1168 const size_t max_versions = 1000000;
1169 /* 8 = log max_versions + one for - or . + one for null terminator */
1170 const size_t buflen = strlen (file) + 8;
1171 char *buffer = mh_xmalloc (buflen);
1172 size_t version;
1173
1174 char *extension = NULL;
1175 if (clobber_policy == NMH_CLOBBER_AUTO &&
1176 ((extension = strrchr (file, '.')) != NULL)) {
1177 *extension++ = '\0';
1178 }
1179
1180 for (version = 1; version < max_versions; ++version) {
1181 int fd;
1182
1183 switch (clobber_policy) {
1184 case NMH_CLOBBER_AUTO: {
1185 snprintf (buffer, buflen, "%s-%ld%s%s", file, (long) version,
1186 extension == NULL ? "" : ".",
1187 extension == NULL ? "" : extension);
1188 break;
1189 }
1190
1191 case NMH_CLOBBER_SUFFIX:
1192 snprintf (buffer, buflen, "%s.%ld", file, (long) version);
1193 break;
1194
1195 default:
1196 /* Should never get here. */
1197 advise (NULL, "will not overwrite %s, invalid clobber policy", buffer);
1198 free (buffer);
1199 ++files_not_clobbered;
1200 return NULL;
1201 }
1202
1203 /* Actually (try to) create the file here to avoid a race
1204 condition on file naming + creation. This won't solve the
1205 problem with old NFS that doesn't support O_EXCL, though.
1206 Let the umask strip off permissions from 0666 as desired.
1207 That's what fopen () would do if it was creating the file. */
1208 if ((fd = open (buffer, O_CREAT | O_EXCL,
1209 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
1210 S_IROTH | S_IWOTH)) >= 0) {
1211 close (fd);
1212 break;
1213 }
1214 }
1215
1216 free (file);
1217
1218 if (version >= max_versions) {
1219 advise (NULL, "will not overwrite %s, too many versions", buffer);
1220 free (buffer);
1221 buffer = NULL;
1222 ++files_not_clobbered;
1223 }
1224
1225 return buffer;
1226 }
1227
1228
1229 static char *
1230 clobber_check (char *original_file) {
1231 /* clobber policy return value
1232 * -------------- ------------
1233 * -always original_file
1234 * -auto original_file-<digits>.extension
1235 * -suffix original_file.<digits>
1236 * -ask original_file, 0, or another filename/path
1237 * -never 0
1238 */
1239
1240 char *file;
1241 char *cwd = NULL;
1242 int check_again;
1243
1244 if (clobber_policy == NMH_CLOBBER_ASK) {
1245 /* Save cwd for possible use in loop below. */
1246 char *slash;
1247
1248 cwd = add (original_file, NULL);
1249 slash = strrchr (cwd, '/');
1250
1251 if (slash) {
1252 *slash = '\0';
1253 } else {
1254 /* original_file wasn't a full path, which shouldn't happen. */
1255 cwd = NULL;
1256 }
1257 }
1258
1259 do {
1260 struct stat st;
1261
1262 file = original_file;
1263 check_again = 0;
1264
1265 switch (clobber_policy) {
1266 case NMH_CLOBBER_ALWAYS:
1267 break;
1268
1269 case NMH_CLOBBER_SUFFIX:
1270 case NMH_CLOBBER_AUTO:
1271 if (stat (file, &st) == OK) {
1272 file = next_version (original_file, clobber_policy);
1273 }
1274 break;
1275
1276 case NMH_CLOBBER_ASK:
1277 if (stat (file, &st) == OK) {
1278 enum answers { NMH_YES, NMH_NO, NMH_RENAME };
1279 static struct swit answer[4] = {
1280 { "yes", 0, NMH_YES }, { "no", 0, NMH_NO }, { "rename", 0, NMH_RENAME }, { NULL, 0, 0 } };
1281 char **ans;
1282
1283 if (isatty (fileno (stdin))) {
1284 char *prompt =
1285 concat ("Overwrite \"", file, "\" [y/n/rename]? ", NULL);
1286 ans = getans (prompt, answer);
1287 free (prompt);
1288 } else {
1289 /* Overwrite, that's what nmh used to do. And warn. */
1290 advise (NULL, "-clobber ask but no tty, so overwrite %s", file);
1291 break;
1292 }
1293
1294 switch ((enum answers) smatch (*ans, answer)) {
1295 case NMH_YES:
1296 break;
1297 case NMH_NO:
1298 free (file);
1299 file = NULL;
1300 ++files_not_clobbered;
1301 break;
1302 case NMH_RENAME: {
1303 char buf[PATH_MAX];
1304 printf ("Enter filename or full path of the new file: ");
1305 if (fgets (buf, sizeof buf, stdin) == NULL ||
1306 buf[0] == '\0') {
1307 file = NULL;
1308 ++files_not_clobbered;
1309 } else {
1310 char *newline = strchr (buf, '\n');
1311 if (newline) {
1312 *newline = '\0';
1313 }
1314 }
1315
1316 free (file);
1317
1318 if (buf[0] == '/') {
1319 /* Full path, use it. */
1320 file = add (buf, NULL);
1321 } else {
1322 /* Relative path. */
1323 file = cwd ? concat (cwd, "/", buf, NULL) : add (buf, NULL);
1324 }
1325
1326 check_again = 1;
1327 break;
1328 }
1329 }
1330 }
1331 break;
1332
1333 case NMH_CLOBBER_NEVER:
1334 if (stat (file, &st) == OK) {
1335 /* Keep count of files that would have been clobbered,
1336 and return that as process exit status. */
1337 advise (NULL, "will not overwrite %s with -clobber never", file);
1338 free (file);
1339 file = NULL;
1340 ++files_not_clobbered;
1341 }
1342 break;
1343 }
1344
1345 original_file = file;
1346 } while (check_again);
1347
1348 if (cwd) {
1349 free (cwd);
1350 }
1351
1352 return file;
1353 }
1354
1355 /* -clobber support */
1356 /******************************************************************************/