]> diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
Quiet lock warning.
[nmh] / uip / mhstoresbr.c
1
2 /*
3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <h/md5.h>
16 #include <errno.h>
17 #include <setjmp.h>
18 #include <signal.h>
19 #include <h/mts.h>
20 #include <h/tws.h>
21 #include <h/mime.h>
22 #include <h/mhparse.h>
23
24 extern int errno;
25
26 /*
27 * The list of top-level contents to display
28 */
29 extern CT *cts;
30
31 int autosw = 0;
32
33 /*
34 * Cache of current directory. This must be
35 * set before these routines are called.
36 */
37 char *cwd;
38
39 /*
40 * The directory in which to store the contents.
41 */
42 static char *dir;
43
44 /*
45 * Type for a compare function for qsort. This keeps
46 * the compiler happy.
47 */
48 typedef int (*qsort_comp) (const void *, const void *);
49
50
51 /* mhmisc.c */
52 int part_ok (CT, int);
53 int type_ok (CT, int);
54 int make_intermediates (char *);
55 void flush_errors (void);
56
57 /* mhshowsbr.c */
58 int show_content_aux (CT, int, int, char *, char *);
59
60 /*
61 * prototypes
62 */
63 void store_all_messages (CT *);
64
65 /*
66 * static prototypes
67 */
68 static void store_single_message (CT);
69 static int store_switch (CT);
70 static int store_generic (CT);
71 static int store_application (CT);
72 static int store_multi (CT);
73 static int store_partial (CT);
74 static int store_external (CT);
75 static int ct_compar (CT *, CT *);
76 static int store_content (CT, CT);
77 static int output_content_file (CT, int);
78 static int check_folder (char *);
79 static int output_content_folder (char *, char *);
80 static int parse_format_string (CT, char *, char *, int, char *);
81 static void get_storeproc (CT);
82 static int copy_some_headers (FILE *, CT);
83
84
85 /*
86 * Main entry point to store content
87 * from a collection of messages.
88 */
89
90 void
91 store_all_messages (CT *cts)
92 {
93 CT ct, *ctp;
94 char *cp;
95
96 /*
97 * Check for the directory in which to
98 * store any contents.
99 */
100 if (autosw)
101 dir = getcpy (cwd);
102 else if ((cp = context_find (nmhstorage)) && *cp)
103 dir = getcpy (cp);
104 else
105 dir = getcpy (cwd);
106
107 for (ctp = cts; *ctp; ctp++) {
108 ct = *ctp;
109 store_single_message (ct);
110 }
111
112 flush_errors ();
113 }
114
115
116 /*
117 * Entry point to store the content
118 * in a (single) message
119 */
120
121 static void
122 store_single_message (CT ct)
123 {
124 if (type_ok (ct, 1)) {
125 umask (ct->c_umask);
126 store_switch (ct);
127 if (ct->c_fp) {
128 fclose (ct->c_fp);
129 ct->c_fp = NULL;
130 }
131 if (ct->c_ceclosefnx)
132 (*ct->c_ceclosefnx) (ct);
133 }
134 }
135
136
137 /*
138 * Switching routine to store different content types
139 */
140
141 static int
142 store_switch (CT ct)
143 {
144 switch (ct->c_type) {
145 case CT_MULTIPART:
146 return store_multi (ct);
147 break;
148
149 case CT_MESSAGE:
150 switch (ct->c_subtype) {
151 case MESSAGE_PARTIAL:
152 return store_partial (ct);
153 break;
154
155 case MESSAGE_EXTERNAL:
156 return store_external (ct);
157
158 case MESSAGE_RFC822:
159 default:
160 return store_generic (ct);
161 break;
162 }
163 break;
164
165 case CT_APPLICATION:
166 return store_application (ct);
167 break;
168
169 case CT_TEXT:
170 case CT_AUDIO:
171 case CT_IMAGE:
172 case CT_VIDEO:
173 return store_generic (ct);
174 break;
175
176 default:
177 adios (NULL, "unknown content type %d", ct->c_type);
178 break;
179 }
180
181 return OK; /* NOT REACHED */
182 }
183
184
185 /*
186 * Generic routine to store a MIME content.
187 * (audio, video, image, text, message/rfc922)
188 */
189
190 static int
191 store_generic (CT ct)
192 {
193 /*
194 * Check if the content specifies a filename.
195 * Don't bother with this for type "message"
196 * (only "message/rfc822" will use store_generic).
197 */
198 if (autosw && ct->c_type != CT_MESSAGE)
199 get_storeproc (ct);
200
201 return store_content (ct, NULL);
202 }
203
204
205 /*
206 * Store content of type "application"
207 */
208
209 static int
210 store_application (CT ct)
211 {
212 char **ap, **ep;
213 CI ci = &ct->c_ctinfo;
214
215 /* Check if the content specifies a filename */
216 if (autosw)
217 get_storeproc (ct);
218
219 /*
220 * If storeproc is not defined, and the content is type
221 * "application/octet-stream", we also check for various
222 * attribute/value pairs which specify if this a tar file.
223 */
224 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
225 int tarP = 0, zP = 0, gzP = 0;
226
227 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
228 /* check for "type=tar" attribute */
229 if (!strcasecmp (*ap, "type")) {
230 if (strcasecmp (*ep, "tar"))
231 break;
232
233 tarP = 1;
234 continue;
235 }
236
237 /* check for "conversions=compress" attribute */
238 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
239 && (!strcasecmp (*ep, "compress") || !strcasecmp (*ep, "x-compress"))) {
240 zP = 1;
241 continue;
242 }
243 /* check for "conversions=gzip" attribute */
244 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
245 && (!strcasecmp (*ep, "gzip") || !strcasecmp (*ep, "x-gzip"))) {
246 gzP = 1;
247 continue;
248 }
249 }
250
251 if (tarP) {
252 ct->c_showproc = add (zP ? "%euncompress | tar tvf -"
253 : (gzP ? "%egzip -dc | tar tvf -"
254 : "%etar tvf -"), NULL);
255 if (!ct->c_storeproc) {
256 if (autosw) {
257 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -"
258 : (gzP ? "| gzip -dc | tar xvpf -"
259 : "| tar xvpf -"), NULL);
260 ct->c_umask = 0022;
261 } else {
262 ct->c_storeproc= add (zP ? "%m%P.tar.Z"
263 : (gzP ? "%m%P.tar.gz"
264 : "%m%P.tar"), NULL);
265 }
266 }
267 }
268 }
269
270 return store_content (ct, NULL);
271 }
272
273
274 /*
275 * Store the content of a multipart message
276 */
277
278 static int
279 store_multi (CT ct)
280 {
281 int result;
282 struct multipart *m = (struct multipart *) ct->c_ctparams;
283 struct part *part;
284
285 result = NOTOK;
286 for (part = m->mp_parts; part; part = part->mp_next) {
287 CT p = part->mp_part;
288
289 if (part_ok (p, 1) && type_ok (p, 1)) {
290 result = store_switch (p);
291 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
292 break;
293 }
294 }
295
296 return result;
297 }
298
299
300 /*
301 * Reassemble and store the contents of a collection
302 * of messages of type "message/partial".
303 */
304
305 static int
306 store_partial (CT ct)
307 {
308 int cur, hi, i;
309 CT p, *ctp, *ctq;
310 CT *base;
311 struct partial *pm, *qm;
312
313 qm = (struct partial *) ct->c_ctparams;
314 if (qm->pm_stored)
315 return OK;
316
317 hi = i = 0;
318 for (ctp = cts; *ctp; ctp++) {
319 p = *ctp;
320 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
321 pm = (struct partial *) p->c_ctparams;
322 if (!pm->pm_stored
323 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
324 pm->pm_marked = pm->pm_partno;
325 if (pm->pm_maxno)
326 hi = pm->pm_maxno;
327 pm->pm_stored = 1;
328 i++;
329 }
330 else
331 pm->pm_marked = 0;
332 }
333 }
334
335 if (hi == 0) {
336 advise (NULL, "missing (at least) last part of multipart message");
337 return NOTOK;
338 }
339
340 if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
341 adios (NULL, "out of memory");
342
343 ctq = base;
344 for (ctp = cts; *ctp; ctp++) {
345 p = *ctp;
346 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
347 pm = (struct partial *) p->c_ctparams;
348 if (pm->pm_marked)
349 *ctq++ = p;
350 }
351 }
352 *ctq = NULL;
353
354 if (i > 1)
355 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
356
357 cur = 1;
358 for (ctq = base; *ctq; ctq++) {
359 p = *ctq;
360 pm = (struct partial *) p->c_ctparams;
361 if (pm->pm_marked != cur) {
362 if (pm->pm_marked == cur - 1) {
363 admonish (NULL,
364 "duplicate part %d of %d part multipart message",
365 pm->pm_marked, hi);
366 continue;
367 }
368
369 missing_part:
370 advise (NULL,
371 "missing %spart %d of %d part multipart message",
372 cur != hi ? "(at least) " : "", cur, hi);
373 goto losing;
374 }
375 else
376 cur++;
377 }
378 if (hi != --cur) {
379 cur = hi;
380 goto missing_part;
381 }
382
383 /*
384 * Now cycle through the sorted list of messages of type
385 * "message/partial" and save/append them to a file.
386 */
387
388 ctq = base;
389 ct = *ctq++;
390 if (store_content (ct, NULL) == NOTOK) {
391 losing:
392 free ((char *) base);
393 return NOTOK;
394 }
395
396 for (; *ctq; ctq++) {
397 p = *ctq;
398 if (store_content (p, ct) == NOTOK)
399 goto losing;
400 }
401
402 free ((char *) base);
403 return OK;
404 }
405
406
407 /*
408 * Store content from a message of type "message/external".
409 */
410
411 static int
412 store_external (CT ct)
413 {
414 int result = NOTOK;
415 struct exbody *e = (struct exbody *) ct->c_ctparams;
416 CT p = e->eb_content;
417
418 if (!type_ok (p, 1))
419 return OK;
420
421 /*
422 * Check if the parameters for the external body
423 * specified a filename.
424 */
425 if (autosw) {
426 char *cp;
427
428 if ((cp = e->eb_name)
429 && *cp != '/'
430 && *cp != '.'
431 && *cp != '|'
432 && *cp != '!'
433 && !strchr (cp, '%')) {
434 if (!ct->c_storeproc)
435 ct->c_storeproc = add (cp, NULL);
436 if (!p->c_storeproc)
437 p->c_storeproc = add (cp, NULL);
438 }
439 }
440
441 /*
442 * Since we will let the Content structure for the
443 * external body substitute for the current content,
444 * we temporarily change its partno (number inside
445 * multipart), so everything looks right.
446 */
447 p->c_partno = ct->c_partno;
448
449 /* we probably need to check if content is really there */
450 result = store_switch (p);
451
452 p->c_partno = NULL;
453 return result;
454 }
455
456
457 /*
458 * Compare the numbering from two different
459 * message/partials (needed for sorting).
460 */
461
462 static int
463 ct_compar (CT *a, CT *b)
464 {
465 struct partial *am = (struct partial *) ((*a)->c_ctparams);
466 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
467
468 return (am->pm_marked - bm->pm_marked);
469 }
470
471
472 /*
473 * Store contents of a message or message part to
474 * a folder, a file, the standard output, or pass
475 * the contents to a command.
476 *
477 * If the current content to be saved is a followup part
478 * to a collection of messages of type "message/partial",
479 * then field "p" is a pointer to the Content structure
480 * to the first message/partial in the group.
481 */
482
483 static int
484 store_content (CT ct, CT p)
485 {
486 int appending = 0, msgnum;
487 int is_partial = 0, first_partial = 0;
488 int last_partial = 0;
489 char *cp, buffer[BUFSIZ];
490
491 /*
492 * Do special processing for messages of
493 * type "message/partial".
494 *
495 * We first check if this content is of type
496 * "message/partial". If it is, then we need to check
497 * whether it is the first and/or last in the group.
498 *
499 * Then if "p" is a valid pointer, it points to the Content
500 * structure of the first partial in the group. So we copy
501 * the file name and/or folder name from that message. In
502 * this case, we also note that we will be appending.
503 */
504 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
505 struct partial *pm = (struct partial *) ct->c_ctparams;
506
507 /* Yep, it's a message/partial */
508 is_partial = 1;
509
510 /* But is it the first and/or last in the collection? */
511 if (pm->pm_partno == 1)
512 first_partial = 1;
513 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
514 last_partial = 1;
515
516 /*
517 * If "p" is a valid pointer, then it points to the
518 * Content structure for the first message in the group.
519 * So we just copy the filename or foldername information
520 * from the previous iteration of this function.
521 */
522 if (p) {
523 appending = 1;
524 ct->c_storage = add (p->c_storage, NULL);
525
526 /* record the folder name */
527 if (p->c_folder) {
528 ct->c_folder = add (p->c_folder, NULL);
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 /*
557 * Check the beginning of storage formatting string
558 * to see if we are saving content to a folder.
559 */
560 if (*cp == '+' || *cp == '@') {
561 char *tmpfilenam, *folder;
562
563 /* Store content in temporary file for now */
564 tmpfilenam = m_scratch ("", invo_name);
565 ct->c_storage = add (tmpfilenam, NULL);
566
567 /* Get the folder name */
568 if (cp[1])
569 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
570 else
571 folder = getfolder (1);
572
573 /* Check if folder exists */
574 if (check_folder (folder) == NOTOK)
575 return NOTOK;
576
577 /* Record the folder name */
578 ct->c_folder = add (folder, NULL);
579
580 if (cp[1])
581 free (folder);
582
583 goto got_filename;
584 }
585
586 /*
587 * Parse and expand the storage formatting string
588 * in `cp' into `buffer'.
589 */
590 parse_format_string (ct, cp, buffer, sizeof(buffer), dir);
591
592 /*
593 * If formatting begins with '|' or '!', then pass
594 * content to standard input of a command and return.
595 */
596 if (buffer[0] == '|' || buffer[0] == '!')
597 return show_content_aux (ct, 1, 0, buffer + 1, dir);
598
599 /* record the filename */
600 ct->c_storage = add (buffer, NULL);
601
602 got_filename:
603 /* flush the output stream */
604 fflush (stdout);
605
606 /* Now save or append the content to a file */
607 if (output_content_file (ct, appending) == NOTOK)
608 return NOTOK;
609
610 /*
611 * If necessary, link the file into a folder and remove
612 * the temporary file. If this message is a partial,
613 * then only do this if it is the last one in the group.
614 */
615 if (ct->c_folder && (!is_partial || last_partial)) {
616 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
617 unlink (ct->c_storage);
618 if (msgnum == NOTOK)
619 return NOTOK;
620 }
621
622 /*
623 * Now print out the name/number of the message
624 * that we are storing.
625 */
626 if (is_partial) {
627 if (first_partial)
628 fprintf (stderr, "reassembling partials ");
629 if (last_partial)
630 fprintf (stderr, "%s", ct->c_file);
631 else
632 fprintf (stderr, "%s,", ct->c_file);
633 } else {
634 fprintf (stderr, "storing message %s", ct->c_file);
635 if (ct->c_partno)
636 fprintf (stderr, " part %s", ct->c_partno);
637 }
638
639 /*
640 * Unless we are in the "middle" of group of message/partials,
641 * we now print the name of the file, folder, and/or message
642 * to which we are storing the content.
643 */
644 if (!is_partial || last_partial) {
645 if (ct->c_folder) {
646 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder, msgnum);
647 } else if (!strcmp(ct->c_storage, "-")) {
648 fprintf (stderr, " to stdout\n");
649 } else {
650 int cwdlen;
651
652 cwdlen = strlen (cwd);
653 fprintf (stderr, " as file %s\n",
654 strncmp (ct->c_storage, cwd, cwdlen)
655 || ct->c_storage[cwdlen] != '/'
656 ? ct->c_storage : ct->c_storage + cwdlen + 1);
657 }
658 }
659
660 return OK;
661 }
662
663
664 /*
665 * Output content to a file
666 */
667
668 static int
669 output_content_file (CT ct, int appending)
670 {
671 int filterstate;
672 char *file, buffer[BUFSIZ];
673 long pos, last;
674 FILE *fp;
675
676 /*
677 * If the pathname is absolute, make sure
678 * all the relevant directories exist.
679 */
680 if (strchr(ct->c_storage, '/')
681 && make_intermediates (ct->c_storage) == NOTOK)
682 return NOTOK;
683
684 if (ct->c_encoding != CE_7BIT) {
685 int cc, fd;
686
687 if (!ct->c_ceopenfnx) {
688 advise (NULL, "don't know how to decode part %s of message %s",
689 ct->c_partno, ct->c_file);
690 return NOTOK;
691 }
692
693 file = appending || !strcmp (ct->c_storage, "-") ? NULL
694 : ct->c_storage;
695 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
696 return NOTOK;
697 if (!strcmp (file, ct->c_storage)) {
698 (*ct->c_ceclosefnx) (ct);
699 return OK;
700 }
701
702 /*
703 * Send to standard output
704 */
705 if (!strcmp (ct->c_storage, "-")) {
706 int gd;
707
708 if ((gd = dup (fileno (stdout))) == NOTOK) {
709 advise ("stdout", "unable to dup");
710 losing:
711 (*ct->c_ceclosefnx) (ct);
712 return NOTOK;
713 }
714 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
715 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
716 appending ? "a" : "w");
717 close (gd);
718 goto losing;
719 }
720 } else {
721 /*
722 * Open output file
723 */
724 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
725 advise (ct->c_storage, "unable to fopen for %s",
726 appending ? "appending" : "writing");
727 goto losing;
728 }
729 }
730
731 /*
732 * Filter the header fields of the initial enclosing
733 * message/partial into the file.
734 */
735 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
736 struct partial *pm = (struct partial *) ct->c_ctparams;
737
738 if (pm->pm_partno == 1)
739 copy_some_headers (fp, ct);
740 }
741
742 for (;;) {
743 switch (cc = read (fd, buffer, sizeof(buffer))) {
744 case NOTOK:
745 advise (file, "error reading content from");
746 break;
747
748 case OK:
749 break;
750
751 default:
752 fwrite (buffer, sizeof(*buffer), cc, fp);
753 continue;
754 }
755 break;
756 }
757
758 (*ct->c_ceclosefnx) (ct);
759
760 if (cc != NOTOK && fflush (fp))
761 advise (ct->c_storage, "error writing to");
762
763 fclose (fp);
764
765 return (cc != NOTOK ? OK : NOTOK);
766 }
767
768 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
769 advise (ct->c_file, "unable to open for reading");
770 return NOTOK;
771 }
772
773 pos = ct->c_begin;
774 last = ct->c_end;
775 fseek (ct->c_fp, pos, SEEK_SET);
776
777 if (!strcmp (ct->c_storage, "-")) {
778 int gd;
779
780 if ((gd = dup (fileno (stdout))) == NOTOK) {
781 advise ("stdout", "unable to dup");
782 return NOTOK;
783 }
784 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
785 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
786 appending ? "a" : "w");
787 close (gd);
788 return NOTOK;
789 }
790 } else {
791 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
792 advise (ct->c_storage, "unable to fopen for %s",
793 appending ? "appending" : "writing");
794 return NOTOK;
795 }
796 }
797
798 /*
799 * Copy a few of the header fields of the initial
800 * enclosing message/partial into the file.
801 */
802 filterstate = 0;
803 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
804 struct partial *pm = (struct partial *) ct->c_ctparams;
805
806 if (pm->pm_partno == 1) {
807 copy_some_headers (fp, ct);
808 filterstate = 1;
809 }
810 }
811
812 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
813 if ((pos += strlen (buffer)) > last) {
814 int diff;
815
816 diff = strlen (buffer) - (pos - last);
817 if (diff >= 0)
818 buffer[diff] = '\0';
819 }
820 /*
821 * If this is the first content of a group of
822 * message/partial contents, then we only copy a few
823 * of the header fields of the enclosed message.
824 */
825 if (filterstate) {
826 switch (buffer[0]) {
827 case ' ':
828 case '\t':
829 if (filterstate < 0)
830 buffer[0] = 0;
831 break;
832
833 case '\n':
834 filterstate = 0;
835 break;
836
837 default:
838 if (!uprf (buffer, XXX_FIELD_PRF)
839 && !uprf (buffer, VRSN_FIELD)
840 && !uprf (buffer, "Subject:")
841 && !uprf (buffer, "Encrypted:")
842 && !uprf (buffer, "Message-ID:")) {
843 filterstate = -1;
844 buffer[0] = 0;
845 break;
846 }
847 filterstate = 1;
848 break;
849 }
850 }
851 fputs (buffer, fp);
852 if (pos >= last)
853 break;
854 }
855
856 if (fflush (fp))
857 advise (ct->c_storage, "error writing to");
858
859 fclose (fp);
860 fclose (ct->c_fp);
861 ct->c_fp = NULL;
862 return OK;
863 }
864
865
866 /*
867 * Check if folder exists, and create
868 * if necessary.
869 */
870
871 static int
872 check_folder (char *folder)
873 {
874 char *folderdir;
875 struct stat st;
876
877 /* expand path to the folder */
878 folderdir = m_mailpath (folder);
879
880 /* Check if folder exists */
881 if (stat (folderdir, &st) == NOTOK) {
882 int answer;
883 char *ep;
884
885 if (errno != ENOENT) {
886 advise (folderdir, "error on folder");
887 return NOTOK;
888 }
889
890 ep = concat ("Create folder \"", folderdir, "\"? ", NULL);
891 answer = getanswer (ep);
892 free (ep);
893
894 if (!answer)
895 return NOTOK;
896
897 if (!makedir (folderdir)) {
898 advise (NULL, "unable to create folder %s", folderdir);
899 return NOTOK;
900 }
901 }
902
903 return OK;
904 }
905
906
907 /*
908 * Add a file to a folder.
909 *
910 * Return the new message number of the file
911 * when added to the folder. Return -1, if
912 * there is an error.
913 */
914
915 static int
916 output_content_folder (char *folder, char *filename)
917 {
918 int msgnum;
919 struct msgs *mp;
920
921 /* Read the folder. */
922 if ((mp = folder_read (folder))) {
923 /* Link file into folder */
924 msgnum = folder_addmsg (&mp, filename, 0, 0, 0, 0);
925 } else {
926 advise (NULL, "unable to read folder %s", folder);
927 return NOTOK;
928 }
929
930 /* free folder structure */
931 folder_free (mp);
932
933 /*
934 * Return msgnum. We are relying on the fact that
935 * msgnum will be -1, if folder_addmsg() had an error.
936 */
937 return msgnum;
938 }
939
940
941 /*
942 * Parse and expand the storage formatting string
943 * pointed to by "cp" into "buffer".
944 */
945
946 static int
947 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
948 {
949 int len;
950 char *bp;
951 CI ci = &ct->c_ctinfo;
952
953 /*
954 * If storage string is "-", just copy it, and
955 * return (send content to standard output).
956 */
957 if (cp[0] == '-' && cp[1] == '\0') {
958 strncpy (buffer, cp, buflen);
959 return 0;
960 }
961
962 bp = buffer;
963 bp[0] = '\0';
964
965 /*
966 * If formatting string is a pathname that doesn't
967 * begin with '/', then preface the path with the
968 * appropriate directory.
969 */
970 if (*cp != '/' && *cp != '|' && *cp != '!') {
971 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
972 len = strlen (bp);
973 bp += len;
974 buflen -= len;
975 }
976
977 for (; *cp; cp++) {
978
979 /* We are processing a storage escape */
980 if (*cp == '%') {
981 switch (*++cp) {
982 case 'a':
983 /*
984 * Insert parameters from Content-Type.
985 * This is only valid for '|' commands.
986 */
987 if (buffer[0] != '|' && buffer[0] != '!') {
988 *bp++ = *--cp;
989 *bp = '\0';
990 buflen--;
991 continue;
992 } else {
993 char **ap, **ep;
994 char *s = "";
995
996 for (ap = ci->ci_attrs, ep = ci->ci_values;
997 *ap; ap++, ep++) {
998 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
999 len = strlen (bp);
1000 bp += len;
1001 buflen -= len;
1002 s = " ";
1003 }
1004 }
1005 break;
1006
1007 case 'm':
1008 /* insert message number */
1009 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
1010 break;
1011
1012 case 'P':
1013 /* insert part number with leading dot */
1014 if (ct->c_partno)
1015 snprintf (bp, buflen, ".%s", ct->c_partno);
1016 break;
1017
1018 case 'p':
1019 /* insert part number withouth leading dot */
1020 if (ct->c_partno)
1021 strncpy (bp, ct->c_partno, buflen);
1022 break;
1023
1024 case 't':
1025 /* insert content type */
1026 strncpy (bp, ci->ci_type, buflen);
1027 break;
1028
1029 case 's':
1030 /* insert content subtype */
1031 strncpy (bp, ci->ci_subtype, buflen);
1032 break;
1033
1034 case '%':
1035 /* insert the character % */
1036 goto raw;
1037
1038 default:
1039 *bp++ = *--cp;
1040 *bp = '\0';
1041 buflen--;
1042 continue;
1043 }
1044
1045 /* Advance bp and decrement buflen */
1046 len = strlen (bp);
1047 bp += len;
1048 buflen -= len;
1049
1050 } else {
1051 raw:
1052 *bp++ = *cp;
1053 *bp = '\0';
1054 buflen--;
1055 }
1056 }
1057
1058 return 0;
1059 }
1060
1061
1062 /*
1063 * Check if the content specifies a filename
1064 * in its MIME parameters.
1065 */
1066
1067 static void
1068 get_storeproc (CT ct)
1069 {
1070 char **ap, **ep, *cp;
1071 CI ci = &ct->c_ctinfo;
1072
1073 /*
1074 * If the storeproc has already been defined,
1075 * we just return (for instance, if this content
1076 * is part of a "message/external".
1077 */
1078 if (ct->c_storeproc)
1079 return;
1080
1081 /*
1082 * Check the attribute/value pairs, for the attribute "name".
1083 * If found, do a few sanity checks and copy the value into
1084 * the storeproc.
1085 */
1086 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1087 if (!strcasecmp (*ap, "name")
1088 && *(cp = *ep) != '/'
1089 && *cp != '.'
1090 && *cp != '|'
1091 && *cp != '!'
1092 && !strchr (cp, '%')) {
1093 ct->c_storeproc = add (cp, NULL);
1094 return;
1095 }
1096 }
1097 }
1098
1099
1100 /*
1101 * Copy some of the header fields of the initial message/partial
1102 * message into the header of the reassembled message.
1103 */
1104
1105 static int
1106 copy_some_headers (FILE *out, CT ct)
1107 {
1108 HF hp;
1109
1110 hp = ct->c_first_hf; /* start at first header field */
1111
1112 while (hp) {
1113 /*
1114 * A few of the header fields of the enclosing
1115 * messages are not copied.
1116 */
1117 if (!uprf (hp->name, XXX_FIELD_PRF)
1118 && strcasecmp (hp->name, VRSN_FIELD)
1119 && strcasecmp (hp->name, "Subject")
1120 && strcasecmp (hp->name, "Encrypted")
1121 && strcasecmp (hp->name, "Message-ID"))
1122 fprintf (out, "%s:%s", hp->name, hp->value);
1123 hp = hp->next; /* next header field */
1124 }
1125
1126 return OK;
1127 }