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 */
235 * Check if we've specified an additional profile
237 if ((cp = getenv ("MHSTORE"))) {
238 if ((fp = fopen (cp, "r"))) {
239 readconfig ((struct node **) 0, fp, cp, 0);
242 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
247 * Read the standard profile setup
249 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
250 readconfig ((struct node **) 0, fp, cp, 0);
254 /* Check for public cache location */
255 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
258 /* Check for private cache location */
259 if (!(cache_private = context_find (nmhprivcache)))
260 cache_private = ".cache";
261 cache_private = getcpy (m_maildir (cache_private));
264 * Cache the current directory before we do any chdirs()'s.
266 cwd = getcpy (pwd());
269 * Check for storage directory. If specified,
270 * then store temporary files there. Else we
271 * store them in standard nmh directory.
273 if ((cp = context_find (nmhstorage)) && *cp)
274 tmp = concat (cp, "/", invo_name, NULL);
276 tmp = add (m_maildir (invo_name), NULL);
278 if (!context_find ("path"))
279 free (path ("./", TFOLDER));
281 if (file && msgs.size)
282 adios (NULL, "cannot specify msg and file at same time!");
285 * check if message is coming from file
288 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
289 adios (NULL, "out of memory");
292 if ((ct = parse_mime (file)))
296 * message(s) are coming from a folder
299 app_msgarg(&msgs, "cur");
301 folder = getfolder (1);
302 maildir = m_maildir (folder);
304 if (chdir (maildir) == NOTOK)
305 adios (maildir, "unable to change directory to");
307 /* read folder and create message structure */
308 if (!(mp = folder_read (folder)))
309 adios (NULL, "unable to read folder %s", folder);
311 /* check for empty folder */
313 adios (NULL, "no messages in %s", folder);
315 /* parse all the message ranges/sequences and set SELECTED */
316 for (msgnum = 0; msgnum < msgs.size; msgnum++)
317 if (!m_convert (mp, msgs.msgs[msgnum]))
319 seq_setprev (mp); /* set the previous-sequence */
321 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
322 adios (NULL, "out of memory");
325 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
326 if (is_selected(mp, msgnum)) {
329 msgnam = m_name (msgnum);
330 if ((ct = parse_mime (msgnam)))
340 SIGNAL (SIGQUIT, quitser);
341 SIGNAL (SIGPIPE, pipeser);
344 * Get the associated umask for the relevant contents.
346 for (ctp = cts; *ctp; ctp++) {
350 if (type_ok (ct, 1) && !ct->c_umask) {
351 if (stat (ct->c_file, &st) != NOTOK)
352 ct->c_umask = ~(st.st_mode & 0777);
354 ct->c_umask = ~m_gmprot();
359 * Store the message content
361 store_all_messages (cts);
363 /* Now free all the structures for the content */
364 for (ctp = cts; *ctp; ctp++)
370 /* If reading from a folder, do some updating */
372 context_replace (pfolder, folder);/* update current folder */
373 seq_setcur (mp, mp->hghsel); /* update current message */
374 seq_save (mp); /* synchronize sequences */
375 context_save (); /* save the context file */
389 fprintf (stderr, "\n");