]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
56b186af99fcace0366592707c09dbd101786173
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/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/readconfig.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
26 #include "h/signals.h"
31 #include "h/mhparse.h"
32 #include "h/mhcachesbr.h"
36 #include "sbr/m_maildir.h"
39 #define MHSTORE_SWITCHES \
40 X("auto", 0, AUTOSW) \
41 X("noauto", 0, NAUTOSW) \
42 X("check", 0, CHECKSW) \
43 X("nocheck", 0, NCHECKSW) \
44 X("verbose", 0, VERBSW) \
45 X("noverbose", 0, NVERBSW) \
46 X("file file", 0, FILESW) /* interface from show */ \
47 X("outfile outfile", 0, OUTFILESW) \
48 X("part number", 0, PARTSW) \
49 X("type content", 0, TYPESW) \
50 X("prefer content", 0, PREFERSW) \
51 X("noprefer", 0, NPREFERSW) \
52 X("rcache policy", 0, RCACHESW) \
53 X("wcache policy", 0, WCACHESW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
57 X("debug", -5, DEBUGSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(MHSTORE
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
68 #define quitser pipeser
76 static void pipeser (int);
80 main (int argc
, char **argv
)
82 int msgnum
, *icachesw
;
84 /* verbosw defaults to 1 for backward compatibility. */
86 const char *clobbersw
= "always";
87 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
88 char *maildir
, buf
[100], **argp
;
91 struct msgs_array msgs
= { 0, 0, NULL
};
92 struct msgs
*mp
= NULL
;
95 int files_not_clobbered
;
98 if (nmh_init(argv
[0], true, true)) { return 1; }
100 set_done(freects_done
);
102 arguments
= getarguments (invo_name
, argc
, argv
, 1);
108 while ((cp
= *argp
++)) {
110 switch (smatch (++cp
, switches
)) {
112 ambigsw (cp
, switches
);
115 die("-%s unknown", cp
);
118 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
120 print_help (buf
, switches
, 1);
123 print_version(invo_name
);
134 icachesw
= &rcachesw
;
137 icachesw
= &wcachesw
;
139 if (!(cp
= *argp
++) || *cp
== '-')
140 die("missing argument to %s", argp
[-2]);
141 switch (*icachesw
= smatch (cp
, cache_policy
)) {
143 ambigsw (cp
, cache_policy
);
146 die("%s unknown", cp
);
160 if (!(cp
= *argp
++) || *cp
== '-')
161 die("missing argument to %s", argp
[-2]);
163 die("too many parts (starting with %s), %d max",
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
172 die("too many types (starting with %s), %d max",
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
180 if (npreferred
>= NPREFS
)
181 die("too many preferred types (starting with %s), %d max",
183 mime_preference
[npreferred
].type
= cp
;
184 cp
= strchr(cp
, '/');
185 if (cp
) *cp
++ = '\0';
186 mime_preference
[npreferred
++].subtype
= cp
;
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 die("missing argument to %s", argp
[-2]);
196 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
200 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
201 die("missing argument to %s", argp
[-2]);
202 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
221 if (*cp
== '+' || *cp
== '@') {
223 die("only one folder at a time!");
224 folder
= pluspath (cp
);
226 app_msgarg(&msgs
, cp
);
229 /* null terminate the list of acceptable parts/types */
234 * Check if we've specified an additional profile
236 if ((cp
= getenv ("MHSTORE"))) {
237 if ((fp
= fopen (cp
, "r"))) {
238 readconfig(NULL
, fp
, cp
, 0);
241 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
246 * Read the standard profile setup
248 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
249 readconfig(NULL
, fp
, cp
, 0);
253 /* Check for public cache location */
254 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
257 /* Check for private cache location */
258 if (!(cache_private
= context_find (nmhprivcache
)))
259 cache_private
= ".cache";
260 cache_private
= mh_xstrdup(m_maildir(cache_private
));
263 * Cache the current directory before we do any chdirs()'s.
265 cwd
= mh_xstrdup(pwd());
267 if (!context_find ("path"))
268 free (path ("./", TFOLDER
));
270 if (file
&& msgs
.size
)
271 die("cannot specify msg and file at same time!");
274 * check if message is coming from file
277 cts
= mh_xcalloc(2, sizeof *cts
);
280 if ((ct
= parse_mime (file
))) {
283 ct
->c_storage
= mh_xstrdup(outfile
);
288 * message(s) are coming from a folder
291 app_msgarg(&msgs
, "cur");
293 folder
= getfolder (1);
294 maildir
= m_maildir (folder
);
296 if (chdir (maildir
) == NOTOK
)
297 adios (maildir
, "unable to change directory to");
299 /* read folder and create message structure */
300 if (!(mp
= folder_read (folder
, 1)))
301 die("unable to read folder %s", folder
);
303 /* check for empty folder */
305 die("no messages in %s", folder
);
307 /* parse all the message ranges/sequences and set SELECTED */
308 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
309 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
311 seq_setprev (mp
); /* set the previous-sequence */
313 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
316 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
317 if (is_selected(mp
, msgnum
)) {
320 msgnam
= m_name (msgnum
);
321 if ((ct
= parse_mime (msgnam
))) {
324 ct
->c_storage
= mh_xstrdup(outfile
);
335 SIGNAL (SIGQUIT
, quitser
);
336 SIGNAL (SIGPIPE
, pipeser
);
339 * Get the associated umask for the relevant contents.
341 for (ctp
= cts
; *ctp
; ctp
++) {
345 if (type_ok (ct
, 1) && !ct
->c_umask
) {
346 if (stat (ct
->c_file
, &st
) != NOTOK
)
347 ct
->c_umask
= ~(st
.st_mode
& 0777);
349 ct
->c_umask
= ~m_gmprot();
354 * Store the message content
356 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
357 store_all_messages (info
);
358 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
359 mhstoreinfo_free(info
);
361 /* Now free all the structures for the content */
362 for (ctp
= cts
; *ctp
; ctp
++)
368 /* If reading from a folder, do some updating */
370 context_replace (pfolder
, folder
);/* update current folder */
371 seq_setcur (mp
, mp
->hghsel
); /* update current message */
372 seq_save (mp
); /* synchronize sequences */
373 context_save (); /* save the context file */
376 done (files_not_clobbered
);
386 fprintf (stderr
, "\n");