1 /* mhstoresbr.c -- routines to save/store the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/read_switch_multiword.h"
10 #include "sbr/concat.h"
11 #include "sbr/smatch.h"
12 #include "sbr/r1bindex.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/folder_addmsg.h"
19 #include "sbr/context_find.h"
21 #include "sbr/error.h"
25 #include "h/fmt_scan.h"
27 #include "h/mhparse.h"
30 #include "mhshowsbr.h"
31 #include "sbr/m_maildir.h"
32 #include "sbr/m_mktemp.h"
34 enum clobber_policy_t
{
35 NMH_CLOBBER_ALWAYS
= 0,
42 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
45 CT
*cts
; /* Top-level list of contents to store. */
46 char *cwd
; /* cached current directory */
47 int autosw
; /* -auto enabled */
48 int verbosw
; /* -verbose enabled */
49 int files_not_clobbered
; /* output flag indicating that store failed
50 in order to not clobber an existing file */
52 /* The following must never be touched by a caller: they are for
53 internal use by the mhstoresbr functions. */
54 char *dir
; /* directory in which to store contents */
55 enum clobber_policy_t clobber_policy
; /* -clobber selection */
58 static bool use_param_as_filename(const char *p
);
61 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
70 info
->files_not_clobbered
= 0;
72 info
->clobber_policy
= clobber_policy (csw
);
78 mhstoreinfo_free (mhstoreinfo_t info
)
86 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
88 return info
->files_not_clobbered
;
93 * Type for a compare function for qsort. This keeps
96 typedef int (*qsort_comp
) (const void *, const void *);
102 static void store_single_message (CT
, mhstoreinfo_t
);
103 static int store_switch (CT
, mhstoreinfo_t
);
104 static int store_generic (CT
, mhstoreinfo_t
);
105 static int store_application (CT
, mhstoreinfo_t
);
106 static int store_multi (CT
, mhstoreinfo_t
);
107 static int store_external (CT
, mhstoreinfo_t
);
108 static int store_content (CT
, mhstoreinfo_t
);
109 static int output_content_file (CT
, int);
110 static int output_content_folder (char *, char *);
111 static int parse_format_string (CT
, char *, char *, int, char *);
112 static void get_storeproc (CT
);
113 static char *clobber_check (char *, mhstoreinfo_t
);
116 * Main entry point to store content
117 * from a collection of messages.
121 store_all_messages (mhstoreinfo_t info
)
127 * Check for the directory in which to
128 * store any contents.
130 if ((cp
= context_find (nmhstorage
)) && *cp
)
131 info
->dir
= mh_xstrdup(cp
);
133 info
->dir
= getcpy (info
->cwd
);
135 for (ctp
= info
->cts
; *ctp
; ctp
++) {
137 store_single_message (ct
, info
);
145 * Entry point to store the content
146 * in a (single) message
150 store_single_message (CT ct
, mhstoreinfo_t info
)
152 if (type_ok (ct
, 1)) {
154 store_switch (ct
, info
);
159 if (ct
->c_ceclosefnx
)
160 (*ct
->c_ceclosefnx
) (ct
);
166 * Switching routine to store different content types
170 store_switch (CT ct
, mhstoreinfo_t info
)
172 switch (ct
->c_type
) {
174 return store_multi (ct
, info
);
177 switch (ct
->c_subtype
) {
178 case MESSAGE_EXTERNAL
:
179 return store_external (ct
, info
);
183 return store_generic (ct
, info
);
188 return store_application (ct
, info
);
194 return store_generic (ct
, info
);
197 return OK
; /* NOT REACHED */
202 * Generic routine to store a MIME content.
203 * (audio, video, image, text, message/rfc822)
207 store_generic (CT ct
, mhstoreinfo_t info
)
210 * Check if the content specifies a filename.
211 * Don't bother with this for type "message"
212 * (only "message/rfc822" will use store_generic).
214 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
217 return store_content (ct
, info
);
222 * Store content of type "application"
226 store_application (CT ct
, mhstoreinfo_t info
)
228 CI ci
= &ct
->c_ctinfo
;
230 /* Check if the content specifies a filename */
235 * If storeproc is not defined, and the content is type
236 * "application/octet-stream", we also check for various
237 * attribute/value pairs which specify if this a tar file.
239 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
245 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
246 if (strcasecmp (cp
, "tar") == 0)
250 /* check for "conversions=compress" attribute */
251 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
252 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
253 if (strcasecmp (cp
, "compress") == 0 ||
254 strcasecmp (cp
, "x-compress") == 0) {
257 if (strcasecmp (cp
, "gzip") == 0 ||
258 strcasecmp (cp
, "x-gzip") == 0) {
264 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
265 : (gzP
? "%egzip -dc | tar tvf -"
266 : "%etar tvf -"), NULL
);
267 if (!ct
->c_storeproc
) {
269 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
270 : (gzP
? "| gzip -dc | tar xvpf -"
271 : "| tar xvpf -"), NULL
);
274 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
275 : (gzP
? "%m%P.tar.gz"
276 : "%m%P.tar"), NULL
);
282 return store_content (ct
, info
);
287 * Store the content of a multipart message
291 store_multi (CT ct
, mhstoreinfo_t info
)
294 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
298 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
299 CT p
= part
->mp_part
;
301 if (part_ok (p
) && type_ok (p
, 1)) {
303 /* Support mhstore -outfile. The MIME parser doesn't
304 load c_storage, so we know that p->c_storage is
306 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
308 result
= store_switch (p
, info
);
310 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
320 * Store content from a message of type "message/external".
324 store_external (CT ct
, mhstoreinfo_t info
)
327 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
328 CT p
= e
->eb_content
;
334 * Check if the parameters for the external body
335 * specified a filename.
340 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
341 if (!ct
->c_storeproc
)
342 ct
->c_storeproc
= mh_xstrdup(cp
);
344 p
->c_storeproc
= mh_xstrdup(cp
);
349 * Since we will let the Content structure for the
350 * external body substitute for the current content,
351 * we temporarily change its partno (number inside
352 * multipart), so everything looks right.
354 p
->c_partno
= ct
->c_partno
;
356 /* we probably need to check if content is really there */
358 /* Support mhstore -outfile. The MIME parser doesn't load
359 c_storage, so we know that p->c_storage is NULL here. */
360 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
362 result
= store_switch (p
, info
);
370 * Store contents of a message or message part to
371 * a folder, a file, the standard output, or pass
372 * the contents to a command.
376 store_content (CT ct
, mhstoreinfo_t info
)
378 bool appending
= false;
380 char *cp
, buffer
[BUFSIZ
];
383 * Get storage formatting string.
385 * 1) If we have storeproc defined, then use that
386 * 2) Else check for a mhn-store-<type>/<subtype> entry
387 * 3) Else check for a mhn-store-<type> entry
388 * 4) Else if content is "message", use "+" (current folder)
389 * 5) Else use string "%m%P.%s".
391 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
392 CI ci
= &ct
->c_ctinfo
;
394 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
396 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
400 if (! ct
->c_storage
) {
402 * Check the beginning of storage formatting string
403 * to see if we are saving content to a folder.
405 if (*cp
== '+' || *cp
== '@') {
406 char *tmpfilenam
, *folder
;
408 /* Store content in temporary file for now */
409 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
410 die("unable to create temporary file in %s",
413 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
415 /* Get the folder name */
417 folder
= pluspath (cp
);
419 folder
= getfolder (1);
421 /* Check if folder exists */
422 create_folder(m_mailpath(folder
), 0, exit
);
424 /* Record the folder name */
425 ct
->c_folder
= mh_xstrdup(folder
);
434 * Parse and expand the storage formatting string
435 * in `cp' into `buffer'.
437 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
440 * If formatting begins with '|' or '!', then pass
441 * content to standard input of a command and return.
443 if (buffer
[0] == '|' || buffer
[0] == '!')
444 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
446 /* record the filename */
447 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
452 /* The output filename was explicitly specified, so use it. */
453 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
460 /* flush the output stream */
463 /* Now save or append the content to a file */
464 if (output_content_file (ct
, appending
) == NOTOK
)
468 * If necessary, link the file into a folder and remove
469 * the temporary file.
472 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
473 (void) m_unlink (ct
->c_storage
);
480 * Now print out the name/number of the message
481 * that we are storing.
483 fprintf (stderr
, "storing message %s", ct
->c_file
);
485 fprintf (stderr
, " part %s", ct
->c_partno
);
488 * We now print the name of the file, folder, and/or message
489 * to which we are storing the content.
492 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
494 } else if (!strcmp(ct
->c_storage
, "-")) {
495 fprintf (stderr
, " to stdout\n");
497 int cwdlen
= strlen (info
->cwd
);
499 fprintf (stderr
, " as file %s\n",
500 !has_prefix(ct
->c_storage
, info
->cwd
)
501 || ct
->c_storage
[cwdlen
] != '/'
502 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
511 * Output content to a file
515 output_content_file (CT ct
, int appending
)
517 char *file
, buffer
[BUFSIZ
];
522 * If the pathname is absolute, make sure
523 * all the relevant directories exist.
525 if (strchr(ct
->c_storage
, '/')
526 && make_intermediates (ct
->c_storage
) == NOTOK
)
529 if (ct
->c_encoding
!= CE_7BIT
) {
532 if (!ct
->c_ceopenfnx
) {
533 inform("don't know how to decode part %s of message %s",
534 ct
->c_partno
, ct
->c_file
);
538 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
540 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
542 if (!strcmp (file
, ct
->c_storage
)) {
543 (*ct
->c_ceclosefnx
) (ct
);
548 * Send to standard output
550 if (!strcmp (ct
->c_storage
, "-")) {
553 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
554 advise ("stdout", "unable to dup");
556 (*ct
->c_ceclosefnx
) (ct
);
559 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
560 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
561 appending
? "a" : "w");
569 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
570 advise (ct
->c_storage
, "unable to fopen for %s",
571 appending
? "appending" : "writing");
577 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
579 advise (file
, "error reading content from");
586 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
587 advise ("output_content_file", "fwrite");
594 (*ct
->c_ceclosefnx
) (ct
);
596 if (cc
!= NOTOK
&& fflush (fp
))
597 advise (ct
->c_storage
, "error writing to");
601 return cc
== NOTOK
? NOTOK
: OK
;
604 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
605 advise (ct
->c_file
, "unable to open for reading");
611 fseek (ct
->c_fp
, pos
, SEEK_SET
);
613 if (!strcmp (ct
->c_storage
, "-")) {
616 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
617 advise ("stdout", "unable to dup");
620 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
621 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
622 appending
? "a" : "w");
627 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
628 advise (ct
->c_storage
, "unable to fopen for %s",
629 appending
? "appending" : "writing");
634 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
635 if ((pos
+= strlen (buffer
)) > last
) {
638 diff
= strlen (buffer
) - (pos
- last
);
648 advise (ct
->c_storage
, "error writing to");
658 * Add a file to a folder.
660 * Return the new message number of the file
661 * when added to the folder. Return -1, if
666 output_content_folder (char *folder
, char *filename
)
671 /* Read the folder. */
672 if (!(mp
= folder_read(folder
, 0))) {
673 inform("unable to read folder %s", folder
);
676 /* Link file into folder */
677 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
679 /* free folder structure */
683 * Return msgnum. We are relying on the fact that
684 * msgnum will be -1, if folder_addmsg() had an error.
691 * Parse and expand the storage formatting string
692 * pointed to by "cp" into "buffer".
696 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
700 CI ci
= &ct
->c_ctinfo
;
703 * If storage string is "-", just copy it, and
704 * return (send content to standard output).
706 if (cp
[0] == '-' && cp
[1] == '\0') {
707 strncpy (buffer
, cp
, buflen
- 1);
715 * If formatting string is a pathname that doesn't
716 * begin with '/', then preface the path with the
717 * appropriate directory.
719 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
720 if (!strcmp(dir
, "/"))
721 dir
= ""; /* Don't start with "//". */
722 snprintf (bp
, buflen
, "%s/", dir
);
730 /* We are processing a storage escape */
735 * Insert parameters from Content-Type.
736 * This is only valid for '|' commands.
738 if (buffer
[0] != '|' && buffer
[0] != '!') {
748 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
749 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
750 pm
->pm_name
, get_param_value(pm
, '?'));
760 /* insert message number */
761 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
765 /* insert part number with leading dot */
767 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
771 /* insert part number without leading dot */
773 strncpy (bp
, ct
->c_partno
, buflen
);
777 /* insert content type */
778 strncpy (bp
, ci
->ci_type
, buflen
);
782 /* insert content subtype */
783 strncpy (bp
, ci
->ci_subtype
, buflen
);
787 /* insert the character % */
797 /* Advance bp and decrement buflen */
815 * Check if the content specifies a filename
816 * in its MIME parameters.
820 get_storeproc (CT ct
)
826 * If the storeproc has already been defined,
827 * we just return (for instance, if this content
828 * is part of a "message/external".
834 * If there's a Content-Disposition header and it has a filename,
835 * use that (RFC-2183).
838 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
839 use_param_as_filename(cp
)) {
840 ct
->c_storeproc
= mh_xstrdup(cp
);
848 * Check the attribute/value pairs, for the attribute "name".
849 * If found, do a few sanity checks and copy the value into
853 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
854 use_param_as_filename(cp
)) {
855 ct
->c_storeproc
= mh_xstrdup(cp
);
862 /******************************************************************************/
863 /* -clobber support */
865 static enum clobber_policy_t
866 clobber_policy (const char *value
)
868 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
869 return NMH_CLOBBER_ALWAYS
;
871 if (! strcasecmp (value
, "auto")) {
872 return NMH_CLOBBER_AUTO
;
874 if (! strcasecmp (value
, "suffix")) {
875 return NMH_CLOBBER_SUFFIX
;
877 if (! strcasecmp (value
, "ask")) {
878 return NMH_CLOBBER_ASK
;
880 if (! strcasecmp (value
, "never")) {
881 return NMH_CLOBBER_NEVER
;
884 die("invalid argument, %s, to clobber", value
);
889 next_version (char *file
, enum clobber_policy_t clobber_policy
)
891 const size_t max_versions
= 1000000;
892 /* 8 = log max_versions + one for - or . + one for null terminator */
893 const size_t buflen
= strlen (file
) + 8;
894 char *buffer
= mh_xmalloc (buflen
);
897 char *extension
= NULL
;
898 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
899 ((extension
= strrchr (file
, '.')) != NULL
)) {
903 for (version
= 1; version
< max_versions
; ++version
) {
906 switch (clobber_policy
) {
907 case NMH_CLOBBER_AUTO
: {
908 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
909 extension
== NULL
? "" : ".",
910 extension
== NULL
? "" : extension
);
914 case NMH_CLOBBER_SUFFIX
:
915 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
919 /* Should never get here. */
920 inform("will not overwrite %s, invalid clobber policy", buffer
);
925 /* Actually (try to) create the file here to avoid a race
926 condition on file naming + creation. This won't solve the
927 problem with old NFS that doesn't support O_EXCL, though.
928 Let the umask strip off permissions from 0666 as desired.
929 That's what fopen () would do if it was creating the file. */
930 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
931 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
932 S_IROTH
| S_IWOTH
)) >= 0) {
940 if (version
>= max_versions
) {
941 inform("will not overwrite %s, too many versions", buffer
);
951 clobber_check (char *original_file
, mhstoreinfo_t info
)
953 /* clobber policy return value
954 * -------------- ------------
955 * -always original_file
956 * -auto original_file-<digits>.extension
957 * -suffix original_file.<digits>
958 * -ask original_file, 0, or another filename/path
966 if (! strcmp (original_file
, "-")) {
967 return original_file
;
970 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
971 /* Save cwd for possible use in loop below. */
974 cwd
= mh_xstrdup(original_file
);
975 slash
= strrchr (cwd
, '/');
980 /* original_file isn't a full path, which should only happen if
990 file
= original_file
;
993 switch (info
->clobber_policy
) {
994 case NMH_CLOBBER_ALWAYS
:
997 case NMH_CLOBBER_SUFFIX
:
998 case NMH_CLOBBER_AUTO
:
999 if (stat (file
, &st
) == OK
) {
1000 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1002 ++info
->files_not_clobbered
;
1007 case NMH_CLOBBER_ASK
:
1008 if (stat (file
, &st
) == OK
) {
1009 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1010 static struct swit answer
[4] = {
1011 { "yes", 0, NMH_YES
},
1012 { "no", 0, NMH_NO
},
1013 { "rename", 0, NMH_RENAME
},
1017 if (isatty (fileno (stdin
))) {
1019 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1020 ans
= read_switch_multiword (prompt
, answer
);
1023 /* Overwrite, that's what nmh used to do. And warn. */
1024 inform("-clobber ask but no tty, so overwrite %s", file
);
1028 switch ((enum answers
) smatch (*ans
, answer
)) {
1034 ++info
->files_not_clobbered
;
1038 fputs("Enter filename or full path of the new file: ", stdout
);
1039 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1042 ++info
->files_not_clobbered
;
1044 trim_suffix_c(buf
, '\n');
1049 if (buf
[0] == '/') {
1050 /* Full path, use it. */
1051 file
= mh_xstrdup(buf
);
1053 /* Relative path. */
1054 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1064 case NMH_CLOBBER_NEVER
:
1065 if (stat (file
, &st
) == OK
) {
1066 /* Keep count of files that would have been clobbered,
1067 and return that as process exit status. */
1068 inform("will not overwrite %s with -clobber never", file
);
1071 ++info
->files_not_clobbered
;
1076 original_file
= file
;
1077 } while (check_again
);
1085 use_param_as_filename(const char *p
)
1087 /* Preserve result of original test that considered an empty string
1089 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1092 /* -clobber support */
1093 /******************************************************************************/