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>
29 static struct swit switches[] = {
42 #define FILESW 6 /* interface from show */
47 { "type content", 0 },
49 { "rcache policy", 0 },
51 { "wcache policy", 0 },
58 * switches for debugging
67 extern char *tmp; /* directory to place temp files */
72 extern char *cache_public;
73 extern char *cache_private;
77 extern char *cwd; /* cache current working directory */
82 extern char *parts[NPARTS + 1];
83 extern char *types[NTYPES + 1];
89 #define quitser pipeser
92 CT parse_mime (char *);
95 int part_ok (CT, int);
96 int type_ok (CT, int);
97 void set_endian (void);
98 void flush_errors (void);
101 void store_all_messages (CT *);
104 void free_content (CT);
106 void freects_done (int) NORETURN;
111 static RETSIGTYPE pipeser (int);
115 main (int argc, char **argv)
117 int msgnum, *icachesw;
118 char *cp, *file = NULL, *folder = NULL;
119 char *maildir, buf[100], **argp;
121 struct msgs_array msgs = { 0, 0, NULL };
122 struct msgs *mp = NULL;
129 setlocale(LC_ALL, "");
131 invo_name = r1bindex (argv[0], '/');
133 /* read user profile/context */
136 arguments = getarguments (invo_name, argc, argv, 1);
142 while ((cp = *argp++)) {
144 switch (smatch (++cp, switches)) {
146 ambigsw (cp, switches);
149 adios (NULL, "-%s unknown", cp);
152 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
154 print_help (buf, switches, 1);
157 print_version(invo_name);
168 icachesw = &rcachesw;
171 icachesw = &wcachesw;
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 switch (*icachesw = smatch (cp, caches)) {
177 ambigsw (cp, caches);
180 adios (NULL, "%s unknown", cp);
194 if (!(cp = *argp++) || *cp == '-')
195 adios (NULL, "missing argument to %s", argp[-2]);
197 adios (NULL, "too many parts (starting with %s), %d max",
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
206 adios (NULL, "too many types (starting with %s), %d max",
212 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
213 adios (NULL, "missing argument to %s", argp[-2]);
214 file = *cp == '-' ? cp : path (cp, TFILE);
228 if (*cp == '+' || *cp == '@') {
230 adios (NULL, "only one folder at a time!");
232 folder = pluspath (cp);
234 app_msgarg(&msgs, cp);
237 /* null terminate the list of acceptable parts/types */
244 * Check if we've specified an additional profile
246 if ((cp = getenv ("MHSTORE"))) {
247 if ((fp = fopen (cp, "r"))) {
248 readconfig ((struct node **) 0, fp, cp, 0);
251 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
256 * Read the standard profile setup
258 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
259 readconfig ((struct node **) 0, fp, cp, 0);
263 /* Check for public cache location */
264 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
267 /* Check for private cache location */
268 if (!(cache_private = context_find (nmhprivcache)))
269 cache_private = ".cache";
270 cache_private = getcpy (m_maildir (cache_private));
273 * Cache the current directory before we do any chdirs()'s.
275 cwd = getcpy (pwd());
278 * Check for storage directory. If specified,
279 * then store temporary files there. Else we
280 * store them in standard nmh directory.
282 if ((cp = context_find (nmhstorage)) && *cp)
283 tmp = concat (cp, "/", invo_name, NULL);
285 tmp = add (m_maildir (invo_name), NULL);
287 if (!context_find ("path"))
288 free (path ("./", TFOLDER));
290 if (file && msgs.size)
291 adios (NULL, "cannot specify msg and file at same time!");
294 * check if message is coming from file
297 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
298 adios (NULL, "out of memory");
301 if ((ct = parse_mime (file)));
305 * message(s) are coming from a folder
308 app_msgarg(&msgs, "cur");
310 folder = getfolder (1);
311 maildir = m_maildir (folder);
313 if (chdir (maildir) == NOTOK)
314 adios (maildir, "unable to change directory to");
316 /* read folder and create message structure */
317 if (!(mp = folder_read (folder)))
318 adios (NULL, "unable to read folder %s", folder);
320 /* check for empty folder */
322 adios (NULL, "no messages in %s", folder);
324 /* parse all the message ranges/sequences and set SELECTED */
325 for (msgnum = 0; msgnum < msgs.size; msgnum++)
326 if (!m_convert (mp, msgs.msgs[msgnum]))
328 seq_setprev (mp); /* set the previous-sequence */
330 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
331 adios (NULL, "out of memory");
334 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
335 if (is_selected(mp, msgnum)) {
338 msgnam = m_name (msgnum);
339 if ((ct = parse_mime (msgnam)))
349 SIGNAL (SIGQUIT, quitser);
350 SIGNAL (SIGPIPE, pipeser);
353 * Get the associated umask for the relevant contents.
355 for (ctp = cts; *ctp; ctp++) {
359 if (type_ok (ct, 1) && !ct->c_umask) {
360 if (stat (ct->c_file, &st) != NOTOK)
361 ct->c_umask = ~(st.st_mode & 0777);
363 ct->c_umask = ~m_gmprot();
368 * Store the message content
370 store_all_messages (cts);
372 /* Now free all the structures for the content */
373 for (ctp = cts; *ctp; ctp++)
379 /* If reading from a folder, do some updating */
381 context_replace (pfolder, folder);/* update current folder */
382 seq_setcur (mp, mp->hghsel); /* update current message */
383 seq_save (mp); /* synchronize sequences */
384 context_save (); /* save the context file */
398 fprintf (stderr, "\n");