]>
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 <h/signals.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
20 #include "sbr/m_maildir.h"
23 #define MHSTORE_SWITCHES \
24 X("auto", 0, AUTOSW) \
25 X("noauto", 0, NAUTOSW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) /* interface from show */ \
31 X("outfile outfile", 0, OUTFILESW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("prefer content", 0, PREFERSW) \
35 X("noprefer", 0, NPREFERSW) \
36 X("rcache policy", 0, RCACHESW) \
37 X("wcache policy", 0, WCACHESW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
41 X("debug", -5, DEBUGSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(MHSTORE
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
52 #define quitser pipeser
60 static void pipeser (int);
64 main (int argc
, char **argv
)
66 int msgnum
, *icachesw
;
68 /* verbosw defaults to 1 for backward compatibility. */
70 const char *clobbersw
= "always";
71 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
72 char *maildir
, buf
[100], **argp
;
75 struct msgs_array msgs
= { 0, 0, NULL
};
76 struct msgs
*mp
= NULL
;
79 int files_not_clobbered
;
82 if (nmh_init(argv
[0], true, true)) { return 1; }
84 set_done(freects_done
);
86 arguments
= getarguments (invo_name
, argc
, argv
, 1);
92 while ((cp
= *argp
++)) {
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
99 die("-%s unknown", cp
);
102 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
104 print_help (buf
, switches
, 1);
107 print_version(invo_name
);
118 icachesw
= &rcachesw
;
121 icachesw
= &wcachesw
;
123 if (!(cp
= *argp
++) || *cp
== '-')
124 die("missing argument to %s", argp
[-2]);
125 switch (*icachesw
= smatch (cp
, cache_policy
)) {
127 ambigsw (cp
, cache_policy
);
130 die("%s unknown", cp
);
144 if (!(cp
= *argp
++) || *cp
== '-')
145 die("missing argument to %s", argp
[-2]);
147 die("too many parts (starting with %s), %d max",
153 if (!(cp
= *argp
++) || *cp
== '-')
154 die("missing argument to %s", argp
[-2]);
156 die("too many types (starting with %s), %d max",
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
164 if (npreferred
>= NPREFS
)
165 die("too many preferred types (starting with %s), %d max",
167 mime_preference
[npreferred
].type
= cp
;
168 cp
= strchr(cp
, '/');
169 if (cp
) *cp
++ = '\0';
170 mime_preference
[npreferred
++].subtype
= cp
;
178 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
179 die("missing argument to %s", argp
[-2]);
180 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
184 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
185 die("missing argument to %s", argp
[-2]);
186 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
205 if (*cp
== '+' || *cp
== '@') {
207 die("only one folder at a time!");
208 folder
= pluspath (cp
);
210 app_msgarg(&msgs
, cp
);
213 /* null terminate the list of acceptable parts/types */
218 * Check if we've specified an additional profile
220 if ((cp
= getenv ("MHSTORE"))) {
221 if ((fp
= fopen (cp
, "r"))) {
222 readconfig(NULL
, fp
, cp
, 0);
225 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
230 * Read the standard profile setup
232 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
233 readconfig(NULL
, fp
, cp
, 0);
237 /* Check for public cache location */
238 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
241 /* Check for private cache location */
242 if (!(cache_private
= context_find (nmhprivcache
)))
243 cache_private
= ".cache";
244 cache_private
= mh_xstrdup(m_maildir(cache_private
));
247 * Cache the current directory before we do any chdirs()'s.
249 cwd
= mh_xstrdup(pwd());
251 if (!context_find ("path"))
252 free (path ("./", TFOLDER
));
254 if (file
&& msgs
.size
)
255 die("cannot specify msg and file at same time!");
258 * check if message is coming from file
261 cts
= mh_xcalloc(2, sizeof *cts
);
264 if ((ct
= parse_mime (file
))) {
267 ct
->c_storage
= mh_xstrdup(outfile
);
272 * message(s) are coming from a folder
275 app_msgarg(&msgs
, "cur");
277 folder
= getfolder (1);
278 maildir
= m_maildir (folder
);
280 if (chdir (maildir
) == NOTOK
)
281 adios (maildir
, "unable to change directory to");
283 /* read folder and create message structure */
284 if (!(mp
= folder_read (folder
, 1)))
285 die("unable to read folder %s", folder
);
287 /* check for empty folder */
289 die("no messages in %s", folder
);
291 /* parse all the message ranges/sequences and set SELECTED */
292 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
293 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
295 seq_setprev (mp
); /* set the previous-sequence */
297 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
300 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
301 if (is_selected(mp
, msgnum
)) {
304 msgnam
= m_name (msgnum
);
305 if ((ct
= parse_mime (msgnam
))) {
308 ct
->c_storage
= mh_xstrdup(outfile
);
319 SIGNAL (SIGQUIT
, quitser
);
320 SIGNAL (SIGPIPE
, pipeser
);
323 * Get the associated umask for the relevant contents.
325 for (ctp
= cts
; *ctp
; ctp
++) {
329 if (type_ok (ct
, 1) && !ct
->c_umask
) {
330 if (stat (ct
->c_file
, &st
) != NOTOK
)
331 ct
->c_umask
= ~(st
.st_mode
& 0777);
333 ct
->c_umask
= ~m_gmprot();
338 * Store the message content
340 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
341 store_all_messages (info
);
342 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
343 mhstoreinfo_free(info
);
345 /* Now free all the structures for the content */
346 for (ctp
= cts
; *ctp
; ctp
++)
352 /* If reading from a folder, do some updating */
354 context_replace (pfolder
, folder
);/* update current folder */
355 seq_setcur (mp
, mp
->hghsel
); /* update current message */
356 seq_save (mp
); /* synchronize sequences */
357 context_save (); /* save the context file */
360 done (files_not_clobbered
);
370 fprintf (stderr
, "\n");