]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
2552cd897e0c167f7129eae1453a704153b27144
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/smatch.h"
10 #include "sbr/m_convert.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/readconfig.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
23 #include "h/signals.h"
28 #include "h/mhparse.h"
29 #include "h/mhcachesbr.h"
33 #include "sbr/m_maildir.h"
36 #define MHSTORE_SWITCHES \
37 X("auto", 0, AUTOSW) \
38 X("noauto", 0, NAUTOSW) \
39 X("check", 0, CHECKSW) \
40 X("nocheck", 0, NCHECKSW) \
41 X("verbose", 0, VERBSW) \
42 X("noverbose", 0, NVERBSW) \
43 X("file file", 0, FILESW) /* interface from show */ \
44 X("outfile outfile", 0, OUTFILESW) \
45 X("part number", 0, PARTSW) \
46 X("type content", 0, TYPESW) \
47 X("prefer content", 0, PREFERSW) \
48 X("noprefer", 0, NPREFERSW) \
49 X("rcache policy", 0, RCACHESW) \
50 X("wcache policy", 0, WCACHESW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
53 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
54 X("debug", -5, DEBUGSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(MHSTORE
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
65 #define quitser pipeser
73 static void pipeser (int);
77 main (int argc
, char **argv
)
79 int msgnum
, *icachesw
;
81 /* verbosw defaults to 1 for backward compatibility. */
83 const char *clobbersw
= "always";
84 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
85 char *maildir
, buf
[100], **argp
;
88 struct msgs_array msgs
= { 0, 0, NULL
};
89 struct msgs
*mp
= NULL
;
92 int files_not_clobbered
;
95 if (nmh_init(argv
[0], true, true)) { return 1; }
97 set_done(freects_done
);
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
105 while ((cp
= *argp
++)) {
107 switch (smatch (++cp
, switches
)) {
109 ambigsw (cp
, switches
);
112 die("-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
117 print_help (buf
, switches
, 1);
120 print_version(invo_name
);
131 icachesw
= &rcachesw
;
134 icachesw
= &wcachesw
;
136 if (!(cp
= *argp
++) || *cp
== '-')
137 die("missing argument to %s", argp
[-2]);
138 switch (*icachesw
= smatch (cp
, cache_policy
)) {
140 ambigsw (cp
, cache_policy
);
143 die("%s unknown", cp
);
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
160 die("too many parts (starting with %s), %d max",
166 if (!(cp
= *argp
++) || *cp
== '-')
167 die("missing argument to %s", argp
[-2]);
169 die("too many types (starting with %s), %d max",
175 if (!(cp
= *argp
++) || *cp
== '-')
176 die("missing argument to %s", argp
[-2]);
177 if (npreferred
>= NPREFS
)
178 die("too many preferred types (starting with %s), %d max",
180 mime_preference
[npreferred
].type
= cp
;
181 cp
= strchr(cp
, '/');
182 if (cp
) *cp
++ = '\0';
183 mime_preference
[npreferred
++].subtype
= cp
;
191 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
192 die("missing argument to %s", argp
[-2]);
193 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
197 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
198 die("missing argument to %s", argp
[-2]);
199 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
209 if (!(cp
= *argp
++) || *cp
== '-')
210 die("missing argument to %s", argp
[-2]);
218 if (*cp
== '+' || *cp
== '@') {
220 die("only one folder at a time!");
221 folder
= pluspath (cp
);
223 app_msgarg(&msgs
, cp
);
226 /* null terminate the list of acceptable parts/types */
231 * Check if we've specified an additional profile
233 if ((cp
= getenv ("MHSTORE"))) {
234 if ((fp
= fopen (cp
, "r"))) {
235 readconfig(NULL
, fp
, cp
, 0);
238 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
243 * Read the standard profile setup
245 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
246 readconfig(NULL
, fp
, cp
, 0);
250 /* Check for public cache location */
251 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
254 /* Check for private cache location */
255 if (!(cache_private
= context_find (nmhprivcache
)))
256 cache_private
= ".cache";
257 cache_private
= mh_xstrdup(m_maildir(cache_private
));
260 * Cache the current directory before we do any chdirs()'s.
262 cwd
= mh_xstrdup(pwd());
264 if (!context_find ("path"))
265 free (path ("./", TFOLDER
));
267 if (file
&& msgs
.size
)
268 die("cannot specify msg and file at same time!");
271 * check if message is coming from file
274 cts
= mh_xcalloc(2, sizeof *cts
);
277 if ((ct
= parse_mime (file
))) {
280 ct
->c_storage
= mh_xstrdup(outfile
);
285 * message(s) are coming from a folder
288 app_msgarg(&msgs
, "cur");
290 folder
= getfolder (1);
291 maildir
= m_maildir (folder
);
293 if (chdir (maildir
) == NOTOK
)
294 adios (maildir
, "unable to change directory to");
296 /* read folder and create message structure */
297 if (!(mp
= folder_read (folder
, 1)))
298 die("unable to read folder %s", folder
);
300 /* check for empty folder */
302 die("no messages in %s", folder
);
304 /* parse all the message ranges/sequences and set SELECTED */
305 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
306 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
308 seq_setprev (mp
); /* set the previous-sequence */
310 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
313 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
314 if (is_selected(mp
, msgnum
)) {
317 msgnam
= m_name (msgnum
);
318 if ((ct
= parse_mime (msgnam
))) {
321 ct
->c_storage
= mh_xstrdup(outfile
);
332 SIGNAL (SIGQUIT
, quitser
);
333 SIGNAL (SIGPIPE
, pipeser
);
336 * Get the associated umask for the relevant contents.
338 for (ctp
= cts
; *ctp
; ctp
++) {
342 if (type_ok (ct
, 1) && !ct
->c_umask
) {
343 if (stat (ct
->c_file
, &st
) != NOTOK
)
344 ct
->c_umask
= ~(st
.st_mode
& 0777);
346 ct
->c_umask
= ~m_gmprot();
351 * Store the message content
353 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
354 store_all_messages (info
);
355 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
356 mhstoreinfo_free(info
);
358 /* Now free all the structures for the content */
359 for (ctp
= cts
; *ctp
; ctp
++)
365 /* If reading from a folder, do some updating */
367 context_replace (pfolder
, folder
);/* update current folder */
368 seq_setcur (mp
, mp
->hghsel
); /* update current message */
369 seq_save (mp
); /* synchronize sequences */
370 context_save (); /* save the context file */
373 done (files_not_clobbered
);
383 fprintf (stderr
, "\n");