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 },
129 extern char *tmp; /* directory to place temp files */
134 extern char *cache_public;
135 extern char *cache_private;
142 extern int nomore; /* flags for moreproc/header display */
147 extern char *cwd; /* cache current working directory */
152 extern char *parts[NPARTS + 1];
153 extern char *types[NTYPES + 1];
160 * variables for mhbuild (mhn -build)
162 static int buildsw = 0;
163 static int ebcdicsw = 0;
164 static int rfc934sw = 0;
167 * what action to take?
169 static int cachesw = 0;
170 static int listsw = 0;
171 static int showsw = 0;
172 static int storesw = 0;
174 #define quitser pipeser
177 CT parse_mime (char *);
180 int part_ok (CT, int);
181 int type_ok (CT, int);
182 void set_endian (void);
183 void flush_errors (void);
186 void show_all_messages (CT *);
189 void list_all_messages (CT *, int, int, int, int);
192 void store_all_messages (CT *);
195 void cache_all_messages (CT *);
198 void free_content (CT);
200 void freects_done (int) NORETURN;
205 static RETSIGTYPE pipeser (int);
209 main (int argc, char **argv)
211 int sizesw = 1, headsw = 1;
212 int msgnum, *icachesw;
213 char *cp, *file = NULL, *folder = NULL;
214 char *maildir, buf[100], **argp;
216 struct msgs_array msgs = { 0, 0, NULL };
217 struct msgs *mp = NULL;
224 setlocale(LC_ALL, "");
226 invo_name = r1bindex (argv[0], '/');
228 /* read user profile/context */
231 arguments = getarguments (invo_name, argc, argv, 1);
237 while ((cp = *argp++)) {
239 switch (smatch (++cp, switches)) {
241 ambigsw (cp, switches);
244 adios (NULL, "-%s unknown", cp);
247 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
249 print_help (buf, switches, 1);
252 print_version(invo_name);
270 icachesw = &rcachesw;
273 icachesw = &wcachesw;
275 if (!(cp = *argp++) || *cp == '-')
276 adios (NULL, "missing argument to %s", argp[-2]);
277 switch (*icachesw = smatch (cp, caches)) {
279 ambigsw (cp, caches);
282 adios (NULL, "%s unknown", cp);
345 if (!(cp = *argp++) || *cp == '-')
346 adios (NULL, "missing argument to %s", argp[-2]);
348 adios (NULL, "too many parts (starting with %s), %d max",
354 if (!(cp = *argp++) || *cp == '-')
355 adios (NULL, "missing argument to %s", argp[-2]);
357 adios (NULL, "too many types (starting with %s), %d max",
363 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
364 adios (NULL, "missing argument to %s", argp[-2]);
365 file = *cp == '-' ? cp : path (cp, TFILE);
369 if (!(cp = *argp++) || *cp == '-')
370 adios (NULL, "missing argument to %s", argp[-2]);
373 formsw = getcpy (etcpath (cp));
377 * Switches for moreproc/mhlproc
380 if (!(progsw = *argp++) || *progsw == '-')
381 adios (NULL, "missing argument to %s", argp[-2]);
389 if (!(cp = *argp++) || *cp == '-')
390 adios (NULL, "missing argument to %s", argp[-2]);
394 * Switches for mhbuild
426 if (*cp == '+' || *cp == '@') {
428 adios (NULL, "only one folder at a time!");
430 folder = pluspath (cp);
432 app_msgarg(&msgs, cp);
435 /* null terminate the list of acceptable parts/types */
441 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
448 * Check if we've specified an additional profile
450 if ((cp = getenv ("MHN"))) {
451 if ((fp = fopen (cp, "r"))) {
452 readconfig ((struct node **) 0, fp, cp, 0);
455 admonish ("", "unable to read $MHN profile (%s)", cp);
460 * Read the standard profile setup
462 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
463 readconfig ((struct node **) 0, fp, cp, 0);
467 /* Check for public cache location */
468 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
471 /* Check for private cache location */
472 if (!(cache_private = context_find (nmhprivcache)))
473 cache_private = ".cache";
474 cache_private = getcpy (m_maildir (cache_private));
477 * Cache the current directory before we do any chdirs()'s.
479 cwd = getcpy (pwd());
482 * Check for storage directory. If specified,
483 * then store temporary files there. Else we
484 * store them in standard nmh directory.
486 if ((cp = context_find (nmhstorage)) && *cp)
487 tmp = concat (cp, "/", invo_name, NULL);
489 tmp = add (m_maildir (invo_name), NULL);
491 if (!context_find ("path"))
492 free (path ("./", TFOLDER));
495 * Process a mhn composition file (mhn -build)
501 if (showsw || storesw || cachesw)
502 adios (NULL, "cannot use -build with -show, -store, -cache");
504 adios (NULL, "need to specify a %s composition file", invo_name);
506 adios (NULL, "only one %s composition file at a time", invo_name);
509 vec[vecp++] = "mhbuild";
512 vec[vecp++] = "-ebcdicsafe";
513 else if (ebcdicsw == -1)
514 vec[vecp++] = "-noebcdicsafe";
517 vec[vecp++] = "-rfc934mode";
518 else if (rfc934sw == -1)
519 vec[vecp++] = "-norfc934mode";
521 vec[vecp++] = msgs.msgs[0];
524 execvp ("mhbuild", vec);
525 fprintf (stderr, "unable to exec ");
530 * Process a mhn composition file (old MH style)
532 if (msgs.size == 1 && !folder && !npart && !cachesw
533 && !showsw && !storesw && !ntype && !file
534 && (cp = getenv ("mhdraft"))
535 && strcmp (cp, msgs.msgs[0]) == 0) {
541 vec[vecp++] = "mhbuild";
544 vec[vecp++] = "-ebcdicsafe";
545 else if (ebcdicsw == -1)
546 vec[vecp++] = "-noebcdicsafe";
549 vec[vecp++] = "-rfc934mode";
550 else if (rfc934sw == -1)
551 vec[vecp++] = "-norfc934mode";
556 execvp ("mhbuild", vec);
557 fprintf (stderr, "unable to exec ");
561 if (file && msgs.size)
562 adios (NULL, "cannot specify msg and file at same time!");
565 * check if message is coming from file
568 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
569 adios (NULL, "out of memory");
572 if ((ct = parse_mime (file)));
576 * message(s) are coming from a folder
579 app_msgarg(&msgs, "cur");
581 folder = getfolder (1);
582 maildir = m_maildir (folder);
584 if (chdir (maildir) == NOTOK)
585 adios (maildir, "unable to change directory to");
587 /* read folder and create message structure */
588 if (!(mp = folder_read (folder)))
589 adios (NULL, "unable to read folder %s", folder);
591 /* check for empty folder */
593 adios (NULL, "no messages in %s", folder);
595 /* parse all the message ranges/sequences and set SELECTED */
596 for (msgnum = 0; msgnum < msgs.size; msgnum++)
597 if (!m_convert (mp, msgs.msgs[msgnum]))
599 seq_setprev (mp); /* set the previous-sequence */
601 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
602 adios (NULL, "out of memory");
605 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
606 if (is_selected(mp, msgnum)) {
609 msgnam = m_name (msgnum);
610 if ((ct = parse_mime (msgnam)))
620 * You can't give more than one of these flags
623 if (showsw + listsw + storesw + cachesw > 1)
624 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
626 /* If no action is specified, assume -show */
627 if (!listsw && !showsw && !storesw && !cachesw)
631 SIGNAL (SIGQUIT, quitser);
632 SIGNAL (SIGPIPE, pipeser);
635 * Get the associated umask for the relevant contents.
637 for (ctp = cts; *ctp; ctp++) {
641 if (type_ok (ct, 1) && !ct->c_umask) {
642 if (stat (ct->c_file, &st) != NOTOK)
643 ct->c_umask = ~(st.st_mode & 0777);
645 ct->c_umask = ~m_gmprot();
650 * List the message content
653 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
656 * Store the message content
659 store_all_messages (cts);
662 * Cache the message content
665 cache_all_messages (cts);
668 * Show the message content
671 show_all_messages (cts);
673 /* Now free all the structures for the content */
674 for (ctp = cts; *ctp; ctp++)
680 /* If reading from a folder, do some updating */
682 context_replace (pfolder, folder);/* update current folder */
683 seq_setcur (mp, mp->hghsel); /* update current message */
684 seq_save (mp); /* synchronize sequences */
685 context_save (); /* save the context file */
699 fprintf (stderr, "\n");