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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
38 #define FILESW 6 /* interface from show */
43 { "type content", 0 },
45 { "rcache policy", 0 },
47 { "wcache policy", 0 },
54 ** switches for debugging
63 extern char *tmp; /* directory to place temp files */
68 extern char *cache_public;
69 extern char *cache_private;
73 extern char *cwd; /* cache current working directory */
78 extern char *parts[NPARTS + 1];
79 extern char *types[NTYPES + 1];
85 #define quitser pipeser
88 CT parse_mime(char *);
93 void set_endian(void);
94 void flush_errors(void);
97 void store_all_messages(CT *);
100 void free_content(CT);
102 void freects_done(int) NORETURN;
107 static RETSIGTYPE pipeser(int);
111 main(int argc, char **argv)
113 int msgnum, *icachesw;
114 char *cp, *file = NULL, *folder = NULL;
115 char *maildir, buf[100], **argp;
117 struct msgs_array msgs = { 0, 0, NULL };
118 struct msgs *mp = NULL;
125 setlocale(LC_ALL, "");
127 invo_name = mhbasename(argv[0]);
129 /* read user profile/context */
132 arguments = getarguments(invo_name, argc, argv, 1);
138 while ((cp = *argp++)) {
140 switch (smatch(++cp, switches)) {
142 ambigsw(cp, switches);
145 adios(NULL, "-%s unknown", cp);
148 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
149 print_help(buf, switches, 1);
152 print_version(invo_name);
163 icachesw = &rcachesw;
166 icachesw = &wcachesw;
168 if (!(cp = *argp++) || *cp == '-')
169 adios(NULL, "missing argument to %s",
171 switch (*icachesw = smatch(cp, caches)) {
176 adios(NULL, "%s unknown", cp);
190 if (!(cp = *argp++) || *cp == '-')
191 adios(NULL, "missing argument to %s",
194 adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
199 if (!(cp = *argp++) || *cp == '-')
200 adios(NULL, "missing argument to %s",
203 adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
208 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
209 adios(NULL, "missing argument to %s",
211 file = *cp == '-' ? cp : getcpy(expanddir(cp));
225 if (*cp == '+' || *cp == '@') {
227 adios(NULL, "only one folder at a time!");
229 folder = getcpy(expandfol(cp));
231 app_msgarg(&msgs, cp);
234 /* null terminate the list of acceptable parts/types */
241 ** Check if we've specified an additional profile
243 if ((cp = getenv("MHSTORE"))) {
244 if ((fp = fopen(cp, "r"))) {
245 readconfig((struct node **) 0, fp, cp, 0);
248 admonish("", "unable to read $MHSTORE profile (%s)",
254 ** Read the standard profile setup
256 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
257 readconfig((struct node **) 0, fp, cp, 0);
261 /* Check for public cache location */
262 if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
265 /* Check for private cache location */
266 if (!(cache_private = context_find(nmhprivcache)))
267 cache_private = ".cache";
268 cache_private = getcpy(toabsdir(cache_private));
271 ** Cache the current directory before we do any chdirs()'s.
276 ** Check for storage directory. If specified,
277 ** then store temporary files there. Else we
278 ** store them in standard nmh directory.
280 if ((cp = context_find(nmhstorage)) && *cp)
281 tmp = concat(cp, "/", invo_name, NULL);
283 tmp = getcpy(toabsdir(invo_name));
285 if (file && msgs.size)
286 adios(NULL, "cannot specify msg and file at same time!");
289 ** check if message is coming from file
292 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
293 adios(NULL, "out of memory");
296 if ((ct = parse_mime(file)));
300 ** message(s) are coming from a folder
303 app_msgarg(&msgs, seq_cur);
305 folder = getcurfol();
306 maildir = toabsdir(folder);
308 if (chdir(maildir) == NOTOK)
309 adios(maildir, "unable to change directory to");
311 /* read folder and create message structure */
312 if (!(mp = folder_read(folder)))
313 adios(NULL, "unable to read folder %s", folder);
315 /* check for empty folder */
317 adios(NULL, "no messages in %s", folder);
319 /* parse all the message ranges/sequences and set SELECTED */
320 for (msgnum = 0; msgnum < msgs.size; msgnum++)
321 if (!m_convert(mp, msgs.msgs[msgnum]))
323 seq_setprev(mp); /* set the previous-sequence */
325 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
327 adios(NULL, "out of memory");
330 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
331 if (is_selected(mp, msgnum)) {
334 msgnam = m_name(msgnum);
335 if ((ct = parse_mime(msgnam)))
345 SIGNAL(SIGQUIT, quitser);
346 SIGNAL(SIGPIPE, pipeser);
349 ** Get the associated umask for the relevant contents.
351 for (ctp = cts; *ctp; ctp++) {
355 if (type_ok(ct, 1) && !ct->c_umask) {
356 if (stat(ct->c_file, &st) != NOTOK)
357 ct->c_umask = ~(st.st_mode & 0777);
359 ct->c_umask = ~m_gmprot();
364 ** Store the message content
366 store_all_messages(cts);
368 /* Now free all the structures for the content */
369 for (ctp = cts; *ctp; ctp++)
375 /* If reading from a folder, do some updating */
377 context_replace(curfolder, folder); /* update current folder */
378 seq_setcur(mp, mp->hghsel); /* update current message */
379 seq_save(mp); /* synchronize sequences */
380 context_save(); /* save the context file */
394 fprintf(stderr, "\n");