]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
a1837bfd7f7d4a7c56ab6e56b8bfbc6f7097e4d4
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/getarguments.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_replace.h"
19 #include "sbr/context_find.h"
20 #include "sbr/readconfig.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
27 #include "h/signals.h"
32 #include "h/mhparse.h"
33 #include "h/mhcachesbr.h"
37 #include "sbr/m_maildir.h"
40 #define MHSTORE_SWITCHES \
41 X("auto", 0, AUTOSW) \
42 X("noauto", 0, NAUTOSW) \
43 X("check", 0, CHECKSW) \
44 X("nocheck", 0, NCHECKSW) \
45 X("verbose", 0, VERBSW) \
46 X("noverbose", 0, NVERBSW) \
47 X("file file", 0, FILESW) /* interface from show */ \
48 X("outfile outfile", 0, OUTFILESW) \
49 X("part number", 0, PARTSW) \
50 X("type content", 0, TYPESW) \
51 X("prefer content", 0, PREFERSW) \
52 X("noprefer", 0, NPREFERSW) \
53 X("rcache policy", 0, RCACHESW) \
54 X("wcache policy", 0, WCACHESW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
57 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
58 X("debug", -5, DEBUGSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(MHSTORE
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
69 #define quitser pipeser
77 static void pipeser (int);
81 main (int argc
, char **argv
)
83 int msgnum
, *icachesw
;
85 /* verbosw defaults to 1 for backward compatibility. */
87 const char *clobbersw
= "always";
88 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
89 char *maildir
, buf
[100], **argp
;
92 struct msgs_array msgs
= { 0, 0, NULL
};
93 struct msgs
*mp
= NULL
;
96 int files_not_clobbered
;
99 if (nmh_init(argv
[0], true, true)) { return 1; }
101 set_done(freects_done
);
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
109 while ((cp
= *argp
++)) {
111 switch (smatch (++cp
, switches
)) {
113 ambigsw (cp
, switches
);
116 die("-%s unknown", cp
);
119 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
135 icachesw
= &rcachesw
;
138 icachesw
= &wcachesw
;
140 if (!(cp
= *argp
++) || *cp
== '-')
141 die("missing argument to %s", argp
[-2]);
142 switch (*icachesw
= smatch (cp
, cache_policy
)) {
144 ambigsw (cp
, cache_policy
);
147 die("%s unknown", cp
);
161 if (!(cp
= *argp
++) || *cp
== '-')
162 die("missing argument to %s", argp
[-2]);
164 die("too many parts (starting with %s), %d max",
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
173 die("too many types (starting with %s), %d max",
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 if (npreferred
>= NPREFS
)
182 die("too many preferred types (starting with %s), %d max",
184 mime_preference
[npreferred
].type
= cp
;
185 cp
= strchr(cp
, '/');
186 if (cp
) *cp
++ = '\0';
187 mime_preference
[npreferred
++].subtype
= cp
;
195 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
196 die("missing argument to %s", argp
[-2]);
197 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
201 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
202 die("missing argument to %s", argp
[-2]);
203 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 die("missing argument to %s", argp
[-2]);
222 if (*cp
== '+' || *cp
== '@') {
224 die("only one folder at a time!");
225 folder
= pluspath (cp
);
227 app_msgarg(&msgs
, cp
);
230 /* null terminate the list of acceptable parts/types */
235 * Check if we've specified an additional profile
237 if ((cp
= getenv ("MHSTORE"))) {
238 if ((fp
= fopen (cp
, "r"))) {
239 readconfig(NULL
, fp
, cp
, 0);
242 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
247 * Read the standard profile setup
249 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
250 readconfig(NULL
, fp
, cp
, 0);
254 /* Check for public cache location */
255 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
258 /* Check for private cache location */
259 if (!(cache_private
= context_find (nmhprivcache
)))
260 cache_private
= ".cache";
261 cache_private
= mh_xstrdup(m_maildir(cache_private
));
264 * Cache the current directory before we do any chdirs()'s.
266 cwd
= mh_xstrdup(pwd());
268 if (!context_find ("path"))
269 free (path ("./", TFOLDER
));
271 if (file
&& msgs
.size
)
272 die("cannot specify msg and file at same time!");
275 * check if message is coming from file
278 cts
= mh_xcalloc(2, sizeof *cts
);
281 if ((ct
= parse_mime (file
))) {
284 ct
->c_storage
= mh_xstrdup(outfile
);
289 * message(s) are coming from a folder
292 app_msgarg(&msgs
, "cur");
294 folder
= getfolder (1);
295 maildir
= m_maildir (folder
);
297 if (chdir (maildir
) == NOTOK
)
298 adios (maildir
, "unable to change directory to");
300 /* read folder and create message structure */
301 if (!(mp
= folder_read (folder
, 1)))
302 die("unable to read folder %s", folder
);
304 /* check for empty folder */
306 die("no messages in %s", folder
);
308 /* parse all the message ranges/sequences and set SELECTED */
309 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
310 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
312 seq_setprev (mp
); /* set the previous-sequence */
314 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
317 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
318 if (is_selected(mp
, msgnum
)) {
321 msgnam
= m_name (msgnum
);
322 if ((ct
= parse_mime (msgnam
))) {
325 ct
->c_storage
= mh_xstrdup(outfile
);
336 SIGNAL (SIGQUIT
, quitser
);
337 SIGNAL (SIGPIPE
, pipeser
);
340 * Get the associated umask for the relevant contents.
342 for (ctp
= cts
; *ctp
; ctp
++) {
346 if (type_ok (ct
, 1) && !ct
->c_umask
) {
347 if (stat (ct
->c_file
, &st
) != NOTOK
)
348 ct
->c_umask
= ~(st
.st_mode
& 0777);
350 ct
->c_umask
= ~m_gmprot();
355 * Store the message content
357 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
358 store_all_messages (info
);
359 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
360 mhstoreinfo_free(info
);
362 /* Now free all the structures for the content */
363 for (ctp
= cts
; *ctp
; ctp
++)
369 /* If reading from a folder, do some updating */
371 context_replace (pfolder
, folder
);/* update current folder */
372 seq_setcur (mp
, mp
->hghsel
); /* update current message */
373 seq_save (mp
); /* synchronize sequences */
374 context_save (); /* save the context file */
377 done (files_not_clobbered
);
387 fprintf (stderr
, "\n");