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