]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
3 * mhstore.c -- store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHSTORE_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("file file", 0, FILESW) /* interface from show */ \
29 X("outfile outfile", 0, OUTFILESW) \
30 X("part number", 0, PARTSW) \
31 X("type content", 0, TYPESW) \
32 X("rcache policy", 0, RCACHESW) \
33 X("wcache policy", 0, WCACHESW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
37 X("debug", -5, DEBUGSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(MHSTORE
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
48 int save_clobber_policy (const char *);
49 extern int files_not_clobbered
;
54 extern char *cache_public
;
55 extern char *cache_private
;
59 extern char *cwd
; /* cache current working directory */
64 extern char *parts
[NPARTS
+ 1];
65 extern char *types
[NTYPES
+ 1];
71 #define quitser pipeser
74 CT
parse_mime (char *);
77 int part_ok (CT
, int);
78 int type_ok (CT
, int);
79 void flush_errors (void);
82 void store_all_messages (CT
*);
86 void freects_done (int) NORETURN
;
91 static void pipeser (int);
95 main (int argc
, char **argv
)
97 int msgnum
, *icachesw
;
98 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
99 char *maildir
, buf
[100], **argp
;
101 struct msgs_array msgs
= { 0, 0, NULL
};
102 struct msgs
*mp
= NULL
;
106 if (nmh_init(argv
[0], 1)) { return 1; }
110 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
118 switch (smatch (++cp
, switches
)) {
120 ambigsw (cp
, switches
);
123 adios (NULL
, "-%s unknown", cp
);
126 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
128 print_help (buf
, switches
, 1);
131 print_version(invo_name
);
142 icachesw
= &rcachesw
;
145 icachesw
= &wcachesw
;
147 if (!(cp
= *argp
++) || *cp
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
149 switch (*icachesw
= smatch (cp
, caches
)) {
151 ambigsw (cp
, caches
);
154 adios (NULL
, "%s unknown", cp
);
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
171 adios (NULL
, "too many parts (starting with %s), %d max",
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
180 adios (NULL
, "too many types (starting with %s), %d max",
186 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
187 adios (NULL
, "missing argument to %s", argp
[-2]);
188 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
192 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
193 adios (NULL
, "missing argument to %s", argp
[-2]);
194 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
206 if (save_clobber_policy (cp
)) {
207 adios (NULL
, "invalid argument, %s, to %s", argp
[-1],
216 if (*cp
== '+' || *cp
== '@') {
218 adios (NULL
, "only one folder at a time!");
220 folder
= pluspath (cp
);
222 app_msgarg(&msgs
, cp
);
225 /* null terminate the list of acceptable parts/types */
230 * Check if we've specified an additional profile
232 if ((cp
= getenv ("MHSTORE"))) {
233 if ((fp
= fopen (cp
, "r"))) {
234 readconfig ((struct node
**) 0, fp
, cp
, 0);
237 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
242 * Read the standard profile setup
244 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
245 readconfig ((struct node
**) 0, fp
, cp
, 0);
249 /* Check for public cache location */
250 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
253 /* Check for private cache location */
254 if (!(cache_private
= context_find (nmhprivcache
)))
255 cache_private
= ".cache";
256 cache_private
= getcpy (m_maildir (cache_private
));
259 * Cache the current directory before we do any chdirs()'s.
261 cwd
= getcpy (pwd());
263 if (!context_find ("path"))
264 free (path ("./", TFOLDER
));
266 if (file
&& msgs
.size
)
267 adios (NULL
, "cannot specify msg and file at same time!");
270 * check if message is coming from file
273 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
274 adios (NULL
, "out of memory");
277 if ((ct
= parse_mime (file
))) {
280 ct
->c_storage
= 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 adios (NULL
, "unable to read folder %s", folder
);
300 /* check for empty folder */
302 adios (NULL
, "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 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
311 adios (NULL
, "out of memory");
314 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
315 if (is_selected(mp
, msgnum
)) {
318 msgnam
= m_name (msgnum
);
319 if ((ct
= parse_mime (msgnam
))) {
322 ct
->c_storage
= add (outfile
, NULL
);
333 SIGNAL (SIGQUIT
, quitser
);
334 SIGNAL (SIGPIPE
, pipeser
);
337 * Get the associated umask for the relevant contents.
339 for (ctp
= cts
; *ctp
; ctp
++) {
343 if (type_ok (ct
, 1) && !ct
->c_umask
) {
344 if (stat (ct
->c_file
, &st
) != NOTOK
)
345 ct
->c_umask
= ~(st
.st_mode
& 0777);
347 ct
->c_umask
= ~m_gmprot();
352 * Store the message content
354 store_all_messages (cts
);
356 /* Now free all the structures for the content */
357 for (ctp
= cts
; *ctp
; ctp
++)
363 /* If reading from a folder, do some updating */
365 context_replace (pfolder
, folder
);/* update current folder */
366 seq_setcur (mp
, mp
->hghsel
); /* update current message */
367 seq_save (mp
); /* synchronize sequences */
368 context_save (); /* save the context file */
371 done (files_not_clobbered
);
381 fprintf (stderr
, "\n");