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