]>
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>
26 * The list of top-level contents to display
33 * Cache of current directory. This must be
34 * set before these routines are called.
39 * The directory in which to store the contents.
44 * Type for a compare function for qsort. This keeps
47 typedef int (*qsort_comp
) (const void *, const void *);
51 int part_ok (CT
, int);
52 int type_ok (CT
, int);
53 int make_intermediates (char *);
54 void flush_errors (void);
57 int show_content_aux (CT
, int, int, char *, char *);
62 void store_all_messages (CT
*);
67 static void store_single_message (CT
);
68 static int store_switch (CT
);
69 static int store_generic (CT
);
70 static int store_application (CT
);
71 static int store_multi (CT
);
72 static int store_partial (CT
);
73 static int store_external (CT
);
74 static int ct_compar (CT
*, CT
*);
75 static int store_content (CT
, CT
);
76 static int output_content_file (CT
, int);
77 static int check_folder (char *);
78 static int output_content_folder (char *, char *);
79 static int parse_format_string (CT
, char *, char *, int, char *);
80 static void get_storeproc (CT
);
81 static int copy_some_headers (FILE *, CT
);
85 * Main entry point to store content
86 * from a collection of messages.
90 store_all_messages (CT
*cts
)
96 * Check for the directory in which to
101 else if ((cp
= context_find (nmhstorage
)) && *cp
)
106 for (ctp
= cts
; *ctp
; ctp
++) {
108 store_single_message (ct
);
116 * Entry point to store the content
117 * in a (single) message
121 store_single_message (CT ct
)
123 if (type_ok (ct
, 1)) {
130 if (ct
->c_ceclosefnx
)
131 (*ct
->c_ceclosefnx
) (ct
);
137 * Switching routine to store different content types
143 switch (ct
->c_type
) {
145 return store_multi (ct
);
149 switch (ct
->c_subtype
) {
150 case MESSAGE_PARTIAL
:
151 return store_partial (ct
);
154 case MESSAGE_EXTERNAL
:
155 return store_external (ct
);
159 return store_generic (ct
);
165 return store_application (ct
);
172 return store_generic (ct
);
176 adios (NULL
, "unknown content type %d", ct
->c_type
);
180 return OK
; /* NOT REACHED */
185 * Generic routine to store a MIME content.
186 * (audio, video, image, text, message/rfc922)
190 store_generic (CT ct
)
193 * Check if the content specifies a filename.
194 * Don't bother with this for type "message"
195 * (only "message/rfc822" will use store_generic).
197 if (autosw
&& ct
->c_type
!= CT_MESSAGE
)
200 return store_content (ct
, NULL
);
205 * Store content of type "application"
209 store_application (CT ct
)
212 CI ci
= &ct
->c_ctinfo
;
214 /* Check if the content specifies a filename */
219 * If storeproc is not defined, and the content is type
220 * "application/octet-stream", we also check for various
221 * attribute/value pairs which specify if this a tar file.
223 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
224 int tarP
= 0, zP
= 0, gzP
= 0;
226 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
227 /* check for "type=tar" attribute */
228 if (!strcasecmp (*ap
, "type")) {
229 if (strcasecmp (*ep
, "tar"))
236 /* check for "conversions=compress" attribute */
237 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
238 && (!strcasecmp (*ep
, "compress") || !strcasecmp (*ep
, "x-compress"))) {
242 /* check for "conversions=gzip" attribute */
243 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
244 && (!strcasecmp (*ep
, "gzip") || !strcasecmp (*ep
, "x-gzip"))) {
251 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
252 : (gzP
? "%egzip -dc | tar tvf -"
253 : "%etar tvf -"), NULL
);
254 if (!ct
->c_storeproc
) {
256 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
257 : (gzP
? "| gzip -dc | tar xvpf -"
258 : "| tar xvpf -"), NULL
);
261 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
262 : (gzP
? "%m%P.tar.gz"
263 : "%m%P.tar"), NULL
);
269 return store_content (ct
, NULL
);
274 * Store the content of a multipart message
281 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
285 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
286 CT p
= part
->mp_part
;
288 if (part_ok (p
, 1) && type_ok (p
, 1)) {
289 result
= store_switch (p
);
290 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
300 * Reassemble and store the contents of a collection
301 * of messages of type "message/partial".
305 store_partial (CT ct
)
310 struct partial
*pm
, *qm
;
312 qm
= (struct partial
*) ct
->c_ctparams
;
317 for (ctp
= cts
; *ctp
; ctp
++) {
319 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
320 pm
= (struct partial
*) p
->c_ctparams
;
322 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
323 pm
->pm_marked
= pm
->pm_partno
;
335 advise (NULL
, "missing (at least) last part of multipart message");
339 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
340 adios (NULL
, "out of memory");
343 for (ctp
= cts
; *ctp
; ctp
++) {
345 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
346 pm
= (struct partial
*) p
->c_ctparams
;
354 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
357 for (ctq
= base
; *ctq
; ctq
++) {
359 pm
= (struct partial
*) p
->c_ctparams
;
360 if (pm
->pm_marked
!= cur
) {
361 if (pm
->pm_marked
== cur
- 1) {
363 "duplicate part %d of %d part multipart message",
370 "missing %spart %d of %d part multipart message",
371 cur
!= hi
? "(at least) " : "", cur
, hi
);
383 * Now cycle through the sorted list of messages of type
384 * "message/partial" and save/append them to a file.
389 if (store_content (ct
, NULL
) == NOTOK
) {
391 free ((char *) base
);
395 for (; *ctq
; ctq
++) {
397 if (store_content (p
, ct
) == NOTOK
)
401 free ((char *) base
);
407 * Store content from a message of type "message/external".
411 store_external (CT ct
)
414 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
415 CT p
= e
->eb_content
;
421 * Check if the parameters for the external body
422 * specified a filename.
427 if ((cp
= e
->eb_name
)
432 && !strchr (cp
, '%')) {
433 if (!ct
->c_storeproc
)
434 ct
->c_storeproc
= add (cp
, NULL
);
436 p
->c_storeproc
= add (cp
, NULL
);
441 * Since we will let the Content structure for the
442 * external body substitute for the current content,
443 * we temporarily change its partno (number inside
444 * multipart), so everything looks right.
446 p
->c_partno
= ct
->c_partno
;
448 /* we probably need to check if content is really there */
449 result
= store_switch (p
);
457 * Compare the numbering from two different
458 * message/partials (needed for sorting).
462 ct_compar (CT
*a
, CT
*b
)
464 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
465 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
467 return (am
->pm_marked
- bm
->pm_marked
);
472 * Store contents of a message or message part to
473 * a folder, a file, the standard output, or pass
474 * the contents to a command.
476 * If the current content to be saved is a followup part
477 * to a collection of messages of type "message/partial",
478 * then field "p" is a pointer to the Content structure
479 * to the first message/partial in the group.
483 store_content (CT ct
, CT p
)
485 int appending
= 0, msgnum
;
486 int is_partial
= 0, first_partial
= 0;
487 int last_partial
= 0;
488 char *cp
, buffer
[BUFSIZ
];
491 * Do special processing for messages of
492 * type "message/partial".
494 * We first check if this content is of type
495 * "message/partial". If it is, then we need to check
496 * whether it is the first and/or last in the group.
498 * Then if "p" is a valid pointer, it points to the Content
499 * structure of the first partial in the group. So we copy
500 * the file name and/or folder name from that message. In
501 * this case, we also note that we will be appending.
503 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
504 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
506 /* Yep, it's a message/partial */
509 /* But is it the first and/or last in the collection? */
510 if (pm
->pm_partno
== 1)
512 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
516 * If "p" is a valid pointer, then it points to the
517 * Content structure for the first message in the group.
518 * So we just copy the filename or foldername information
519 * from the previous iteration of this function.
523 ct
->c_storage
= add (p
->c_storage
, NULL
);
525 /* record the folder name */
527 ct
->c_folder
= add (p
->c_folder
, NULL
);
534 * Get storage formatting string.
536 * 1) If we have storeproc defined, then use that
537 * 2) Else check for a mhn-store-<type>/<subtype> entry
538 * 3) Else check for a mhn-store-<type> entry
539 * 4) Else if content is "message", use "+" (current folder)
540 * 5) Else use string "%m%P.%s".
542 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
543 CI ci
= &ct
->c_ctinfo
;
545 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
546 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
547 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
548 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
549 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
550 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
556 * Check the beginning of storage formatting string
557 * to see if we are saving content to a folder.
559 if (*cp
== '+' || *cp
== '@') {
560 char *tmpfilenam
, *folder
;
562 /* Store content in temporary file for now */
563 tmpfilenam
= m_scratch ("", invo_name
);
564 ct
->c_storage
= add (tmpfilenam
, NULL
);
566 /* Get the folder name */
568 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
570 folder
= getfolder (1);
572 /* Check if folder exists */
573 if (check_folder (folder
) == NOTOK
)
576 /* Record the folder name */
577 ct
->c_folder
= add (folder
, NULL
);
586 * Parse and expand the storage formatting string
587 * in `cp' into `buffer'.
589 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
592 * If formatting begins with '|' or '!', then pass
593 * content to standard input of a command and return.
595 if (buffer
[0] == '|' || buffer
[0] == '!')
596 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
598 /* record the filename */
599 ct
->c_storage
= add (buffer
, NULL
);
602 /* flush the output stream */
605 /* Now save or append the content to a file */
606 if (output_content_file (ct
, appending
) == NOTOK
)
610 * If necessary, link the file into a folder and remove
611 * the temporary file. If this message is a partial,
612 * then only do this if it is the last one in the group.
614 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
615 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
616 unlink (ct
->c_storage
);
622 * Now print out the name/number of the message
623 * that we are storing.
627 fprintf (stderr
, "reassembling partials ");
629 fprintf (stderr
, "%s", ct
->c_file
);
631 fprintf (stderr
, "%s,", ct
->c_file
);
633 fprintf (stderr
, "storing message %s", ct
->c_file
);
635 fprintf (stderr
, " part %s", ct
->c_partno
);
639 * Unless we are in the "middle" of group of message/partials,
640 * we now print the name of the file, folder, and/or message
641 * to which we are storing the content.
643 if (!is_partial
|| last_partial
) {
645 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
646 } else if (!strcmp(ct
->c_storage
, "-")) {
647 fprintf (stderr
, " to stdout\n");
651 cwdlen
= strlen (cwd
);
652 fprintf (stderr
, " as file %s\n",
653 strncmp (ct
->c_storage
, cwd
, cwdlen
)
654 || ct
->c_storage
[cwdlen
] != '/'
655 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
664 * Output content to a file
668 output_content_file (CT ct
, int appending
)
671 char *file
, buffer
[BUFSIZ
];
676 * If the pathname is absolute, make sure
677 * all the relevant directories exist.
679 if (strchr(ct
->c_storage
, '/')
680 && make_intermediates (ct
->c_storage
) == NOTOK
)
683 if (ct
->c_encoding
!= CE_7BIT
) {
686 if (!ct
->c_ceopenfnx
) {
687 advise (NULL
, "don't know how to decode part %s of message %s",
688 ct
->c_partno
, ct
->c_file
);
692 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
694 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
696 if (!strcmp (file
, ct
->c_storage
)) {
697 (*ct
->c_ceclosefnx
) (ct
);
702 * Send to standard output
704 if (!strcmp (ct
->c_storage
, "-")) {
707 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
708 advise ("stdout", "unable to dup");
710 (*ct
->c_ceclosefnx
) (ct
);
713 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
714 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
715 appending
? "a" : "w");
723 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
724 advise (ct
->c_storage
, "unable to fopen for %s",
725 appending
? "appending" : "writing");
731 * Filter the header fields of the initial enclosing
732 * message/partial into the file.
734 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
735 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
737 if (pm
->pm_partno
== 1)
738 copy_some_headers (fp
, ct
);
742 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
744 advise (file
, "error reading content from");
751 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
757 (*ct
->c_ceclosefnx
) (ct
);
759 if (cc
!= NOTOK
&& fflush (fp
))
760 advise (ct
->c_storage
, "error writing to");
764 return (cc
!= NOTOK
? OK
: NOTOK
);
767 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
768 advise (ct
->c_file
, "unable to open for reading");
774 fseek (ct
->c_fp
, pos
, SEEK_SET
);
776 if (!strcmp (ct
->c_storage
, "-")) {
779 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
780 advise ("stdout", "unable to dup");
783 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
784 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
785 appending
? "a" : "w");
790 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
791 advise (ct
->c_storage
, "unable to fopen for %s",
792 appending
? "appending" : "writing");
798 * Copy a few of the header fields of the initial
799 * enclosing message/partial into the file.
802 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
803 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
805 if (pm
->pm_partno
== 1) {
806 copy_some_headers (fp
, ct
);
811 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
812 if ((pos
+= strlen (buffer
)) > last
) {
815 diff
= strlen (buffer
) - (pos
- last
);
820 * If this is the first content of a group of
821 * message/partial contents, then we only copy a few
822 * of the header fields of the enclosed message.
837 if (!uprf (buffer
, XXX_FIELD_PRF
)
838 && !uprf (buffer
, VRSN_FIELD
)
839 && !uprf (buffer
, "Subject:")
840 && !uprf (buffer
, "Encrypted:")
841 && !uprf (buffer
, "Message-ID:")) {
856 advise (ct
->c_storage
, "error writing to");
866 * Check if folder exists, and create
871 check_folder (char *folder
)
876 /* expand path to the folder */
877 folderdir
= m_mailpath (folder
);
879 /* Check if folder exists */
880 if (stat (folderdir
, &st
) == NOTOK
) {
884 if (errno
!= ENOENT
) {
885 advise (folderdir
, "error on folder");
889 ep
= concat ("Create folder \"", folderdir
, "\"? ", NULL
);
890 answer
= getanswer (ep
);
896 if (!makedir (folderdir
)) {
897 advise (NULL
, "unable to create folder %s", folderdir
);
907 * Add a file to a folder.
909 * Return the new message number of the file
910 * when added to the folder. Return -1, if
915 output_content_folder (char *folder
, char *filename
)
920 /* Read the folder. */
921 if ((mp
= folder_read (folder
))) {
922 /* Link file into folder */
923 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
925 advise (NULL
, "unable to read folder %s", folder
);
929 /* free folder structure */
933 * Return msgnum. We are relying on the fact that
934 * msgnum will be -1, if folder_addmsg() had an error.
941 * Parse and expand the storage formatting string
942 * pointed to by "cp" into "buffer".
946 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
950 CI ci
= &ct
->c_ctinfo
;
953 * If storage string is "-", just copy it, and
954 * return (send content to standard output).
956 if (cp
[0] == '-' && cp
[1] == '\0') {
957 strncpy (buffer
, cp
, buflen
);
965 * If formatting string is a pathname that doesn't
966 * begin with '/', then preface the path with the
967 * appropriate directory.
969 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
970 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
978 /* We are processing a storage escape */
983 * Insert parameters from Content-Type.
984 * This is only valid for '|' commands.
986 if (buffer
[0] != '|' && buffer
[0] != '!') {
995 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
997 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
1007 /* insert message number */
1008 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1012 /* insert part number with leading dot */
1014 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1018 /* insert part number withouth leading dot */
1020 strncpy (bp
, ct
->c_partno
, buflen
);
1024 /* insert content type */
1025 strncpy (bp
, ci
->ci_type
, buflen
);
1029 /* insert content subtype */
1030 strncpy (bp
, ci
->ci_subtype
, buflen
);
1034 /* insert the character % */
1044 /* Advance bp and decrement buflen */
1062 * Check if the content specifies a filename
1063 * in its MIME parameters.
1067 get_storeproc (CT ct
)
1069 char **ap
, **ep
, *cp
;
1070 CI ci
= &ct
->c_ctinfo
;
1073 * If the storeproc has already been defined,
1074 * we just return (for instance, if this content
1075 * is part of a "message/external".
1077 if (ct
->c_storeproc
)
1081 * Check the attribute/value pairs, for the attribute "name".
1082 * If found, do a few sanity checks and copy the value into
1085 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1086 if (!strcasecmp (*ap
, "name")
1087 && *(cp
= *ep
) != '/'
1091 && !strchr (cp
, '%')) {
1092 ct
->c_storeproc
= add (cp
, NULL
);
1100 * Copy some of the header fields of the initial message/partial
1101 * message into the header of the reassembled message.
1105 copy_some_headers (FILE *out
, CT ct
)
1109 hp
= ct
->c_first_hf
; /* start at first header field */
1113 * A few of the header fields of the enclosing
1114 * messages are not copied.
1116 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1117 && strcasecmp (hp
->name
, VRSN_FIELD
)
1118 && strcasecmp (hp
->name
, "Subject")
1119 && strcasecmp (hp
->name
, "Encrypted")
1120 && strcasecmp (hp
->name
, "Message-ID"))
1121 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1122 hp
= hp
->next
; /* next header field */