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 *);
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)
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 = getcpy(zP ? "%euncompress | tar tvf -" : (gzP ? "%egzip -dc | tar tvf -" : "%etar tvf -"));
253 if (!ct->c_storeproc) {
255 ct->c_storeproc = getcpy(zP ? "| uncompress | tar xvpf -" : (gzP ? "| gzip -dc | tar xvpf -" : "| tar xvpf -"));
258 ct->c_storeproc= getcpy(zP ? "%m%P.tar.Z" : (gzP ? "%m%P.tar.gz" : "%m%P.tar"));
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".
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;
316 if (!pm->pm_stored &&
317 strcmp(qm->pm_partid, pm->pm_partid)
319 pm->pm_marked = pm->pm_partno;
330 advise(NULL, "missing (at least) last part of multipart message");
334 if ((base = (CT *) calloc((size_t) (i + 1), sizeof(*base))) == NULL)
335 adios(NULL, "out of memory");
338 for (ctp = cts; *ctp; ctp++) {
340 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
341 pm = (struct partial *) p->c_ctparams;
349 qsort((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
352 for (ctq = base; *ctq; ctq++) {
354 pm = (struct partial *) p->c_ctparams;
355 if (pm->pm_marked != cur) {
356 if (pm->pm_marked == cur - 1) {
357 admonish(NULL, "duplicate part %d of %d part multipart message", pm->pm_marked, hi);
362 advise (NULL, "missing %spart %d of %d part multipart message", cur != hi ? "(at least) " : "", cur, hi);
373 ** Now cycle through the sorted list of messages of type
374 ** "message/partial" and save/append them to a file.
379 if (store_content(ct, NULL) == NOTOK) {
385 for (; *ctq; ctq++) {
387 if (store_content(p, ct) == NOTOK)
397 ** Store content from a message of type "message/external".
401 store_external(CT ct)
404 struct exbody *e = (struct exbody *) ct->c_ctparams;
405 CT p = e->eb_content;
411 ** Check if the parameters for the external body
412 ** specified a filename.
417 if ((cp = e->eb_name) && *cp != '/' && *cp != '.' &&
418 *cp != '|' && *cp != '!' && !strchr(cp, '%')) {
419 if (!ct->c_storeproc)
420 ct->c_storeproc = getcpy(cp);
422 p->c_storeproc = getcpy(cp);
427 ** Since we will let the Content structure for the
428 ** external body substitute for the current content,
429 ** we temporarily change its partno (number inside
430 ** multipart), so everything looks right.
432 p->c_partno = ct->c_partno;
434 /* we probably need to check if content is really there */
435 result = store_switch(p);
443 ** Compare the numbering from two different
444 ** message/partials (needed for sorting).
448 ct_compar(CT *a, CT *b)
450 struct partial *am = (struct partial *) ((*a)->c_ctparams);
451 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
453 return (am->pm_marked - bm->pm_marked);
458 ** Store contents of a message or message part to
459 ** a folder, a file, the standard output, or pass
460 ** the contents to a command.
462 ** If the current content to be saved is a followup part
463 ** to a collection of messages of type "message/partial",
464 ** then field "p" is a pointer to the Content structure
465 ** to the first message/partial in the group.
469 store_content(CT ct, CT p)
471 int appending = 0, msgnum = 0;
472 int is_partial = 0, first_partial = 0;
473 int last_partial = 0;
474 char *cp, buffer[BUFSIZ];
477 ** Do special processing for messages of
478 ** type "message/partial".
480 ** We first check if this content is of type
481 ** "message/partial". If it is, then we need to check
482 ** whether it is the first and/or last in the group.
484 ** Then if "p" is a valid pointer, it points to the Content
485 ** structure of the first partial in the group. So we copy
486 ** the file name and/or folder name from that message. In
487 ** this case, we also note that we will be appending.
489 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
490 struct partial *pm = (struct partial *) ct->c_ctparams;
492 /* Yep, it's a message/partial */
495 /* But is it the first and/or last in the collection? */
496 if (pm->pm_partno == 1)
498 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
502 ** If "p" is a valid pointer, then it points to the
503 ** Content structure for the first message in the group.
504 ** So we just copy the filename or foldername information
505 ** from the previous iteration of this function.
509 ct->c_storage = getcpy(p->c_storage);
511 /* record the folder name */
513 ct->c_folder = getcpy(p->c_folder);
520 ** Get storage formatting string.
522 ** 1) If we have storeproc defined, then use that
523 ** 2) Else check for a mhstore-store-<type>/<subtype> entry
524 ** 3) Else check for a mhstore-store-<type> entry
525 ** 4) Else if content is "message", use "+" (current folder)
526 ** 5) Else use string "%m%P.%s".
528 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
529 CI ci = &ct->c_ctinfo;
531 snprintf(buffer, sizeof(buffer), "%s-store-%s/%s",
532 invo_name, ci->ci_type, ci->ci_subtype);
533 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
534 snprintf(buffer, sizeof(buffer), "%s-store-%s",
535 invo_name, ci->ci_type);
536 if ((cp = context_find(buffer)) == NULL ||
538 cp = ct->c_type == CT_MESSAGE ?
545 ** Check the beginning of storage formatting string
546 ** to see if we are saving content to a folder.
548 if (*cp == '+' || *cp == '@') {
549 char *tmpfilenam, *folder;
551 /* Store content in temporary file for now */
552 tmpfilenam = m_mktemp(invo_name, NULL, NULL);
553 ct->c_storage = getcpy(tmpfilenam);
555 /* Get the folder name */
557 folder = getcpy(expandfol(cp));
559 folder = getcurfol();
561 /* Check if folder exists */
562 create_folder(toabsdir(folder), 0, exit);
564 /* Record the folder name */
565 ct->c_folder = getcpy(folder);
574 ** Parse and expand the storage formatting string
575 ** in `cp' into `buffer'.
577 parse_format_string(ct, cp, buffer, sizeof(buffer), dir);
580 ** If formatting begins with '|' or '!', then pass
581 ** content to standard input of a command and return.
583 if (buffer[0] == '|' || buffer[0] == '!')
584 return show_content_aux(ct, 1, 0, buffer + 1, dir);
586 /* record the filename */
587 ct->c_storage = getcpy(buffer);
590 /* flush the output stream */
593 /* Now save or append the content to a file */
594 if (output_content_file(ct, appending) == NOTOK)
598 ** If necessary, link the file into a folder and remove
599 ** the temporary file. If this message is a partial,
600 ** then only do this if it is the last one in the group.
602 if (ct->c_folder && (!is_partial || last_partial)) {
603 msgnum = output_content_folder(ct->c_folder, ct->c_storage);
604 unlink(ct->c_storage);
610 ** Now print out the name/number of the message
611 ** that we are storing.
615 fprintf(stderr, "reassembling partials ");
617 fprintf(stderr, "%s", ct->c_file);
619 fprintf(stderr, "%s,", ct->c_file);
621 fprintf(stderr, "storing message %s", ct->c_file);
623 fprintf(stderr, " part %s", ct->c_partno);
627 ** Unless we are in the "middle" of group of message/partials,
628 ** we now print the name of the file, folder, and/or message
629 ** to which we are storing the content.
631 if (!is_partial || last_partial) {
633 fprintf(stderr, " to folder %s as message %d\n",
634 ct->c_folder, msgnum);
635 } else if (strcmp(ct->c_storage, "-")==0) {
636 fprintf(stderr, " to stdout\n");
640 cwdlen = strlen(cwd);
641 fprintf(stderr, " as file %s\n",
642 strncmp(ct->c_storage, cwd,
644 ct->c_storage[cwdlen] != '/' ?
646 ct->c_storage + cwdlen + 1);
655 ** Output content to a file
659 output_content_file(CT ct, int appending)
662 char *file, buffer[BUFSIZ];
667 ** If the pathname is absolute, make sure
668 ** all the relevant directories exist.
670 if (strchr(ct->c_storage, '/') && make_intermediates(ct->c_storage)
674 if (ct->c_encoding != CE_7BIT) {
677 if (!ct->c_ceopenfnx) {
678 advise(NULL, "don't know how to decode part %s of message %s", ct->c_partno, ct->c_file);
682 file = appending || strcmp(ct->c_storage, "-")==0 ?
683 NULL : ct->c_storage;
684 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
686 if (strcmp(file, ct->c_storage)==0) {
687 (*ct->c_ceclosefnx) (ct);
692 ** Send to standard output
694 if (strcmp(ct->c_storage, "-")==0) {
697 if ((gd = dup(fileno(stdout))) == NOTOK) {
698 advise("stdout", "unable to dup");
700 (*ct->c_ceclosefnx) (ct);
703 if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
704 advise("stdout", "unable to fdopen (%d, \"%s\") from", gd, appending ? "a" : "w");
712 if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
714 advise(ct->c_storage, "unable to fopen for %s",
716 "appending" : "writing");
722 ** Filter the header fields of the initial enclosing
723 ** message/partial into the file.
725 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
726 struct partial *pm = (struct partial *) ct->c_ctparams;
728 if (pm->pm_partno == 1)
729 copy_some_headers(fp, ct);
733 switch (cc = read(fd, buffer, sizeof(buffer))) {
735 advise(file, "error reading content from");
742 fwrite(buffer, sizeof(*buffer), cc, fp);
748 (*ct->c_ceclosefnx) (ct);
750 if (cc != NOTOK && fflush(fp))
751 advise(ct->c_storage, "error writing to");
755 return (cc != NOTOK ? OK : NOTOK);
758 if (!ct->c_fp && (ct->c_fp = fopen(ct->c_file, "r")) == NULL) {
759 advise(ct->c_file, "unable to open for reading");
765 fseek(ct->c_fp, pos, SEEK_SET);
767 if (strcmp(ct->c_storage, "-")==0) {
770 if ((gd = dup(fileno(stdout))) == NOTOK) {
771 advise("stdout", "unable to dup");
774 if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
775 advise("stdout", "unable to fdopen (%d, \"%s\") from",
776 gd, appending ? "a" : "w");
781 if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
783 advise(ct->c_storage, "unable to fopen for %s",
784 appending ? "appending" : "writing");
790 ** Copy a few of the header fields of the initial
791 ** enclosing message/partial into the file.
794 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
795 struct partial *pm = (struct partial *) ct->c_ctparams;
797 if (pm->pm_partno == 1) {
798 copy_some_headers(fp, ct);
803 while (fgets(buffer, sizeof(buffer) - 1, ct->c_fp)) {
804 if ((pos += strlen(buffer)) > last) {
807 diff = strlen(buffer) - (pos - last);
812 ** If this is the first content of a group of
813 ** message/partial contents, then we only copy a few
814 ** of the header fields of the enclosed message.
829 if (!uprf(buffer, XXX_FIELD_PRF) && !uprf(buffer, VRSN_FIELD) && !uprf(buffer, "Subject:") && !uprf(buffer, "Encrypted:") && !uprf(buffer, "Message-ID:")) {
844 advise(ct->c_storage, "error writing to");
854 ** Add a file to a folder.
856 ** Return the new message number of the file
857 ** when added to the folder. Return -1, if
858 ** there is an error.
862 output_content_folder(char *folder, char *filename)
867 /* Read the folder. */
868 if ((mp = folder_read(folder))) {
869 /* Link file into folder */
870 msgnum = folder_addmsg(&mp, filename, 0, 0, 0, 0, NULL);
872 advise(NULL, "unable to read folder %s", folder);
876 /* free folder structure */
880 ** Return msgnum. We are relying on the fact that
881 ** msgnum will be -1, if folder_addmsg() had an error.
888 ** Parse and expand the storage formatting string
889 ** pointed to by "cp" into "buffer".
893 parse_format_string(CT ct, char *cp, char *buffer, int buflen, char *dir)
897 CI ci = &ct->c_ctinfo;
900 ** If storage string is "-", just copy it, and
901 ** return (send content to standard output).
903 if (cp[0] == '-' && cp[1] == '\0') {
904 strncpy(buffer, cp, buflen);
912 ** If formatting string is a pathname that doesn't
913 ** begin with '/', then preface the path with the
914 ** appropriate directory.
916 if (*cp != '/' && *cp != '|' && *cp != '!') {
917 snprintf(bp, buflen, "%s/", dir[1] ? dir : "");
925 /* We are processing a storage escape */
930 ** Insert parameters from Content-Type.
931 ** This is only valid for '|' commands.
933 if (buffer[0] != '|' && buffer[0] != '!') {
942 for (ap=ci->ci_attrs, ep=ci->ci_values;
956 /* insert message number */
957 snprintf(bp, buflen, "%s",
958 mhbasename(ct->c_file));
962 /* insert part number with leading dot */
964 snprintf(bp, buflen, ".%s",
969 /* insert part number withouth leading dot */
971 strncpy(bp, ct->c_partno, buflen);
975 /* insert content type */
976 strncpy(bp, ci->ci_type, buflen);
980 /* insert content subtype */
981 strncpy(bp, ci->ci_subtype, buflen);
985 /* insert the character % */
995 /* Advance bp and decrement buflen */
1013 ** Check if the content specifies a filename
1014 ** in its MIME parameters.
1018 get_storeproc(CT ct)
1020 char **ap, **ep, *cp;
1021 CI ci = &ct->c_ctinfo;
1024 ** If the storeproc has already been defined,
1025 ** we just return (for instance, if this content
1026 ** is part of a "message/external".
1028 if (ct->c_storeproc)
1032 ** Check the attribute/value pairs, for the attribute "name".
1033 ** If found, do a few sanity checks and copy the value into
1036 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1037 if (!mh_strcasecmp(*ap, "name") && *(cp = *ep) != '/' &&
1038 *cp != '.' && *cp != '|' && *cp != '!' &&
1040 ct->c_storeproc = getcpy(cp);
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 */