2 ** mhstoresbr.c -- routines to save/store the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
19 #include <h/mhparse.h>
24 ** The list of top-level contents to display
31 ** Cache of current directory. This must be
32 ** set before these routines are called.
37 ** The directory in which to store the contents.
42 ** Type for a compare function for qsort. This keeps
43 ** the compiler happy.
45 typedef int (*qsort_comp) (const void *, const void *);
49 int part_ok (CT, int);
50 int type_ok (CT, int);
51 int make_intermediates (char *);
52 void flush_errors (void);
55 int show_content_aux (CT, int, int, char *, char *);
60 void store_all_messages (CT *);
65 static void store_single_message (CT);
66 static int store_switch (CT);
67 static int store_generic (CT);
68 static int store_application (CT);
69 static int store_multi (CT);
70 static int store_partial (CT);
71 static int store_external (CT);
72 static int ct_compar (CT *, CT *);
73 static int store_content (CT, CT);
74 static int output_content_file (CT, int);
75 static int output_content_folder (char *, char *);
76 static int parse_format_string (CT, char *, char *, int, char *);
77 static void get_storeproc (CT);
78 static int copy_some_headers (FILE *, CT);
82 ** Main entry point to store content
83 ** from a collection of messages.
87 store_all_messages (CT *cts)
93 ** Check for the directory in which to
94 ** store any contents.
98 else if ((cp = context_find (nmhstorage)) && *cp)
103 for (ctp = cts; *ctp; ctp++) {
105 store_single_message (ct);
113 ** Entry point to store the content
114 ** in a (single) message
118 store_single_message (CT ct)
120 if (type_ok (ct, 1)) {
127 if (ct->c_ceclosefnx)
128 (*ct->c_ceclosefnx) (ct);
134 ** Switching routine to store different content types
140 switch (ct->c_type) {
142 return store_multi (ct);
146 switch (ct->c_subtype) {
147 case MESSAGE_PARTIAL:
148 return store_partial (ct);
151 case MESSAGE_EXTERNAL:
152 return store_external (ct);
156 return store_generic (ct);
162 return store_application (ct);
169 return store_generic (ct);
173 adios (NULL, "unknown content type %d", ct->c_type);
177 return OK; /* NOT REACHED */
182 ** Generic routine to store a MIME content.
183 ** (audio, video, image, text, message/rfc922)
187 store_generic (CT ct)
190 ** Check if the content specifies a filename.
191 ** Don't bother with this for type "message"
192 ** (only "message/rfc822" will use store_generic).
194 if (autosw && ct->c_type != CT_MESSAGE)
197 return store_content (ct, NULL);
202 ** Store content of type "application"
206 store_application (CT ct)
209 CI ci = &ct->c_ctinfo;
211 /* Check if the content specifies a filename */
216 ** If storeproc is not defined, and the content is type
217 ** "application/octet-stream", we also check for various
218 ** attribute/value pairs which specify if this a tar file.
220 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
221 int tarP = 0, zP = 0, gzP = 0;
223 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
224 /* check for "type=tar" attribute */
225 if (!mh_strcasecmp (*ap, "type")) {
226 if (mh_strcasecmp (*ep, "tar"))
233 /* check for "conversions=compress" attribute */
234 if ((!mh_strcasecmp (*ap, "conversions") ||
235 !mh_strcasecmp (*ap, "x-conversions"))
236 && (!mh_strcasecmp (*ep, "compress") ||
237 !mh_strcasecmp (*ep, "x-compress"))) {
241 /* check for "conversions=gzip" attribute */
242 if ((!mh_strcasecmp (*ap, "conversions") ||
243 !mh_strcasecmp (*ap, "x-conversions"))
244 && (!mh_strcasecmp (*ep, "gzip") ||
245 !mh_strcasecmp (*ep, "x-gzip"))) {
252 ct->c_showproc = add (zP ? "%euncompress | tar tvf -" : (gzP ? "%egzip -dc | tar tvf -" : "%etar tvf -"), NULL);
253 if (!ct->c_storeproc) {
255 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -" : (gzP ? "| gzip -dc | tar xvpf -" : "| tar xvpf -"), NULL);
258 ct->c_storeproc= add (zP ? "%m%P.tar.Z" : (gzP ? "%m%P.tar.gz" : "%m%P.tar"), NULL);
264 return store_content (ct, NULL);
269 ** Store the content of a multipart message
276 struct multipart *m = (struct multipart *) ct->c_ctparams;
280 for (part = m->mp_parts; part; part = part->mp_next) {
281 CT p = part->mp_part;
283 if (part_ok (p, 1) && type_ok (p, 1)) {
284 result = store_switch (p);
285 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
295 ** Reassemble and store the contents of a collection
296 ** of messages of type "message/partial".
300 store_partial (CT ct)
305 struct partial *pm, *qm;
307 qm = (struct partial *) ct->c_ctparams;
312 for (ctp = cts; *ctp; ctp++) {
314 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
315 pm = (struct partial *) p->c_ctparams;
317 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
318 pm->pm_marked = pm->pm_partno;
329 advise (NULL, "missing (at least) last part of multipart message");
333 if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
334 adios (NULL, "out of memory");
337 for (ctp = cts; *ctp; ctp++) {
339 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
340 pm = (struct partial *) p->c_ctparams;
348 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
351 for (ctq = base; *ctq; ctq++) {
353 pm = (struct partial *) p->c_ctparams;
354 if (pm->pm_marked != cur) {
355 if (pm->pm_marked == cur - 1) {
356 admonish (NULL, "duplicate part %d of %d part multipart message", pm->pm_marked, hi);
361 advise (NULL, "missing %spart %d of %d part multipart message", cur != hi ? "(at least) " : "", cur, hi);
372 ** Now cycle through the sorted list of messages of type
373 ** "message/partial" and save/append them to a file.
378 if (store_content (ct, NULL) == NOTOK) {
380 free ((char *) base);
384 for (; *ctq; ctq++) {
386 if (store_content (p, ct) == NOTOK)
390 free ((char *) base);
396 ** Store content from a message of type "message/external".
400 store_external (CT ct)
403 struct exbody *e = (struct exbody *) ct->c_ctparams;
404 CT p = e->eb_content;
410 ** Check if the parameters for the external body
411 ** specified a filename.
416 if ((cp = e->eb_name)
421 && !strchr (cp, '%')) {
422 if (!ct->c_storeproc)
423 ct->c_storeproc = add (cp, NULL);
425 p->c_storeproc = add (cp, NULL);
430 ** Since we will let the Content structure for the
431 ** external body substitute for the current content,
432 ** we temporarily change its partno (number inside
433 ** multipart), so everything looks right.
435 p->c_partno = ct->c_partno;
437 /* we probably need to check if content is really there */
438 result = store_switch (p);
446 ** Compare the numbering from two different
447 ** message/partials (needed for sorting).
451 ct_compar (CT *a, CT *b)
453 struct partial *am = (struct partial *) ((*a)->c_ctparams);
454 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
456 return (am->pm_marked - bm->pm_marked);
461 ** Store contents of a message or message part to
462 ** a folder, a file, the standard output, or pass
463 ** the contents to a command.
465 ** If the current content to be saved is a followup part
466 ** to a collection of messages of type "message/partial",
467 ** then field "p" is a pointer to the Content structure
468 ** to the first message/partial in the group.
472 store_content (CT ct, CT p)
474 int appending = 0, msgnum = 0;
475 int is_partial = 0, first_partial = 0;
476 int last_partial = 0;
477 char *cp, buffer[BUFSIZ];
480 ** Do special processing for messages of
481 ** type "message/partial".
483 ** We first check if this content is of type
484 ** "message/partial". If it is, then we need to check
485 ** whether it is the first and/or last in the group.
487 ** Then if "p" is a valid pointer, it points to the Content
488 ** structure of the first partial in the group. So we copy
489 ** the file name and/or folder name from that message. In
490 ** this case, we also note that we will be appending.
492 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
493 struct partial *pm = (struct partial *) ct->c_ctparams;
495 /* Yep, it's a message/partial */
498 /* But is it the first and/or last in the collection? */
499 if (pm->pm_partno == 1)
501 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
505 ** If "p" is a valid pointer, then it points to the
506 ** Content structure for the first message in the group.
507 ** So we just copy the filename or foldername information
508 ** from the previous iteration of this function.
512 ct->c_storage = add (p->c_storage, NULL);
514 /* record the folder name */
516 ct->c_folder = add (p->c_folder, NULL);
523 ** Get storage formatting string.
525 ** 1) If we have storeproc defined, then use that
526 ** 2) Else check for a mhstore-store-<type>/<subtype> entry
527 ** 3) Else check for a mhstore-store-<type> entry
528 ** 4) Else if content is "message", use "+" (current folder)
529 ** 5) Else use string "%m%P.%s".
531 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
532 CI ci = &ct->c_ctinfo;
534 snprintf (buffer, sizeof(buffer), "%s-store-%s/%s",
535 invo_name, ci->ci_type, ci->ci_subtype);
536 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
537 snprintf (buffer, sizeof(buffer), "%s-store-%s",
538 invo_name, ci->ci_type);
539 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
540 cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
546 ** Check the beginning of storage formatting string
547 ** to see if we are saving content to a folder.
549 if (*cp == '+' || *cp == '@') {
550 char *tmpfilenam, *folder;
552 /* Store content in temporary file for now */
553 tmpfilenam = m_mktemp(invo_name, NULL, NULL);
554 ct->c_storage = add (tmpfilenam, NULL);
556 /* Get the folder name */
558 folder = pluspath (cp);
560 folder = getfolder (1);
562 /* Check if folder exists */
563 create_folder(m_mailpath(folder), 0, exit);
565 /* Record the folder name */
566 ct->c_folder = add (folder, NULL);
575 ** Parse and expand the storage formatting string
576 ** in `cp' into `buffer'.
578 parse_format_string (ct, cp, buffer, sizeof(buffer), dir);
581 ** If formatting begins with '|' or '!', then pass
582 ** content to standard input of a command and return.
584 if (buffer[0] == '|' || buffer[0] == '!')
585 return show_content_aux (ct, 1, 0, buffer + 1, dir);
587 /* record the filename */
588 ct->c_storage = add (buffer, NULL);
591 /* flush the output stream */
594 /* Now save or append the content to a file */
595 if (output_content_file (ct, appending) == NOTOK)
599 ** If necessary, link the file into a folder and remove
600 ** the temporary file. If this message is a partial,
601 ** then only do this if it is the last one in the group.
603 if (ct->c_folder && (!is_partial || last_partial)) {
604 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
605 unlink (ct->c_storage);
611 ** Now print out the name/number of the message
612 ** that we are storing.
616 fprintf (stderr, "reassembling partials ");
618 fprintf (stderr, "%s", ct->c_file);
620 fprintf (stderr, "%s,", ct->c_file);
622 fprintf (stderr, "storing message %s", ct->c_file);
624 fprintf (stderr, " part %s", ct->c_partno);
628 ** Unless we are in the "middle" of group of message/partials,
629 ** we now print the name of the file, folder, and/or message
630 ** to which we are storing the content.
632 if (!is_partial || last_partial) {
634 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder, msgnum);
635 } else if (!strcmp(ct->c_storage, "-")) {
636 fprintf (stderr, " to stdout\n");
640 cwdlen = strlen (cwd);
641 fprintf (stderr, " as file %s\n",
642 strncmp (ct->c_storage, cwd, cwdlen)
643 || ct->c_storage[cwdlen] != '/'
644 ? ct->c_storage : ct->c_storage + cwdlen + 1);
653 ** Output content to a file
657 output_content_file (CT ct, int appending)
660 char *file, buffer[BUFSIZ];
665 ** If the pathname is absolute, make sure
666 ** all the relevant directories exist.
668 if (strchr(ct->c_storage, '/')
669 && make_intermediates (ct->c_storage) == NOTOK)
672 if (ct->c_encoding != CE_7BIT) {
675 if (!ct->c_ceopenfnx) {
676 advise (NULL, "don't know how to decode part %s of message %s",
677 ct->c_partno, ct->c_file);
681 file = appending || !strcmp (ct->c_storage, "-") ? NULL
683 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
685 if (!strcmp (file, ct->c_storage)) {
686 (*ct->c_ceclosefnx) (ct);
691 ** Send to standard output
693 if (!strcmp (ct->c_storage, "-")) {
696 if ((gd = dup (fileno (stdout))) == NOTOK) {
697 advise ("stdout", "unable to dup");
699 (*ct->c_ceclosefnx) (ct);
702 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
703 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
704 appending ? "a" : "w");
712 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
713 advise (ct->c_storage, "unable to fopen for %s",
714 appending ? "appending" : "writing");
720 ** Filter the header fields of the initial enclosing
721 ** message/partial into the file.
723 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
724 struct partial *pm = (struct partial *) ct->c_ctparams;
726 if (pm->pm_partno == 1)
727 copy_some_headers (fp, ct);
731 switch (cc = read (fd, buffer, sizeof(buffer))) {
733 advise (file, "error reading content from");
740 fwrite (buffer, sizeof(*buffer), cc, fp);
746 (*ct->c_ceclosefnx) (ct);
748 if (cc != NOTOK && fflush (fp))
749 advise (ct->c_storage, "error writing to");
753 return (cc != NOTOK ? OK : NOTOK);
756 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
757 advise (ct->c_file, "unable to open for reading");
763 fseek (ct->c_fp, pos, SEEK_SET);
765 if (!strcmp (ct->c_storage, "-")) {
768 if ((gd = dup (fileno (stdout))) == NOTOK) {
769 advise ("stdout", "unable to dup");
772 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
773 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
774 appending ? "a" : "w");
779 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
780 advise (ct->c_storage, "unable to fopen for %s",
781 appending ? "appending" : "writing");
787 ** Copy a few of the header fields of the initial
788 ** enclosing message/partial into the file.
791 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
792 struct partial *pm = (struct partial *) ct->c_ctparams;
794 if (pm->pm_partno == 1) {
795 copy_some_headers (fp, ct);
800 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
801 if ((pos += strlen (buffer)) > last) {
804 diff = strlen (buffer) - (pos - last);
809 ** If this is the first content of a group of
810 ** message/partial contents, then we only copy a few
811 ** of the header fields of the enclosed message.
826 if (!uprf (buffer, XXX_FIELD_PRF)
827 && !uprf (buffer, VRSN_FIELD)
828 && !uprf (buffer, "Subject:")
829 && !uprf (buffer, "Encrypted:")
830 && !uprf (buffer, "Message-ID:")) {
845 advise (ct->c_storage, "error writing to");
855 ** Add a file to a folder.
857 ** Return the new message number of the file
858 ** when added to the folder. Return -1, if
859 ** there is an error.
863 output_content_folder (char *folder, char *filename)
868 /* Read the folder. */
869 if ((mp = folder_read (folder))) {
870 /* Link file into folder */
871 msgnum = folder_addmsg (&mp, filename, 0, 0, 0, 0, (char *)0);
873 advise (NULL, "unable to read folder %s", folder);
877 /* free folder structure */
881 ** Return msgnum. We are relying on the fact that
882 ** msgnum will be -1, if folder_addmsg() had an error.
889 ** Parse and expand the storage formatting string
890 ** pointed to by "cp" into "buffer".
894 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
898 CI ci = &ct->c_ctinfo;
901 ** If storage string is "-", just copy it, and
902 ** return (send content to standard output).
904 if (cp[0] == '-' && cp[1] == '\0') {
905 strncpy (buffer, cp, buflen);
913 ** If formatting string is a pathname that doesn't
914 ** begin with '/', then preface the path with the
915 ** appropriate directory.
917 if (*cp != '/' && *cp != '|' && *cp != '!') {
918 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
926 /* We are processing a storage escape */
931 ** Insert parameters from Content-Type.
932 ** This is only valid for '|' commands.
934 if (buffer[0] != '|' && buffer[0] != '!') {
943 for (ap = ci->ci_attrs, ep = ci->ci_values;
945 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
955 /* insert message number */
956 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
960 /* insert part number with leading dot */
962 snprintf (bp, buflen, ".%s", ct->c_partno);
966 /* insert part number withouth leading dot */
968 strncpy (bp, ct->c_partno, buflen);
972 /* insert content type */
973 strncpy (bp, ci->ci_type, buflen);
977 /* insert content subtype */
978 strncpy (bp, ci->ci_subtype, buflen);
982 /* insert the character % */
992 /* Advance bp and decrement buflen */
1010 ** Check if the content specifies a filename
1011 ** in its MIME parameters.
1015 get_storeproc (CT ct)
1017 char **ap, **ep, *cp;
1018 CI ci = &ct->c_ctinfo;
1021 ** If the storeproc has already been defined,
1022 ** we just return (for instance, if this content
1023 ** is part of a "message/external".
1025 if (ct->c_storeproc)
1029 ** Check the attribute/value pairs, for the attribute "name".
1030 ** If found, do a few sanity checks and copy the value into
1033 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1034 if (!mh_strcasecmp (*ap, "name")
1035 && *(cp = *ep) != '/'
1039 && !strchr (cp, '%')) {
1040 ct->c_storeproc = add (cp, NULL);
1048 ** Copy some of the header fields of the initial message/partial
1049 ** message into the header of the reassembled message.
1053 copy_some_headers (FILE *out, CT ct)
1057 hp = ct->c_first_hf; /* start at first header field */
1061 ** A few of the header fields of the enclosing
1062 ** messages are not copied.
1064 if (!uprf (hp->name, XXX_FIELD_PRF)
1065 && mh_strcasecmp (hp->name, VRSN_FIELD)
1066 && mh_strcasecmp (hp->name, "Subject")
1067 && mh_strcasecmp (hp->name, "Encrypted")
1068 && mh_strcasecmp (hp->name, "Message-ID"))
1069 fprintf (out, "%s:%s", hp->name, hp->value);
1070 hp = hp->next; /* next header field */