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 },
52 * switches for debugging
61 extern char *tmp; /* directory to place temp files */
66 extern char *cache_public;
67 extern char *cache_private;
71 extern char *cwd; /* cache current working directory */
76 extern char *parts[NPARTS + 1];
77 extern char *types[NTYPES + 1];
83 #define quitser pipeser
86 CT parse_mime (char *);
89 int part_ok (CT, int);
90 int type_ok (CT, int);
91 void flush_errors (void);
94 void store_all_messages (CT *);
97 void free_content (CT);
99 void freects_done (int) NORETURN;
104 static void pipeser (int);
108 main (int argc, char **argv)
110 int msgnum, *icachesw;
111 char *cp, *file = NULL, *folder = NULL;
112 char *maildir, buf[100], **argp;
114 struct msgs_array msgs = { 0, 0, NULL };
115 struct msgs *mp = NULL;
122 setlocale(LC_ALL, "");
124 invo_name = r1bindex (argv[0], '/');
126 /* read user profile/context */
129 arguments = getarguments (invo_name, argc, argv, 1);
135 while ((cp = *argp++)) {
137 switch (smatch (++cp, switches)) {
139 ambigsw (cp, switches);
142 adios (NULL, "-%s unknown", cp);
145 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
147 print_help (buf, switches, 1);
150 print_version(invo_name);
161 icachesw = &rcachesw;
164 icachesw = &wcachesw;
166 if (!(cp = *argp++) || *cp == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
168 switch (*icachesw = smatch (cp, caches)) {
170 ambigsw (cp, caches);
173 adios (NULL, "%s unknown", cp);
187 if (!(cp = *argp++) || *cp == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
190 adios (NULL, "too many parts (starting with %s), %d max",
196 if (!(cp = *argp++) || *cp == '-')
197 adios (NULL, "missing argument to %s", argp[-2]);
199 adios (NULL, "too many types (starting with %s), %d max",
205 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
206 adios (NULL, "missing argument to %s", argp[-2]);
207 file = *cp == '-' ? cp : path (cp, TFILE);
221 if (*cp == '+' || *cp == '@') {
223 adios (NULL, "only one folder at a time!");
225 folder = pluspath (cp);
227 app_msgarg(&msgs, cp);
230 /* null terminate the list of acceptable parts/types */
237 * Check if we've specified an additional profile
239 if ((cp = getenv ("MHSTORE"))) {
240 if ((fp = fopen (cp, "r"))) {
241 readconfig ((struct node **) 0, fp, cp, 0);
244 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
249 * Read the standard profile setup
251 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
252 readconfig ((struct node **) 0, fp, cp, 0);
256 /* Check for public cache location */
257 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
260 /* Check for private cache location */
261 if (!(cache_private = context_find (nmhprivcache)))
262 cache_private = ".cache";
263 cache_private = getcpy (m_maildir (cache_private));
266 * Cache the current directory before we do any chdirs()'s.
268 cwd = getcpy (pwd());
271 * Check for storage directory. If specified,
272 * then store temporary files there. Else we
273 * store them in standard nmh directory.
275 if ((cp = context_find (nmhstorage)) && *cp)
276 tmp = concat (cp, "/", invo_name, NULL);
278 tmp = add (m_maildir (invo_name), NULL);
280 if (!context_find ("path"))
281 free (path ("./", TFOLDER));
283 if (file && msgs.size)
284 adios (NULL, "cannot specify msg and file at same time!");
287 * check if message is coming from file
290 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
291 adios (NULL, "out of memory");
294 if ((ct = parse_mime (file)))
298 * message(s) are coming from a folder
301 app_msgarg(&msgs, "cur");
303 folder = getfolder (1);
304 maildir = m_maildir (folder);
306 if (chdir (maildir) == NOTOK)
307 adios (maildir, "unable to change directory to");
309 /* read folder and create message structure */
310 if (!(mp = folder_read (folder)))
311 adios (NULL, "unable to read folder %s", folder);
313 /* check for empty folder */
315 adios (NULL, "no messages in %s", folder);
317 /* parse all the message ranges/sequences and set SELECTED */
318 for (msgnum = 0; msgnum < msgs.size; msgnum++)
319 if (!m_convert (mp, msgs.msgs[msgnum]))
321 seq_setprev (mp); /* set the previous-sequence */
323 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
324 adios (NULL, "out of memory");
327 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
328 if (is_selected(mp, msgnum)) {
331 msgnam = m_name (msgnum);
332 if ((ct = parse_mime (msgnam)))
342 SIGNAL (SIGQUIT, quitser);
343 SIGNAL (SIGPIPE, pipeser);
346 * Get the associated umask for the relevant contents.
348 for (ctp = cts; *ctp; ctp++) {
352 if (type_ok (ct, 1) && !ct->c_umask) {
353 if (stat (ct->c_file, &st) != NOTOK)
354 ct->c_umask = ~(st.st_mode & 0777);
356 ct->c_umask = ~m_gmprot();
361 * Store the message content
363 store_all_messages (cts);
365 /* Now free all the structures for the content */
366 for (ctp = cts; *ctp; ctp++)
372 /* If reading from a folder, do some updating */
374 context_replace (pfolder, folder);/* update current folder */
375 seq_setcur (mp, mp->hghsel); /* update current message */
376 seq_save (mp); /* synchronize sequences */
377 context_save (); /* save the context file */
391 fprintf (stderr, "\n");