]>
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
);
51 extern char *cache_public
;
52 extern char *cache_private
;
55 typedef struct mhstoreinfo
*mhstoreinfo_t
;
56 mhstoreinfo_t
mhstoreinfo_create(CT
*, char *, const char *, int, int);
57 int mhstoreinfo_files_not_clobbered(const mhstoreinfo_t
);
58 void mhstoreinfo_free(mhstoreinfo_t
);
63 extern char *parts
[NPARTS
+ 1];
64 extern char *types
[NTYPES
+ 1];
67 #define quitser pipeser
71 CT
parse_mime (char *);
74 int part_ok (CT
, int);
75 int type_ok (CT
, int);
76 void flush_errors (void);
79 void store_all_messages (mhstoreinfo_t
);
83 void freects_done (int) NORETURN
;
88 static void pipeser (int);
92 main (int argc
, char **argv
)
94 /* verbosw defaults to 1 for backward compatibility. */
95 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
96 const char *clobbersw
= "always";
97 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
98 char *maildir
, buf
[100], **argp
;
101 struct msgs_array msgs
= { 0, 0, NULL
};
102 struct msgs
*mp
= NULL
;
105 int files_not_clobbered
;
108 if (nmh_init(argv
[0], 1)) { return 1; }
112 arguments
= getarguments (invo_name
, argc
, argv
, 1);
118 while ((cp
= *argp
++)) {
120 switch (smatch (++cp
, switches
)) {
122 ambigsw (cp
, switches
);
125 adios (NULL
, "-%s unknown", cp
);
128 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
130 print_help (buf
, switches
, 1);
133 print_version(invo_name
);
144 icachesw
= &rcachesw
;
147 icachesw
= &wcachesw
;
149 if (!(cp
= *argp
++) || *cp
== '-')
150 adios (NULL
, "missing argument to %s", argp
[-2]);
151 switch (*icachesw
= smatch (cp
, caches
)) {
153 ambigsw (cp
, caches
);
156 adios (NULL
, "%s unknown", cp
);
170 if (!(cp
= *argp
++) || *cp
== '-')
171 adios (NULL
, "missing argument to %s", argp
[-2]);
173 adios (NULL
, "too many parts (starting with %s), %d max",
179 if (!(cp
= *argp
++) || *cp
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
182 adios (NULL
, "too many types (starting with %s), %d max",
188 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
189 adios (NULL
, "missing argument to %s", argp
[-2]);
190 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
215 if (*cp
== '+' || *cp
== '@') {
217 adios (NULL
, "only one folder at a time!");
219 folder
= pluspath (cp
);
221 app_msgarg(&msgs
, cp
);
224 /* null terminate the list of acceptable parts/types */
229 * Check if we've specified an additional profile
231 if ((cp
= getenv ("MHSTORE"))) {
232 if ((fp
= fopen (cp
, "r"))) {
233 readconfig ((struct node
**) 0, fp
, cp
, 0);
236 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
241 * Read the standard profile setup
243 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
244 readconfig ((struct node
**) 0, fp
, cp
, 0);
248 /* Check for public cache location */
249 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
252 /* Check for private cache location */
253 if (!(cache_private
= context_find (nmhprivcache
)))
254 cache_private
= ".cache";
255 cache_private
= getcpy (m_maildir (cache_private
));
258 * Cache the current directory before we do any chdirs()'s.
260 cwd
= add(pwd(), NULL
);
262 if (!context_find ("path"))
263 free (path ("./", TFOLDER
));
265 if (file
&& msgs
.size
)
266 adios (NULL
, "cannot specify msg and file at same time!");
269 * check if message is coming from file
272 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
273 adios (NULL
, "out of memory");
276 if ((ct
= parse_mime (file
))) {
279 ct
->c_storage
= outfile
;
284 * message(s) are coming from a folder
287 app_msgarg(&msgs
, "cur");
289 folder
= getfolder (1);
290 maildir
= m_maildir (folder
);
292 if (chdir (maildir
) == NOTOK
)
293 adios (maildir
, "unable to change directory to");
295 /* read folder and create message structure */
296 if (!(mp
= folder_read (folder
, 1)))
297 adios (NULL
, "unable to read folder %s", folder
);
299 /* check for empty folder */
301 adios (NULL
, "no messages in %s", folder
);
303 /* parse all the message ranges/sequences and set SELECTED */
304 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
305 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
307 seq_setprev (mp
); /* set the previous-sequence */
309 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
310 adios (NULL
, "out of memory");
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
= add (outfile
, NULL
);
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");