]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
628d059ff30d055c39da79010dd5a66610887f8a
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/context_find.h"
10 #include "sbr/readconfig.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
17 #include "h/signals.h"
22 #include "h/mhparse.h"
23 #include "h/mhcachesbr.h"
27 #include "sbr/m_maildir.h"
30 #define MHSTORE_SWITCHES \
31 X("auto", 0, AUTOSW) \
32 X("noauto", 0, NAUTOSW) \
33 X("check", 0, CHECKSW) \
34 X("nocheck", 0, NCHECKSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("file file", 0, FILESW) /* interface from show */ \
38 X("outfile outfile", 0, OUTFILESW) \
39 X("part number", 0, PARTSW) \
40 X("type content", 0, TYPESW) \
41 X("prefer content", 0, PREFERSW) \
42 X("noprefer", 0, NPREFERSW) \
43 X("rcache policy", 0, RCACHESW) \
44 X("wcache policy", 0, WCACHESW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
47 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
48 X("debug", -5, DEBUGSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHSTORE
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
59 #define quitser pipeser
67 static void pipeser (int);
71 main (int argc
, char **argv
)
73 int msgnum
, *icachesw
;
75 /* verbosw defaults to 1 for backward compatibility. */
77 const char *clobbersw
= "always";
78 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
79 char *maildir
, buf
[100], **argp
;
82 struct msgs_array msgs
= { 0, 0, NULL
};
83 struct msgs
*mp
= NULL
;
86 int files_not_clobbered
;
89 if (nmh_init(argv
[0], true, true)) { return 1; }
91 set_done(freects_done
);
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
99 while ((cp
= *argp
++)) {
101 switch (smatch (++cp
, switches
)) {
103 ambigsw (cp
, switches
);
106 die("-%s unknown", cp
);
109 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
125 icachesw
= &rcachesw
;
128 icachesw
= &wcachesw
;
130 if (!(cp
= *argp
++) || *cp
== '-')
131 die("missing argument to %s", argp
[-2]);
132 switch (*icachesw
= smatch (cp
, cache_policy
)) {
134 ambigsw (cp
, cache_policy
);
137 die("%s unknown", cp
);
151 if (!(cp
= *argp
++) || *cp
== '-')
152 die("missing argument to %s", argp
[-2]);
154 die("too many parts (starting with %s), %d max",
160 if (!(cp
= *argp
++) || *cp
== '-')
161 die("missing argument to %s", argp
[-2]);
163 die("too many types (starting with %s), %d max",
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 if (npreferred
>= NPREFS
)
172 die("too many preferred types (starting with %s), %d max",
174 mime_preference
[npreferred
].type
= cp
;
175 cp
= strchr(cp
, '/');
176 if (cp
) *cp
++ = '\0';
177 mime_preference
[npreferred
++].subtype
= cp
;
185 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
186 die("missing argument to %s", argp
[-2]);
187 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
191 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
192 die("missing argument to %s", argp
[-2]);
193 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
212 if (*cp
== '+' || *cp
== '@') {
214 die("only one folder at a time!");
215 folder
= pluspath (cp
);
217 app_msgarg(&msgs
, cp
);
220 /* null terminate the list of acceptable parts/types */
225 * Check if we've specified an additional profile
227 if ((cp
= getenv ("MHSTORE"))) {
228 if ((fp
= fopen (cp
, "r"))) {
229 readconfig(NULL
, fp
, cp
, 0);
232 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
237 * Read the standard profile setup
239 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
240 readconfig(NULL
, fp
, cp
, 0);
244 /* Check for public cache location */
245 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
248 /* Check for private cache location */
249 if (!(cache_private
= context_find (nmhprivcache
)))
250 cache_private
= ".cache";
251 cache_private
= mh_xstrdup(m_maildir(cache_private
));
254 * Cache the current directory before we do any chdirs()'s.
256 cwd
= mh_xstrdup(pwd());
258 if (!context_find ("path"))
259 free (path ("./", TFOLDER
));
261 if (file
&& msgs
.size
)
262 die("cannot specify msg and file at same time!");
265 * check if message is coming from file
268 cts
= mh_xcalloc(2, sizeof *cts
);
271 if ((ct
= parse_mime (file
))) {
274 ct
->c_storage
= mh_xstrdup(outfile
);
279 * message(s) are coming from a folder
282 app_msgarg(&msgs
, "cur");
284 folder
= getfolder (1);
285 maildir
= m_maildir (folder
);
287 if (chdir (maildir
) == NOTOK
)
288 adios (maildir
, "unable to change directory to");
290 /* read folder and create message structure */
291 if (!(mp
= folder_read (folder
, 1)))
292 die("unable to read folder %s", folder
);
294 /* check for empty folder */
296 die("no messages in %s", folder
);
298 /* parse all the message ranges/sequences and set SELECTED */
299 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
300 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
302 seq_setprev (mp
); /* set the previous-sequence */
304 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
307 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
308 if (is_selected(mp
, msgnum
)) {
311 msgnam
= m_name (msgnum
);
312 if ((ct
= parse_mime (msgnam
))) {
315 ct
->c_storage
= mh_xstrdup(outfile
);
326 SIGNAL (SIGQUIT
, quitser
);
327 SIGNAL (SIGPIPE
, pipeser
);
330 * Get the associated umask for the relevant contents.
332 for (ctp
= cts
; *ctp
; ctp
++) {
336 if (type_ok (ct
, 1) && !ct
->c_umask
) {
337 if (stat (ct
->c_file
, &st
) != NOTOK
)
338 ct
->c_umask
= ~(st
.st_mode
& 0777);
340 ct
->c_umask
= ~m_gmprot();
345 * Store the message content
347 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
348 store_all_messages (info
);
349 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
350 mhstoreinfo_free(info
);
352 /* Now free all the structures for the content */
353 for (ctp
= cts
; *ctp
; ctp
++)
359 /* If reading from a folder, do some updating */
361 context_replace (pfolder
, folder
);/* update current folder */
362 seq_setcur (mp
, mp
->hghsel
); /* update current message */
363 seq_save (mp
); /* synchronize sequences */
364 context_save (); /* save the context file */
367 done (files_not_clobbered
);
377 fprintf (stderr
, "\n");