]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/readconfig.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
29 #include "h/signals.h"
34 #include "h/mhparse.h"
35 #include "h/mhcachesbr.h"
39 #include "sbr/m_maildir.h"
42 #define MHSTORE_SWITCHES \
43 X("auto", 0, AUTOSW) \
44 X("noauto", 0, NAUTOSW) \
45 X("check", 0, CHECKSW) \
46 X("nocheck", 0, NCHECKSW) \
47 X("verbose", 0, VERBSW) \
48 X("noverbose", 0, NVERBSW) \
49 X("file file", 0, FILESW) /* interface from show */ \
50 X("outfile outfile", 0, OUTFILESW) \
51 X("part number", 0, PARTSW) \
52 X("type content", 0, TYPESW) \
53 X("prefer content", 0, PREFERSW) \
54 X("noprefer", 0, NPREFERSW) \
55 X("rcache policy", 0, RCACHESW) \
56 X("wcache policy", 0, WCACHESW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
60 X("debug", -5, DEBUGSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHSTORE
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
71 #define quitser pipeser
79 static void pipeser (int);
83 main (int argc
, char **argv
)
85 int msgnum
, *icachesw
;
87 /* verbosw defaults to 1 for backward compatibility. */
89 const char *clobbersw
= "always";
90 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
91 char *maildir
, buf
[100], **argp
;
94 struct msgs_array msgs
= { 0, 0, NULL
};
95 struct msgs
*mp
= NULL
;
98 int files_not_clobbered
;
101 if (nmh_init(argv
[0], true, true)) { return 1; }
103 set_done(freects_done
);
105 arguments
= getarguments (invo_name
, argc
, argv
, 1);
111 while ((cp
= *argp
++)) {
113 switch (smatch (++cp
, switches
)) {
115 ambigsw (cp
, switches
);
118 die("-%s unknown", cp
);
121 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
123 print_help (buf
, switches
, 1);
126 print_version(invo_name
);
137 icachesw
= &rcachesw
;
140 icachesw
= &wcachesw
;
142 if (!(cp
= *argp
++) || *cp
== '-')
143 die("missing argument to %s", argp
[-2]);
144 switch (*icachesw
= smatch (cp
, cache_policy
)) {
146 ambigsw (cp
, cache_policy
);
149 die("%s unknown", cp
);
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
166 die("too many parts (starting with %s), %d max",
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
175 die("too many types (starting with %s), %d max",
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 if (npreferred
>= NPREFS
)
184 die("too many preferred types (starting with %s), %d max",
186 mime_preference
[npreferred
].type
= cp
;
187 cp
= strchr(cp
, '/');
188 if (cp
) *cp
++ = '\0';
189 mime_preference
[npreferred
++].subtype
= cp
;
197 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
198 die("missing argument to %s", argp
[-2]);
199 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
203 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 die("missing argument to %s", argp
[-2]);
205 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
215 if (!(cp
= *argp
++) || *cp
== '-')
216 die("missing argument to %s", argp
[-2]);
224 if (*cp
== '+' || *cp
== '@') {
226 die("only one folder at a time!");
227 folder
= pluspath (cp
);
229 app_msgarg(&msgs
, cp
);
232 /* null terminate the list of acceptable parts/types */
237 * Check if we've specified an additional profile
239 if ((cp
= getenv ("MHSTORE"))) {
240 if ((fp
= fopen (cp
, "r"))) {
241 readconfig(NULL
, fp
, cp
, 0);
244 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
249 * Read the standard profile setup
251 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
252 readconfig(NULL
, fp
, cp
, 0);
256 /* Check for public cache location */
257 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
260 /* Check for private cache location */
261 if (!(cache_private
= context_find (nmhprivcache
)))
262 cache_private
= ".cache";
263 cache_private
= mh_xstrdup(m_maildir(cache_private
));
266 * Cache the current directory before we do any chdirs()'s.
268 cwd
= mh_xstrdup(pwd());
270 if (!context_find ("path"))
271 free (path ("./", TFOLDER
));
273 if (file
&& msgs
.size
)
274 die("cannot specify msg and file at same time!");
277 * check if message is coming from file
280 cts
= mh_xcalloc(2, sizeof *cts
);
283 if ((ct
= parse_mime (file
))) {
286 ct
->c_storage
= mh_xstrdup(outfile
);
291 * message(s) are coming from a folder
294 app_msgarg(&msgs
, "cur");
296 folder
= getfolder (1);
297 maildir
= m_maildir (folder
);
299 if (chdir (maildir
) == NOTOK
)
300 adios (maildir
, "unable to change directory to");
302 /* read folder and create message structure */
303 if (!(mp
= folder_read (folder
, 1)))
304 die("unable to read folder %s", folder
);
306 /* check for empty folder */
308 die("no messages in %s", folder
);
310 /* parse all the message ranges/sequences and set SELECTED */
311 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
312 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
314 seq_setprev (mp
); /* set the previous-sequence */
316 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
319 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
320 if (is_selected(mp
, msgnum
)) {
323 msgnam
= m_name (msgnum
);
324 if ((ct
= parse_mime (msgnam
))) {
327 ct
->c_storage
= mh_xstrdup(outfile
);
338 SIGNAL (SIGQUIT
, quitser
);
339 SIGNAL (SIGPIPE
, pipeser
);
342 * Get the associated umask for the relevant contents.
344 for (ctp
= cts
; *ctp
; ctp
++) {
348 if (type_ok (ct
, 1) && !ct
->c_umask
) {
349 if (stat (ct
->c_file
, &st
) != NOTOK
)
350 ct
->c_umask
= ~(st
.st_mode
& 0777);
352 ct
->c_umask
= ~m_gmprot();
357 * Store the message content
359 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
360 store_all_messages (info
);
361 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
362 mhstoreinfo_free(info
);
364 /* Now free all the structures for the content */
365 for (ctp
= cts
; *ctp
; ctp
++)
371 /* If reading from a folder, do some updating */
373 context_replace (pfolder
, folder
);/* update current folder */
374 seq_setcur (mp
, mp
->hghsel
); /* update current message */
375 seq_save (mp
); /* synchronize sequences */
376 context_save (); /* save the context file */
379 done (files_not_clobbered
);
389 fprintf (stderr
, "\n");