2 ** mhstore.c -- store the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
39 #define FILESW 6 /* interface from show */
44 { "type content", 0 },
46 { "rcache policy", 0 },
48 { "wcache policy", 0 },
55 ** switches for debugging
64 extern char *tmp; /* directory to place temp files */
69 extern char *cache_public;
70 extern char *cache_private;
74 extern char *cwd; /* cache current working directory */
79 extern char *parts[NPARTS + 1];
80 extern char *types[NTYPES + 1];
86 #define quitser pipeser
89 CT parse_mime(char *);
94 void set_endian(void);
95 void flush_errors(void);
98 void store_all_messages(CT *);
101 void free_content(CT);
103 void freects_done(int) NORETURN;
108 static RETSIGTYPE pipeser(int);
112 main(int argc, char **argv)
114 int msgnum, *icachesw;
115 char *cp, *file = NULL, *folder = NULL;
116 char *maildir, buf[100], **argp;
118 struct msgs_array msgs = { 0, 0, NULL };
119 struct msgs *mp = NULL;
126 setlocale(LC_ALL, "");
128 invo_name = mhbasename(argv[0]);
130 /* read user profile/context */
133 arguments = getarguments(invo_name, argc, argv, 1);
139 while ((cp = *argp++)) {
141 switch (smatch(++cp, switches)) {
143 ambigsw(cp, switches);
146 adios(NULL, "-%s unknown", cp);
149 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
150 print_help(buf, switches, 1);
153 print_version(invo_name);
164 icachesw = &rcachesw;
167 icachesw = &wcachesw;
169 if (!(cp = *argp++) || *cp == '-')
170 adios(NULL, "missing argument to %s",
172 switch (*icachesw = smatch(cp, caches)) {
177 adios(NULL, "%s unknown", cp);
191 if (!(cp = *argp++) || *cp == '-')
192 adios(NULL, "missing argument to %s",
195 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
200 if (!(cp = *argp++) || *cp == '-')
201 adios(NULL, "missing argument to %s",
204 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
209 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
210 adios(NULL, "missing argument to %s",
212 file = *cp == '-' ? cp : getcpy(expanddir(cp));
226 if (*cp == '+' || *cp == '@') {
228 adios(NULL, "only one folder at a time!");
230 folder = getcpy(expandfol(cp));
232 app_msgarg(&msgs, cp);
235 /* null terminate the list of acceptable parts/types */
242 ** Check if we've specified an additional profile
244 if ((cp = getenv("MHSTORE"))) {
245 if ((fp = fopen(cp, "r"))) {
246 readconfig((struct node **) 0, fp, cp, 0);
249 admonish("", "unable to read $MHSTORE profile (%s)",
255 ** Read the standard profile setup
257 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
258 readconfig((struct node **) 0, fp, cp, 0);
262 /* Check for public cache location */
263 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
266 /* Check for private cache location */
267 if (!(cache_private = context_find(nmhprivcache)))
268 cache_private = ".cache";
269 cache_private = getcpy(toabsdir(cache_private));
272 ** Cache the current directory before we do any chdirs()'s.
277 ** Check for storage directory. If specified,
278 ** then store temporary files there. Else we
279 ** store them in standard nmh directory.
281 if ((cp = context_find(nmhstorage)) && *cp)
282 tmp = concat(cp, "/", invo_name, NULL);
284 tmp = getcpy(toabsdir(invo_name));
286 if (file && msgs.size)
287 adios(NULL, "cannot specify msg and file at same time!");
290 ** check if message is coming from file
293 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
294 adios(NULL, "out of memory");
297 if ((ct = parse_mime(file)));
301 ** message(s) are coming from a folder
304 app_msgarg(&msgs, seq_cur);
306 folder = getcurfol();
307 maildir = toabsdir(folder);
309 if (chdir(maildir) == NOTOK)
310 adios(maildir, "unable to change directory to");
312 /* read folder and create message structure */
313 if (!(mp = folder_read(folder)))
314 adios(NULL, "unable to read folder %s", folder);
316 /* check for empty folder */
318 adios(NULL, "no messages in %s", folder);
320 /* parse all the message ranges/sequences and set SELECTED */
321 for (msgnum = 0; msgnum < msgs.size; msgnum++)
322 if (!m_convert(mp, msgs.msgs[msgnum]))
324 seq_setprev(mp); /* set the previous-sequence */
326 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
328 adios(NULL, "out of memory");
331 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
332 if (is_selected(mp, msgnum)) {
335 msgnam = m_name(msgnum);
336 if ((ct = parse_mime(msgnam)))
346 SIGNAL(SIGQUIT, quitser);
347 SIGNAL(SIGPIPE, pipeser);
350 ** Get the associated umask for the relevant contents.
352 for (ctp = cts; *ctp; ctp++) {
356 if (type_ok(ct, 1) && !ct->c_umask) {
357 if (stat(ct->c_file, &st) != NOTOK)
358 ct->c_umask = ~(st.st_mode & 0777);
360 ct->c_umask = ~m_gmprot();
365 ** Store the message content
367 store_all_messages(cts);
369 /* Now free all the structures for the content */
370 for (ctp = cts; *ctp; ctp++)
376 /* If reading from a folder, do some updating */
378 context_replace(curfolder, folder); /* update current folder */
379 seq_setcur(mp, mp->hghsel); /* update current message */
380 seq_save(mp); /* synchronize sequences */
381 context_save(); /* save the context file */
395 fprintf(stderr, "\n");