]>
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/signals.h"
19 #include "h/mhparse.h"
20 #include "h/mhcachesbr.h"
24 #include "sbr/m_maildir.h"
27 #define MHSTORE_SWITCHES \
28 X("auto", 0, AUTOSW) \
29 X("noauto", 0, NAUTOSW) \
30 X("check", 0, CHECKSW) \
31 X("nocheck", 0, NCHECKSW) \
32 X("verbose", 0, VERBSW) \
33 X("noverbose", 0, NVERBSW) \
34 X("file file", 0, FILESW) /* interface from show */ \
35 X("outfile outfile", 0, OUTFILESW) \
36 X("part number", 0, PARTSW) \
37 X("type content", 0, TYPESW) \
38 X("prefer content", 0, PREFERSW) \
39 X("noprefer", 0, NPREFERSW) \
40 X("rcache policy", 0, RCACHESW) \
41 X("wcache policy", 0, WCACHESW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
45 X("debug", -5, DEBUGSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(MHSTORE
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
56 #define quitser pipeser
64 static void pipeser (int);
68 main (int argc
, char **argv
)
70 int msgnum
, *icachesw
;
72 /* verbosw defaults to 1 for backward compatibility. */
74 const char *clobbersw
= "always";
75 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
76 char *maildir
, buf
[100], **argp
;
79 struct msgs_array msgs
= { 0, 0, NULL
};
80 struct msgs
*mp
= NULL
;
83 int files_not_clobbered
;
86 if (nmh_init(argv
[0], true, true)) { return 1; }
88 set_done(freects_done
);
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
122 icachesw
= &rcachesw
;
125 icachesw
= &wcachesw
;
127 if (!(cp
= *argp
++) || *cp
== '-')
128 die("missing argument to %s", argp
[-2]);
129 switch (*icachesw
= smatch (cp
, cache_policy
)) {
131 ambigsw (cp
, cache_policy
);
134 die("%s unknown", cp
);
148 if (!(cp
= *argp
++) || *cp
== '-')
149 die("missing argument to %s", argp
[-2]);
151 die("too many parts (starting with %s), %d max",
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
160 die("too many types (starting with %s), %d max",
166 if (!(cp
= *argp
++) || *cp
== '-')
167 die("missing argument to %s", argp
[-2]);
168 if (npreferred
>= NPREFS
)
169 die("too many preferred types (starting with %s), %d max",
171 mime_preference
[npreferred
].type
= cp
;
172 cp
= strchr(cp
, '/');
173 if (cp
) *cp
++ = '\0';
174 mime_preference
[npreferred
++].subtype
= cp
;
182 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
183 die("missing argument to %s", argp
[-2]);
184 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
188 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
189 die("missing argument to %s", argp
[-2]);
190 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
209 if (*cp
== '+' || *cp
== '@') {
211 die("only one folder at a time!");
212 folder
= pluspath (cp
);
214 app_msgarg(&msgs
, cp
);
217 /* null terminate the list of acceptable parts/types */
222 * Check if we've specified an additional profile
224 if ((cp
= getenv ("MHSTORE"))) {
225 if ((fp
= fopen (cp
, "r"))) {
226 readconfig(NULL
, fp
, cp
, 0);
229 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
234 * Read the standard profile setup
236 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
237 readconfig(NULL
, fp
, cp
, 0);
241 /* Check for public cache location */
242 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
245 /* Check for private cache location */
246 if (!(cache_private
= context_find (nmhprivcache
)))
247 cache_private
= ".cache";
248 cache_private
= mh_xstrdup(m_maildir(cache_private
));
251 * Cache the current directory before we do any chdirs()'s.
253 cwd
= mh_xstrdup(pwd());
255 if (!context_find ("path"))
256 free (path ("./", TFOLDER
));
258 if (file
&& msgs
.size
)
259 die("cannot specify msg and file at same time!");
262 * check if message is coming from file
265 cts
= mh_xcalloc(2, sizeof *cts
);
268 if ((ct
= parse_mime (file
))) {
271 ct
->c_storage
= mh_xstrdup(outfile
);
276 * message(s) are coming from a folder
279 app_msgarg(&msgs
, "cur");
281 folder
= getfolder (1);
282 maildir
= m_maildir (folder
);
284 if (chdir (maildir
) == NOTOK
)
285 adios (maildir
, "unable to change directory to");
287 /* read folder and create message structure */
288 if (!(mp
= folder_read (folder
, 1)))
289 die("unable to read folder %s", folder
);
291 /* check for empty folder */
293 die("no messages in %s", folder
);
295 /* parse all the message ranges/sequences and set SELECTED */
296 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
297 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
299 seq_setprev (mp
); /* set the previous-sequence */
301 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
304 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
305 if (is_selected(mp
, msgnum
)) {
308 msgnam
= m_name (msgnum
);
309 if ((ct
= parse_mime (msgnam
))) {
312 ct
->c_storage
= mh_xstrdup(outfile
);
323 SIGNAL (SIGQUIT
, quitser
);
324 SIGNAL (SIGPIPE
, pipeser
);
327 * Get the associated umask for the relevant contents.
329 for (ctp
= cts
; *ctp
; ctp
++) {
333 if (type_ok (ct
, 1) && !ct
->c_umask
) {
334 if (stat (ct
->c_file
, &st
) != NOTOK
)
335 ct
->c_umask
= ~(st
.st_mode
& 0777);
337 ct
->c_umask
= ~m_gmprot();
342 * Store the message content
344 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
345 store_all_messages (info
);
346 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
347 mhstoreinfo_free(info
);
349 /* Now free all the structures for the content */
350 for (ctp
= cts
; *ctp
; ctp
++)
356 /* If reading from a folder, do some updating */
358 context_replace (pfolder
, folder
);/* update current folder */
359 seq_setcur (mp
, mp
->hghsel
); /* update current message */
360 seq_save (mp
); /* synchronize sequences */
361 context_save (); /* save the context file */
364 done (files_not_clobbered
);
374 fprintf (stderr
, "\n");