2 ** mhstore.c -- store the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
26 static struct swit switches[] = {
39 #define FILESW 6 /* interface from show */
44 { "type content", 0 },
46 { "rcache policy", 0 },
48 { "wcache policy", 0 },
55 ** switches for debugging
64 extern char *tmp; /* directory to place temp files */
69 extern char *cache_public;
70 extern char *cache_private;
74 extern char *cwd; /* cache current working directory */
79 extern char *parts[NPARTS + 1];
80 extern char *types[NTYPES + 1];
86 #define quitser pipeser
89 CT parse_mime (char *);
92 int part_ok (CT, int);
93 int type_ok (CT, int);
94 void set_endian (void);
95 void flush_errors (void);
98 void store_all_messages (CT *);
101 void free_content (CT);
103 void freects_done (int) NORETURN;
108 static RETSIGTYPE pipeser (int);
112 main (int argc, char **argv)
114 int msgnum, *icachesw;
115 char *cp, *file = NULL, *folder = NULL;
116 char *maildir, buf[100], **argp;
118 struct msgs_array msgs = { 0, 0, NULL };
119 struct msgs *mp = NULL;
126 setlocale(LC_ALL, "");
128 invo_name = r1bindex (argv[0], '/');
130 /* read user profile/context */
133 arguments = getarguments (invo_name, argc, argv, 1);
139 while ((cp = *argp++)) {
141 switch (smatch (++cp, switches)) {
143 ambigsw (cp, switches);
146 adios (NULL, "-%s unknown", cp);
149 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
151 print_help (buf, switches, 1);
154 print_version(invo_name);
165 icachesw = &rcachesw;
168 icachesw = &wcachesw;
170 if (!(cp = *argp++) || *cp == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
172 switch (*icachesw = smatch (cp, caches)) {
174 ambigsw (cp, caches);
177 adios (NULL, "%s unknown", cp);
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
194 adios (NULL, "too many parts (starting with %s), %d max",
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
203 adios (NULL, "too many types (starting with %s), %d max",
209 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
210 adios (NULL, "missing argument to %s", argp[-2]);
211 file = *cp == '-' ? cp : path (cp, TFILE);
225 if (*cp == '+' || *cp == '@') {
227 adios (NULL, "only one folder at a time!");
229 folder = pluspath (cp);
231 app_msgarg(&msgs, cp);
234 /* null terminate the list of acceptable parts/types */
241 ** Check if we've specified an additional profile
243 if ((cp = getenv ("MHSTORE"))) {
244 if ((fp = fopen (cp, "r"))) {
245 readconfig ((struct node **) 0, fp, cp, 0);
248 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
253 ** Read the standard profile setup
255 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
256 readconfig ((struct node **) 0, fp, cp, 0);
260 /* Check for public cache location */
261 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
264 /* Check for private cache location */
265 if (!(cache_private = context_find (nmhprivcache)))
266 cache_private = ".cache";
267 cache_private = getcpy (m_maildir (cache_private));
270 ** Cache the current directory before we do any chdirs()'s.
272 cwd = getcpy (pwd());
275 ** Check for storage directory. If specified,
276 ** then store temporary files there. Else we
277 ** store them in standard nmh directory.
279 if ((cp = context_find (nmhstorage)) && *cp)
280 tmp = concat (cp, "/", invo_name, NULL);
282 tmp = add (m_maildir (invo_name), NULL);
284 if (!context_find ("path"))
285 free (path ("./", TFOLDER));
287 if (file && msgs.size)
288 adios (NULL, "cannot specify msg and file at same time!");
291 ** check if message is coming from file
294 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
295 adios (NULL, "out of memory");
298 if ((ct = parse_mime (file)));
302 ** message(s) are coming from a folder
305 app_msgarg(&msgs, "cur");
307 folder = getfolder (1);
308 maildir = m_maildir (folder);
310 if (chdir (maildir) == NOTOK)
311 adios (maildir, "unable to change directory to");
313 /* read folder and create message structure */
314 if (!(mp = folder_read (folder)))
315 adios (NULL, "unable to read folder %s", folder);
317 /* check for empty folder */
319 adios (NULL, "no messages in %s", folder);
321 /* parse all the message ranges/sequences and set SELECTED */
322 for (msgnum = 0; msgnum < msgs.size; msgnum++)
323 if (!m_convert (mp, msgs.msgs[msgnum]))
325 seq_setprev (mp); /* set the previous-sequence */
327 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1),
329 adios (NULL, "out of memory");
332 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
333 if (is_selected(mp, msgnum)) {
336 msgnam = m_name (msgnum);
337 if ((ct = parse_mime (msgnam)))
347 SIGNAL (SIGQUIT, quitser);
348 SIGNAL (SIGPIPE, pipeser);
351 ** Get the associated umask for the relevant contents.
353 for (ctp = cts; *ctp; ctp++) {
357 if (type_ok (ct, 1) && !ct->c_umask) {
358 if (stat (ct->c_file, &st) != NOTOK)
359 ct->c_umask = ~(st.st_mode & 0777);
361 ct->c_umask = ~m_gmprot();
366 ** Store the message content
368 store_all_messages (cts);
370 /* Now free all the structures for the content */
371 for (ctp = cts; *ctp; ctp++)
377 /* If reading from a folder, do some updating */
379 context_replace (pfolder, folder); /* update current folder */
380 seq_setcur (mp, mp->hghsel); /* update current message */
381 seq_save (mp); /* synchronize sequences */
382 context_save (); /* save the context file */
396 fprintf (stderr, "\n");