2 ** mhstore.c -- 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>
17 #include <h/mhparse.h>
20 static struct swit switches[] = {
25 #define FILESW 2 /* interface from show */
30 { "type content", 0 },
42 extern char *tmp; /* directory to place temp files */
47 extern char *parts[NPARTS + 1];
48 extern char *types[NTYPES + 1];
53 #define quitser pipeser
56 CT parse_mime(char *);
61 void set_endian(void);
62 void flush_errors(void);
65 void free_content(CT);
66 extern CT *cts; /* The list of top-level contents to display */
67 void freects_done(int) NORETURN;
72 static void pipeser(int);
77 ** Cache of current directory. This must be
78 ** set before these routines are called.
83 ** The directory in which to store the contents.
88 ** Type for a compare function for qsort. This keeps
89 ** the compiler happy.
91 typedef int (*qsort_comp) (const void *, const void *);
97 int make_intermediates(char *);
98 void flush_errors(void);
101 int show_content_aux(CT, int, char *, char *);
106 static void store_single_message(CT);
107 static int store_switch(CT);
108 static int store_generic(CT);
109 static int store_multi(CT);
110 static int store_partial(CT);
111 static int store_external(CT);
112 static int ct_compar(CT *, CT *);
113 static int store_content(CT, CT);
114 static int output_content_file(CT, int);
115 static int output_content_folder(char *, char *);
116 static int parse_format_string(CT, char *, char *, int, char *);
117 static void get_storeproc(CT);
118 static int copy_some_headers(FILE *, CT);
119 static void store_all_messages(CT *);
123 main(int argc, char **argv)
126 char *cp, *file = NULL, *folder = NULL;
127 char *maildir, buf[100], **argp;
129 struct msgs_array msgs = { 0, 0, NULL };
130 struct msgs *mp = NULL;
137 setlocale(LC_ALL, "");
139 invo_name = mhbasename(argv[0]);
141 /* read user profile/context */
144 arguments = getarguments(invo_name, argc, argv, 1);
150 while ((cp = *argp++)) {
152 switch (smatch(++cp, switches)) {
154 ambigsw(cp, switches);
157 adios(NULL, "-%s unknown", cp);
160 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
161 print_help(buf, switches, 1);
164 print_version(invo_name);
175 if (!(cp = *argp++) || *cp == '-')
176 adios(NULL, "missing argument to %s",
179 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
184 if (!(cp = *argp++) || *cp == '-')
185 adios(NULL, "missing argument to %s",
188 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
193 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
194 adios(NULL, "missing argument to %s",
196 file = *cp == '-' ? cp : getcpy(expanddir(cp));
204 if (*cp == '+' || *cp == '@') {
206 adios(NULL, "only one folder at a time!");
208 folder = getcpy(expandfol(cp));
210 app_msgarg(&msgs, cp);
213 /* null terminate the list of acceptable parts/types */
220 ** Check if we've specified an additional profile
222 if ((cp = getenv("MHSTORE"))) {
223 if ((fp = fopen(cp, "r"))) {
224 readconfig((struct node **) 0, fp, cp, 0);
227 admonish("", "unable to read $MHSTORE profile (%s)",
233 ** Read the standard profile setup
235 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
236 readconfig((struct node **) 0, fp, cp, 0);
241 ** Cache the current directory before we do any chdirs()'s.
246 ** Check for storage directory. If specified,
247 ** then store temporary files there. Else we
248 ** store them in standard nmh directory.
250 if ((cp = context_find(nmhstorage)) && *cp)
251 tmp = concat(cp, "/", invo_name, NULL);
253 tmp = getcpy(toabsdir(invo_name));
255 if (file && msgs.size)
256 adios(NULL, "cannot specify msg and file at same time!");
259 ** check if message is coming from file
262 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
263 adios(NULL, "out of memory");
266 if ((ct = parse_mime(file)))
270 ** message(s) are coming from a folder
273 app_msgarg(&msgs, seq_cur);
275 folder = getcurfol();
276 maildir = toabsdir(folder);
278 if (chdir(maildir) == NOTOK)
279 adios(maildir, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp = folder_read(folder)))
283 adios(NULL, "unable to read folder %s", folder);
285 /* check for empty folder */
287 adios(NULL, "no messages in %s", folder);
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum = 0; msgnum < msgs.size; msgnum++)
291 if (!m_convert(mp, msgs.msgs[msgnum]))
293 seq_setprev(mp); /* set the previous-sequence */
295 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
297 adios(NULL, "out of memory");
300 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
301 if (is_selected(mp, msgnum)) {
304 msgnam = m_name(msgnum);
305 if ((ct = parse_mime(msgnam)))
315 SIGNAL(SIGQUIT, quitser);
316 SIGNAL(SIGPIPE, pipeser);
319 ** Get the associated umask for the relevant contents.
321 for (ctp = cts; *ctp; ctp++) {
325 if (type_ok(ct, 1) && !ct->c_umask) {
326 if (stat(ct->c_file, &st) != NOTOK)
327 ct->c_umask = ~(st.st_mode & 0777);
329 ct->c_umask = ~m_gmprot();
334 ** Store the message content
336 store_all_messages(cts);
338 /* Now free all the structures for the content */
339 for (ctp = cts; *ctp; ctp++)
345 /* If reading from a folder, do some updating */
347 context_replace(curfolder, folder); /* update current folder */
348 seq_setcur(mp, mp->hghsel); /* update current message */
349 seq_save(mp); /* synchronize sequences */
350 context_save(); /* save the context file */
364 fprintf(stderr, "\n");
374 ** Main entry point to store content from a collection of messages.
377 store_all_messages(CT *cts)
383 ** Check for the directory in which to
384 ** store any contents.
388 if ((cp = context_find(nmhstorage)) && *cp)
393 for (ctp = cts; *ctp; ctp++) {
395 store_single_message(ct);
403 ** Entry point to store the content
404 ** in a (single) message
408 store_single_message(CT ct)
410 if (type_ok(ct, 1)) {
417 if (ct->c_ceclosefnx)
418 (*ct->c_ceclosefnx) (ct);
424 ** Switching routine to store different content types
430 switch (ct->c_type) {
432 return store_multi(ct);
436 switch (ct->c_subtype) {
437 case MESSAGE_PARTIAL:
438 return store_partial(ct);
441 case MESSAGE_EXTERNAL:
442 return store_external(ct);
446 return store_generic(ct);
456 return store_generic(ct);
460 adios(NULL, "unknown content type %d", ct->c_type);
464 return OK; /* NOT REACHED */
469 ** Generic routine to store a MIME content.
470 ** (application, audio, video, image, text, message/rfc922)
477 ** Check if the content specifies a filename.
478 ** Don't bother with this for type "message"
479 ** (only "message/rfc822" will use store_generic).
481 if (autosw && ct->c_type != CT_MESSAGE)
484 return store_content(ct, NULL);
489 ** Store the content of a multipart message
496 struct multipart *m = (struct multipart *) ct->c_ctparams;
500 for (part = m->mp_parts; part; part = part->mp_next) {
501 CT p = part->mp_part;
503 if (part_ok(p, 1) && type_ok(p, 1)) {
504 result = store_switch(p);
505 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
515 ** Reassemble and store the contents of a collection
516 ** of messages of type "message/partial".
525 struct partial *pm, *qm;
527 qm = (struct partial *) ct->c_ctparams;
532 for (ctp = cts; *ctp; ctp++) {
534 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
535 pm = (struct partial *) p->c_ctparams;
536 if (!pm->pm_stored &&
537 strcmp(qm->pm_partid, pm->pm_partid)
539 pm->pm_marked = pm->pm_partno;
550 advise(NULL, "missing (at least) last part of multipart message");
554 if ((base = (CT *) calloc((size_t) (i + 1), sizeof(*base))) == NULL)
555 adios(NULL, "out of memory");
558 for (ctp = cts; *ctp; ctp++) {
560 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
561 pm = (struct partial *) p->c_ctparams;
569 qsort((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
572 for (ctq = base; *ctq; ctq++) {
574 pm = (struct partial *) p->c_ctparams;
575 if (pm->pm_marked != cur) {
576 if (pm->pm_marked == cur - 1) {
577 admonish(NULL, "duplicate part %d of %d part multipart message", pm->pm_marked, hi);
582 advise (NULL, "missing %spart %d of %d part multipart message", cur != hi ? "(at least) " : "", cur, hi);
593 ** Now cycle through the sorted list of messages of type
594 ** "message/partial" and save/append them to a file.
599 if (store_content(ct, NULL) == NOTOK) {
605 for (; *ctq; ctq++) {
607 if (store_content(p, ct) == NOTOK)
617 ** Store content from a message of type "message/external".
621 store_external(CT ct)
624 struct exbody *e = (struct exbody *) ct->c_ctparams;
625 CT p = e->eb_content;
631 ** Check if the parameters for the external body
632 ** specified a filename.
637 if ((cp = e->eb_name) && *cp != '/' && *cp != '.' &&
638 *cp != '|' && *cp != '!' && !strchr(cp, '%')) {
639 if (!ct->c_storeproc)
640 ct->c_storeproc = getcpy(cp);
642 p->c_storeproc = getcpy(cp);
647 ** Since we will let the Content structure for the
648 ** external body substitute for the current content,
649 ** we temporarily change its partno (number inside
650 ** multipart), so everything looks right.
652 p->c_partno = ct->c_partno;
654 /* we probably need to check if content is really there */
655 result = store_switch(p);
663 ** Compare the numbering from two different
664 ** message/partials (needed for sorting).
668 ct_compar(CT *a, CT *b)
670 struct partial *am = (struct partial *) ((*a)->c_ctparams);
671 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
673 return (am->pm_marked - bm->pm_marked);
678 ** Store contents of a message or message part to
679 ** a folder, a file, the standard output, or pass
680 ** the contents to a command.
682 ** If the current content to be saved is a followup part
683 ** to a collection of messages of type "message/partial",
684 ** then field "p" is a pointer to the Content structure
685 ** to the first message/partial in the group.
689 store_content(CT ct, CT p)
691 int appending = 0, msgnum = 0;
692 int is_partial = 0, first_partial = 0;
693 int last_partial = 0;
694 char *cp, buffer[BUFSIZ];
697 ** Do special processing for messages of
698 ** type "message/partial".
700 ** We first check if this content is of type
701 ** "message/partial". If it is, then we need to check
702 ** whether it is the first and/or last in the group.
704 ** Then if "p" is a valid pointer, it points to the Content
705 ** structure of the first partial in the group. So we copy
706 ** the file name and/or folder name from that message. In
707 ** this case, we also note that we will be appending.
709 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
710 struct partial *pm = (struct partial *) ct->c_ctparams;
712 /* Yep, it's a message/partial */
715 /* But is it the first and/or last in the collection? */
716 if (pm->pm_partno == 1)
718 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
722 ** If "p" is a valid pointer, then it points to the
723 ** Content structure for the first message in the group.
724 ** So we just copy the filename or foldername information
725 ** from the previous iteration of this function.
729 ct->c_storage = getcpy(p->c_storage);
731 /* record the folder name */
733 ct->c_folder = getcpy(p->c_folder);
740 ** Get storage formatting string.
742 ** 1) If we have storeproc defined, then use that
743 ** 2) Else check for a mhstore-store-<type>/<subtype> entry
744 ** 3) Else check for a mhstore-store-<type> entry
745 ** 4) Else if content is "message", use "+" (current folder)
746 ** 5) Else use string "%m%P.%s".
748 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
749 CI ci = &ct->c_ctinfo;
751 snprintf(buffer, sizeof(buffer), "%s-store-%s/%s",
752 invo_name, ci->ci_type, ci->ci_subtype);
753 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
754 snprintf(buffer, sizeof(buffer), "%s-store-%s",
755 invo_name, ci->ci_type);
756 if ((cp = context_find(buffer)) == NULL ||
758 cp = ct->c_type == CT_MESSAGE ?
765 ** Check the beginning of storage formatting string
766 ** to see if we are saving content to a folder.
768 if (*cp == '+' || *cp == '@') {
769 char *tmpfilenam, *folder;
771 /* Store content in temporary file for now */
772 tmpfilenam = m_mktemp(invo_name, NULL, NULL);
773 ct->c_storage = getcpy(tmpfilenam);
775 /* Get the folder name */
777 folder = getcpy(expandfol(cp));
779 folder = getcurfol();
781 /* Check if folder exists */
782 create_folder(toabsdir(folder), 0, exit);
784 /* Record the folder name */
785 ct->c_folder = getcpy(folder);
794 ** Parse and expand the storage formatting string
795 ** in `cp' into `buffer'.
797 parse_format_string(ct, cp, buffer, sizeof(buffer), dir);
800 ** If formatting begins with '|' or '!', then pass
801 ** content to standard input of a command and return.
803 if (buffer[0] == '|' || buffer[0] == '!')
804 return show_content_aux(ct, 0, buffer + 1, dir);
806 /* record the filename */
807 ct->c_storage = getcpy(buffer);
810 /* flush the output stream */
813 /* Now save or append the content to a file */
814 if (output_content_file(ct, appending) == NOTOK)
818 ** If necessary, link the file into a folder and remove
819 ** the temporary file. If this message is a partial,
820 ** then only do this if it is the last one in the group.
822 if (ct->c_folder && (!is_partial || last_partial)) {
823 msgnum = output_content_folder(ct->c_folder, ct->c_storage);
824 unlink(ct->c_storage);
830 ** Now print out the name/number of the message
831 ** that we are storing.
835 fprintf(stderr, "reassembling partials ");
837 fprintf(stderr, "%s", ct->c_file);
839 fprintf(stderr, "%s,", ct->c_file);
841 fprintf(stderr, "storing message %s", ct->c_file);
843 fprintf(stderr, " part %s", ct->c_partno);
847 ** Unless we are in the "middle" of group of message/partials,
848 ** we now print the name of the file, folder, and/or message
849 ** to which we are storing the content.
851 if (!is_partial || last_partial) {
853 fprintf(stderr, " to folder %s as message %d\n",
854 ct->c_folder, msgnum);
855 } else if (strcmp(ct->c_storage, "-")==0) {
856 fprintf(stderr, " to stdout\n");
860 cwdlen = strlen(cwd);
861 fprintf(stderr, " as file %s\n",
862 strncmp(ct->c_storage, cwd,
864 ct->c_storage[cwdlen] != '/' ?
866 ct->c_storage + cwdlen + 1);
875 ** Output content to a file
879 output_content_file(CT ct, int appending)
882 char *file, buffer[BUFSIZ];
887 ** If the pathname is absolute, make sure
888 ** all the relevant directories exist.
890 if (strchr(ct->c_storage, '/') && make_intermediates(ct->c_storage)
894 if (ct->c_encoding != CE_7BIT) {
897 if (!ct->c_ceopenfnx) {
898 advise(NULL, "don't know how to decode part %s of message %s", ct->c_partno, ct->c_file);
902 file = appending || strcmp(ct->c_storage, "-")==0 ?
903 NULL : ct->c_storage;
904 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
906 if (strcmp(file, ct->c_storage)==0) {
907 (*ct->c_ceclosefnx) (ct);
912 ** Send to standard output
914 if (strcmp(ct->c_storage, "-")==0) {
917 if ((gd = dup(fileno(stdout))) == NOTOK) {
918 advise("stdout", "unable to dup");
920 (*ct->c_ceclosefnx) (ct);
923 if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
924 advise("stdout", "unable to fdopen (%d, \"%s\") from", gd, appending ? "a" : "w");
932 if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
934 advise(ct->c_storage, "unable to fopen for %s",
936 "appending" : "writing");
942 ** Filter the header fields of the initial enclosing
943 ** message/partial into the file.
945 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
946 struct partial *pm = (struct partial *) ct->c_ctparams;
948 if (pm->pm_partno == 1)
949 copy_some_headers(fp, ct);
953 switch (cc = read(fd, buffer, sizeof(buffer))) {
955 advise(file, "error reading content from");
962 fwrite(buffer, sizeof(*buffer), cc, fp);
968 (*ct->c_ceclosefnx) (ct);
970 if (cc != NOTOK && fflush(fp))
971 advise(ct->c_storage, "error writing to");
975 return (cc != NOTOK ? OK : NOTOK);
978 if (!ct->c_fp && (ct->c_fp = fopen(ct->c_file, "r")) == NULL) {
979 advise(ct->c_file, "unable to open for reading");
985 fseek(ct->c_fp, pos, SEEK_SET);
987 if (strcmp(ct->c_storage, "-")==0) {
990 if ((gd = dup(fileno(stdout))) == NOTOK) {
991 advise("stdout", "unable to dup");
994 if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
995 advise("stdout", "unable to fdopen (%d, \"%s\") from",
996 gd, appending ? "a" : "w");
1001 if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
1003 advise(ct->c_storage, "unable to fopen for %s",
1004 appending ? "appending" : "writing");
1010 ** Copy a few of the header fields of the initial
1011 ** enclosing message/partial into the file.
1014 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
1015 struct partial *pm = (struct partial *) ct->c_ctparams;
1017 if (pm->pm_partno == 1) {
1018 copy_some_headers(fp, ct);
1023 while (fgets(buffer, sizeof(buffer) - 1, ct->c_fp)) {
1024 if ((pos += strlen(buffer)) > last) {
1027 diff = strlen(buffer) - (pos - last);
1029 buffer[diff] = '\0';
1032 ** If this is the first content of a group of
1033 ** message/partial contents, then we only copy a few
1034 ** of the header fields of the enclosed message.
1037 switch (buffer[0]) {
1040 if (filterstate < 0)
1049 if (!uprf(buffer, XXX_FIELD_PRF) && !uprf(buffer, VRSN_FIELD) && !uprf(buffer, "Subject:") && !uprf(buffer, "Message-ID:")) {
1064 advise(ct->c_storage, "error writing to");
1074 ** Add a file to a folder.
1076 ** Return the new message number of the file
1077 ** when added to the folder. Return -1, if
1078 ** there is an error.
1082 output_content_folder(char *folder, char *filename)
1087 /* Read the folder. */
1088 if ((mp = folder_read(folder))) {
1089 /* Link file into folder */
1090 msgnum = folder_addmsg(&mp, filename, 0, 0, 0, 0, NULL);
1092 advise(NULL, "unable to read folder %s", folder);
1096 /* free folder structure */
1100 ** Return msgnum. We are relying on the fact that
1101 ** msgnum will be -1, if folder_addmsg() had an error.
1108 ** Parse and expand the storage formatting string
1109 ** pointed to by "cp" into "buffer".
1113 parse_format_string(CT ct, char *cp, char *buffer, int buflen, char *dir)
1117 CI ci = &ct->c_ctinfo;
1120 ** If storage string is "-", just copy it, and
1121 ** return (send content to standard output).
1123 if (cp[0] == '-' && cp[1] == '\0') {
1124 strncpy(buffer, cp, buflen);
1132 ** If formatting string is a pathname that doesn't
1133 ** begin with '/', then preface the path with the
1134 ** appropriate directory.
1136 if (*cp != '/' && *cp != '|' && *cp != '!') {
1137 snprintf(bp, buflen, "%s/", dir[1] ? dir : "");
1145 /* We are processing a storage escape */
1150 ** Insert parameters from Content-Type.
1151 ** This is only valid for '|' commands.
1153 if (buffer[0] != '|' && buffer[0] != '!') {
1162 for (ap=ci->ci_attrs, ep=ci->ci_values;
1164 snprintf(bp, buflen,
1176 /* insert message number */
1177 snprintf(bp, buflen, "%s",
1178 mhbasename(ct->c_file));
1182 /* insert part number with leading dot */
1184 snprintf(bp, buflen, ".%s",
1189 /* insert part number withouth leading dot */
1191 strncpy(bp, ct->c_partno, buflen);
1195 /* insert content type */
1196 strncpy(bp, ci->ci_type, buflen);
1200 /* insert content subtype */
1201 strncpy(bp, ci->ci_subtype, buflen);
1205 /* insert the character % */
1215 /* Advance bp and decrement buflen */
1233 ** Check if the content specifies a filename
1234 ** in its MIME parameters.
1238 get_storeproc(CT ct)
1240 char **ap, **ep, *cp;
1241 CI ci = &ct->c_ctinfo;
1244 ** If the storeproc has already been defined,
1245 ** we just return (for instance, if this content
1246 ** is part of a "message/external".
1248 if (ct->c_storeproc)
1252 ** Check the attribute/value pairs, for the attribute "name".
1253 ** If found, do a few sanity checks and copy the value into
1256 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1257 if (!mh_strcasecmp(*ap, "name") && *(cp = *ep) != '/' &&
1258 *cp != '.' && *cp != '|' && *cp != '!' &&
1260 ct->c_storeproc = getcpy(cp);
1268 ** Copy some of the header fields of the initial message/partial
1269 ** message into the header of the reassembled message.
1273 copy_some_headers(FILE *out, CT ct)
1277 hp = ct->c_first_hf; /* start at first header field */
1281 ** A few of the header fields of the enclosing
1282 ** messages are not copied.
1284 if (!uprf(hp->name, XXX_FIELD_PRF) &&
1285 mh_strcasecmp(hp->name, VRSN_FIELD) &&
1286 mh_strcasecmp(hp->name, "Subject") &&
1287 mh_strcasecmp(hp->name, "Message-ID"))
1288 fprintf(out, "%s:%s", hp->name, hp->value);
1289 hp = hp->next; /* next header field */