]>
diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
22 #include <h/mhparse.h>
27 * The list of top-level contents to display
34 * Cache of current directory. This must be
35 * set before these routines are called.
40 * The directory in which to store the contents.
45 * Type for a compare function for qsort. This keeps
48 typedef int (*qsort_comp
) (const void *, const void *);
52 int part_ok (CT
, int);
53 int type_ok (CT
, int);
54 int make_intermediates (char *);
55 void flush_errors (void);
58 int show_content_aux (CT
, int, int, char *, char *);
63 void store_all_messages (CT
*);
68 static void store_single_message (CT
);
69 static int store_switch (CT
);
70 static int store_generic (CT
);
71 static int store_application (CT
);
72 static int store_multi (CT
);
73 static int store_partial (CT
);
74 static int store_external (CT
);
75 static int ct_compar (CT
*, CT
*);
76 static int store_content (CT
, CT
);
77 static int output_content_file (CT
, int);
78 static int check_folder (char *);
79 static int output_content_folder (char *, char *);
80 static int parse_format_string (CT
, char *, char *, int, char *);
81 static void get_storeproc (CT
);
82 static int copy_some_headers (FILE *, CT
);
86 * Main entry point to store content
87 * from a collection of messages.
91 store_all_messages (CT
*cts
)
97 * Check for the directory in which to
102 else if ((cp
= context_find (nmhstorage
)) && *cp
)
107 for (ctp
= cts
; *ctp
; ctp
++) {
109 store_single_message (ct
);
117 * Entry point to store the content
118 * in a (single) message
122 store_single_message (CT ct
)
124 if (type_ok (ct
, 1)) {
131 if (ct
->c_ceclosefnx
)
132 (*ct
->c_ceclosefnx
) (ct
);
138 * Switching routine to store different content types
144 switch (ct
->c_type
) {
146 return store_multi (ct
);
150 switch (ct
->c_subtype
) {
151 case MESSAGE_PARTIAL
:
152 return store_partial (ct
);
155 case MESSAGE_EXTERNAL
:
156 return store_external (ct
);
160 return store_generic (ct
);
166 return store_application (ct
);
173 return store_generic (ct
);
177 adios (NULL
, "unknown content type %d", ct
->c_type
);
181 return OK
; /* NOT REACHED */
186 * Generic routine to store a MIME content.
187 * (audio, video, image, text, message/rfc922)
191 store_generic (CT ct
)
194 * Check if the content specifies a filename.
195 * Don't bother with this for type "message"
196 * (only "message/rfc822" will use store_generic).
198 if (autosw
&& ct
->c_type
!= CT_MESSAGE
)
201 return store_content (ct
, NULL
);
206 * Store content of type "application"
210 store_application (CT ct
)
213 CI ci
= &ct
->c_ctinfo
;
215 /* Check if the content specifies a filename */
220 * If storeproc is not defined, and the content is type
221 * "application/octet-stream", we also check for various
222 * attribute/value pairs which specify if this a tar file.
224 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
225 int tarP
= 0, zP
= 0, gzP
= 0;
227 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
228 /* check for "type=tar" attribute */
229 if (!strcasecmp (*ap
, "type")) {
230 if (strcasecmp (*ep
, "tar"))
237 /* check for "conversions=compress" attribute */
238 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
239 && (!strcasecmp (*ep
, "compress") || !strcasecmp (*ep
, "x-compress"))) {
243 /* check for "conversions=gzip" attribute */
244 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
245 && (!strcasecmp (*ep
, "gzip") || !strcasecmp (*ep
, "x-gzip"))) {
252 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
253 : (gzP
? "%egzip -dc | tar tvf -"
254 : "%etar tvf -"), NULL
);
255 if (!ct
->c_storeproc
) {
257 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
258 : (gzP
? "| gzip -dc | tar xvpf -"
259 : "| tar xvpf -"), NULL
);
262 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
263 : (gzP
? "%m%P.tar.gz"
264 : "%m%P.tar"), NULL
);
270 return store_content (ct
, NULL
);
275 * Store the content of a multipart message
282 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
286 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
287 CT p
= part
->mp_part
;
289 if (part_ok (p
, 1) && type_ok (p
, 1)) {
290 result
= store_switch (p
);
291 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
301 * Reassemble and store the contents of a collection
302 * of messages of type "message/partial".
306 store_partial (CT ct
)
311 struct partial
*pm
, *qm
;
313 qm
= (struct partial
*) ct
->c_ctparams
;
318 for (ctp
= cts
; *ctp
; ctp
++) {
320 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
321 pm
= (struct partial
*) p
->c_ctparams
;
323 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
324 pm
->pm_marked
= pm
->pm_partno
;
336 advise (NULL
, "missing (at least) last part of multipart message");
340 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
341 adios (NULL
, "out of memory");
344 for (ctp
= cts
; *ctp
; ctp
++) {
346 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
347 pm
= (struct partial
*) p
->c_ctparams
;
355 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
358 for (ctq
= base
; *ctq
; ctq
++) {
360 pm
= (struct partial
*) p
->c_ctparams
;
361 if (pm
->pm_marked
!= cur
) {
362 if (pm
->pm_marked
== cur
- 1) {
364 "duplicate part %d of %d part multipart message",
371 "missing %spart %d of %d part multipart message",
372 cur
!= hi
? "(at least) " : "", cur
, hi
);
384 * Now cycle through the sorted list of messages of type
385 * "message/partial" and save/append them to a file.
390 if (store_content (ct
, NULL
) == NOTOK
) {
392 free ((char *) base
);
396 for (; *ctq
; ctq
++) {
398 if (store_content (p
, ct
) == NOTOK
)
402 free ((char *) base
);
408 * Store content from a message of type "message/external".
412 store_external (CT ct
)
415 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
416 CT p
= e
->eb_content
;
422 * Check if the parameters for the external body
423 * specified a filename.
428 if ((cp
= e
->eb_name
)
433 && !strchr (cp
, '%')) {
434 if (!ct
->c_storeproc
)
435 ct
->c_storeproc
= add (cp
, NULL
);
437 p
->c_storeproc
= add (cp
, NULL
);
442 * Since we will let the Content structure for the
443 * external body substitute for the current content,
444 * we temporarily change its partno (number inside
445 * multipart), so everything looks right.
447 p
->c_partno
= ct
->c_partno
;
449 /* we probably need to check if content is really there */
450 result
= store_switch (p
);
458 * Compare the numbering from two different
459 * message/partials (needed for sorting).
463 ct_compar (CT
*a
, CT
*b
)
465 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
466 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
468 return (am
->pm_marked
- bm
->pm_marked
);
473 * Store contents of a message or message part to
474 * a folder, a file, the standard output, or pass
475 * the contents to a command.
477 * If the current content to be saved is a followup part
478 * to a collection of messages of type "message/partial",
479 * then field "p" is a pointer to the Content structure
480 * to the first message/partial in the group.
484 store_content (CT ct
, CT p
)
486 int appending
= 0, msgnum
;
487 int is_partial
= 0, first_partial
= 0;
488 int last_partial
= 0;
489 char *cp
, buffer
[BUFSIZ
];
492 * Do special processing for messages of
493 * type "message/partial".
495 * We first check if this content is of type
496 * "message/partial". If it is, then we need to check
497 * whether it is the first and/or last in the group.
499 * Then if "p" is a valid pointer, it points to the Content
500 * structure of the first partial in the group. So we copy
501 * the file name and/or folder name from that message. In
502 * this case, we also note that we will be appending.
504 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
505 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
507 /* Yep, it's a message/partial */
510 /* But is it the first and/or last in the collection? */
511 if (pm
->pm_partno
== 1)
513 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
517 * If "p" is a valid pointer, then it points to the
518 * Content structure for the first message in the group.
519 * So we just copy the filename or foldername information
520 * from the previous iteration of this function.
524 ct
->c_storage
= add (p
->c_storage
, NULL
);
526 /* record the folder name */
528 ct
->c_folder
= add (p
->c_folder
, NULL
);
535 * Get storage formatting string.
537 * 1) If we have storeproc defined, then use that
538 * 2) Else check for a mhn-store-<type>/<subtype> entry
539 * 3) Else check for a mhn-store-<type> entry
540 * 4) Else if content is "message", use "+" (current folder)
541 * 5) Else use string "%m%P.%s".
543 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
544 CI ci
= &ct
->c_ctinfo
;
546 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
547 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
548 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
549 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
550 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
551 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
557 * Check the beginning of storage formatting string
558 * to see if we are saving content to a folder.
560 if (*cp
== '+' || *cp
== '@') {
561 char *tmpfilenam
, *folder
;
563 /* Store content in temporary file for now */
564 tmpfilenam
= m_scratch ("", invo_name
);
565 ct
->c_storage
= add (tmpfilenam
, NULL
);
567 /* Get the folder name */
569 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
571 folder
= getfolder (1);
573 /* Check if folder exists */
574 if (check_folder (folder
) == NOTOK
)
577 /* Record the folder name */
578 ct
->c_folder
= add (folder
, NULL
);
587 * Parse and expand the storage formatting string
588 * in `cp' into `buffer'.
590 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
593 * If formatting begins with '|' or '!', then pass
594 * content to standard input of a command and return.
596 if (buffer
[0] == '|' || buffer
[0] == '!')
597 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
599 /* record the filename */
600 ct
->c_storage
= add (buffer
, NULL
);
603 /* flush the output stream */
606 /* Now save or append the content to a file */
607 if (output_content_file (ct
, appending
) == NOTOK
)
611 * If necessary, link the file into a folder and remove
612 * the temporary file. If this message is a partial,
613 * then only do this if it is the last one in the group.
615 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
616 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
617 unlink (ct
->c_storage
);
623 * Now print out the name/number of the message
624 * that we are storing.
628 fprintf (stderr
, "reassembling partials ");
630 fprintf (stderr
, "%s", ct
->c_file
);
632 fprintf (stderr
, "%s,", ct
->c_file
);
634 fprintf (stderr
, "storing message %s", ct
->c_file
);
636 fprintf (stderr
, " part %s", ct
->c_partno
);
640 * Unless we are in the "middle" of group of message/partials,
641 * we now print the name of the file, folder, and/or message
642 * to which we are storing the content.
644 if (!is_partial
|| last_partial
) {
646 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
647 } else if (!strcmp(ct
->c_storage
, "-")) {
648 fprintf (stderr
, " to stdout\n");
652 cwdlen
= strlen (cwd
);
653 fprintf (stderr
, " as file %s\n",
654 strncmp (ct
->c_storage
, cwd
, cwdlen
)
655 || ct
->c_storage
[cwdlen
] != '/'
656 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
665 * Output content to a file
669 output_content_file (CT ct
, int appending
)
672 char *file
, buffer
[BUFSIZ
];
677 * If the pathname is absolute, make sure
678 * all the relevant directories exist.
680 if (strchr(ct
->c_storage
, '/')
681 && make_intermediates (ct
->c_storage
) == NOTOK
)
684 if (ct
->c_encoding
!= CE_7BIT
) {
687 if (!ct
->c_ceopenfnx
) {
688 advise (NULL
, "don't know how to decode part %s of message %s",
689 ct
->c_partno
, ct
->c_file
);
693 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
695 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
697 if (!strcmp (file
, ct
->c_storage
)) {
698 (*ct
->c_ceclosefnx
) (ct
);
703 * Send to standard output
705 if (!strcmp (ct
->c_storage
, "-")) {
708 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
709 advise ("stdout", "unable to dup");
711 (*ct
->c_ceclosefnx
) (ct
);
714 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
715 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
716 appending
? "a" : "w");
724 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
725 advise (ct
->c_storage
, "unable to fopen for %s",
726 appending
? "appending" : "writing");
732 * Filter the header fields of the initial enclosing
733 * message/partial into the file.
735 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
736 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
738 if (pm
->pm_partno
== 1)
739 copy_some_headers (fp
, ct
);
743 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
745 advise (file
, "error reading content from");
752 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
758 (*ct
->c_ceclosefnx
) (ct
);
760 if (cc
!= NOTOK
&& fflush (fp
))
761 advise (ct
->c_storage
, "error writing to");
765 return (cc
!= NOTOK
? OK
: NOTOK
);
768 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
769 advise (ct
->c_file
, "unable to open for reading");
775 fseek (ct
->c_fp
, pos
, SEEK_SET
);
777 if (!strcmp (ct
->c_storage
, "-")) {
780 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
781 advise ("stdout", "unable to dup");
784 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
785 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
786 appending
? "a" : "w");
791 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
792 advise (ct
->c_storage
, "unable to fopen for %s",
793 appending
? "appending" : "writing");
799 * Copy a few of the header fields of the initial
800 * enclosing message/partial into the file.
803 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
804 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
806 if (pm
->pm_partno
== 1) {
807 copy_some_headers (fp
, ct
);
812 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
813 if ((pos
+= strlen (buffer
)) > last
) {
816 diff
= strlen (buffer
) - (pos
- last
);
821 * If this is the first content of a group of
822 * message/partial contents, then we only copy a few
823 * of the header fields of the enclosed message.
838 if (!uprf (buffer
, XXX_FIELD_PRF
)
839 && !uprf (buffer
, VRSN_FIELD
)
840 && !uprf (buffer
, "Subject:")
841 && !uprf (buffer
, "Encrypted:")
842 && !uprf (buffer
, "Message-ID:")) {
857 advise (ct
->c_storage
, "error writing to");
867 * Check if folder exists, and create
872 check_folder (char *folder
)
877 /* expand path to the folder */
878 folderdir
= m_mailpath (folder
);
880 /* Check if folder exists */
881 if (stat (folderdir
, &st
) == NOTOK
) {
885 if (errno
!= ENOENT
) {
886 advise (folderdir
, "error on folder");
890 ep
= concat ("Create folder \"", folderdir
, "\"? ", NULL
);
891 answer
= getanswer (ep
);
897 if (!makedir (folderdir
)) {
898 advise (NULL
, "unable to create folder %s", folderdir
);
908 * Add a file to a folder.
910 * Return the new message number of the file
911 * when added to the folder. Return -1, if
916 output_content_folder (char *folder
, char *filename
)
921 /* Read the folder. */
922 if ((mp
= folder_read (folder
))) {
923 /* Link file into folder */
924 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0);
926 advise (NULL
, "unable to read folder %s", folder
);
930 /* free folder structure */
934 * Return msgnum. We are relying on the fact that
935 * msgnum will be -1, if folder_addmsg() had an error.
942 * Parse and expand the storage formatting string
943 * pointed to by "cp" into "buffer".
947 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
951 CI ci
= &ct
->c_ctinfo
;
954 * If storage string is "-", just copy it, and
955 * return (send content to standard output).
957 if (cp
[0] == '-' && cp
[1] == '\0') {
958 strncpy (buffer
, cp
, buflen
);
966 * If formatting string is a pathname that doesn't
967 * begin with '/', then preface the path with the
968 * appropriate directory.
970 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
971 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
979 /* We are processing a storage escape */
984 * Insert parameters from Content-Type.
985 * This is only valid for '|' commands.
987 if (buffer
[0] != '|' && buffer
[0] != '!') {
996 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
998 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
1008 /* insert message number */
1009 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1013 /* insert part number with leading dot */
1015 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1019 /* insert part number withouth leading dot */
1021 strncpy (bp
, ct
->c_partno
, buflen
);
1025 /* insert content type */
1026 strncpy (bp
, ci
->ci_type
, buflen
);
1030 /* insert content subtype */
1031 strncpy (bp
, ci
->ci_subtype
, buflen
);
1035 /* insert the character % */
1045 /* Advance bp and decrement buflen */
1063 * Check if the content specifies a filename
1064 * in its MIME parameters.
1068 get_storeproc (CT ct
)
1070 char **ap
, **ep
, *cp
;
1071 CI ci
= &ct
->c_ctinfo
;
1074 * If the storeproc has already been defined,
1075 * we just return (for instance, if this content
1076 * is part of a "message/external".
1078 if (ct
->c_storeproc
)
1082 * Check the attribute/value pairs, for the attribute "name".
1083 * If found, do a few sanity checks and copy the value into
1086 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1087 if (!strcasecmp (*ap
, "name")
1088 && *(cp
= *ep
) != '/'
1092 && !strchr (cp
, '%')) {
1093 ct
->c_storeproc
= add (cp
, NULL
);
1101 * Copy some of the header fields of the initial message/partial
1102 * message into the header of the reassembled message.
1106 copy_some_headers (FILE *out
, CT ct
)
1110 hp
= ct
->c_first_hf
; /* start at first header field */
1114 * A few of the header fields of the enclosing
1115 * messages are not copied.
1117 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1118 && strcasecmp (hp
->name
, VRSN_FIELD
)
1119 && strcasecmp (hp
->name
, "Subject")
1120 && strcasecmp (hp
->name
, "Encrypted")
1121 && strcasecmp (hp
->name
, "Message-ID"))
1122 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1123 hp
= hp
->next
; /* next header field */