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