]>
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/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/readconfig.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
20 #include "h/signals.h"
25 #include "h/mhparse.h"
26 #include "h/mhcachesbr.h"
30 #include "sbr/m_maildir.h"
33 #define MHSTORE_SWITCHES \
34 X("auto", 0, AUTOSW) \
35 X("noauto", 0, NAUTOSW) \
36 X("check", 0, CHECKSW) \
37 X("nocheck", 0, NCHECKSW) \
38 X("verbose", 0, VERBSW) \
39 X("noverbose", 0, NVERBSW) \
40 X("file file", 0, FILESW) /* interface from show */ \
41 X("outfile outfile", 0, OUTFILESW) \
42 X("part number", 0, PARTSW) \
43 X("type content", 0, TYPESW) \
44 X("prefer content", 0, PREFERSW) \
45 X("noprefer", 0, NPREFERSW) \
46 X("rcache policy", 0, RCACHESW) \
47 X("wcache policy", 0, WCACHESW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
51 X("debug", -5, DEBUGSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(MHSTORE
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
62 #define quitser pipeser
70 static void pipeser (int);
74 main (int argc
, char **argv
)
76 int msgnum
, *icachesw
;
78 /* verbosw defaults to 1 for backward compatibility. */
80 const char *clobbersw
= "always";
81 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
82 char *maildir
, buf
[100], **argp
;
85 struct msgs_array msgs
= { 0, 0, NULL
};
86 struct msgs
*mp
= NULL
;
89 int files_not_clobbered
;
92 if (nmh_init(argv
[0], true, true)) { return 1; }
94 set_done(freects_done
);
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
109 die("-%s unknown", cp
);
112 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
114 print_help (buf
, switches
, 1);
117 print_version(invo_name
);
128 icachesw
= &rcachesw
;
131 icachesw
= &wcachesw
;
133 if (!(cp
= *argp
++) || *cp
== '-')
134 die("missing argument to %s", argp
[-2]);
135 switch (*icachesw
= smatch (cp
, cache_policy
)) {
137 ambigsw (cp
, cache_policy
);
140 die("%s unknown", cp
);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 die("missing argument to %s", argp
[-2]);
157 die("too many parts (starting with %s), %d max",
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
166 die("too many types (starting with %s), %d max",
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
174 if (npreferred
>= NPREFS
)
175 die("too many preferred types (starting with %s), %d max",
177 mime_preference
[npreferred
].type
= cp
;
178 cp
= strchr(cp
, '/');
179 if (cp
) *cp
++ = '\0';
180 mime_preference
[npreferred
++].subtype
= cp
;
188 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
189 die("missing argument to %s", argp
[-2]);
190 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 die("missing argument to %s", argp
[-2]);
196 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
215 if (*cp
== '+' || *cp
== '@') {
217 die("only one folder at a time!");
218 folder
= pluspath (cp
);
220 app_msgarg(&msgs
, cp
);
223 /* null terminate the list of acceptable parts/types */
228 * Check if we've specified an additional profile
230 if ((cp
= getenv ("MHSTORE"))) {
231 if ((fp
= fopen (cp
, "r"))) {
232 readconfig(NULL
, fp
, cp
, 0);
235 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
240 * Read the standard profile setup
242 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
243 readconfig(NULL
, fp
, cp
, 0);
247 /* Check for public cache location */
248 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
251 /* Check for private cache location */
252 if (!(cache_private
= context_find (nmhprivcache
)))
253 cache_private
= ".cache";
254 cache_private
= mh_xstrdup(m_maildir(cache_private
));
257 * Cache the current directory before we do any chdirs()'s.
259 cwd
= mh_xstrdup(pwd());
261 if (!context_find ("path"))
262 free (path ("./", TFOLDER
));
264 if (file
&& msgs
.size
)
265 die("cannot specify msg and file at same time!");
268 * check if message is coming from file
271 cts
= mh_xcalloc(2, sizeof *cts
);
274 if ((ct
= parse_mime (file
))) {
277 ct
->c_storage
= mh_xstrdup(outfile
);
282 * message(s) are coming from a folder
285 app_msgarg(&msgs
, "cur");
287 folder
= getfolder (1);
288 maildir
= m_maildir (folder
);
290 if (chdir (maildir
) == NOTOK
)
291 adios (maildir
, "unable to change directory to");
293 /* read folder and create message structure */
294 if (!(mp
= folder_read (folder
, 1)))
295 die("unable to read folder %s", folder
);
297 /* check for empty folder */
299 die("no messages in %s", folder
);
301 /* parse all the message ranges/sequences and set SELECTED */
302 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
303 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
305 seq_setprev (mp
); /* set the previous-sequence */
307 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
310 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
311 if (is_selected(mp
, msgnum
)) {
314 msgnam
= m_name (msgnum
);
315 if ((ct
= parse_mime (msgnam
))) {
318 ct
->c_storage
= mh_xstrdup(outfile
);
329 SIGNAL (SIGQUIT
, quitser
);
330 SIGNAL (SIGPIPE
, pipeser
);
333 * Get the associated umask for the relevant contents.
335 for (ctp
= cts
; *ctp
; ctp
++) {
339 if (type_ok (ct
, 1) && !ct
->c_umask
) {
340 if (stat (ct
->c_file
, &st
) != NOTOK
)
341 ct
->c_umask
= ~(st
.st_mode
& 0777);
343 ct
->c_umask
= ~m_gmprot();
348 * Store the message content
350 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
351 store_all_messages (info
);
352 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
353 mhstoreinfo_free(info
);
355 /* Now free all the structures for the content */
356 for (ctp
= cts
; *ctp
; ctp
++)
362 /* If reading from a folder, do some updating */
364 context_replace (pfolder
, folder
);/* update current folder */
365 seq_setcur (mp
, mp
->hghsel
); /* update current message */
366 seq_save (mp
); /* synchronize sequences */
367 context_save (); /* save the context file */
370 done (files_not_clobbered
);
380 fprintf (stderr
, "\n");