3 * mhstore.c -- store the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
30 * We allocate space for message names (msgs array)
31 * this number of elements at a time.
36 static struct swit switches[] = {
49 #define FILESW 6 /* interface from show */
54 { "type content", 0 },
56 { "rcache policy", 0 },
58 { "wcache policy", 0 },
65 * switches for debugging
75 extern char *tmp; /* directory to place temp files */
80 extern char *cache_public;
81 extern char *cache_private;
85 extern char *cwd; /* cache current working directory */
90 extern char *parts[NPARTS + 1];
91 extern char *types[NTYPES + 1];
97 /* The list of top-level contents to display */
100 #define quitser pipeser
103 CT parse_mime (char *);
106 int part_ok (CT, int);
107 int type_ok (CT, int);
108 void set_endian (void);
109 void flush_errors (void);
112 void store_all_messages (CT *);
115 void free_content (CT);
120 static RETSIGTYPE pipeser (int);
124 main (int argc, char **argv)
126 int nummsgs, maxmsgs, msgnum, *icachesw;
127 char *cp, *file = NULL, *folder = NULL;
128 char *maildir, buf[100], **argp;
129 char **arguments, **msgs;
130 struct msgs *mp = NULL;
135 setlocale(LC_ALL, "");
137 invo_name = r1bindex (argv[0], '/');
139 /* read user profile/context */
142 arguments = getarguments (invo_name, argc, argv, 1);
146 * Allocate the initial space to record message
147 * names, ranges, and sequences.
151 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
156 while ((cp = *argp++)) {
158 switch (smatch (++cp, switches)) {
160 ambigsw (cp, switches);
163 adios (NULL, "-%s unknown", cp);
166 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
168 print_help (buf, switches, 1);
171 print_version(invo_name);
182 icachesw = &rcachesw;
185 icachesw = &wcachesw;
187 if (!(cp = *argp++) || *cp == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
189 switch (*icachesw = smatch (cp, caches)) {
191 ambigsw (cp, caches);
194 adios (NULL, "%s unknown", cp);
208 if (!(cp = *argp++) || *cp == '-')
209 adios (NULL, "missing argument to %s", argp[-2]);
211 adios (NULL, "too many parts (starting with %s), %d max",
217 if (!(cp = *argp++) || *cp == '-')
218 adios (NULL, "missing argument to %s", argp[-2]);
220 adios (NULL, "too many types (starting with %s), %d max",
226 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
227 adios (NULL, "missing argument to %s", argp[-2]);
228 file = *cp == '-' ? cp : path (cp, TFILE);
242 if (*cp == '+' || *cp == '@') {
244 adios (NULL, "only one folder at a time!");
246 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
249 * Check if we need to allocate more space
250 * for message names/ranges/sequences.
252 if (nummsgs >= maxmsgs) {
254 msgs = (char **) mh_xrealloc (msgs,
255 (size_t) (maxmsgs * sizeof(*msgs)));
257 msgs[nummsgs++] = cp;
261 /* null terminate the list of acceptable parts/types */
268 * Check if we've specified an additional profile
270 if ((cp = getenv ("MHSTORE"))) {
271 if ((fp = fopen (cp, "r"))) {
272 readconfig ((struct node **) 0, fp, cp, 0);
275 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
280 * Read the standard profile setup
282 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
283 readconfig ((struct node **) 0, fp, cp, 0);
287 /* Check for public cache location */
288 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
291 /* Check for private cache location */
292 if (!(cache_private = context_find (nmhprivcache)))
293 cache_private = ".cache";
294 cache_private = getcpy (m_maildir (cache_private));
297 * Cache the current directory before we do any chdirs()'s.
299 cwd = getcpy (pwd());
302 * Check for storage directory. If specified,
303 * then store temporary files there. Else we
304 * store them in standard nmh directory.
306 if ((cp = context_find (nmhstorage)) && *cp)
307 tmp = concat (cp, "/", invo_name, NULL);
309 tmp = add (m_maildir (invo_name), NULL);
311 if (!context_find ("path"))
312 free (path ("./", TFOLDER));
315 adios (NULL, "cannot specify msg and file at same time!");
318 * check if message is coming from file
321 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
322 adios (NULL, "out of memory");
325 if ((ct = parse_mime (file)));
329 * message(s) are coming from a folder
332 msgs[nummsgs++] = "cur";
334 folder = getfolder (1);
335 maildir = m_maildir (folder);
337 if (chdir (maildir) == NOTOK)
338 adios (maildir, "unable to change directory to");
340 /* read folder and create message structure */
341 if (!(mp = folder_read (folder)))
342 adios (NULL, "unable to read folder %s", folder);
344 /* check for empty folder */
346 adios (NULL, "no messages in %s", folder);
348 /* parse all the message ranges/sequences and set SELECTED */
349 for (msgnum = 0; msgnum < nummsgs; msgnum++)
350 if (!m_convert (mp, msgs[msgnum]))
352 seq_setprev (mp); /* set the previous-sequence */
354 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
355 adios (NULL, "out of memory");
358 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
359 if (is_selected(mp, msgnum)) {
362 msgnam = m_name (msgnum);
363 if ((ct = parse_mime (msgnam)))
373 SIGNAL (SIGQUIT, quitser);
374 SIGNAL (SIGPIPE, pipeser);
377 * Get the associated umask for the relevant contents.
379 for (ctp = cts; *ctp; ctp++) {
383 if (type_ok (ct, 1) && !ct->c_umask) {
384 if (stat (ct->c_file, &st) != NOTOK)
385 ct->c_umask = ~(st.st_mode & 0777);
387 ct->c_umask = ~m_gmprot();
392 * Store the message content
394 store_all_messages (cts);
396 /* Now free all the structures for the content */
397 for (ctp = cts; *ctp; ctp++)
403 /* If reading from a folder, do some updating */
405 context_replace (pfolder, folder);/* update current folder */
406 seq_setcur (mp, mp->hghsel); /* update current message */
407 seq_save (mp); /* synchronize sequences */
408 context_save (); /* save the context file */
421 fprintf (stderr, "\n");
440 return 1; /* dead code to satisfy the compiler */