3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
10 #include <h/signals.h>
15 #include <zotnet/mts/mts.h>
16 #include <zotnet/tws/tws.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
44 typedef int (*qsort_comp) (const void *, const void *);
48 int part_ok (CT, int);
49 int type_ok (CT, int);
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 check_folder (char *);
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
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;
223 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
224 /* check for "type=tar" attribute */
225 if (!strcasecmp (*ap, "type")) {
226 if (strcasecmp (*ep, "tar"))
233 /* check for "conversions=compress" attribute */
234 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
235 && (!strcasecmp (*ep, "compress") || !strcasecmp (*ep, "x-compress"))) {
242 ct->c_showproc = add (zP ? "%euncompress | tar tvf -"
243 : "%etar tvf -", NULL);
244 if (!ct->c_storeproc)
246 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -"
247 : "| tar xvpf -", NULL);
250 ct->c_storeproc = add (zP ? "%m%P.tar.Z" : "%m%P.tar", NULL);
255 return store_content (ct, NULL);
260 * Store the content of a multipart message
267 struct multipart *m = (struct multipart *) ct->c_ctparams;
271 for (part = m->mp_parts; part; part = part->mp_next) {
272 CT p = part->mp_part;
274 if (part_ok (p, 1) && type_ok (p, 1)) {
275 result = store_switch (p);
276 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
286 * Reassemble and store the contents of a collection
287 * of messages of type "message/partial".
291 store_partial (CT ct)
296 struct partial *pm, *qm;
298 qm = (struct partial *) ct->c_ctparams;
303 for (ctp = cts; *ctp; ctp++) {
305 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
306 pm = (struct partial *) p->c_ctparams;
308 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
309 pm->pm_marked = pm->pm_partno;
321 advise (NULL, "missing (at least) last part of multipart message");
325 if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
326 adios (NULL, "out of memory");
329 for (ctp = cts; *ctp; ctp++) {
331 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
332 pm = (struct partial *) p->c_ctparams;
340 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
343 for (ctq = base; *ctq; ctq++) {
345 pm = (struct partial *) p->c_ctparams;
346 if (pm->pm_marked != cur) {
347 if (pm->pm_marked == cur - 1) {
349 "duplicate part %d of %d part multipart message",
356 "missing %spart %d of %d part multipart message",
357 cur != hi ? "(at least) " : "", cur, hi);
369 * Now cycle through the sorted list of messages of type
370 * "message/partial" and save/append them to a file.
375 if (store_content (ct, NULL) == NOTOK) {
377 free ((char *) base);
381 for (; *ctq; ctq++) {
383 if (store_content (p, ct) == NOTOK)
387 free ((char *) base);
393 * Store content from a message of type "message/external".
397 store_external (CT ct)
400 struct exbody *e = (struct exbody *) ct->c_ctparams;
401 CT p = e->eb_content;
407 * Check if the parameters for the external body
408 * specified a filename.
413 if ((cp = e->eb_name)
418 && !strchr (cp, '%')) {
419 if (!ct->c_storeproc)
420 ct->c_storeproc = add (cp, NULL);
422 p->c_storeproc = add (cp, NULL);
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;
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 = add (p->c_storage, NULL);
511 /* record the folder name */
513 ct->c_folder = add (p->c_folder, NULL);
520 * Get storage formatting string.
522 * 1) If we have storeproc defined, then use that
523 * 2) Else check for a mhn-store-<type>/<subtype> entry
524 * 3) Else check for a mhn-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", invo_name, ci->ci_type);
535 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
536 cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
542 * Check the beginning of storage formatting string
543 * to see if we are saving content to a folder.
545 if (*cp == '+' || *cp == '@') {
546 char *tmpfilenam, *folder;
548 /* Store content in temporary file for now */
549 tmpfilenam = m_scratch ("", invo_name);
550 ct->c_storage = add (tmpfilenam, NULL);
552 /* Get the folder name */
554 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
556 folder = getfolder (1);
558 /* Check if folder exists */
559 if (check_folder (folder) == NOTOK)
562 /* Record the folder name */
563 ct->c_folder = add (folder, NULL);
572 * Parse and expand the storage formatting string
573 * in `cp' into `buffer'.
575 parse_format_string (ct, cp, buffer, sizeof(buffer), dir);
578 * If formatting begins with '|' or '!', then pass
579 * content to standard input of a command and return.
581 if (buffer[0] == '|' || buffer[0] == '!')
582 return show_content_aux (ct, 1, 0, buffer + 1, dir);
584 /* record the filename */
585 ct->c_storage = add (buffer, NULL);
588 /* flush the output stream */
591 /* Now save or append the content to a file */
592 if (output_content_file (ct, appending) == NOTOK)
596 * If necessary, link the file into a folder and remove
597 * the temporary file. If this message is a partial,
598 * then only do this if it is the last one in the group.
600 if (ct->c_folder && (!is_partial || last_partial)) {
601 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
602 unlink (ct->c_storage);
608 * Now print out the name/number of the message
609 * that we are storing.
613 fprintf (stderr, "reassembling partials ");
615 fprintf (stderr, "%s", ct->c_file);
617 fprintf (stderr, "%s,", ct->c_file);
619 fprintf (stderr, "storing message %s", ct->c_file);
621 fprintf (stderr, " part %s", ct->c_partno);
625 * Unless we are in the "middle" of group of message/partials,
626 * we now print the name of the file, folder, and/or message
627 * to which we are storing the content.
629 if (!is_partial || last_partial) {
631 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder, msgnum);
632 } else if (!strcmp(ct->c_storage, "-")) {
633 fprintf (stderr, " to stdout\n");
637 cwdlen = strlen (cwd);
638 fprintf (stderr, " as file %s\n",
639 strncmp (ct->c_storage, cwd, cwdlen)
640 || ct->c_storage[cwdlen] != '/'
641 ? ct->c_storage : ct->c_storage + cwdlen + 1);
650 * Output content to a file
654 output_content_file (CT ct, int appending)
657 char *file, buffer[BUFSIZ];
662 * If the pathname is absolute, make sure
663 * all the relevant directories exist.
665 if (strchr(ct->c_storage, '/')
666 && make_intermediates (ct->c_storage) == NOTOK)
669 if (ct->c_encoding != CE_7BIT) {
672 if (!ct->c_ceopenfnx) {
673 advise (NULL, "don't know how to decode part %s of message %s",
674 ct->c_partno, ct->c_file);
678 file = appending || !strcmp (ct->c_storage, "-") ? NULL
680 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
682 if (!strcmp (file, ct->c_storage)) {
683 (*ct->c_ceclosefnx) (ct);
688 * Send to standard output
690 if (!strcmp (ct->c_storage, "-")) {
693 if ((gd = dup (fileno (stdout))) == NOTOK) {
694 advise ("stdout", "unable to dup");
696 (*ct->c_ceclosefnx) (ct);
699 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
700 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
701 appending ? "a" : "w");
709 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
710 advise (ct->c_storage, "unable to fopen for %s",
711 appending ? "appending" : "writing");
717 * Filter the header fields of the initial enclosing
718 * message/partial into the file.
720 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
721 struct partial *pm = (struct partial *) ct->c_ctparams;
723 if (pm->pm_partno == 1)
724 copy_some_headers (fp, ct);
728 switch (cc = read (fd, buffer, sizeof(buffer))) {
730 advise (file, "error reading content from");
737 fwrite (buffer, sizeof(*buffer), cc, fp);
743 (*ct->c_ceclosefnx) (ct);
745 if (cc != NOTOK && fflush (fp))
746 advise (ct->c_storage, "error writing to");
750 return (cc != NOTOK ? OK : NOTOK);
753 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
754 advise (ct->c_file, "unable to open for reading");
760 fseek (ct->c_fp, pos, SEEK_SET);
762 if (!strcmp (ct->c_storage, "-")) {
765 if ((gd = dup (fileno (stdout))) == NOTOK) {
766 advise ("stdout", "unable to dup");
769 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
770 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
771 appending ? "a" : "w");
776 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
777 advise (ct->c_storage, "unable to fopen for %s",
778 appending ? "appending" : "writing");
784 * Copy a few of the header fields of the initial
785 * enclosing message/partial into the file.
788 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
789 struct partial *pm = (struct partial *) ct->c_ctparams;
791 if (pm->pm_partno == 1) {
792 copy_some_headers (fp, ct);
797 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
798 if ((pos += strlen (buffer)) > last) {
801 diff = strlen (buffer) - (pos - last);
806 * If this is the first content of a group of
807 * message/partial contents, then we only copy a few
808 * of the header fields of the enclosed message.
823 if (!uprf (buffer, XXX_FIELD_PRF)
824 && !uprf (buffer, VRSN_FIELD)
825 && !uprf (buffer, "Subject:")
826 && !uprf (buffer, "Encrypted:")
827 && !uprf (buffer, "Message-ID:")) {
842 advise (ct->c_storage, "error writing to");
852 * Check if folder exists, and create
857 check_folder (char *folder)
862 /* expand path to the folder */
863 folderdir = m_mailpath (folder);
865 /* Check if folder exists */
866 if (stat (folderdir, &st) == NOTOK) {
870 if (errno != ENOENT) {
871 advise (folderdir, "error on folder");
875 ep = concat ("Create folder \"", folderdir, "\"? ", NULL);
876 answer = getanswer (ep);
882 if (!makedir (folderdir)) {
883 advise (NULL, "unable to create folder %s", folderdir);
893 * Add a file to a folder.
895 * Return the new message number of the file
896 * when added to the folder. Return -1, if
901 output_content_folder (char *folder, char *filename)
906 /* Read the folder. */
907 if ((mp = folder_read (folder))) {
908 /* Link file into folder */
909 msgnum = folder_addmsg (&mp, filename, 0, 0, 0);
911 advise (NULL, "unable to read folder %s", folder);
915 /* free folder structure */
919 * Return msgnum. We are relying on the fact that
920 * msgnum will be -1, if folder_addmsg() had an error.
927 * Parse and expand the storage formatting string
928 * pointed to by "cp" into "buffer".
932 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
936 CI ci = &ct->c_ctinfo;
939 * If storage string is "-", just copy it, and
940 * return (send content to standard output).
942 if (cp[0] == '-' && cp[1] == '\0') {
943 strncpy (buffer, cp, buflen);
951 * If formatting string is a pathname that doesn't
952 * begin with '/', then preface the path with the
953 * appropriate directory.
955 if (*cp != '/' && *cp != '|' && *cp != '!') {
956 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
964 /* We are processing a storage escape */
969 * Insert parameters from Content-Type.
970 * This is only valid for '|' commands.
972 if (buffer[0] != '|' && buffer[0] != '!') {
981 for (ap = ci->ci_attrs, ep = ci->ci_values;
983 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
993 /* insert message number */
994 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
998 /* insert part number with leading dot */
1000 snprintf (bp, buflen, ".%s", ct->c_partno);
1004 /* insert part number withouth leading dot */
1006 strncpy (bp, ct->c_partno, buflen);
1010 /* insert content type */
1011 strncpy (bp, ci->ci_type, buflen);
1015 /* insert content subtype */
1016 strncpy (bp, ci->ci_subtype, buflen);
1020 /* insert the character % */
1030 /* Advance bp and decrement buflen */
1048 * Check if the content specifies a filename
1049 * in its MIME parameters.
1053 get_storeproc (CT ct)
1055 char **ap, **ep, *cp;
1056 CI ci = &ct->c_ctinfo;
1059 * If the storeproc has already been defined,
1060 * we just return (for instance, if this content
1061 * is part of a "message/external".
1063 if (ct->c_storeproc)
1067 * Check the attribute/value pairs, for the attribute "name".
1068 * If found, do a few sanity checks and copy the value into
1071 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1072 if (!strcasecmp (*ap, "name")
1073 && *(cp = *ep) != '/'
1077 && !strchr (cp, '%')) {
1078 ct->c_storeproc = add (cp, NULL);
1086 * Copy some of the header fields of the initial message/partial
1087 * message into the header of the reassembled message.
1091 copy_some_headers (FILE *out, CT ct)
1095 hp = ct->c_first_hf; /* start at first header field */
1099 * A few of the header fields of the enclosing
1100 * messages are not copied.
1102 if (!uprf (hp->name, XXX_FIELD_PRF)
1103 && strcasecmp (hp->name, VRSN_FIELD)
1104 && strcasecmp (hp->name, "Subject")
1105 && strcasecmp (hp->name, "Encrypted")
1106 && strcasecmp (hp->name, "Message-ID"))
1107 fprintf (out, "%s:%s", hp->name, hp->value);
1108 hp = hp->next; /* next header field */