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