3 * mhn.c -- display, list, cache, or store the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
29 static struct swit switches[] = {
61 { "noserialonly", 0 },
74 #define FILESW 22 /* interface from show */
77 { "form formfile", 0 },
81 { "type content", 0 },
83 { "rcache policy", 0 },
85 { "wcache policy", 0 },
92 * switches for debugging
98 * switches for moreproc/mhlproc
101 { "moreproc program", -4 },
103 { "nomoreproc", -3 },
105 { "length lines", -4 },
107 { "width columns", -4 },
110 * switches for mhbuild
117 { "ebcdicsafe", -10 },
119 { "noebcdicsafe", -12 },
121 { "rfc934mode", -10 },
123 { "norfc934mode", -12 },
130 extern char *tmp; /* directory to place temp files */
135 extern char *cache_public;
136 extern char *cache_private;
143 extern int nomore; /* flags for moreproc/header display */
148 extern char *cwd; /* cache current working directory */
153 extern char *parts[NPARTS + 1];
154 extern char *types[NTYPES + 1];
161 * variables for mhbuild (mhn -build)
163 static int buildsw = 0;
164 static int ebcdicsw = 0;
165 static int rfc934sw = 0;
168 * what action to take?
170 static int cachesw = 0;
171 static int listsw = 0;
172 static int showsw = 0;
173 static int storesw = 0;
175 #define quitser pipeser
178 CT parse_mime (char *);
181 int part_ok (CT, int);
182 int type_ok (CT, int);
183 void set_endian (void);
184 void flush_errors (void);
187 void show_all_messages (CT *);
190 void list_all_messages (CT *, int, int, int, int);
193 void store_all_messages (CT *);
196 void cache_all_messages (CT *);
199 void free_content (CT);
201 void freects_done (int);
206 static RETSIGTYPE pipeser (int);
210 main (int argc, char **argv)
212 int sizesw = 1, headsw = 1;
213 int msgnum, *icachesw;
214 char *cp, *file = NULL, *folder = NULL;
215 char *maildir, buf[100], **argp;
217 struct msgs_array msgs = { 0, 0, NULL };
218 struct msgs *mp = NULL;
225 setlocale(LC_ALL, "");
227 invo_name = r1bindex (argv[0], '/');
229 /* read user profile/context */
232 arguments = getarguments (invo_name, argc, argv, 1);
238 while ((cp = *argp++)) {
240 switch (smatch (++cp, switches)) {
242 ambigsw (cp, switches);
245 adios (NULL, "-%s unknown", cp);
248 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
250 print_help (buf, switches, 1);
253 print_version(invo_name);
271 icachesw = &rcachesw;
274 icachesw = &wcachesw;
276 if (!(cp = *argp++) || *cp == '-')
277 adios (NULL, "missing argument to %s", argp[-2]);
278 switch (*icachesw = smatch (cp, caches)) {
280 ambigsw (cp, caches);
283 adios (NULL, "%s unknown", cp);
346 if (!(cp = *argp++) || *cp == '-')
347 adios (NULL, "missing argument to %s", argp[-2]);
349 adios (NULL, "too many parts (starting with %s), %d max",
355 if (!(cp = *argp++) || *cp == '-')
356 adios (NULL, "missing argument to %s", argp[-2]);
358 adios (NULL, "too many types (starting with %s), %d max",
364 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
365 adios (NULL, "missing argument to %s", argp[-2]);
366 file = *cp == '-' ? cp : path (cp, TFILE);
370 if (!(cp = *argp++) || *cp == '-')
371 adios (NULL, "missing argument to %s", argp[-2]);
374 formsw = getcpy (etcpath (cp));
378 * Switches for moreproc/mhlproc
381 if (!(progsw = *argp++) || *progsw == '-')
382 adios (NULL, "missing argument to %s", argp[-2]);
390 if (!(cp = *argp++) || *cp == '-')
391 adios (NULL, "missing argument to %s", argp[-2]);
395 * Switches for mhbuild
427 if (*cp == '+' || *cp == '@') {
429 adios (NULL, "only one folder at a time!");
431 folder = pluspath (cp);
433 app_msgarg(&msgs, cp);
436 /* null terminate the list of acceptable parts/types */
442 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
449 * Check if we've specified an additional profile
451 if ((cp = getenv ("MHN"))) {
452 if ((fp = fopen (cp, "r"))) {
453 readconfig ((struct node **) 0, fp, cp, 0);
456 admonish ("", "unable to read $MHN profile (%s)", cp);
461 * Read the standard profile setup
463 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
464 readconfig ((struct node **) 0, fp, cp, 0);
468 /* Check for public cache location */
469 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
472 /* Check for private cache location */
473 if (!(cache_private = context_find (nmhprivcache)))
474 cache_private = ".cache";
475 cache_private = getcpy (m_maildir (cache_private));
478 * Cache the current directory before we do any chdirs()'s.
480 cwd = getcpy (pwd());
483 * Check for storage directory. If specified,
484 * then store temporary files there. Else we
485 * store them in standard nmh directory.
487 if ((cp = context_find (nmhstorage)) && *cp)
488 tmp = concat (cp, "/", invo_name, NULL);
490 tmp = add (m_maildir (invo_name), NULL);
492 if (!context_find ("path"))
493 free (path ("./", TFOLDER));
496 * Process a mhn composition file (mhn -build)
502 if (showsw || storesw || cachesw)
503 adios (NULL, "cannot use -build with -show, -store, -cache");
505 adios (NULL, "need to specify a %s composition file", invo_name);
507 adios (NULL, "only one %s composition file at a time", invo_name);
510 vec[vecp++] = "mhbuild";
513 vec[vecp++] = "-ebcdicsafe";
514 else if (ebcdicsw == -1)
515 vec[vecp++] = "-noebcdicsafe";
518 vec[vecp++] = "-rfc934mode";
519 else if (rfc934sw == -1)
520 vec[vecp++] = "-norfc934mode";
522 vec[vecp++] = msgs.msgs[0];
525 execvp ("mhbuild", vec);
526 fprintf (stderr, "unable to exec ");
531 * Process a mhn composition file (old MH style)
533 if (msgs.size == 1 && !folder && !npart && !cachesw
534 && !showsw && !storesw && !ntype && !file
535 && (cp = getenv ("mhdraft"))
536 && strcmp (cp, msgs.msgs[0]) == 0) {
542 vec[vecp++] = "mhbuild";
545 vec[vecp++] = "-ebcdicsafe";
546 else if (ebcdicsw == -1)
547 vec[vecp++] = "-noebcdicsafe";
550 vec[vecp++] = "-rfc934mode";
551 else if (rfc934sw == -1)
552 vec[vecp++] = "-norfc934mode";
557 execvp ("mhbuild", vec);
558 fprintf (stderr, "unable to exec ");
562 if (file && msgs.size)
563 adios (NULL, "cannot specify msg and file at same time!");
566 * check if message is coming from file
569 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
570 adios (NULL, "out of memory");
573 if ((ct = parse_mime (file)));
577 * message(s) are coming from a folder
580 app_msgarg(&msgs, "cur");
582 folder = getfolder (1);
583 maildir = m_maildir (folder);
585 if (chdir (maildir) == NOTOK)
586 adios (maildir, "unable to change directory to");
588 /* read folder and create message structure */
589 if (!(mp = folder_read (folder)))
590 adios (NULL, "unable to read folder %s", folder);
592 /* check for empty folder */
594 adios (NULL, "no messages in %s", folder);
596 /* parse all the message ranges/sequences and set SELECTED */
597 for (msgnum = 0; msgnum < msgs.size; msgnum++)
598 if (!m_convert (mp, msgs.msgs[msgnum]))
600 seq_setprev (mp); /* set the previous-sequence */
602 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
603 adios (NULL, "out of memory");
606 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
607 if (is_selected(mp, msgnum)) {
610 msgnam = m_name (msgnum);
611 if ((ct = parse_mime (msgnam)))
621 * You can't give more than one of these flags
624 if (showsw + listsw + storesw + cachesw > 1)
625 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
627 /* If no action is specified, assume -show */
628 if (!listsw && !showsw && !storesw && !cachesw)
632 SIGNAL (SIGQUIT, quitser);
633 SIGNAL (SIGPIPE, pipeser);
636 * Get the associated umask for the relevant contents.
638 for (ctp = cts; *ctp; ctp++) {
642 if (type_ok (ct, 1) && !ct->c_umask) {
643 if (stat (ct->c_file, &st) != NOTOK)
644 ct->c_umask = ~(st.st_mode & 0777);
646 ct->c_umask = ~m_gmprot();
651 * List the message content
654 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
657 * Store the message content
660 store_all_messages (cts);
663 * Cache the message content
666 cache_all_messages (cts);
669 * Show the message content
672 show_all_messages (cts);
674 /* Now free all the structures for the content */
675 for (ctp = cts; *ctp; ctp++)
681 /* If reading from a folder, do some updating */
683 context_replace (pfolder, folder);/* update current folder */
684 seq_setcur (mp, mp->hghsel); /* update current message */
685 seq_save (mp); /* synchronize sequences */
686 context_save (); /* save the context file */
700 fprintf (stderr, "\n");