]>
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 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
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)
186 store_generic (CT ct
)
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") || !mh_strcasecmp (*ap
, "x-conversions"))
234 && (!mh_strcasecmp (*ep
, "compress") || !mh_strcasecmp (*ep
, "x-compress"))) {
238 /* check for "conversions=gzip" attribute */
239 if ((!mh_strcasecmp (*ap
, "conversions") || !mh_strcasecmp (*ap
, "x-conversions"))
240 && (!mh_strcasecmp (*ep
, "gzip") || !mh_strcasecmp (*ep
, "x-gzip"))) {
247 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
248 : (gzP
? "%egzip -dc | tar tvf -"
249 : "%etar tvf -"), NULL
);
250 if (!ct
->c_storeproc
) {
252 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
253 : (gzP
? "| gzip -dc | tar xvpf -"
254 : "| tar xvpf -"), NULL
);
257 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
258 : (gzP
? "%m%P.tar.gz"
259 : "%m%P.tar"), NULL
);
265 return store_content (ct
, NULL
);
270 * Store the content of a multipart message
277 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
281 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
282 CT p
= part
->mp_part
;
284 if (part_ok (p
, 1) && type_ok (p
, 1)) {
285 result
= store_switch (p
);
286 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
296 * Reassemble and store the contents of a collection
297 * of messages of type "message/partial".
301 store_partial (CT ct
)
306 struct partial
*pm
, *qm
;
308 qm
= (struct partial
*) ct
->c_ctparams
;
313 for (ctp
= cts
; *ctp
; ctp
++) {
315 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
316 pm
= (struct partial
*) p
->c_ctparams
;
318 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
319 pm
->pm_marked
= pm
->pm_partno
;
331 advise (NULL
, "missing (at least) last part of multipart message");
335 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
336 adios (NULL
, "out of memory");
339 for (ctp
= cts
; *ctp
; ctp
++) {
341 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
342 pm
= (struct partial
*) p
->c_ctparams
;
350 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
353 for (ctq
= base
; *ctq
; ctq
++) {
355 pm
= (struct partial
*) p
->c_ctparams
;
356 if (pm
->pm_marked
!= cur
) {
357 if (pm
->pm_marked
== cur
- 1) {
359 "duplicate part %d of %d part multipart message",
366 "missing %spart %d of %d part multipart message",
367 cur
!= hi
? "(at least) " : "", cur
, hi
);
379 * Now cycle through the sorted list of messages of type
380 * "message/partial" and save/append them to a file.
385 if (store_content (ct
, NULL
) == NOTOK
) {
387 free ((char *) base
);
391 for (; *ctq
; ctq
++) {
393 if (store_content (p
, ct
) == NOTOK
)
397 free ((char *) base
);
403 * Store content from a message of type "message/external".
407 store_external (CT ct
)
410 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
411 CT p
= e
->eb_content
;
417 * Check if the parameters for the external body
418 * specified a filename.
423 if ((cp
= e
->eb_name
)
428 && !strchr (cp
, '%')) {
429 if (!ct
->c_storeproc
)
430 ct
->c_storeproc
= add (cp
, NULL
);
432 p
->c_storeproc
= add (cp
, NULL
);
437 * Since we will let the Content structure for the
438 * external body substitute for the current content,
439 * we temporarily change its partno (number inside
440 * multipart), so everything looks right.
442 p
->c_partno
= ct
->c_partno
;
444 /* we probably need to check if content is really there */
445 result
= store_switch (p
);
453 * Compare the numbering from two different
454 * message/partials (needed for sorting).
458 ct_compar (CT
*a
, CT
*b
)
460 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
461 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
463 return (am
->pm_marked
- bm
->pm_marked
);
468 * Store contents of a message or message part to
469 * a folder, a file, the standard output, or pass
470 * the contents to a command.
472 * If the current content to be saved is a followup part
473 * to a collection of messages of type "message/partial",
474 * then field "p" is a pointer to the Content structure
475 * to the first message/partial in the group.
479 store_content (CT ct
, CT p
)
481 int appending
= 0, msgnum
= 0;
482 int is_partial
= 0, first_partial
= 0;
483 int last_partial
= 0;
484 char *cp
, buffer
[BUFSIZ
];
487 * Do special processing for messages of
488 * type "message/partial".
490 * We first check if this content is of type
491 * "message/partial". If it is, then we need to check
492 * whether it is the first and/or last in the group.
494 * Then if "p" is a valid pointer, it points to the Content
495 * structure of the first partial in the group. So we copy
496 * the file name and/or folder name from that message. In
497 * this case, we also note that we will be appending.
499 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
500 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
502 /* Yep, it's a message/partial */
505 /* But is it the first and/or last in the collection? */
506 if (pm
->pm_partno
== 1)
508 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
512 * If "p" is a valid pointer, then it points to the
513 * Content structure for the first message in the group.
514 * So we just copy the filename or foldername information
515 * from the previous iteration of this function.
519 ct
->c_storage
= add (p
->c_storage
, NULL
);
521 /* record the folder name */
523 ct
->c_folder
= add (p
->c_folder
, NULL
);
530 * Get storage formatting string.
532 * 1) If we have storeproc defined, then use that
533 * 2) Else check for a mhn-store-<type>/<subtype> entry
534 * 3) Else check for a mhn-store-<type> entry
535 * 4) Else if content is "message", use "+" (current folder)
536 * 5) Else use string "%m%P.%s".
538 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
539 CI ci
= &ct
->c_ctinfo
;
541 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
542 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
543 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
544 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
545 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
546 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
552 * Check the beginning of storage formatting string
553 * to see if we are saving content to a folder.
555 if (*cp
== '+' || *cp
== '@') {
556 char *tmpfilenam
, *folder
;
558 /* Store content in temporary file for now */
559 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
560 ct
->c_storage
= add (tmpfilenam
, NULL
);
562 /* Get the folder name */
564 folder
= pluspath (cp
);
566 folder
= getfolder (1);
568 /* Check if folder exists */
569 create_folder(m_mailpath(folder
), 0, exit
);
571 /* Record the folder name */
572 ct
->c_folder
= add (folder
, NULL
);
581 * Parse and expand the storage formatting string
582 * in `cp' into `buffer'.
584 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
587 * If formatting begins with '|' or '!', then pass
588 * content to standard input of a command and return.
590 if (buffer
[0] == '|' || buffer
[0] == '!')
591 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
593 /* record the filename */
594 ct
->c_storage
= add (buffer
, NULL
);
597 /* flush the output stream */
600 /* Now save or append the content to a file */
601 if (output_content_file (ct
, appending
) == NOTOK
)
605 * If necessary, link the file into a folder and remove
606 * the temporary file. If this message is a partial,
607 * then only do this if it is the last one in the group.
609 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
610 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
611 unlink (ct
->c_storage
);
617 * Now print out the name/number of the message
618 * that we are storing.
622 fprintf (stderr
, "reassembling partials ");
624 fprintf (stderr
, "%s", ct
->c_file
);
626 fprintf (stderr
, "%s,", ct
->c_file
);
628 fprintf (stderr
, "storing message %s", ct
->c_file
);
630 fprintf (stderr
, " part %s", ct
->c_partno
);
634 * Unless we are in the "middle" of group of message/partials,
635 * we now print the name of the file, folder, and/or message
636 * to which we are storing the content.
638 if (!is_partial
|| last_partial
) {
640 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
641 } else if (!strcmp(ct
->c_storage
, "-")) {
642 fprintf (stderr
, " to stdout\n");
646 cwdlen
= strlen (cwd
);
647 fprintf (stderr
, " as file %s\n",
648 strncmp (ct
->c_storage
, cwd
, cwdlen
)
649 || ct
->c_storage
[cwdlen
] != '/'
650 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
659 * Output content to a file
663 output_content_file (CT ct
, int appending
)
666 char *file
, buffer
[BUFSIZ
];
671 * If the pathname is absolute, make sure
672 * all the relevant directories exist.
674 if (strchr(ct
->c_storage
, '/')
675 && make_intermediates (ct
->c_storage
) == NOTOK
)
678 if (ct
->c_encoding
!= CE_7BIT
) {
681 if (!ct
->c_ceopenfnx
) {
682 advise (NULL
, "don't know how to decode part %s of message %s",
683 ct
->c_partno
, ct
->c_file
);
687 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
689 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
691 if (!strcmp (file
, ct
->c_storage
)) {
692 (*ct
->c_ceclosefnx
) (ct
);
697 * Send to standard output
699 if (!strcmp (ct
->c_storage
, "-")) {
702 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
703 advise ("stdout", "unable to dup");
705 (*ct
->c_ceclosefnx
) (ct
);
708 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
709 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
710 appending
? "a" : "w");
718 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
719 advise (ct
->c_storage
, "unable to fopen for %s",
720 appending
? "appending" : "writing");
726 * Filter the header fields of the initial enclosing
727 * message/partial into the file.
729 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
730 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
732 if (pm
->pm_partno
== 1)
733 copy_some_headers (fp
, ct
);
737 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
739 advise (file
, "error reading content from");
746 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
752 (*ct
->c_ceclosefnx
) (ct
);
754 if (cc
!= NOTOK
&& fflush (fp
))
755 advise (ct
->c_storage
, "error writing to");
759 return (cc
!= NOTOK
? OK
: NOTOK
);
762 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
763 advise (ct
->c_file
, "unable to open for reading");
769 fseek (ct
->c_fp
, pos
, SEEK_SET
);
771 if (!strcmp (ct
->c_storage
, "-")) {
774 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
775 advise ("stdout", "unable to dup");
778 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
779 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
780 appending
? "a" : "w");
785 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
786 advise (ct
->c_storage
, "unable to fopen for %s",
787 appending
? "appending" : "writing");
793 * Copy a few of the header fields of the initial
794 * enclosing message/partial into the file.
797 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
798 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
800 if (pm
->pm_partno
== 1) {
801 copy_some_headers (fp
, ct
);
806 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
807 if ((pos
+= strlen (buffer
)) > last
) {
810 diff
= strlen (buffer
) - (pos
- last
);
815 * If this is the first content of a group of
816 * message/partial contents, then we only copy a few
817 * of the header fields of the enclosed message.
832 if (!uprf (buffer
, XXX_FIELD_PRF
)
833 && !uprf (buffer
, VRSN_FIELD
)
834 && !uprf (buffer
, "Subject:")
835 && !uprf (buffer
, "Encrypted:")
836 && !uprf (buffer
, "Message-ID:")) {
851 advise (ct
->c_storage
, "error writing to");
861 * Add a file to a folder.
863 * Return the new message number of the file
864 * when added to the folder. Return -1, if
869 output_content_folder (char *folder
, char *filename
)
874 /* Read the folder. */
875 if ((mp
= folder_read (folder
))) {
876 /* Link file into folder */
877 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
879 advise (NULL
, "unable to read folder %s", folder
);
883 /* free folder structure */
887 * Return msgnum. We are relying on the fact that
888 * msgnum will be -1, if folder_addmsg() had an error.
895 * Parse and expand the storage formatting string
896 * pointed to by "cp" into "buffer".
900 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
904 CI ci
= &ct
->c_ctinfo
;
907 * If storage string is "-", just copy it, and
908 * return (send content to standard output).
910 if (cp
[0] == '-' && cp
[1] == '\0') {
911 strncpy (buffer
, cp
, buflen
);
919 * If formatting string is a pathname that doesn't
920 * begin with '/', then preface the path with the
921 * appropriate directory.
923 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
924 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
932 /* We are processing a storage escape */
937 * Insert parameters from Content-Type.
938 * This is only valid for '|' commands.
940 if (buffer
[0] != '|' && buffer
[0] != '!') {
949 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
951 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
961 /* insert message number */
962 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
966 /* insert part number with leading dot */
968 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
972 /* insert part number withouth leading dot */
974 strncpy (bp
, ct
->c_partno
, buflen
);
978 /* insert content type */
979 strncpy (bp
, ci
->ci_type
, buflen
);
983 /* insert content subtype */
984 strncpy (bp
, ci
->ci_subtype
, buflen
);
988 /* insert the character % */
998 /* Advance bp and decrement buflen */
1016 * Check if the content specifies a filename
1017 * in its MIME parameters.
1021 get_storeproc (CT ct
)
1023 char **ap
, **ep
, *cp
;
1024 CI ci
= &ct
->c_ctinfo
;
1027 * If the storeproc has already been defined,
1028 * we just return (for instance, if this content
1029 * is part of a "message/external".
1031 if (ct
->c_storeproc
)
1035 * Check the attribute/value pairs, for the attribute "name".
1036 * If found, do a few sanity checks and copy the value into
1039 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1040 if (!mh_strcasecmp (*ap
, "name")
1041 && *(cp
= *ep
) != '/'
1045 && !strchr (cp
, '%')) {
1046 ct
->c_storeproc
= add (cp
, NULL
);
1054 * Copy some of the header fields of the initial message/partial
1055 * message into the header of the reassembled message.
1059 copy_some_headers (FILE *out
, CT ct
)
1063 hp
= ct
->c_first_hf
; /* start at first header field */
1067 * A few of the header fields of the enclosing
1068 * messages are not copied.
1070 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1071 && mh_strcasecmp (hp
->name
, VRSN_FIELD
)
1072 && mh_strcasecmp (hp
->name
, "Subject")
1073 && mh_strcasecmp (hp
->name
, "Encrypted")
1074 && mh_strcasecmp (hp
->name
, "Message-ID"))
1075 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1076 hp
= hp
->next
; /* next header field */