3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
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.
14 #include <h/signals.h>
22 #include <h/mhparse.h>
27 * The list of top-level contents to display
34 * Cache of current directory. This must be
35 * set before these routines are called.
40 * The directory in which to store the contents.
45 * Type for a compare function for qsort. This keeps
48 typedef int (*qsort_comp) (const void *, const void *);
52 int part_ok (CT, int);
53 int type_ok (CT, int);
54 int make_intermediates (char *);
55 void flush_errors (void);
58 int show_content_aux (CT, int, int, char *, char *);
63 void store_all_messages (CT *);
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);
85 * Main entry point to store content
86 * from a collection of messages.
90 store_all_messages (CT *cts)
96 * Check for the directory in which to
101 else if ((cp = context_find (nmhstorage)) && *cp)
106 for (ctp = cts; *ctp; ctp++) {
108 store_single_message (ct);
116 * Entry point to store the content
117 * in a (single) message
121 store_single_message (CT ct)
123 if (type_ok (ct, 1)) {
130 if (ct->c_ceclosefnx)
131 (*ct->c_ceclosefnx) (ct);
137 * Switching routine to store different content types
143 switch (ct->c_type) {
145 return store_multi (ct);
149 switch (ct->c_subtype) {
150 case MESSAGE_PARTIAL:
151 return store_partial (ct);
154 case MESSAGE_EXTERNAL:
155 return store_external (ct);
159 return store_generic (ct);
165 return store_application (ct);
172 return store_generic (ct);
176 adios (NULL, "unknown content type %d", ct->c_type);
180 return OK; /* NOT REACHED */
185 * Generic routine to store a MIME content.
186 * (audio, video, image, text, message/rfc922)
190 store_generic (CT ct)
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).
197 if (autosw && ct->c_type != CT_MESSAGE)
200 return store_content (ct, NULL);
205 * Store content of type "application"
209 store_application (CT ct)
212 CI ci = &ct->c_ctinfo;
214 /* Check if the content specifies a filename */
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.
223 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
224 int tarP = 0, zP = 0, gzP = 0;
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"))
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"))) {
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"))) {
251 ct->c_showproc = add (zP ? "%euncompress | tar tvf -"
252 : (gzP ? "%egzip -dc | tar tvf -"
253 : "%etar tvf -"), NULL);
254 if (!ct->c_storeproc) {
256 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -"
257 : (gzP ? "| gzip -dc | tar xvpf -"
258 : "| tar xvpf -"), NULL);
261 ct->c_storeproc= add (zP ? "%m%P.tar.Z"
262 : (gzP ? "%m%P.tar.gz"
263 : "%m%P.tar"), NULL);
269 return store_content (ct, NULL);
274 * Store the content of a multipart message
281 struct multipart *m = (struct multipart *) ct->c_ctparams;
285 for (part = m->mp_parts; part; part = part->mp_next) {
286 CT p = part->mp_part;
288 if (part_ok (p, 1) && type_ok (p, 1)) {
289 result = store_switch (p);
290 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
300 * Reassemble and store the contents of a collection
301 * of messages of type "message/partial".
305 store_partial (CT ct)
310 struct partial *pm, *qm;
312 qm = (struct partial *) ct->c_ctparams;
317 for (ctp = cts; *ctp; ctp++) {
319 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
320 pm = (struct partial *) p->c_ctparams;
322 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
323 pm->pm_marked = pm->pm_partno;
335 advise (NULL, "missing (at least) last part of multipart message");
339 if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
340 adios (NULL, "out of memory");
343 for (ctp = cts; *ctp; ctp++) {
345 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
346 pm = (struct partial *) p->c_ctparams;
354 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
357 for (ctq = base; *ctq; ctq++) {
359 pm = (struct partial *) p->c_ctparams;
360 if (pm->pm_marked != cur) {
361 if (pm->pm_marked == cur - 1) {
363 "duplicate part %d of %d part multipart message",
370 "missing %spart %d of %d part multipart message",
371 cur != hi ? "(at least) " : "", cur, hi);
383 * Now cycle through the sorted list of messages of type
384 * "message/partial" and save/append them to a file.
389 if (store_content (ct, NULL) == NOTOK) {
391 free ((char *) base);
395 for (; *ctq; ctq++) {
397 if (store_content (p, ct) == NOTOK)
401 free ((char *) base);
407 * Store content from a message of type "message/external".
411 store_external (CT ct)
414 struct exbody *e = (struct exbody *) ct->c_ctparams;
415 CT p = e->eb_content;
421 * Check if the parameters for the external body
422 * specified a filename.
427 if ((cp = e->eb_name)
432 && !strchr (cp, '%')) {
433 if (!ct->c_storeproc)
434 ct->c_storeproc = add (cp, NULL);
436 p->c_storeproc = add (cp, NULL);
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.
446 p->c_partno = ct->c_partno;
448 /* we probably need to check if content is really there */
449 result = store_switch (p);
457 * Compare the numbering from two different
458 * message/partials (needed for sorting).
462 ct_compar (CT *a, CT *b)
464 struct partial *am = (struct partial *) ((*a)->c_ctparams);
465 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
467 return (am->pm_marked - bm->pm_marked);
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.
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.
483 store_content (CT ct, CT p)
485 int appending = 0, msgnum = 0;
486 int is_partial = 0, first_partial = 0;
487 int last_partial = 0;
488 char *cp, buffer[BUFSIZ];
491 * Do special processing for messages of
492 * type "message/partial".
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.
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.
503 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
504 struct partial *pm = (struct partial *) ct->c_ctparams;
506 /* Yep, it's a message/partial */
509 /* But is it the first and/or last in the collection? */
510 if (pm->pm_partno == 1)
512 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
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.
523 ct->c_storage = add (p->c_storage, NULL);
525 /* record the folder name */
527 ct->c_folder = add (p->c_folder, NULL);
534 * Get storage formatting string.
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".
542 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
543 CI ci = &ct->c_ctinfo;
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";
556 * Check the beginning of storage formatting string
557 * to see if we are saving content to a folder.
559 if (*cp == '+' || *cp == '@') {
560 char *tmpfilenam, *folder;
562 /* Store content in temporary file for now */
563 tmpfilenam = m_mktemp(invo_name, NULL, NULL);
564 ct->c_storage = add (tmpfilenam, NULL);
566 /* Get the folder name */
568 folder = pluspath (cp);
570 folder = getfolder (1);
572 /* Check if folder exists */
573 create_folder(m_mailpath(folder), 0, exit);
575 /* Record the folder name */
576 ct->c_folder = add (folder, NULL);
585 * Parse and expand the storage formatting string
586 * in `cp' into `buffer'.
588 parse_format_string (ct, cp, buffer, sizeof(buffer), dir);
591 * If formatting begins with '|' or '!', then pass
592 * content to standard input of a command and return.
594 if (buffer[0] == '|' || buffer[0] == '!')
595 return show_content_aux (ct, 1, 0, buffer + 1, dir);
597 /* record the filename */
598 ct->c_storage = add (buffer, NULL);
601 /* flush the output stream */
604 /* Now save or append the content to a file */
605 if (output_content_file (ct, appending) == NOTOK)
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.
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);
621 * Now print out the name/number of the message
622 * that we are storing.
626 fprintf (stderr, "reassembling partials ");
628 fprintf (stderr, "%s", ct->c_file);
630 fprintf (stderr, "%s,", ct->c_file);
632 fprintf (stderr, "storing message %s", ct->c_file);
634 fprintf (stderr, " part %s", ct->c_partno);
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.
642 if (!is_partial || last_partial) {
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");
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);
663 * Output content to a file
667 output_content_file (CT ct, int appending)
670 char *file, buffer[BUFSIZ];
675 * If the pathname is absolute, make sure
676 * all the relevant directories exist.
678 if (strchr(ct->c_storage, '/')
679 && make_intermediates (ct->c_storage) == NOTOK)
682 if (ct->c_encoding != CE_7BIT) {
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);
691 file = appending || !strcmp (ct->c_storage, "-") ? NULL
693 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
695 if (!strcmp (file, ct->c_storage)) {
696 (*ct->c_ceclosefnx) (ct);
701 * Send to standard output
703 if (!strcmp (ct->c_storage, "-")) {
706 if ((gd = dup (fileno (stdout))) == NOTOK) {
707 advise ("stdout", "unable to dup");
709 (*ct->c_ceclosefnx) (ct);
712 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
713 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
714 appending ? "a" : "w");
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");
730 * Filter the header fields of the initial enclosing
731 * message/partial into the file.
733 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
734 struct partial *pm = (struct partial *) ct->c_ctparams;
736 if (pm->pm_partno == 1)
737 copy_some_headers (fp, ct);
741 switch (cc = read (fd, buffer, sizeof(buffer))) {
743 advise (file, "error reading content from");
750 fwrite (buffer, sizeof(*buffer), cc, fp);
756 (*ct->c_ceclosefnx) (ct);
758 if (cc != NOTOK && fflush (fp))
759 advise (ct->c_storage, "error writing to");
763 return (cc != NOTOK ? OK : NOTOK);
766 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
767 advise (ct->c_file, "unable to open for reading");
773 fseek (ct->c_fp, pos, SEEK_SET);
775 if (!strcmp (ct->c_storage, "-")) {
778 if ((gd = dup (fileno (stdout))) == NOTOK) {
779 advise ("stdout", "unable to dup");
782 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
783 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
784 appending ? "a" : "w");
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");
797 * Copy a few of the header fields of the initial
798 * enclosing message/partial into the file.
801 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
802 struct partial *pm = (struct partial *) ct->c_ctparams;
804 if (pm->pm_partno == 1) {
805 copy_some_headers (fp, ct);
810 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
811 if ((pos += strlen (buffer)) > last) {
814 diff = strlen (buffer) - (pos - last);
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.
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:")) {
855 advise (ct->c_storage, "error writing to");
865 * Add a file to a folder.
867 * Return the new message number of the file
868 * when added to the folder. Return -1, if
873 output_content_folder (char *folder, char *filename)
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);
883 advise (NULL, "unable to read folder %s", folder);
887 /* free folder structure */
891 * Return msgnum. We are relying on the fact that
892 * msgnum will be -1, if folder_addmsg() had an error.
899 * Parse and expand the storage formatting string
900 * pointed to by "cp" into "buffer".
904 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
908 CI ci = &ct->c_ctinfo;
911 * If storage string is "-", just copy it, and
912 * return (send content to standard output).
914 if (cp[0] == '-' && cp[1] == '\0') {
915 strncpy (buffer, cp, buflen);
923 * If formatting string is a pathname that doesn't
924 * begin with '/', then preface the path with the
925 * appropriate directory.
927 if (*cp != '/' && *cp != '|' && *cp != '!') {
928 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
936 /* We are processing a storage escape */
941 * Insert parameters from Content-Type.
942 * This is only valid for '|' commands.
944 if (buffer[0] != '|' && buffer[0] != '!') {
953 for (ap = ci->ci_attrs, ep = ci->ci_values;
955 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
965 /* insert message number */
966 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
970 /* insert part number with leading dot */
972 snprintf (bp, buflen, ".%s", ct->c_partno);
976 /* insert part number withouth leading dot */
978 strncpy (bp, ct->c_partno, buflen);
982 /* insert content type */
983 strncpy (bp, ci->ci_type, buflen);
987 /* insert content subtype */
988 strncpy (bp, ci->ci_subtype, buflen);
992 /* insert the character % */
1002 /* Advance bp and decrement buflen */
1020 * Check if the content specifies a filename
1021 * in its MIME parameters.
1025 get_storeproc (CT ct)
1027 char **ap, **ep, *cp;
1028 CI ci = &ct->c_ctinfo;
1031 * If the storeproc has already been defined,
1032 * we just return (for instance, if this content
1033 * is part of a "message/external".
1035 if (ct->c_storeproc)
1039 * Check the attribute/value pairs, for the attribute "name".
1040 * If found, do a few sanity checks and copy the value into
1043 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1044 if (!mh_strcasecmp (*ap, "name")
1045 && *(cp = *ep) != '/'
1049 && !strchr (cp, '%')) {
1050 ct->c_storeproc = add (cp, NULL);
1058 * Copy some of the header fields of the initial message/partial
1059 * message into the header of the reassembled message.
1063 copy_some_headers (FILE *out, CT ct)
1067 hp = ct->c_first_hf; /* start at first header field */
1071 * A few of the header fields of the enclosing
1072 * messages are not copied.
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 */