3 * mhn.c -- display, list, cache, or 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[] = {
55 { "noserialonly", 0 },
68 #define FILESW 22 /* interface from show */
71 { "form formfile", 0 },
75 { "type content", 0 },
77 { "rcache policy", 0 },
79 { "wcache policy", 0 },
86 * switches for debugging
92 * switches for moreproc/mhlproc
95 { "moreproc program", -4 },
99 { "length lines", -4 },
101 { "width columns", -4 },
104 * switches for mhbuild
111 { "ebcdicsafe", -10 },
113 { "noebcdicsafe", -12 },
115 { "rfc934mode", -10 },
117 { "norfc934mode", -12 },
123 extern char *tmp; /* directory to place temp files */
128 extern char *cache_public;
129 extern char *cache_private;
136 extern int nomore; /* flags for moreproc/header display */
141 extern char *cwd; /* cache current working directory */
146 extern char *parts[NPARTS + 1];
147 extern char *types[NTYPES + 1];
154 * variables for mhbuild (mhn -build)
156 static int buildsw = 0;
157 static int ebcdicsw = 0;
158 static int rfc934sw = 0;
161 * what action to take?
163 static int cachesw = 0;
164 static int listsw = 0;
165 static int showsw = 0;
166 static int storesw = 0;
168 #define quitser pipeser
171 CT parse_mime (char *);
174 int part_ok (CT, int);
175 int type_ok (CT, int);
176 void flush_errors (void);
179 void show_all_messages (CT *);
182 void list_all_messages (CT *, int, int, int, int);
185 void store_all_messages (CT *);
188 void cache_all_messages (CT *);
191 void free_content (CT);
193 void freects_done (int) NORETURN;
198 static void pipeser (int);
202 main (int argc, char **argv)
204 int sizesw = 1, headsw = 1;
205 int msgnum, *icachesw;
206 char *cp, *file = NULL, *folder = NULL;
207 char *maildir, buf[100], **argp;
209 struct msgs_array msgs = { 0, 0, NULL };
210 struct msgs *mp = NULL;
217 setlocale(LC_ALL, "");
219 invo_name = r1bindex (argv[0], '/');
221 /* read user profile/context */
224 arguments = getarguments (invo_name, argc, argv, 1);
230 while ((cp = *argp++)) {
232 switch (smatch (++cp, switches)) {
234 ambigsw (cp, switches);
237 adios (NULL, "-%s unknown", cp);
240 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
242 print_help (buf, switches, 1);
245 print_version(invo_name);
263 icachesw = &rcachesw;
266 icachesw = &wcachesw;
268 if (!(cp = *argp++) || *cp == '-')
269 adios (NULL, "missing argument to %s", argp[-2]);
270 switch (*icachesw = smatch (cp, caches)) {
272 ambigsw (cp, caches);
275 adios (NULL, "%s unknown", cp);
338 if (!(cp = *argp++) || *cp == '-')
339 adios (NULL, "missing argument to %s", argp[-2]);
341 adios (NULL, "too many parts (starting with %s), %d max",
347 if (!(cp = *argp++) || *cp == '-')
348 adios (NULL, "missing argument to %s", argp[-2]);
350 adios (NULL, "too many types (starting with %s), %d max",
356 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
357 adios (NULL, "missing argument to %s", argp[-2]);
358 file = *cp == '-' ? cp : path (cp, TFILE);
362 if (!(cp = *argp++) || *cp == '-')
363 adios (NULL, "missing argument to %s", argp[-2]);
366 formsw = getcpy (etcpath (cp));
370 * Switches for moreproc/mhlproc
373 if (!(progsw = *argp++) || *progsw == '-')
374 adios (NULL, "missing argument to %s", argp[-2]);
382 if (!(cp = *argp++) || *cp == '-')
383 adios (NULL, "missing argument to %s", argp[-2]);
387 * Switches for mhbuild
419 if (*cp == '+' || *cp == '@') {
421 adios (NULL, "only one folder at a time!");
423 folder = pluspath (cp);
425 app_msgarg(&msgs, cp);
428 /* null terminate the list of acceptable parts/types */
435 * Check if we've specified an additional profile
437 if ((cp = getenv ("MHN"))) {
438 if ((fp = fopen (cp, "r"))) {
439 readconfig ((struct node **) 0, fp, cp, 0);
442 admonish ("", "unable to read $MHN profile (%s)", cp);
447 * Read the standard profile setup
449 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
450 readconfig ((struct node **) 0, fp, cp, 0);
454 /* Check for public cache location */
455 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
458 /* Check for private cache location */
459 if (!(cache_private = context_find (nmhprivcache)))
460 cache_private = ".cache";
461 cache_private = getcpy (m_maildir (cache_private));
464 * Cache the current directory before we do any chdirs()'s.
466 cwd = getcpy (pwd());
469 * Check for storage directory. If specified,
470 * then store temporary files there. Else we
471 * store them in standard nmh directory.
473 if ((cp = context_find (nmhstorage)) && *cp)
474 tmp = concat (cp, "/", invo_name, NULL);
476 tmp = add (m_maildir (invo_name), NULL);
478 if (!context_find ("path"))
479 free (path ("./", TFOLDER));
482 * Process a mhn composition file (mhn -build)
488 if (showsw || storesw || cachesw)
489 adios (NULL, "cannot use -build with -show, -store, -cache");
491 adios (NULL, "need to specify a %s composition file", invo_name);
493 adios (NULL, "only one %s composition file at a time", invo_name);
496 vec[vecp++] = "mhbuild";
499 vec[vecp++] = "-ebcdicsafe";
500 else if (ebcdicsw == -1)
501 vec[vecp++] = "-noebcdicsafe";
504 vec[vecp++] = "-rfc934mode";
505 else if (rfc934sw == -1)
506 vec[vecp++] = "-norfc934mode";
508 vec[vecp++] = msgs.msgs[0];
511 execvp ("mhbuild", vec);
512 fprintf (stderr, "unable to exec ");
517 * Process a mhn composition file (old MH style)
519 if (msgs.size == 1 && !folder && !npart && !cachesw
520 && !showsw && !storesw && !ntype && !file
521 && (cp = getenv ("mhdraft"))
522 && strcmp (cp, msgs.msgs[0]) == 0) {
528 vec[vecp++] = "mhbuild";
531 vec[vecp++] = "-ebcdicsafe";
532 else if (ebcdicsw == -1)
533 vec[vecp++] = "-noebcdicsafe";
536 vec[vecp++] = "-rfc934mode";
537 else if (rfc934sw == -1)
538 vec[vecp++] = "-norfc934mode";
543 execvp ("mhbuild", vec);
544 fprintf (stderr, "unable to exec ");
548 if (file && msgs.size)
549 adios (NULL, "cannot specify msg and file at same time!");
552 * check if message is coming from file
555 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
556 adios (NULL, "out of memory");
559 if ((ct = parse_mime (file)))
563 * message(s) are coming from a folder
566 app_msgarg(&msgs, "cur");
568 folder = getfolder (1);
569 maildir = m_maildir (folder);
571 if (chdir (maildir) == NOTOK)
572 adios (maildir, "unable to change directory to");
574 /* read folder and create message structure */
575 if (!(mp = folder_read (folder)))
576 adios (NULL, "unable to read folder %s", folder);
578 /* check for empty folder */
580 adios (NULL, "no messages in %s", folder);
582 /* parse all the message ranges/sequences and set SELECTED */
583 for (msgnum = 0; msgnum < msgs.size; msgnum++)
584 if (!m_convert (mp, msgs.msgs[msgnum]))
586 seq_setprev (mp); /* set the previous-sequence */
588 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
589 adios (NULL, "out of memory");
592 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
593 if (is_selected(mp, msgnum)) {
596 msgnam = m_name (msgnum);
597 if ((ct = parse_mime (msgnam)))
607 * You can't give more than one of these flags
610 if (showsw + listsw + storesw + cachesw > 1)
611 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
613 /* If no action is specified, assume -show */
614 if (!listsw && !showsw && !storesw && !cachesw)
618 SIGNAL (SIGQUIT, quitser);
619 SIGNAL (SIGPIPE, pipeser);
622 * Get the associated umask for the relevant contents.
624 for (ctp = cts; *ctp; ctp++) {
628 if (type_ok (ct, 1) && !ct->c_umask) {
629 if (stat (ct->c_file, &st) != NOTOK)
630 ct->c_umask = ~(st.st_mode & 0777);
632 ct->c_umask = ~m_gmprot();
637 * List the message content
640 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
643 * Store the message content
646 store_all_messages (cts);
649 * Cache the message content
652 cache_all_messages (cts);
655 * Show the message content
658 show_all_messages (cts);
660 /* Now free all the structures for the content */
661 for (ctp = cts; *ctp; ctp++)
667 /* If reading from a folder, do some updating */
669 context_replace (pfolder, folder);/* update current folder */
670 seq_setcur (mp, mp->hghsel); /* update current message */
671 seq_save (mp); /* synchronize sequences */
672 context_save (); /* save the context file */
686 fprintf (stderr, "\n");