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 set_endian (void);
92 void flush_errors (void);
95 void store_all_messages (CT *);
98 void free_content (CT);
100 void freects_done (int) NORETURN;
105 static void pipeser (int);
109 main (int argc, char **argv)
111 int msgnum, *icachesw;
112 char *cp, *file = NULL, *folder = NULL;
113 char *maildir, buf[100], **argp;
115 struct msgs_array msgs = { 0, 0, NULL };
116 struct msgs *mp = NULL;
123 setlocale(LC_ALL, "");
125 invo_name = r1bindex (argv[0], '/');
127 /* read user profile/context */
130 arguments = getarguments (invo_name, argc, argv, 1);
136 while ((cp = *argp++)) {
138 switch (smatch (++cp, switches)) {
140 ambigsw (cp, switches);
143 adios (NULL, "-%s unknown", cp);
146 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
148 print_help (buf, switches, 1);
151 print_version(invo_name);
162 icachesw = &rcachesw;
165 icachesw = &wcachesw;
167 if (!(cp = *argp++) || *cp == '-')
168 adios (NULL, "missing argument to %s", argp[-2]);
169 switch (*icachesw = smatch (cp, caches)) {
171 ambigsw (cp, caches);
174 adios (NULL, "%s unknown", cp);
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
191 adios (NULL, "too many parts (starting with %s), %d max",
197 if (!(cp = *argp++) || *cp == '-')
198 adios (NULL, "missing argument to %s", argp[-2]);
200 adios (NULL, "too many types (starting with %s), %d max",
206 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
207 adios (NULL, "missing argument to %s", argp[-2]);
208 file = *cp == '-' ? cp : path (cp, TFILE);
222 if (*cp == '+' || *cp == '@') {
224 adios (NULL, "only one folder at a time!");
226 folder = pluspath (cp);
228 app_msgarg(&msgs, cp);
231 /* null terminate the list of acceptable parts/types */
238 * Check if we've specified an additional profile
240 if ((cp = getenv ("MHSTORE"))) {
241 if ((fp = fopen (cp, "r"))) {
242 readconfig ((struct node **) 0, fp, cp, 0);
245 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
250 * Read the standard profile setup
252 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
253 readconfig ((struct node **) 0, fp, cp, 0);
257 /* Check for public cache location */
258 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
261 /* Check for private cache location */
262 if (!(cache_private = context_find (nmhprivcache)))
263 cache_private = ".cache";
264 cache_private = getcpy (m_maildir (cache_private));
267 * Cache the current directory before we do any chdirs()'s.
269 cwd = getcpy (pwd());
272 * Check for storage directory. If specified,
273 * then store temporary files there. Else we
274 * store them in standard nmh directory.
276 if ((cp = context_find (nmhstorage)) && *cp)
277 tmp = concat (cp, "/", invo_name, NULL);
279 tmp = add (m_maildir (invo_name), NULL);
281 if (!context_find ("path"))
282 free (path ("./", TFOLDER));
284 if (file && msgs.size)
285 adios (NULL, "cannot specify msg and file at same time!");
288 * check if message is coming from file
291 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
292 adios (NULL, "out of memory");
295 if ((ct = parse_mime (file)))
299 * message(s) are coming from a folder
302 app_msgarg(&msgs, "cur");
304 folder = getfolder (1);
305 maildir = m_maildir (folder);
307 if (chdir (maildir) == NOTOK)
308 adios (maildir, "unable to change directory to");
310 /* read folder and create message structure */
311 if (!(mp = folder_read (folder)))
312 adios (NULL, "unable to read folder %s", folder);
314 /* check for empty folder */
316 adios (NULL, "no messages in %s", folder);
318 /* parse all the message ranges/sequences and set SELECTED */
319 for (msgnum = 0; msgnum < msgs.size; msgnum++)
320 if (!m_convert (mp, msgs.msgs[msgnum]))
322 seq_setprev (mp); /* set the previous-sequence */
324 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
325 adios (NULL, "out of memory");
328 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
329 if (is_selected(mp, msgnum)) {
332 msgnam = m_name (msgnum);
333 if ((ct = parse_mime (msgnam)))
343 SIGNAL (SIGQUIT, quitser);
344 SIGNAL (SIGPIPE, pipeser);
347 * Get the associated umask for the relevant contents.
349 for (ctp = cts; *ctp; ctp++) {
353 if (type_ok (ct, 1) && !ct->c_umask) {
354 if (stat (ct->c_file, &st) != NOTOK)
355 ct->c_umask = ~(st.st_mode & 0777);
357 ct->c_umask = ~m_gmprot();
362 * Store the message content
364 store_all_messages (cts);
366 /* Now free all the structures for the content */
367 for (ctp = cts; *ctp; ctp++)
373 /* If reading from a folder, do some updating */
375 context_replace (pfolder, folder);/* update current folder */
376 seq_setcur (mp, mp->hghsel); /* update current message */
377 seq_save (mp); /* synchronize sequences */
378 context_save (); /* save the context file */
392 fprintf (stderr, "\n");