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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
27 static struct swit switches[] = {
40 #define FILESW 6 /* interface from show */
45 { "type content", 0 },
47 { "rcache policy", 0 },
49 { "wcache policy", 0 },
56 * switches for debugging
65 extern char *tmp; /* directory to place temp files */
70 extern char *cache_public;
71 extern char *cache_private;
75 extern char *cwd; /* cache current working directory */
80 extern char *parts[NPARTS + 1];
81 extern char *types[NTYPES + 1];
87 #define quitser pipeser
90 CT parse_mime (char *);
93 int part_ok (CT, int);
94 int type_ok (CT, int);
95 void set_endian (void);
96 void flush_errors (void);
99 void store_all_messages (CT *);
102 void free_content (CT);
104 void freects_done (int) NORETURN;
109 static RETSIGTYPE pipeser (int);
113 main (int argc, char **argv)
115 int msgnum, *icachesw;
116 char *cp, *file = NULL, *folder = NULL;
117 char *maildir, buf[100], **argp;
119 struct msgs_array msgs = { 0, 0, NULL };
120 struct msgs *mp = NULL;
127 setlocale(LC_ALL, "");
129 invo_name = r1bindex (argv[0], '/');
131 /* read user profile/context */
134 arguments = getarguments (invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
142 switch (smatch (++cp, switches)) {
144 ambigsw (cp, switches);
147 adios (NULL, "-%s unknown", cp);
150 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
152 print_help (buf, switches, 1);
155 print_version(invo_name);
166 icachesw = &rcachesw;
169 icachesw = &wcachesw;
171 if (!(cp = *argp++) || *cp == '-')
172 adios (NULL, "missing argument to %s", argp[-2]);
173 switch (*icachesw = smatch (cp, caches)) {
175 ambigsw (cp, caches);
178 adios (NULL, "%s unknown", cp);
192 if (!(cp = *argp++) || *cp == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
195 adios (NULL, "too many parts (starting with %s), %d max",
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
204 adios (NULL, "too many types (starting with %s), %d max",
210 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
211 adios (NULL, "missing argument to %s", argp[-2]);
212 file = *cp == '-' ? cp : path (cp, TFILE);
226 if (*cp == '+' || *cp == '@') {
228 adios (NULL, "only one folder at a time!");
230 folder = pluspath (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)", cp);
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 (m_maildir (cache_private));
271 * Cache the current directory before we do any chdirs()'s.
273 cwd = getcpy (pwd());
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 = add (m_maildir (invo_name), NULL);
285 if (!context_find ("path"))
286 free (path ("./", TFOLDER));
288 if (file && msgs.size)
289 adios (NULL, "cannot specify msg and file at same time!");
292 * check if message is coming from file
295 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
296 adios (NULL, "out of memory");
299 if ((ct = parse_mime (file)));
303 * message(s) are coming from a folder
306 app_msgarg(&msgs, "cur");
308 folder = getfolder (1);
309 maildir = m_maildir (folder);
311 if (chdir (maildir) == NOTOK)
312 adios (maildir, "unable to change directory to");
314 /* read folder and create message structure */
315 if (!(mp = folder_read (folder)))
316 adios (NULL, "unable to read folder %s", folder);
318 /* check for empty folder */
320 adios (NULL, "no messages in %s", folder);
322 /* parse all the message ranges/sequences and set SELECTED */
323 for (msgnum = 0; msgnum < msgs.size; msgnum++)
324 if (!m_convert (mp, msgs.msgs[msgnum]))
326 seq_setprev (mp); /* set the previous-sequence */
328 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
329 adios (NULL, "out of memory");
332 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
333 if (is_selected(mp, msgnum)) {
336 msgnam = m_name (msgnum);
337 if ((ct = parse_mime (msgnam)))
347 SIGNAL (SIGQUIT, quitser);
348 SIGNAL (SIGPIPE, pipeser);
351 * Get the associated umask for the relevant contents.
353 for (ctp = cts; *ctp; ctp++) {
357 if (type_ok (ct, 1) && !ct->c_umask) {
358 if (stat (ct->c_file, &st) != NOTOK)
359 ct->c_umask = ~(st.st_mode & 0777);
361 ct->c_umask = ~m_gmprot();
366 * Store the message content
368 store_all_messages (cts);
370 /* Now free all the structures for the content */
371 for (ctp = cts; *ctp; ctp++)
377 /* If reading from a folder, do some updating */
379 context_replace (pfolder, folder);/* update current folder */
380 seq_setcur (mp, mp->hghsel); /* update current message */
381 seq_save (mp); /* synchronize sequences */
382 context_save (); /* save the context file */
396 fprintf (stderr, "\n");