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 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
27 static struct swit switches[] = {
59 { "noserialonly", 0 },
72 #define FILESW 22 /* interface from show */
75 { "form formfile", 0 },
79 { "type content", 0 },
81 { "rcache policy", 0 },
83 { "wcache policy", 0 },
90 * switches for debugging
96 * switches for moreproc/mhlproc
99 { "moreproc program", -4 },
101 { "nomoreproc", -3 },
103 { "length lines", -4 },
105 { "width columns", -4 },
108 * switches for mhbuild
115 { "ebcdicsafe", -10 },
117 { "noebcdicsafe", -12 },
119 { "rfc934mode", -10 },
121 { "norfc934mode", -12 },
127 extern char *tmp; /* directory to place temp files */
132 extern char *cache_public;
133 extern char *cache_private;
140 extern int nomore; /* flags for moreproc/header display */
145 extern char *cwd; /* cache current working directory */
150 extern char *parts[NPARTS + 1];
151 extern char *types[NTYPES + 1];
158 * variables for mhbuild (mhn -build)
160 static int buildsw = 0;
161 static int ebcdicsw = 0;
162 static int rfc934sw = 0;
165 * what action to take?
167 static int cachesw = 0;
168 static int listsw = 0;
169 static int showsw = 0;
170 static int storesw = 0;
172 #define quitser pipeser
175 CT parse_mime (char *);
178 int part_ok (CT, int);
179 int type_ok (CT, int);
180 void set_endian (void);
181 void flush_errors (void);
184 void show_all_messages (CT *);
187 void list_all_messages (CT *, int, int, int, int);
190 void store_all_messages (CT *);
193 void cache_all_messages (CT *);
196 void free_content (CT);
198 void freects_done (int) NORETURN;
203 static RETSIGTYPE pipeser (int);
207 main (int argc, char **argv)
209 int sizesw = 1, headsw = 1;
210 int msgnum, *icachesw;
211 char *cp, *file = NULL, *folder = NULL;
212 char *maildir, buf[100], **argp;
214 struct msgs_array msgs = { 0, 0, NULL };
215 struct msgs *mp = NULL;
222 setlocale(LC_ALL, "");
224 invo_name = r1bindex (argv[0], '/');
226 /* read user profile/context */
229 arguments = getarguments (invo_name, argc, argv, 1);
235 while ((cp = *argp++)) {
237 switch (smatch (++cp, switches)) {
239 ambigsw (cp, switches);
242 adios (NULL, "-%s unknown", cp);
245 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
247 print_help (buf, switches, 1);
250 print_version(invo_name);
268 icachesw = &rcachesw;
271 icachesw = &wcachesw;
273 if (!(cp = *argp++) || *cp == '-')
274 adios (NULL, "missing argument to %s", argp[-2]);
275 switch (*icachesw = smatch (cp, caches)) {
277 ambigsw (cp, caches);
280 adios (NULL, "%s unknown", cp);
343 if (!(cp = *argp++) || *cp == '-')
344 adios (NULL, "missing argument to %s", argp[-2]);
346 adios (NULL, "too many parts (starting with %s), %d max",
352 if (!(cp = *argp++) || *cp == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
355 adios (NULL, "too many types (starting with %s), %d max",
361 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
362 adios (NULL, "missing argument to %s", argp[-2]);
363 file = *cp == '-' ? cp : path (cp, TFILE);
367 if (!(cp = *argp++) || *cp == '-')
368 adios (NULL, "missing argument to %s", argp[-2]);
371 formsw = getcpy (etcpath (cp));
375 * Switches for moreproc/mhlproc
378 if (!(progsw = *argp++) || *progsw == '-')
379 adios (NULL, "missing argument to %s", argp[-2]);
387 if (!(cp = *argp++) || *cp == '-')
388 adios (NULL, "missing argument to %s", argp[-2]);
392 * Switches for mhbuild
424 if (*cp == '+' || *cp == '@') {
426 adios (NULL, "only one folder at a time!");
428 folder = pluspath (cp);
430 app_msgarg(&msgs, cp);
433 /* null terminate the list of acceptable parts/types */
439 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
446 * Check if we've specified an additional profile
448 if ((cp = getenv ("MHN"))) {
449 if ((fp = fopen (cp, "r"))) {
450 readconfig ((struct node **) 0, fp, cp, 0);
453 admonish ("", "unable to read $MHN profile (%s)", cp);
458 * Read the standard profile setup
460 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
461 readconfig ((struct node **) 0, fp, cp, 0);
465 /* Check for public cache location */
466 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
469 /* Check for private cache location */
470 if (!(cache_private = context_find (nmhprivcache)))
471 cache_private = ".cache";
472 cache_private = getcpy (m_maildir (cache_private));
475 * Cache the current directory before we do any chdirs()'s.
477 cwd = getcpy (pwd());
480 * Check for storage directory. If specified,
481 * then store temporary files there. Else we
482 * store them in standard nmh directory.
484 if ((cp = context_find (nmhstorage)) && *cp)
485 tmp = concat (cp, "/", invo_name, NULL);
487 tmp = add (m_maildir (invo_name), NULL);
489 if (!context_find ("path"))
490 free (path ("./", TFOLDER));
493 * Process a mhn composition file (mhn -build)
499 if (showsw || storesw || cachesw)
500 adios (NULL, "cannot use -build with -show, -store, -cache");
502 adios (NULL, "need to specify a %s composition file", invo_name);
504 adios (NULL, "only one %s composition file at a time", invo_name);
507 vec[vecp++] = "mhbuild";
510 vec[vecp++] = "-ebcdicsafe";
511 else if (ebcdicsw == -1)
512 vec[vecp++] = "-noebcdicsafe";
515 vec[vecp++] = "-rfc934mode";
516 else if (rfc934sw == -1)
517 vec[vecp++] = "-norfc934mode";
519 vec[vecp++] = msgs.msgs[0];
522 execvp ("mhbuild", vec);
523 fprintf (stderr, "unable to exec ");
528 * Process a mhn composition file (old MH style)
530 if (msgs.size == 1 && !folder && !npart && !cachesw
531 && !showsw && !storesw && !ntype && !file
532 && (cp = getenv ("mhdraft"))
533 && strcmp (cp, msgs.msgs[0]) == 0) {
539 vec[vecp++] = "mhbuild";
542 vec[vecp++] = "-ebcdicsafe";
543 else if (ebcdicsw == -1)
544 vec[vecp++] = "-noebcdicsafe";
547 vec[vecp++] = "-rfc934mode";
548 else if (rfc934sw == -1)
549 vec[vecp++] = "-norfc934mode";
554 execvp ("mhbuild", vec);
555 fprintf (stderr, "unable to exec ");
559 if (file && msgs.size)
560 adios (NULL, "cannot specify msg and file at same time!");
563 * check if message is coming from file
566 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
567 adios (NULL, "out of memory");
570 if ((ct = parse_mime (file)));
574 * message(s) are coming from a folder
577 app_msgarg(&msgs, "cur");
579 folder = getfolder (1);
580 maildir = m_maildir (folder);
582 if (chdir (maildir) == NOTOK)
583 adios (maildir, "unable to change directory to");
585 /* read folder and create message structure */
586 if (!(mp = folder_read (folder)))
587 adios (NULL, "unable to read folder %s", folder);
589 /* check for empty folder */
591 adios (NULL, "no messages in %s", folder);
593 /* parse all the message ranges/sequences and set SELECTED */
594 for (msgnum = 0; msgnum < msgs.size; msgnum++)
595 if (!m_convert (mp, msgs.msgs[msgnum]))
597 seq_setprev (mp); /* set the previous-sequence */
599 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
600 adios (NULL, "out of memory");
603 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
604 if (is_selected(mp, msgnum)) {
607 msgnam = m_name (msgnum);
608 if ((ct = parse_mime (msgnam)))
618 * You can't give more than one of these flags
621 if (showsw + listsw + storesw + cachesw > 1)
622 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
624 /* If no action is specified, assume -show */
625 if (!listsw && !showsw && !storesw && !cachesw)
629 SIGNAL (SIGQUIT, quitser);
630 SIGNAL (SIGPIPE, pipeser);
633 * Get the associated umask for the relevant contents.
635 for (ctp = cts; *ctp; ctp++) {
639 if (type_ok (ct, 1) && !ct->c_umask) {
640 if (stat (ct->c_file, &st) != NOTOK)
641 ct->c_umask = ~(st.st_mode & 0777);
643 ct->c_umask = ~m_gmprot();
648 * List the message content
651 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
654 * Store the message content
657 store_all_messages (cts);
660 * Cache the message content
663 cache_all_messages (cts);
666 * Show the message content
669 show_all_messages (cts);
671 /* Now free all the structures for the content */
672 for (ctp = cts; *ctp; ctp++)
678 /* If reading from a folder, do some updating */
680 context_replace (pfolder, folder);/* update current folder */
681 seq_setcur (mp, mp->hghsel); /* update current message */
682 seq_save (mp); /* synchronize sequences */
683 context_save (); /* save the context file */
697 fprintf (stderr, "\n");