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 static struct swit switches[] = {
36 #define FILESW 6 /* interface from show */
41 { "type content", 0 },
43 { "rcache policy", 0 },
45 { "wcache policy", 0 },
51 { "clobber always|auto|suffix|ask|never", 0 },
54 * switches for debugging
62 int save_clobber_policy (const char *);
63 extern int files_not_clobbered;
66 extern char *tmp; /* directory to place temp files */
71 extern char *cache_public;
72 extern char *cache_private;
76 extern char *cwd; /* cache current working directory */
81 extern char *parts[NPARTS + 1];
82 extern char *types[NTYPES + 1];
88 #define quitser pipeser
91 CT parse_mime (char *);
94 int part_ok (CT, int);
95 int type_ok (CT, int);
96 void flush_errors (void);
99 void store_all_messages (CT *);
102 void free_content (CT);
104 void freects_done (int) NORETURN;
109 static void 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);
222 if (!(cp = *argp++) || *cp == '-')
223 adios (NULL, "missing argument to %s", argp[-2]);
224 if (save_clobber_policy (cp)) {
225 adios (NULL, "invalid argument, %s, to %s", argp[-1],
234 if (*cp == '+' || *cp == '@') {
236 adios (NULL, "only one folder at a time!");
238 folder = pluspath (cp);
240 app_msgarg(&msgs, cp);
243 /* null terminate the list of acceptable parts/types */
248 * Check if we've specified an additional profile
250 if ((cp = getenv ("MHSTORE"))) {
251 if ((fp = fopen (cp, "r"))) {
252 readconfig ((struct node **) 0, fp, cp, 0);
255 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
260 * Read the standard profile setup
262 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
263 readconfig ((struct node **) 0, fp, cp, 0);
267 /* Check for public cache location */
268 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
271 /* Check for private cache location */
272 if (!(cache_private = context_find (nmhprivcache)))
273 cache_private = ".cache";
274 cache_private = getcpy (m_maildir (cache_private));
277 * Cache the current directory before we do any chdirs()'s.
279 cwd = getcpy (pwd());
282 * Check for storage directory. If specified,
283 * then store temporary files there. Else we
284 * store them in standard nmh directory.
286 if ((cp = context_find (nmhstorage)) && *cp)
287 tmp = concat (cp, "/", invo_name, NULL);
289 tmp = add (m_maildir (invo_name), NULL);
291 if (!context_find ("path"))
292 free (path ("./", TFOLDER));
294 if (file && msgs.size)
295 adios (NULL, "cannot specify msg and file at same time!");
298 * check if message is coming from file
301 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
302 adios (NULL, "out of memory");
305 if ((ct = parse_mime (file)))
309 * message(s) are coming from a folder
312 app_msgarg(&msgs, "cur");
314 folder = getfolder (1);
315 maildir = m_maildir (folder);
317 if (chdir (maildir) == NOTOK)
318 adios (maildir, "unable to change directory to");
320 /* read folder and create message structure */
321 if (!(mp = folder_read (folder)))
322 adios (NULL, "unable to read folder %s", folder);
324 /* check for empty folder */
326 adios (NULL, "no messages in %s", folder);
328 /* parse all the message ranges/sequences and set SELECTED */
329 for (msgnum = 0; msgnum < msgs.size; msgnum++)
330 if (!m_convert (mp, msgs.msgs[msgnum]))
332 seq_setprev (mp); /* set the previous-sequence */
334 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
335 adios (NULL, "out of memory");
338 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
339 if (is_selected(mp, msgnum)) {
342 msgnam = m_name (msgnum);
343 if ((ct = parse_mime (msgnam)))
353 SIGNAL (SIGQUIT, quitser);
354 SIGNAL (SIGPIPE, pipeser);
357 * Get the associated umask for the relevant contents.
359 for (ctp = cts; *ctp; ctp++) {
363 if (type_ok (ct, 1) && !ct->c_umask) {
364 if (stat (ct->c_file, &st) != NOTOK)
365 ct->c_umask = ~(st.st_mode & 0777);
367 ct->c_umask = ~m_gmprot();
372 * Store the message content
374 store_all_messages (cts);
376 /* Now free all the structures for the content */
377 for (ctp = cts; *ctp; ctp++)
383 /* If reading from a folder, do some updating */
385 context_replace (pfolder, folder);/* update current folder */
386 seq_setcur (mp, mp->hghsel); /* update current message */
387 seq_save (mp); /* synchronize sequences */
388 context_save (); /* save the context file */
391 done (files_not_clobbered);
402 fprintf (stderr, "\n");