]>
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
95 if ((cp
= context_find (nmhstorage
)) && *cp
)
100 for (ctp
= cts
; *ctp
; ctp
++) {
102 store_single_message (ct
);
110 * Entry point to store the content
111 * in a (single) message
115 store_single_message (CT ct
)
117 if (type_ok (ct
, 1)) {
124 if (ct
->c_ceclosefnx
)
125 (*ct
->c_ceclosefnx
) (ct
);
131 * Switching routine to store different content types
137 switch (ct
->c_type
) {
139 return store_multi (ct
);
143 switch (ct
->c_subtype
) {
144 case MESSAGE_PARTIAL
:
145 return store_partial (ct
);
148 case MESSAGE_EXTERNAL
:
149 return store_external (ct
);
153 return store_generic (ct
);
159 return store_application (ct
);
166 return store_generic (ct
);
170 adios (NULL
, "unknown content type %d", ct
->c_type
);
174 return OK
; /* NOT REACHED */
179 * Generic routine to store a MIME content.
180 * (audio, video, image, text, message/rfc922)
184 store_generic (CT ct
)
187 * Check if the content specifies a filename.
188 * Don't bother with this for type "message"
189 * (only "message/rfc822" will use store_generic).
191 if (autosw
&& ct
->c_type
!= CT_MESSAGE
)
194 return store_content (ct
, NULL
);
199 * Store content of type "application"
203 store_application (CT ct
)
206 CI ci
= &ct
->c_ctinfo
;
208 /* Check if the content specifies a filename */
213 * If storeproc is not defined, and the content is type
214 * "application/octet-stream", we also check for various
215 * attribute/value pairs which specify if this a tar file.
217 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
218 int tarP
= 0, zP
= 0, gzP
= 0;
220 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
221 /* check for "type=tar" attribute */
222 if (!mh_strcasecmp (*ap
, "type")) {
223 if (mh_strcasecmp (*ep
, "tar"))
230 /* check for "conversions=compress" attribute */
231 if ((!mh_strcasecmp (*ap
, "conversions") || !mh_strcasecmp (*ap
, "x-conversions"))
232 && (!mh_strcasecmp (*ep
, "compress") || !mh_strcasecmp (*ep
, "x-compress"))) {
236 /* check for "conversions=gzip" attribute */
237 if ((!mh_strcasecmp (*ap
, "conversions") || !mh_strcasecmp (*ap
, "x-conversions"))
238 && (!mh_strcasecmp (*ep
, "gzip") || !mh_strcasecmp (*ep
, "x-gzip"))) {
245 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
246 : (gzP
? "%egzip -dc | tar tvf -"
247 : "%etar tvf -"), NULL
);
248 if (!ct
->c_storeproc
) {
250 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
251 : (gzP
? "| gzip -dc | tar xvpf -"
252 : "| tar xvpf -"), NULL
);
255 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
256 : (gzP
? "%m%P.tar.gz"
257 : "%m%P.tar"), NULL
);
263 return store_content (ct
, NULL
);
268 * Store the content of a multipart message
275 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
279 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
280 CT p
= part
->mp_part
;
282 if (part_ok (p
, 1) && type_ok (p
, 1)) {
283 result
= store_switch (p
);
284 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
294 * Reassemble and store the contents of a collection
295 * of messages of type "message/partial".
299 store_partial (CT ct
)
304 struct partial
*pm
, *qm
;
306 qm
= (struct partial
*) ct
->c_ctparams
;
311 for (ctp
= cts
; *ctp
; ctp
++) {
313 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
314 pm
= (struct partial
*) p
->c_ctparams
;
316 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
317 pm
->pm_marked
= pm
->pm_partno
;
329 advise (NULL
, "missing (at least) last part of multipart message");
333 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
334 adios (NULL
, "out of memory");
337 for (ctp
= cts
; *ctp
; ctp
++) {
339 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
340 pm
= (struct partial
*) p
->c_ctparams
;
348 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
351 for (ctq
= base
; *ctq
; ctq
++) {
353 pm
= (struct partial
*) p
->c_ctparams
;
354 if (pm
->pm_marked
!= cur
) {
355 if (pm
->pm_marked
== cur
- 1) {
357 "duplicate part %d of %d part multipart message",
364 "missing %spart %d of %d part multipart message",
365 cur
!= hi
? "(at least) " : "", cur
, hi
);
377 * Now cycle through the sorted list of messages of type
378 * "message/partial" and save/append them to a file.
383 if (store_content (ct
, NULL
) == NOTOK
) {
385 free ((char *) base
);
389 for (; *ctq
; ctq
++) {
391 if (store_content (p
, ct
) == NOTOK
)
395 free ((char *) base
);
401 * Store content from a message of type "message/external".
405 store_external (CT ct
)
408 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
409 CT p
= e
->eb_content
;
415 * Check if the parameters for the external body
416 * specified a filename.
421 if ((cp
= e
->eb_name
)
426 && !strchr (cp
, '%')) {
427 if (!ct
->c_storeproc
)
428 ct
->c_storeproc
= add (cp
, NULL
);
430 p
->c_storeproc
= add (cp
, NULL
);
435 * Since we will let the Content structure for the
436 * external body substitute for the current content,
437 * we temporarily change its partno (number inside
438 * multipart), so everything looks right.
440 p
->c_partno
= ct
->c_partno
;
442 /* we probably need to check if content is really there */
443 result
= store_switch (p
);
451 * Compare the numbering from two different
452 * message/partials (needed for sorting).
456 ct_compar (CT
*a
, CT
*b
)
458 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
459 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
461 return (am
->pm_marked
- bm
->pm_marked
);
466 * Store contents of a message or message part to
467 * a folder, a file, the standard output, or pass
468 * the contents to a command.
470 * If the current content to be saved is a followup part
471 * to a collection of messages of type "message/partial",
472 * then field "p" is a pointer to the Content structure
473 * to the first message/partial in the group.
477 store_content (CT ct
, CT p
)
479 int appending
= 0, msgnum
= 0;
480 int is_partial
= 0, first_partial
= 0;
481 int last_partial
= 0;
482 char *cp
, buffer
[BUFSIZ
];
485 * Do special processing for messages of
486 * type "message/partial".
488 * We first check if this content is of type
489 * "message/partial". If it is, then we need to check
490 * whether it is the first and/or last in the group.
492 * Then if "p" is a valid pointer, it points to the Content
493 * structure of the first partial in the group. So we copy
494 * the file name and/or folder name from that message. In
495 * this case, we also note that we will be appending.
497 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
498 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
500 /* Yep, it's a message/partial */
503 /* But is it the first and/or last in the collection? */
504 if (pm
->pm_partno
== 1)
506 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
510 * If "p" is a valid pointer, then it points to the
511 * Content structure for the first message in the group.
512 * So we just copy the filename or foldername information
513 * from the previous iteration of this function.
517 ct
->c_storage
= add (p
->c_storage
, NULL
);
519 /* record the folder name */
521 ct
->c_folder
= add (p
->c_folder
, NULL
);
528 * Get storage formatting string.
530 * 1) If we have storeproc defined, then use that
531 * 2) Else check for a mhn-store-<type>/<subtype> entry
532 * 3) Else check for a mhn-store-<type> entry
533 * 4) Else if content is "message", use "+" (current folder)
534 * 5) Else use string "%m%P.%s".
536 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
537 CI ci
= &ct
->c_ctinfo
;
539 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
540 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
541 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
542 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
543 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
544 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
550 * Check the beginning of storage formatting string
551 * to see if we are saving content to a folder.
553 if (*cp
== '+' || *cp
== '@') {
554 char *tmpfilenam
, *folder
;
556 /* Store content in temporary file for now */
557 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
558 ct
->c_storage
= add (tmpfilenam
, NULL
);
560 /* Get the folder name */
562 folder
= pluspath (cp
);
564 folder
= getfolder (1);
566 /* Check if folder exists */
567 create_folder(m_mailpath(folder
), 0, exit
);
569 /* Record the folder name */
570 ct
->c_folder
= add (folder
, NULL
);
579 * Parse and expand the storage formatting string
580 * in `cp' into `buffer'.
582 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
585 * If formatting begins with '|' or '!', then pass
586 * content to standard input of a command and return.
588 if (buffer
[0] == '|' || buffer
[0] == '!')
589 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
591 /* record the filename */
592 ct
->c_storage
= add (buffer
, NULL
);
595 /* flush the output stream */
598 /* Now save or append the content to a file */
599 if (output_content_file (ct
, appending
) == NOTOK
)
603 * If necessary, link the file into a folder and remove
604 * the temporary file. If this message is a partial,
605 * then only do this if it is the last one in the group.
607 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
608 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
609 unlink (ct
->c_storage
);
615 * Now print out the name/number of the message
616 * that we are storing.
620 fprintf (stderr
, "reassembling partials ");
622 fprintf (stderr
, "%s", ct
->c_file
);
624 fprintf (stderr
, "%s,", ct
->c_file
);
626 fprintf (stderr
, "storing message %s", ct
->c_file
);
628 fprintf (stderr
, " part %s", ct
->c_partno
);
632 * Unless we are in the "middle" of group of message/partials,
633 * we now print the name of the file, folder, and/or message
634 * to which we are storing the content.
636 if (!is_partial
|| last_partial
) {
638 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
639 } else if (!strcmp(ct
->c_storage
, "-")) {
640 fprintf (stderr
, " to stdout\n");
644 cwdlen
= strlen (cwd
);
645 fprintf (stderr
, " as file %s\n",
646 strncmp (ct
->c_storage
, cwd
, cwdlen
)
647 || ct
->c_storage
[cwdlen
] != '/'
648 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
657 * Output content to a file
661 output_content_file (CT ct
, int appending
)
664 char *file
, buffer
[BUFSIZ
];
669 * If the pathname is absolute, make sure
670 * all the relevant directories exist.
672 if (strchr(ct
->c_storage
, '/')
673 && make_intermediates (ct
->c_storage
) == NOTOK
)
676 if (ct
->c_encoding
!= CE_7BIT
) {
679 if (!ct
->c_ceopenfnx
) {
680 advise (NULL
, "don't know how to decode part %s of message %s",
681 ct
->c_partno
, ct
->c_file
);
685 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
687 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
689 if (!strcmp (file
, ct
->c_storage
)) {
690 (*ct
->c_ceclosefnx
) (ct
);
695 * Send to standard output
697 if (!strcmp (ct
->c_storage
, "-")) {
700 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
701 advise ("stdout", "unable to dup");
703 (*ct
->c_ceclosefnx
) (ct
);
706 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
707 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
708 appending
? "a" : "w");
716 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
717 advise (ct
->c_storage
, "unable to fopen for %s",
718 appending
? "appending" : "writing");
724 * Filter the header fields of the initial enclosing
725 * message/partial into the file.
727 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
728 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
730 if (pm
->pm_partno
== 1)
731 copy_some_headers (fp
, ct
);
735 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
737 advise (file
, "error reading content from");
744 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
750 (*ct
->c_ceclosefnx
) (ct
);
752 if (cc
!= NOTOK
&& fflush (fp
))
753 advise (ct
->c_storage
, "error writing to");
757 return (cc
!= NOTOK
? OK
: NOTOK
);
760 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
761 advise (ct
->c_file
, "unable to open for reading");
767 fseek (ct
->c_fp
, pos
, SEEK_SET
);
769 if (!strcmp (ct
->c_storage
, "-")) {
772 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
773 advise ("stdout", "unable to dup");
776 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
777 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
778 appending
? "a" : "w");
783 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
784 advise (ct
->c_storage
, "unable to fopen for %s",
785 appending
? "appending" : "writing");
791 * Copy a few of the header fields of the initial
792 * enclosing message/partial into the file.
795 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
796 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
798 if (pm
->pm_partno
== 1) {
799 copy_some_headers (fp
, ct
);
804 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
805 if ((pos
+= strlen (buffer
)) > last
) {
808 diff
= strlen (buffer
) - (pos
- last
);
813 * If this is the first content of a group of
814 * message/partial contents, then we only copy a few
815 * of the header fields of the enclosed message.
830 if (!uprf (buffer
, XXX_FIELD_PRF
)
831 && !uprf (buffer
, VRSN_FIELD
)
832 && !uprf (buffer
, "Subject:")
833 && !uprf (buffer
, "Encrypted:")
834 && !uprf (buffer
, "Message-ID:")) {
849 advise (ct
->c_storage
, "error writing to");
859 * Add a file to a folder.
861 * Return the new message number of the file
862 * when added to the folder. Return -1, if
867 output_content_folder (char *folder
, char *filename
)
872 /* Read the folder. */
873 if ((mp
= folder_read (folder
))) {
874 /* Link file into folder */
875 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
877 advise (NULL
, "unable to read folder %s", folder
);
881 /* free folder structure */
885 * Return msgnum. We are relying on the fact that
886 * msgnum will be -1, if folder_addmsg() had an error.
893 * Parse and expand the storage formatting string
894 * pointed to by "cp" into "buffer".
898 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
902 CI ci
= &ct
->c_ctinfo
;
905 * If storage string is "-", just copy it, and
906 * return (send content to standard output).
908 if (cp
[0] == '-' && cp
[1] == '\0') {
909 strncpy (buffer
, cp
, buflen
);
917 * If formatting string is a pathname that doesn't
918 * begin with '/', then preface the path with the
919 * appropriate directory.
921 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
922 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
930 /* We are processing a storage escape */
935 * Insert parameters from Content-Type.
936 * This is only valid for '|' commands.
938 if (buffer
[0] != '|' && buffer
[0] != '!') {
947 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
949 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
959 /* insert message number */
960 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
964 /* insert part number with leading dot */
966 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
970 /* insert part number withouth leading dot */
972 strncpy (bp
, ct
->c_partno
, buflen
);
976 /* insert content type */
977 strncpy (bp
, ci
->ci_type
, buflen
);
981 /* insert content subtype */
982 strncpy (bp
, ci
->ci_subtype
, buflen
);
986 /* insert the character % */
996 /* Advance bp and decrement buflen */
1014 * Check if the content specifies a filename
1015 * in its MIME parameters.
1019 get_storeproc (CT ct
)
1021 char **ap
, **ep
, *cp
;
1022 CI ci
= &ct
->c_ctinfo
;
1025 * If the storeproc has already been defined,
1026 * we just return (for instance, if this content
1027 * is part of a "message/external".
1029 if (ct
->c_storeproc
)
1033 * Check the attribute/value pairs, for the attribute "name".
1034 * If found, do a few sanity checks and copy the value into
1037 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1038 if (!mh_strcasecmp (*ap
, "name")
1039 && *(cp
= *ep
) != '/'
1043 && !strchr (cp
, '%')) {
1044 ct
->c_storeproc
= add (cp
, NULL
);
1052 * Copy some of the header fields of the initial message/partial
1053 * message into the header of the reassembled message.
1057 copy_some_headers (FILE *out
, CT ct
)
1061 hp
= ct
->c_first_hf
; /* start at first header field */
1065 * A few of the header fields of the enclosing
1066 * messages are not copied.
1068 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1069 && mh_strcasecmp (hp
->name
, VRSN_FIELD
)
1070 && mh_strcasecmp (hp
->name
, "Subject")
1071 && mh_strcasecmp (hp
->name
, "Encrypted")
1072 && mh_strcasecmp (hp
->name
, "Message-ID"))
1073 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1074 hp
= hp
->next
; /* next header field */