]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
92cff1b9dc17935f32331142348eb4c8aa428e7a
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
13 #include "h/signals.h"
18 #include "h/mhparse.h"
19 #include "h/mhcachesbr.h"
23 #include "sbr/m_maildir.h"
26 #define MHSTORE_SWITCHES \
27 X("auto", 0, AUTOSW) \
28 X("noauto", 0, NAUTOSW) \
29 X("check", 0, CHECKSW) \
30 X("nocheck", 0, NCHECKSW) \
31 X("verbose", 0, VERBSW) \
32 X("noverbose", 0, NVERBSW) \
33 X("file file", 0, FILESW) /* interface from show */ \
34 X("outfile outfile", 0, OUTFILESW) \
35 X("part number", 0, PARTSW) \
36 X("type content", 0, TYPESW) \
37 X("prefer content", 0, PREFERSW) \
38 X("noprefer", 0, NPREFERSW) \
39 X("rcache policy", 0, RCACHESW) \
40 X("wcache policy", 0, WCACHESW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
44 X("debug", -5, DEBUGSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(MHSTORE
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
55 #define quitser pipeser
63 static void pipeser (int);
67 main (int argc
, char **argv
)
69 int msgnum
, *icachesw
;
71 /* verbosw defaults to 1 for backward compatibility. */
73 const char *clobbersw
= "always";
74 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
75 char *maildir
, buf
[100], **argp
;
78 struct msgs_array msgs
= { 0, 0, NULL
};
79 struct msgs
*mp
= NULL
;
82 int files_not_clobbered
;
85 if (nmh_init(argv
[0], true, true)) { return 1; }
87 set_done(freects_done
);
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
102 die("-%s unknown", cp
);
105 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
121 icachesw
= &rcachesw
;
124 icachesw
= &wcachesw
;
126 if (!(cp
= *argp
++) || *cp
== '-')
127 die("missing argument to %s", argp
[-2]);
128 switch (*icachesw
= smatch (cp
, cache_policy
)) {
130 ambigsw (cp
, cache_policy
);
133 die("%s unknown", cp
);
147 if (!(cp
= *argp
++) || *cp
== '-')
148 die("missing argument to %s", argp
[-2]);
150 die("too many parts (starting with %s), %d max",
156 if (!(cp
= *argp
++) || *cp
== '-')
157 die("missing argument to %s", argp
[-2]);
159 die("too many types (starting with %s), %d max",
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 if (npreferred
>= NPREFS
)
168 die("too many preferred types (starting with %s), %d max",
170 mime_preference
[npreferred
].type
= cp
;
171 cp
= strchr(cp
, '/');
172 if (cp
) *cp
++ = '\0';
173 mime_preference
[npreferred
++].subtype
= cp
;
181 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
182 die("missing argument to %s", argp
[-2]);
183 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
187 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
188 die("missing argument to %s", argp
[-2]);
189 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
208 if (*cp
== '+' || *cp
== '@') {
210 die("only one folder at a time!");
211 folder
= pluspath (cp
);
213 app_msgarg(&msgs
, cp
);
216 /* null terminate the list of acceptable parts/types */
221 * Check if we've specified an additional profile
223 if ((cp
= getenv ("MHSTORE"))) {
224 if ((fp
= fopen (cp
, "r"))) {
225 readconfig(NULL
, fp
, cp
, 0);
228 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
233 * Read the standard profile setup
235 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
236 readconfig(NULL
, fp
, cp
, 0);
240 /* Check for public cache location */
241 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
244 /* Check for private cache location */
245 if (!(cache_private
= context_find (nmhprivcache
)))
246 cache_private
= ".cache";
247 cache_private
= mh_xstrdup(m_maildir(cache_private
));
250 * Cache the current directory before we do any chdirs()'s.
252 cwd
= mh_xstrdup(pwd());
254 if (!context_find ("path"))
255 free (path ("./", TFOLDER
));
257 if (file
&& msgs
.size
)
258 die("cannot specify msg and file at same time!");
261 * check if message is coming from file
264 cts
= mh_xcalloc(2, sizeof *cts
);
267 if ((ct
= parse_mime (file
))) {
270 ct
->c_storage
= mh_xstrdup(outfile
);
275 * message(s) are coming from a folder
278 app_msgarg(&msgs
, "cur");
280 folder
= getfolder (1);
281 maildir
= m_maildir (folder
);
283 if (chdir (maildir
) == NOTOK
)
284 adios (maildir
, "unable to change directory to");
286 /* read folder and create message structure */
287 if (!(mp
= folder_read (folder
, 1)))
288 die("unable to read folder %s", folder
);
290 /* check for empty folder */
292 die("no messages in %s", folder
);
294 /* parse all the message ranges/sequences and set SELECTED */
295 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
296 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
298 seq_setprev (mp
); /* set the previous-sequence */
300 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
303 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
304 if (is_selected(mp
, msgnum
)) {
307 msgnam
= m_name (msgnum
);
308 if ((ct
= parse_mime (msgnam
))) {
311 ct
->c_storage
= mh_xstrdup(outfile
);
322 SIGNAL (SIGQUIT
, quitser
);
323 SIGNAL (SIGPIPE
, pipeser
);
326 * Get the associated umask for the relevant contents.
328 for (ctp
= cts
; *ctp
; ctp
++) {
332 if (type_ok (ct
, 1) && !ct
->c_umask
) {
333 if (stat (ct
->c_file
, &st
) != NOTOK
)
334 ct
->c_umask
= ~(st
.st_mode
& 0777);
336 ct
->c_umask
= ~m_gmprot();
341 * Store the message content
343 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
344 store_all_messages (info
);
345 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
346 mhstoreinfo_free(info
);
348 /* Now free all the structures for the content */
349 for (ctp
= cts
; *ctp
; ctp
++)
355 /* If reading from a folder, do some updating */
357 context_replace (pfolder
, folder
);/* update current folder */
358 seq_setcur (mp
, mp
->hghsel
); /* update current message */
359 seq_save (mp
); /* synchronize sequences */
360 context_save (); /* save the context file */
363 done (files_not_clobbered
);
373 fprintf (stderr
, "\n");