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 */
434 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
441 * Check if we've specified an additional profile
443 if ((cp = getenv ("MHN"))) {
444 if ((fp = fopen (cp, "r"))) {
445 readconfig ((struct node **) 0, fp, cp, 0);
448 admonish ("", "unable to read $MHN profile (%s)", cp);
453 * Read the standard profile setup
455 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
456 readconfig ((struct node **) 0, fp, cp, 0);
460 /* Check for public cache location */
461 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
464 /* Check for private cache location */
465 if (!(cache_private = context_find (nmhprivcache)))
466 cache_private = ".cache";
467 cache_private = getcpy (m_maildir (cache_private));
470 * Cache the current directory before we do any chdirs()'s.
472 cwd = getcpy (pwd());
475 * Check for storage directory. If specified,
476 * then store temporary files there. Else we
477 * store them in standard nmh directory.
479 if ((cp = context_find (nmhstorage)) && *cp)
480 tmp = concat (cp, "/", invo_name, NULL);
482 tmp = add (m_maildir (invo_name), NULL);
484 if (!context_find ("path"))
485 free (path ("./", TFOLDER));
488 * Process a mhn composition file (mhn -build)
494 if (showsw || storesw || cachesw)
495 adios (NULL, "cannot use -build with -show, -store, -cache");
497 adios (NULL, "need to specify a %s composition file", invo_name);
499 adios (NULL, "only one %s composition file at a time", invo_name);
502 vec[vecp++] = "mhbuild";
505 vec[vecp++] = "-ebcdicsafe";
506 else if (ebcdicsw == -1)
507 vec[vecp++] = "-noebcdicsafe";
510 vec[vecp++] = "-rfc934mode";
511 else if (rfc934sw == -1)
512 vec[vecp++] = "-norfc934mode";
514 vec[vecp++] = msgs.msgs[0];
517 execvp ("mhbuild", vec);
518 fprintf (stderr, "unable to exec ");
523 * Process a mhn composition file (old MH style)
525 if (msgs.size == 1 && !folder && !npart && !cachesw
526 && !showsw && !storesw && !ntype && !file
527 && (cp = getenv ("mhdraft"))
528 && strcmp (cp, msgs.msgs[0]) == 0) {
534 vec[vecp++] = "mhbuild";
537 vec[vecp++] = "-ebcdicsafe";
538 else if (ebcdicsw == -1)
539 vec[vecp++] = "-noebcdicsafe";
542 vec[vecp++] = "-rfc934mode";
543 else if (rfc934sw == -1)
544 vec[vecp++] = "-norfc934mode";
549 execvp ("mhbuild", vec);
550 fprintf (stderr, "unable to exec ");
554 if (file && msgs.size)
555 adios (NULL, "cannot specify msg and file at same time!");
558 * check if message is coming from file
561 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
562 adios (NULL, "out of memory");
565 if ((ct = parse_mime (file)))
569 * message(s) are coming from a folder
572 app_msgarg(&msgs, "cur");
574 folder = getfolder (1);
575 maildir = m_maildir (folder);
577 if (chdir (maildir) == NOTOK)
578 adios (maildir, "unable to change directory to");
580 /* read folder and create message structure */
581 if (!(mp = folder_read (folder)))
582 adios (NULL, "unable to read folder %s", folder);
584 /* check for empty folder */
586 adios (NULL, "no messages in %s", folder);
588 /* parse all the message ranges/sequences and set SELECTED */
589 for (msgnum = 0; msgnum < msgs.size; msgnum++)
590 if (!m_convert (mp, msgs.msgs[msgnum]))
592 seq_setprev (mp); /* set the previous-sequence */
594 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
595 adios (NULL, "out of memory");
598 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
599 if (is_selected(mp, msgnum)) {
602 msgnam = m_name (msgnum);
603 if ((ct = parse_mime (msgnam)))
613 * You can't give more than one of these flags
616 if (showsw + listsw + storesw + cachesw > 1)
617 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
619 /* If no action is specified, assume -show */
620 if (!listsw && !showsw && !storesw && !cachesw)
624 SIGNAL (SIGQUIT, quitser);
625 SIGNAL (SIGPIPE, pipeser);
628 * Get the associated umask for the relevant contents.
630 for (ctp = cts; *ctp; ctp++) {
634 if (type_ok (ct, 1) && !ct->c_umask) {
635 if (stat (ct->c_file, &st) != NOTOK)
636 ct->c_umask = ~(st.st_mode & 0777);
638 ct->c_umask = ~m_gmprot();
643 * List the message content
646 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
649 * Store the message content
652 store_all_messages (cts);
655 * Cache the message content
658 cache_all_messages (cts);
661 * Show the message content
664 show_all_messages (cts);
666 /* Now free all the structures for the content */
667 for (ctp = cts; *ctp; ctp++)
673 /* If reading from a folder, do some updating */
675 context_replace (pfolder, folder);/* update current folder */
676 seq_setcur (mp, mp->hghsel); /* update current message */
677 seq_save (mp); /* synchronize sequences */
678 context_save (); /* save the context file */
692 fprintf (stderr, "\n");