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
68 extern char *tmp; /* directory to place temp files */
73 extern char *cache_public;
74 extern char *cache_private;
78 extern char *cwd; /* cache current working directory */
83 extern char *parts[NPARTS + 1];
84 extern char *types[NTYPES + 1];
90 /* The list of top-level contents to display */
93 #define quitser pipeser
96 CT parse_mime (char *);
99 int part_ok (CT, int);
100 int type_ok (CT, int);
101 void set_endian (void);
102 void flush_errors (void);
105 void store_all_messages (CT *);
108 void free_content (CT);
113 static RETSIGTYPE pipeser (int);
114 static int freectp_done (int);
118 main (int argc, char **argv)
120 int msgnum, *icachesw;
121 char *cp, *file = NULL, *folder = NULL;
122 char *maildir, buf[100], **argp;
124 struct msgs_array msgs = { 0, 0, NULL };
125 struct msgs *mp = NULL;
132 setlocale(LC_ALL, "");
134 invo_name = r1bindex (argv[0], '/');
136 /* read user profile/context */
139 arguments = getarguments (invo_name, argc, argv, 1);
145 while ((cp = *argp++)) {
147 switch (smatch (++cp, switches)) {
149 ambigsw (cp, switches);
152 adios (NULL, "-%s unknown", cp);
155 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
157 print_help (buf, switches, 1);
160 print_version(invo_name);
171 icachesw = &rcachesw;
174 icachesw = &wcachesw;
176 if (!(cp = *argp++) || *cp == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
178 switch (*icachesw = smatch (cp, caches)) {
180 ambigsw (cp, caches);
183 adios (NULL, "%s unknown", cp);
197 if (!(cp = *argp++) || *cp == '-')
198 adios (NULL, "missing argument to %s", argp[-2]);
200 adios (NULL, "too many parts (starting with %s), %d max",
206 if (!(cp = *argp++) || *cp == '-')
207 adios (NULL, "missing argument to %s", argp[-2]);
209 adios (NULL, "too many types (starting with %s), %d max",
215 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
216 adios (NULL, "missing argument to %s", argp[-2]);
217 file = *cp == '-' ? cp : path (cp, TFILE);
231 if (*cp == '+' || *cp == '@') {
233 adios (NULL, "only one folder at a time!");
235 folder = pluspath (cp);
237 app_msgarg(&msgs, cp);
240 /* null terminate the list of acceptable parts/types */
247 * Check if we've specified an additional profile
249 if ((cp = getenv ("MHSTORE"))) {
250 if ((fp = fopen (cp, "r"))) {
251 readconfig ((struct node **) 0, fp, cp, 0);
254 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
259 * Read the standard profile setup
261 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
262 readconfig ((struct node **) 0, fp, cp, 0);
266 /* Check for public cache location */
267 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
270 /* Check for private cache location */
271 if (!(cache_private = context_find (nmhprivcache)))
272 cache_private = ".cache";
273 cache_private = getcpy (m_maildir (cache_private));
276 * Cache the current directory before we do any chdirs()'s.
278 cwd = getcpy (pwd());
281 * Check for storage directory. If specified,
282 * then store temporary files there. Else we
283 * store them in standard nmh directory.
285 if ((cp = context_find (nmhstorage)) && *cp)
286 tmp = concat (cp, "/", invo_name, NULL);
288 tmp = add (m_maildir (invo_name), NULL);
290 if (!context_find ("path"))
291 free (path ("./", TFOLDER));
293 if (file && msgs.size)
294 adios (NULL, "cannot specify msg and file at same time!");
297 * check if message is coming from file
300 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
301 adios (NULL, "out of memory");
304 if ((ct = parse_mime (file)));
308 * message(s) are coming from a folder
311 app_msgarg(&msgs, "cur");
313 folder = getfolder (1);
314 maildir = m_maildir (folder);
316 if (chdir (maildir) == NOTOK)
317 adios (maildir, "unable to change directory to");
319 /* read folder and create message structure */
320 if (!(mp = folder_read (folder)))
321 adios (NULL, "unable to read folder %s", folder);
323 /* check for empty folder */
325 adios (NULL, "no messages in %s", folder);
327 /* parse all the message ranges/sequences and set SELECTED */
328 for (msgnum = 0; msgnum < msgs.size; msgnum++)
329 if (!m_convert (mp, msgs.msgs[msgnum]))
331 seq_setprev (mp); /* set the previous-sequence */
333 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
334 adios (NULL, "out of memory");
337 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
338 if (is_selected(mp, msgnum)) {
341 msgnam = m_name (msgnum);
342 if ((ct = parse_mime (msgnam)))
352 SIGNAL (SIGQUIT, quitser);
353 SIGNAL (SIGPIPE, pipeser);
356 * Get the associated umask for the relevant contents.
358 for (ctp = cts; *ctp; ctp++) {
362 if (type_ok (ct, 1) && !ct->c_umask) {
363 if (stat (ct->c_file, &st) != NOTOK)
364 ct->c_umask = ~(st.st_mode & 0777);
366 ct->c_umask = ~m_gmprot();
371 * Store the message content
373 store_all_messages (cts);
375 /* Now free all the structures for the content */
376 for (ctp = cts; *ctp; ctp++)
382 /* If reading from a folder, do some updating */
384 context_replace (pfolder, folder);/* update current folder */
385 seq_setcur (mp, mp->hghsel); /* update current message */
386 seq_save (mp); /* synchronize sequences */
387 context_save (); /* save the context file */
400 fprintf (stderr, "\n");
410 freectp_done (int status)
419 return 1; /* dead code to satisfy the compiler */