]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
d0977c9d140c4e6d734e6c6fe6e32b1ef1316205
1 /* mhstore.c -- 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/seq_setcur.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/readconfig.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
25 #include "h/signals.h"
30 #include "h/mhparse.h"
31 #include "h/mhcachesbr.h"
35 #include "sbr/m_maildir.h"
38 #define MHSTORE_SWITCHES \
39 X("auto", 0, AUTOSW) \
40 X("noauto", 0, NAUTOSW) \
41 X("check", 0, CHECKSW) \
42 X("nocheck", 0, NCHECKSW) \
43 X("verbose", 0, VERBSW) \
44 X("noverbose", 0, NVERBSW) \
45 X("file file", 0, FILESW) /* interface from show */ \
46 X("outfile outfile", 0, OUTFILESW) \
47 X("part number", 0, PARTSW) \
48 X("type content", 0, TYPESW) \
49 X("prefer content", 0, PREFERSW) \
50 X("noprefer", 0, NPREFERSW) \
51 X("rcache policy", 0, RCACHESW) \
52 X("wcache policy", 0, WCACHESW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
55 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
56 X("debug", -5, DEBUGSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(MHSTORE
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
67 #define quitser pipeser
75 static void pipeser (int);
79 main (int argc
, char **argv
)
81 int msgnum
, *icachesw
;
83 /* verbosw defaults to 1 for backward compatibility. */
85 const char *clobbersw
= "always";
86 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
87 char *maildir
, buf
[100], **argp
;
90 struct msgs_array msgs
= { 0, 0, NULL
};
91 struct msgs
*mp
= NULL
;
94 int files_not_clobbered
;
97 if (nmh_init(argv
[0], true, true)) { return 1; }
99 set_done(freects_done
);
101 arguments
= getarguments (invo_name
, argc
, argv
, 1);
107 while ((cp
= *argp
++)) {
109 switch (smatch (++cp
, switches
)) {
111 ambigsw (cp
, switches
);
114 die("-%s unknown", cp
);
117 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
119 print_help (buf
, switches
, 1);
122 print_version(invo_name
);
133 icachesw
= &rcachesw
;
136 icachesw
= &wcachesw
;
138 if (!(cp
= *argp
++) || *cp
== '-')
139 die("missing argument to %s", argp
[-2]);
140 switch (*icachesw
= smatch (cp
, cache_policy
)) {
142 ambigsw (cp
, cache_policy
);
145 die("%s unknown", cp
);
159 if (!(cp
= *argp
++) || *cp
== '-')
160 die("missing argument to %s", argp
[-2]);
162 die("too many parts (starting with %s), %d max",
168 if (!(cp
= *argp
++) || *cp
== '-')
169 die("missing argument to %s", argp
[-2]);
171 die("too many types (starting with %s), %d max",
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 if (npreferred
>= NPREFS
)
180 die("too many preferred types (starting with %s), %d max",
182 mime_preference
[npreferred
].type
= cp
;
183 cp
= strchr(cp
, '/');
184 if (cp
) *cp
++ = '\0';
185 mime_preference
[npreferred
++].subtype
= cp
;
193 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
194 die("missing argument to %s", argp
[-2]);
195 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
199 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
200 die("missing argument to %s", argp
[-2]);
201 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
211 if (!(cp
= *argp
++) || *cp
== '-')
212 die("missing argument to %s", argp
[-2]);
220 if (*cp
== '+' || *cp
== '@') {
222 die("only one folder at a time!");
223 folder
= pluspath (cp
);
225 app_msgarg(&msgs
, cp
);
228 /* null terminate the list of acceptable parts/types */
233 * Check if we've specified an additional profile
235 if ((cp
= getenv ("MHSTORE"))) {
236 if ((fp
= fopen (cp
, "r"))) {
237 readconfig(NULL
, fp
, cp
, 0);
240 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
245 * Read the standard profile setup
247 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
248 readconfig(NULL
, fp
, cp
, 0);
252 /* Check for public cache location */
253 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
256 /* Check for private cache location */
257 if (!(cache_private
= context_find (nmhprivcache
)))
258 cache_private
= ".cache";
259 cache_private
= mh_xstrdup(m_maildir(cache_private
));
262 * Cache the current directory before we do any chdirs()'s.
264 cwd
= mh_xstrdup(pwd());
266 if (!context_find ("path"))
267 free (path ("./", TFOLDER
));
269 if (file
&& msgs
.size
)
270 die("cannot specify msg and file at same time!");
273 * check if message is coming from file
276 cts
= mh_xcalloc(2, sizeof *cts
);
279 if ((ct
= parse_mime (file
))) {
282 ct
->c_storage
= mh_xstrdup(outfile
);
287 * message(s) are coming from a folder
290 app_msgarg(&msgs
, "cur");
292 folder
= getfolder (1);
293 maildir
= m_maildir (folder
);
295 if (chdir (maildir
) == NOTOK
)
296 adios (maildir
, "unable to change directory to");
298 /* read folder and create message structure */
299 if (!(mp
= folder_read (folder
, 1)))
300 die("unable to read folder %s", folder
);
302 /* check for empty folder */
304 die("no messages in %s", folder
);
306 /* parse all the message ranges/sequences and set SELECTED */
307 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
308 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
310 seq_setprev (mp
); /* set the previous-sequence */
312 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
315 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
316 if (is_selected(mp
, msgnum
)) {
319 msgnam
= m_name (msgnum
);
320 if ((ct
= parse_mime (msgnam
))) {
323 ct
->c_storage
= mh_xstrdup(outfile
);
334 SIGNAL (SIGQUIT
, quitser
);
335 SIGNAL (SIGPIPE
, pipeser
);
338 * Get the associated umask for the relevant contents.
340 for (ctp
= cts
; *ctp
; ctp
++) {
344 if (type_ok (ct
, 1) && !ct
->c_umask
) {
345 if (stat (ct
->c_file
, &st
) != NOTOK
)
346 ct
->c_umask
= ~(st
.st_mode
& 0777);
348 ct
->c_umask
= ~m_gmprot();
353 * Store the message content
355 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
356 store_all_messages (info
);
357 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
358 mhstoreinfo_free(info
);
360 /* Now free all the structures for the content */
361 for (ctp
= cts
; *ctp
; ctp
++)
367 /* If reading from a folder, do some updating */
369 context_replace (pfolder
, folder
);/* update current folder */
370 seq_setcur (mp
, mp
->hghsel
); /* update current message */
371 seq_save (mp
); /* synchronize sequences */
372 context_save (); /* save the context file */
375 done (files_not_clobbered
);
385 fprintf (stderr
, "\n");