]>
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
;
57 extern char *parts
[NPARTS
+ 1];
58 extern char *types
[NTYPES
+ 1];
61 #define quitser pipeser
65 CT
parse_mime (char *);
68 int part_ok (CT
, int);
69 int type_ok (CT
, int);
70 void flush_errors (void);
74 void freects_done (int) NORETURN
;
79 static void pipeser (int);
83 main (int argc
, char **argv
)
85 /* verbosw defaults to 1 for backward compatibility. */
86 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
87 const char *clobbersw
= "always";
88 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
89 char *maildir
, buf
[100], **argp
;
92 struct msgs_array msgs
= { 0, 0, NULL
};
93 struct msgs
*mp
= NULL
;
96 int files_not_clobbered
;
99 if (nmh_init(argv
[0], 1)) { return 1; }
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
109 while ((cp
= *argp
++)) {
111 switch (smatch (++cp
, switches
)) {
113 ambigsw (cp
, switches
);
116 adios (NULL
, "-%s unknown", cp
);
119 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
135 icachesw
= &rcachesw
;
138 icachesw
= &wcachesw
;
140 if (!(cp
= *argp
++) || *cp
== '-')
141 adios (NULL
, "missing argument to %s", argp
[-2]);
142 switch (*icachesw
= smatch (cp
, caches
)) {
144 ambigsw (cp
, caches
);
147 adios (NULL
, "%s unknown", cp
);
161 if (!(cp
= *argp
++) || *cp
== '-')
162 adios (NULL
, "missing argument to %s", argp
[-2]);
164 adios (NULL
, "too many parts (starting with %s), %d max",
170 if (!(cp
= *argp
++) || *cp
== '-')
171 adios (NULL
, "missing argument to %s", argp
[-2]);
173 adios (NULL
, "too many types (starting with %s), %d max",
179 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
180 adios (NULL
, "missing argument to %s", argp
[-2]);
181 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
185 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
186 adios (NULL
, "missing argument to %s", argp
[-2]);
187 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 adios (NULL
, "missing argument to %s", argp
[-2]);
206 if (*cp
== '+' || *cp
== '@') {
208 adios (NULL
, "only one folder at a time!");
210 folder
= pluspath (cp
);
212 app_msgarg(&msgs
, cp
);
215 /* null terminate the list of acceptable parts/types */
220 * Check if we've specified an additional profile
222 if ((cp
= getenv ("MHSTORE"))) {
223 if ((fp
= fopen (cp
, "r"))) {
224 readconfig ((struct node
**) 0, fp
, cp
, 0);
227 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
232 * Read the standard profile setup
234 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
235 readconfig ((struct node
**) 0, fp
, cp
, 0);
239 /* Check for public cache location */
240 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
243 /* Check for private cache location */
244 if (!(cache_private
= context_find (nmhprivcache
)))
245 cache_private
= ".cache";
246 cache_private
= getcpy (m_maildir (cache_private
));
249 * Cache the current directory before we do any chdirs()'s.
251 cwd
= add(pwd(), NULL
);
253 if (!context_find ("path"))
254 free (path ("./", TFOLDER
));
256 if (file
&& msgs
.size
)
257 adios (NULL
, "cannot specify msg and file at same time!");
260 * check if message is coming from file
263 if (!(cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof(*cts
))))
264 adios (NULL
, "out of memory");
267 if ((ct
= parse_mime (file
))) {
270 ct
->c_storage
= add (outfile
, NULL
);
275 * message(s) are coming from a folder
278 app_msgarg(&msgs
, "cur");
280 folder
= getfolder (1);
281 maildir
= m_maildir (folder
);
283 if (chdir (maildir
) == NOTOK
)
284 adios (maildir
, "unable to change directory to");
286 /* read folder and create message structure */
287 if (!(mp
= folder_read (folder
, 1)))
288 adios (NULL
, "unable to read folder %s", folder
);
290 /* check for empty folder */
292 adios (NULL
, "no messages in %s", folder
);
294 /* parse all the message ranges/sequences and set SELECTED */
295 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
296 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
298 seq_setprev (mp
); /* set the previous-sequence */
300 if (!(cts
= (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
301 adios (NULL
, "out of memory");
304 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
305 if (is_selected(mp
, msgnum
)) {
308 msgnam
= m_name (msgnum
);
309 if ((ct
= parse_mime (msgnam
))) {
312 ct
->c_storage
= add (outfile
, NULL
);
323 SIGNAL (SIGQUIT
, quitser
);
324 SIGNAL (SIGPIPE
, pipeser
);
327 * Get the associated umask for the relevant contents.
329 for (ctp
= cts
; *ctp
; ctp
++) {
333 if (type_ok (ct
, 1) && !ct
->c_umask
) {
334 if (stat (ct
->c_file
, &st
) != NOTOK
)
335 ct
->c_umask
= ~(st
.st_mode
& 0777);
337 ct
->c_umask
= ~m_gmprot();
342 * Store the message content
344 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
345 store_all_messages (info
);
346 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
347 mhstoreinfo_free(info
);
349 /* Now free all the structures for the content */
350 for (ctp
= cts
; *ctp
; ctp
++)
356 /* If reading from a folder, do some updating */
358 context_replace (pfolder
, folder
);/* update current folder */
359 seq_setcur (mp
, mp
->hghsel
); /* update current message */
360 seq_save (mp
); /* synchronize sequences */
361 context_save (); /* save the context file */
364 done (files_not_clobbered
);
374 fprintf (stderr
, "\n");