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>
18 #include <h/mhparse.h>
23 ** The list of top-level contents to display
30 ** Cache of current directory. This must be
31 ** set before these routines are called.
36 ** The directory in which to store the contents.
41 ** Type for a compare function for qsort. This keeps
42 ** the compiler happy.
44 typedef int (*qsort_comp) (const void *, const void *);
50 int make_intermediates(char *);
51 void flush_errors(void);
54 int show_content_aux(CT, int, int, char *, char *);
59 void store_all_messages(CT *);
64 static void store_single_message(CT);
65 static int store_switch(CT);
66 static int store_generic(CT);
67 static int store_application(CT);
68 static int store_multi(CT);
69 static int store_partial(CT);
70 static int store_external(CT);
71 static int ct_compar(CT *, CT *);
72 static int store_content(CT, CT);
73 static int output_content_file(CT, int);
74 static int output_content_folder(char *, char *);
75 static int parse_format_string(CT, char *, char *, int, char *);
76 static void get_storeproc(CT);
77 static int copy_some_headers(FILE *, CT);
81 ** Main entry point to store content
82 ** from a collection of messages.
86 store_all_messages(CT *cts)
92 ** Check for the directory in which to
93 ** store any contents.
97 else if ((cp = context_find(nmhstorage)) && *cp)
102 for (ctp = cts; *ctp; ctp++) {
104 store_single_message(ct);
112 ** Entry point to store the content
113 ** in a (single) message
117 store_single_message(CT ct)
119 if (type_ok(ct, 1)) {
126 if (ct->c_ceclosefnx)
127 (*ct->c_ceclosefnx) (ct);
133 ** Switching routine to store different content types
139 switch (ct->c_type) {
141 return store_multi(ct);
145 switch (ct->c_subtype) {
146 case MESSAGE_PARTIAL:
147 return store_partial(ct);
150 case MESSAGE_EXTERNAL:
151 return store_external(ct);
155 return store_generic(ct);
161 return store_application(ct);
168 return store_generic(ct);
172 adios(NULL, "unknown content type %d", ct->c_type);
176 return OK; /* NOT REACHED */
181 ** Generic routine to store a MIME content.
182 ** (audio, video, image, text, message/rfc922)
189 ** Check if the content specifies a filename.
190 ** Don't bother with this for type "message"
191 ** (only "message/rfc822" will use store_generic).
193 if (autosw && ct->c_type != CT_MESSAGE)
196 return store_content(ct, NULL);
201 ** Store content of type "application"
205 store_application(CT ct)
208 CI ci = &ct->c_ctinfo;
210 /* Check if the content specifies a filename */
215 ** If storeproc is not defined, and the content is type
216 ** "application/octet-stream", we also check for various
217 ** attribute/value pairs which specify if this a tar file.
219 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
220 int tarP = 0, zP = 0, gzP = 0;
222 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
223 /* check for "type=tar" attribute */
224 if (!mh_strcasecmp(*ap, "type")) {
225 if (mh_strcasecmp(*ep, "tar"))
232 /* check for "conversions=compress" attribute */
233 if ((!mh_strcasecmp(*ap, "conversions") ||
234 !mh_strcasecmp(*ap, "x-conversions"))
235 && (!mh_strcasecmp(*ep, "compress") ||
236 !mh_strcasecmp(*ep, "x-compress"))) {
240 /* check for "conversions=gzip" attribute */
241 if ((!mh_strcasecmp(*ap, "conversions") ||
242 !mh_strcasecmp(*ap, "x-conversions"))
243 && (!mh_strcasecmp(*ep, "gzip") ||
244 !mh_strcasecmp(*ep, "x-gzip"))) {
251 ct->c_showproc = getcpy(zP ? "%euncompress | tar tvf -" : (gzP ? "%egzip -dc | tar tvf -" : "%etar tvf -"));
252 if (!ct->c_storeproc) {
254 ct->c_storeproc = getcpy(zP ? "| uncompress | tar xvpf -" : (gzP ? "| gzip -dc | tar xvpf -" : "| tar xvpf -"));
257 ct->c_storeproc= getcpy(zP ? "%m%P.tar.Z" : (gzP ? "%m%P.tar.gz" : "%m%P.tar"));
263 return store_content(ct, NULL);
268 ** Store the content of a multipart message
275 struct multipart *m = (struct multipart *) ct->c_ctparams;
279 for (part = m->mp_parts; part; part = part->mp_next) {
280 CT p = part->mp_part;
282 if (part_ok(p, 1) && type_ok(p, 1)) {
283 result = store_switch(p);
284 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
294 ** Reassemble and store the contents of a collection
295 ** of messages of type "message/partial".
304 struct partial *pm, *qm;
306 qm = (struct partial *) ct->c_ctparams;
311 for (ctp = cts; *ctp; ctp++) {
313 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
314 pm = (struct partial *) p->c_ctparams;
315 if (!pm->pm_stored &&
316 strcmp(qm->pm_partid, pm->pm_partid)
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) {
384 for (; *ctq; ctq++) {
386 if (store_content(p, ct) == NOTOK)
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) && *cp != '/' && *cp != '.' &&
417 *cp != '|' && *cp != '!' && !strchr(cp, '%')) {
418 if (!ct->c_storeproc)
419 ct->c_storeproc = getcpy(cp);
421 p->c_storeproc = getcpy(cp);
426 ** Since we will let the Content structure for the
427 ** external body substitute for the current content,
428 ** we temporarily change its partno (number inside
429 ** multipart), so everything looks right.
431 p->c_partno = ct->c_partno;
433 /* we probably need to check if content is really there */
434 result = store_switch(p);
442 ** Compare the numbering from two different
443 ** message/partials (needed for sorting).
447 ct_compar(CT *a, CT *b)
449 struct partial *am = (struct partial *) ((*a)->c_ctparams);
450 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
452 return (am->pm_marked - bm->pm_marked);
457 ** Store contents of a message or message part to
458 ** a folder, a file, the standard output, or pass
459 ** the contents to a command.
461 ** If the current content to be saved is a followup part
462 ** to a collection of messages of type "message/partial",
463 ** then field "p" is a pointer to the Content structure
464 ** to the first message/partial in the group.
468 store_content(CT ct, CT p)
470 int appending = 0, msgnum = 0;
471 int is_partial = 0, first_partial = 0;
472 int last_partial = 0;
473 char *cp, buffer[BUFSIZ];
476 ** Do special processing for messages of
477 ** type "message/partial".
479 ** We first check if this content is of type
480 ** "message/partial". If it is, then we need to check
481 ** whether it is the first and/or last in the group.
483 ** Then if "p" is a valid pointer, it points to the Content
484 ** structure of the first partial in the group. So we copy
485 ** the file name and/or folder name from that message. In
486 ** this case, we also note that we will be appending.
488 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
489 struct partial *pm = (struct partial *) ct->c_ctparams;
491 /* Yep, it's a message/partial */
494 /* But is it the first and/or last in the collection? */
495 if (pm->pm_partno == 1)
497 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
501 ** If "p" is a valid pointer, then it points to the
502 ** Content structure for the first message in the group.
503 ** So we just copy the filename or foldername information
504 ** from the previous iteration of this function.
508 ct->c_storage = getcpy(p->c_storage);
510 /* record the folder name */
512 ct->c_folder = getcpy(p->c_folder);
519 ** Get storage formatting string.
521 ** 1) If we have storeproc defined, then use that
522 ** 2) Else check for a mhstore-store-<type>/<subtype> entry
523 ** 3) Else check for a mhstore-store-<type> entry
524 ** 4) Else if content is "message", use "+" (current folder)
525 ** 5) Else use string "%m%P.%s".
527 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
528 CI ci = &ct->c_ctinfo;
530 snprintf(buffer, sizeof(buffer), "%s-store-%s/%s",
531 invo_name, ci->ci_type, ci->ci_subtype);
532 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
533 snprintf(buffer, sizeof(buffer), "%s-store-%s",
534 invo_name, ci->ci_type);
535 if ((cp = context_find(buffer)) == NULL ||
537 cp = ct->c_type == CT_MESSAGE ?
544 ** Check the beginning of storage formatting string
545 ** to see if we are saving content to a folder.
547 if (*cp == '+' || *cp == '@') {
548 char *tmpfilenam, *folder;
550 /* Store content in temporary file for now */
551 tmpfilenam = m_mktemp(invo_name, NULL, NULL);
552 ct->c_storage = getcpy(tmpfilenam);
554 /* Get the folder name */
556 folder = getcpy(expandfol(cp));
558 folder = getcurfol();
560 /* Check if folder exists */
561 create_folder(toabsdir(folder), 0, exit);
563 /* Record the folder name */
564 ct->c_folder = getcpy(folder);
573 ** Parse and expand the storage formatting string
574 ** in `cp' into `buffer'.
576 parse_format_string(ct, cp, buffer, sizeof(buffer), dir);
579 ** If formatting begins with '|' or '!', then pass
580 ** content to standard input of a command and return.
582 if (buffer[0] == '|' || buffer[0] == '!')
583 return show_content_aux(ct, 1, 0, buffer + 1, dir);
585 /* record the filename */
586 ct->c_storage = getcpy(buffer);
589 /* flush the output stream */
592 /* Now save or append the content to a file */
593 if (output_content_file(ct, appending) == NOTOK)
597 ** If necessary, link the file into a folder and remove
598 ** the temporary file. If this message is a partial,
599 ** then only do this if it is the last one in the group.
601 if (ct->c_folder && (!is_partial || last_partial)) {
602 msgnum = output_content_folder(ct->c_folder, ct->c_storage);
603 unlink(ct->c_storage);
609 ** Now print out the name/number of the message
610 ** that we are storing.
614 fprintf(stderr, "reassembling partials ");
616 fprintf(stderr, "%s", ct->c_file);
618 fprintf(stderr, "%s,", ct->c_file);
620 fprintf(stderr, "storing message %s", ct->c_file);
622 fprintf(stderr, " part %s", ct->c_partno);
626 ** Unless we are in the "middle" of group of message/partials,
627 ** we now print the name of the file, folder, and/or message
628 ** to which we are storing the content.
630 if (!is_partial || last_partial) {
632 fprintf(stderr, " to folder %s as message %d\n",
633 ct->c_folder, msgnum);
634 } else if (strcmp(ct->c_storage, "-")==0) {
635 fprintf(stderr, " to stdout\n");
639 cwdlen = strlen(cwd);
640 fprintf(stderr, " as file %s\n",
641 strncmp(ct->c_storage, cwd,
643 ct->c_storage[cwdlen] != '/' ?
645 ct->c_storage + cwdlen + 1);
654 ** Output content to a file
658 output_content_file(CT ct, int appending)
661 char *file, buffer[BUFSIZ];
666 ** If the pathname is absolute, make sure
667 ** all the relevant directories exist.
669 if (strchr(ct->c_storage, '/') && make_intermediates(ct->c_storage)
673 if (ct->c_encoding != CE_7BIT) {
676 if (!ct->c_ceopenfnx) {
677 advise(NULL, "don't know how to decode part %s of message %s", ct->c_partno, ct->c_file);
681 file = appending || strcmp(ct->c_storage, "-")==0 ?
682 NULL : ct->c_storage;
683 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
685 if (strcmp(file, ct->c_storage)==0) {
686 (*ct->c_ceclosefnx) (ct);
691 ** Send to standard output
693 if (strcmp(ct->c_storage, "-")==0) {
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, appending ? "a" : "w");
711 if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
713 advise(ct->c_storage, "unable to fopen for %s",
715 "appending" : "writing");
721 ** Filter the header fields of the initial enclosing
722 ** message/partial into the file.
724 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
725 struct partial *pm = (struct partial *) ct->c_ctparams;
727 if (pm->pm_partno == 1)
728 copy_some_headers(fp, ct);
732 switch (cc = read(fd, buffer, sizeof(buffer))) {
734 advise(file, "error reading content from");
741 fwrite(buffer, sizeof(*buffer), cc, fp);
747 (*ct->c_ceclosefnx) (ct);
749 if (cc != NOTOK && fflush(fp))
750 advise(ct->c_storage, "error writing to");
754 return (cc != NOTOK ? OK : NOTOK);
757 if (!ct->c_fp && (ct->c_fp = fopen(ct->c_file, "r")) == NULL) {
758 advise(ct->c_file, "unable to open for reading");
764 fseek(ct->c_fp, pos, SEEK_SET);
766 if (strcmp(ct->c_storage, "-")==0) {
769 if ((gd = dup(fileno(stdout))) == NOTOK) {
770 advise("stdout", "unable to dup");
773 if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
774 advise("stdout", "unable to fdopen (%d, \"%s\") from",
775 gd, appending ? "a" : "w");
780 if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
782 advise(ct->c_storage, "unable to fopen for %s",
783 appending ? "appending" : "writing");
789 ** Copy a few of the header fields of the initial
790 ** enclosing message/partial into the file.
793 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
794 struct partial *pm = (struct partial *) ct->c_ctparams;
796 if (pm->pm_partno == 1) {
797 copy_some_headers(fp, ct);
802 while (fgets(buffer, sizeof(buffer) - 1, ct->c_fp)) {
803 if ((pos += strlen(buffer)) > last) {
806 diff = strlen(buffer) - (pos - last);
811 ** If this is the first content of a group of
812 ** message/partial contents, then we only copy a few
813 ** of the header fields of the enclosed message.
828 if (!uprf(buffer, XXX_FIELD_PRF) && !uprf(buffer, VRSN_FIELD) && !uprf(buffer, "Subject:") && !uprf(buffer, "Encrypted:") && !uprf(buffer, "Message-ID:")) {
843 advise(ct->c_storage, "error writing to");
853 ** Add a file to a folder.
855 ** Return the new message number of the file
856 ** when added to the folder. Return -1, if
857 ** there is an error.
861 output_content_folder(char *folder, char *filename)
866 /* Read the folder. */
867 if ((mp = folder_read(folder))) {
868 /* Link file into folder */
869 msgnum = folder_addmsg(&mp, filename, 0, 0, 0, 0, NULL);
871 advise(NULL, "unable to read folder %s", folder);
875 /* free folder structure */
879 ** Return msgnum. We are relying on the fact that
880 ** msgnum will be -1, if folder_addmsg() had an error.
887 ** Parse and expand the storage formatting string
888 ** pointed to by "cp" into "buffer".
892 parse_format_string(CT ct, char *cp, char *buffer, int buflen, char *dir)
896 CI ci = &ct->c_ctinfo;
899 ** If storage string is "-", just copy it, and
900 ** return (send content to standard output).
902 if (cp[0] == '-' && cp[1] == '\0') {
903 strncpy(buffer, cp, buflen);
911 ** If formatting string is a pathname that doesn't
912 ** begin with '/', then preface the path with the
913 ** appropriate directory.
915 if (*cp != '/' && *cp != '|' && *cp != '!') {
916 snprintf(bp, buflen, "%s/", dir[1] ? dir : "");
924 /* We are processing a storage escape */
929 ** Insert parameters from Content-Type.
930 ** This is only valid for '|' commands.
932 if (buffer[0] != '|' && buffer[0] != '!') {
941 for (ap=ci->ci_attrs, ep=ci->ci_values;
955 /* insert message number */
956 snprintf(bp, buflen, "%s",
957 mhbasename(ct->c_file));
961 /* insert part number with leading dot */
963 snprintf(bp, buflen, ".%s",
968 /* insert part number withouth leading dot */
970 strncpy(bp, ct->c_partno, buflen);
974 /* insert content type */
975 strncpy(bp, ci->ci_type, buflen);
979 /* insert content subtype */
980 strncpy(bp, ci->ci_subtype, buflen);
984 /* insert the character % */
994 /* Advance bp and decrement buflen */
1012 ** Check if the content specifies a filename
1013 ** in its MIME parameters.
1017 get_storeproc(CT ct)
1019 char **ap, **ep, *cp;
1020 CI ci = &ct->c_ctinfo;
1023 ** If the storeproc has already been defined,
1024 ** we just return (for instance, if this content
1025 ** is part of a "message/external".
1027 if (ct->c_storeproc)
1031 ** Check the attribute/value pairs, for the attribute "name".
1032 ** If found, do a few sanity checks and copy the value into
1035 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1036 if (!mh_strcasecmp(*ap, "name") && *(cp = *ep) != '/' &&
1037 *cp != '.' && *cp != '|' && *cp != '!' &&
1039 ct->c_storeproc = getcpy(cp);
1047 ** Copy some of the header fields of the initial message/partial
1048 ** message into the header of the reassembled message.
1052 copy_some_headers(FILE *out, CT ct)
1056 hp = ct->c_first_hf; /* start at first header field */
1060 ** A few of the header fields of the enclosing
1061 ** messages are not copied.
1063 if (!uprf(hp->name, XXX_FIELD_PRF) &&
1064 mh_strcasecmp(hp->name, VRSN_FIELD) &&
1065 mh_strcasecmp(hp->name, "Subject") &&
1066 mh_strcasecmp(hp->name, "Encrypted") &&
1067 mh_strcasecmp(hp->name, "Message-ID"))
1068 fprintf(out, "%s:%s", hp->name, hp->value);
1069 hp = hp->next; /* next header field */