]>
diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #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
45 typedef int (*qsort_comp
) (const void *, const void *);
49 int part_ok (CT
, int);
50 int type_ok (CT
, int);
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
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, 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") || !mh_strcasecmp (*ap
, "x-conversions"))
235 && (!mh_strcasecmp (*ep
, "compress") || !mh_strcasecmp (*ep
, "x-compress"))) {
239 /* check for "conversions=gzip" attribute */
240 if ((!mh_strcasecmp (*ap
, "conversions") || !mh_strcasecmp (*ap
, "x-conversions"))
241 && (!mh_strcasecmp (*ep
, "gzip") || !mh_strcasecmp (*ep
, "x-gzip"))) {
248 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
249 : (gzP
? "%egzip -dc | tar tvf -"
250 : "%etar tvf -"), NULL
);
251 if (!ct
->c_storeproc
) {
253 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
254 : (gzP
? "| gzip -dc | tar xvpf -"
255 : "| tar xvpf -"), NULL
);
258 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
259 : (gzP
? "%m%P.tar.gz"
260 : "%m%P.tar"), NULL
);
266 return store_content (ct
, NULL
);
271 * Store the content of a multipart message
278 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
282 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
283 CT p
= part
->mp_part
;
285 if (part_ok (p
, 1) && type_ok (p
, 1)) {
286 result
= store_switch (p
);
287 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
297 * Reassemble and store the contents of a collection
298 * of messages of type "message/partial".
302 store_partial (CT ct
)
307 struct partial
*pm
, *qm
;
309 qm
= (struct partial
*) ct
->c_ctparams
;
314 for (ctp
= cts
; *ctp
; ctp
++) {
316 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
317 pm
= (struct partial
*) p
->c_ctparams
;
319 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
320 pm
->pm_marked
= pm
->pm_partno
;
332 advise (NULL
, "missing (at least) last part of multipart message");
336 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
337 adios (NULL
, "out of memory");
340 for (ctp
= cts
; *ctp
; ctp
++) {
342 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
343 pm
= (struct partial
*) p
->c_ctparams
;
351 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
354 for (ctq
= base
; *ctq
; ctq
++) {
356 pm
= (struct partial
*) p
->c_ctparams
;
357 if (pm
->pm_marked
!= cur
) {
358 if (pm
->pm_marked
== cur
- 1) {
360 "duplicate part %d of %d part multipart message",
367 "missing %spart %d of %d part multipart message",
368 cur
!= hi
? "(at least) " : "", cur
, hi
);
380 * Now cycle through the sorted list of messages of type
381 * "message/partial" and save/append them to a file.
386 if (store_content (ct
, NULL
) == NOTOK
) {
388 free ((char *) base
);
392 for (; *ctq
; ctq
++) {
394 if (store_content (p
, ct
) == NOTOK
)
398 free ((char *) base
);
404 * Store content from a message of type "message/external".
408 store_external (CT ct
)
411 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
412 CT p
= e
->eb_content
;
418 * Check if the parameters for the external body
419 * specified a filename.
424 if ((cp
= e
->eb_name
)
429 && !strchr (cp
, '%')) {
430 if (!ct
->c_storeproc
)
431 ct
->c_storeproc
= add (cp
, NULL
);
433 p
->c_storeproc
= add (cp
, NULL
);
438 * Since we will let the Content structure for the
439 * external body substitute for the current content,
440 * we temporarily change its partno (number inside
441 * multipart), so everything looks right.
443 p
->c_partno
= ct
->c_partno
;
445 /* we probably need to check if content is really there */
446 result
= store_switch (p
);
454 * Compare the numbering from two different
455 * message/partials (needed for sorting).
459 ct_compar (CT
*a
, CT
*b
)
461 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
462 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
464 return (am
->pm_marked
- bm
->pm_marked
);
469 * Store contents of a message or message part to
470 * a folder, a file, the standard output, or pass
471 * the contents to a command.
473 * If the current content to be saved is a followup part
474 * to a collection of messages of type "message/partial",
475 * then field "p" is a pointer to the Content structure
476 * to the first message/partial in the group.
480 store_content (CT ct
, CT p
)
482 int appending
= 0, msgnum
= 0;
483 int is_partial
= 0, first_partial
= 0;
484 int last_partial
= 0;
485 char *cp
, buffer
[BUFSIZ
];
488 * Do special processing for messages of
489 * type "message/partial".
491 * We first check if this content is of type
492 * "message/partial". If it is, then we need to check
493 * whether it is the first and/or last in the group.
495 * Then if "p" is a valid pointer, it points to the Content
496 * structure of the first partial in the group. So we copy
497 * the file name and/or folder name from that message. In
498 * this case, we also note that we will be appending.
500 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
501 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
503 /* Yep, it's a message/partial */
506 /* But is it the first and/or last in the collection? */
507 if (pm
->pm_partno
== 1)
509 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
513 * If "p" is a valid pointer, then it points to the
514 * Content structure for the first message in the group.
515 * So we just copy the filename or foldername information
516 * from the previous iteration of this function.
520 ct
->c_storage
= add (p
->c_storage
, NULL
);
522 /* record the folder name */
524 ct
->c_folder
= add (p
->c_folder
, NULL
);
531 * Get storage formatting string.
533 * 1) If we have storeproc defined, then use that
534 * 2) Else check for a mhn-store-<type>/<subtype> entry
535 * 3) Else check for a mhn-store-<type> entry
536 * 4) Else if content is "message", use "+" (current folder)
537 * 5) Else use string "%m%P.%s".
539 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
540 CI ci
= &ct
->c_ctinfo
;
542 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
543 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
544 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
545 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
546 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
547 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
553 * Check the beginning of storage formatting string
554 * to see if we are saving content to a folder.
556 if (*cp
== '+' || *cp
== '@') {
557 char *tmpfilenam
, *folder
;
559 /* Store content in temporary file for now */
560 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
561 ct
->c_storage
= add (tmpfilenam
, NULL
);
563 /* Get the folder name */
565 folder
= pluspath (cp
);
567 folder
= getfolder (1);
569 /* Check if folder exists */
570 create_folder(m_mailpath(folder
), 0, exit
);
572 /* Record the folder name */
573 ct
->c_folder
= add (folder
, NULL
);
582 * Parse and expand the storage formatting string
583 * in `cp' into `buffer'.
585 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
588 * If formatting begins with '|' or '!', then pass
589 * content to standard input of a command and return.
591 if (buffer
[0] == '|' || buffer
[0] == '!')
592 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
594 /* record the filename */
595 ct
->c_storage
= add (buffer
, NULL
);
598 /* flush the output stream */
601 /* Now save or append the content to a file */
602 if (output_content_file (ct
, appending
) == NOTOK
)
606 * If necessary, link the file into a folder and remove
607 * the temporary file. If this message is a partial,
608 * then only do this if it is the last one in the group.
610 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
611 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
612 unlink (ct
->c_storage
);
618 * Now print out the name/number of the message
619 * that we are storing.
623 fprintf (stderr
, "reassembling partials ");
625 fprintf (stderr
, "%s", ct
->c_file
);
627 fprintf (stderr
, "%s,", ct
->c_file
);
629 fprintf (stderr
, "storing message %s", ct
->c_file
);
631 fprintf (stderr
, " part %s", ct
->c_partno
);
635 * Unless we are in the "middle" of group of message/partials,
636 * we now print the name of the file, folder, and/or message
637 * to which we are storing the content.
639 if (!is_partial
|| last_partial
) {
641 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
642 } else if (!strcmp(ct
->c_storage
, "-")) {
643 fprintf (stderr
, " to stdout\n");
647 cwdlen
= strlen (cwd
);
648 fprintf (stderr
, " as file %s\n",
649 strncmp (ct
->c_storage
, cwd
, cwdlen
)
650 || ct
->c_storage
[cwdlen
] != '/'
651 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
660 * Output content to a file
664 output_content_file (CT ct
, int appending
)
667 char *file
, buffer
[BUFSIZ
];
672 * If the pathname is absolute, make sure
673 * all the relevant directories exist.
675 if (strchr(ct
->c_storage
, '/')
676 && make_intermediates (ct
->c_storage
) == NOTOK
)
679 if (ct
->c_encoding
!= CE_7BIT
) {
682 if (!ct
->c_ceopenfnx
) {
683 advise (NULL
, "don't know how to decode part %s of message %s",
684 ct
->c_partno
, ct
->c_file
);
688 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
690 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
692 if (!strcmp (file
, ct
->c_storage
)) {
693 (*ct
->c_ceclosefnx
) (ct
);
698 * Send to standard output
700 if (!strcmp (ct
->c_storage
, "-")) {
703 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
704 advise ("stdout", "unable to dup");
706 (*ct
->c_ceclosefnx
) (ct
);
709 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
710 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
711 appending
? "a" : "w");
719 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
720 advise (ct
->c_storage
, "unable to fopen for %s",
721 appending
? "appending" : "writing");
727 * Filter the header fields of the initial enclosing
728 * message/partial into the file.
730 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
731 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
733 if (pm
->pm_partno
== 1)
734 copy_some_headers (fp
, ct
);
738 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
740 advise (file
, "error reading content from");
747 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
753 (*ct
->c_ceclosefnx
) (ct
);
755 if (cc
!= NOTOK
&& fflush (fp
))
756 advise (ct
->c_storage
, "error writing to");
760 return (cc
!= NOTOK
? OK
: NOTOK
);
763 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
764 advise (ct
->c_file
, "unable to open for reading");
770 fseek (ct
->c_fp
, pos
, SEEK_SET
);
772 if (!strcmp (ct
->c_storage
, "-")) {
775 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
776 advise ("stdout", "unable to dup");
779 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
780 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
781 appending
? "a" : "w");
786 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
787 advise (ct
->c_storage
, "unable to fopen for %s",
788 appending
? "appending" : "writing");
794 * Copy a few of the header fields of the initial
795 * enclosing message/partial into the file.
798 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
799 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
801 if (pm
->pm_partno
== 1) {
802 copy_some_headers (fp
, ct
);
807 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
808 if ((pos
+= strlen (buffer
)) > last
) {
811 diff
= strlen (buffer
) - (pos
- last
);
816 * If this is the first content of a group of
817 * message/partial contents, then we only copy a few
818 * of the header fields of the enclosed message.
833 if (!uprf (buffer
, XXX_FIELD_PRF
)
834 && !uprf (buffer
, VRSN_FIELD
)
835 && !uprf (buffer
, "Subject:")
836 && !uprf (buffer
, "Encrypted:")
837 && !uprf (buffer
, "Message-ID:")) {
852 advise (ct
->c_storage
, "error writing to");
862 * Add a file to a folder.
864 * Return the new message number of the file
865 * when added to the folder. Return -1, if
870 output_content_folder (char *folder
, char *filename
)
875 /* Read the folder. */
876 if ((mp
= folder_read (folder
))) {
877 /* Link file into folder */
878 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
880 advise (NULL
, "unable to read folder %s", folder
);
884 /* free folder structure */
888 * Return msgnum. We are relying on the fact that
889 * msgnum will be -1, if folder_addmsg() had an error.
896 * Parse and expand the storage formatting string
897 * pointed to by "cp" into "buffer".
901 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
905 CI ci
= &ct
->c_ctinfo
;
908 * If storage string is "-", just copy it, and
909 * return (send content to standard output).
911 if (cp
[0] == '-' && cp
[1] == '\0') {
912 strncpy (buffer
, cp
, buflen
);
920 * If formatting string is a pathname that doesn't
921 * begin with '/', then preface the path with the
922 * appropriate directory.
924 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
925 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
933 /* We are processing a storage escape */
938 * Insert parameters from Content-Type.
939 * This is only valid for '|' commands.
941 if (buffer
[0] != '|' && buffer
[0] != '!') {
950 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
952 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
962 /* insert message number */
963 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
967 /* insert part number with leading dot */
969 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
973 /* insert part number withouth leading dot */
975 strncpy (bp
, ct
->c_partno
, buflen
);
979 /* insert content type */
980 strncpy (bp
, ci
->ci_type
, buflen
);
984 /* insert content subtype */
985 strncpy (bp
, ci
->ci_subtype
, buflen
);
989 /* insert the character % */
999 /* Advance bp and decrement buflen */
1017 * Check if the content specifies a filename
1018 * in its MIME parameters.
1022 get_storeproc (CT ct
)
1024 char **ap
, **ep
, *cp
;
1025 CI ci
= &ct
->c_ctinfo
;
1028 * If the storeproc has already been defined,
1029 * we just return (for instance, if this content
1030 * is part of a "message/external".
1032 if (ct
->c_storeproc
)
1036 * Check the attribute/value pairs, for the attribute "name".
1037 * If found, do a few sanity checks and copy the value into
1040 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1041 if (!mh_strcasecmp (*ap
, "name")
1042 && *(cp
= *ep
) != '/'
1046 && !strchr (cp
, '%')) {
1047 ct
->c_storeproc
= add (cp
, NULL
);
1055 * Copy some of the header fields of the initial message/partial
1056 * message into the header of the reassembled message.
1060 copy_some_headers (FILE *out
, CT ct
)
1064 hp
= ct
->c_first_hf
; /* start at first header field */
1068 * A few of the header fields of the enclosing
1069 * messages are not copied.
1071 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1072 && mh_strcasecmp (hp
->name
, VRSN_FIELD
)
1073 && mh_strcasecmp (hp
->name
, "Subject")
1074 && mh_strcasecmp (hp
->name
, "Encrypted")
1075 && mh_strcasecmp (hp
->name
, "Message-ID"))
1076 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1077 hp
= hp
->next
; /* next header field */