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 set_endian (void);
177 void flush_errors (void);
180 void show_all_messages (CT *);
183 void list_all_messages (CT *, int, int, int, int);
186 void store_all_messages (CT *);
189 void cache_all_messages (CT *);
192 void free_content (CT);
194 void freects_done (int) NORETURN;
199 static void pipeser (int);
203 main (int argc, char **argv)
205 int sizesw = 1, headsw = 1;
206 int msgnum, *icachesw;
207 char *cp, *file = NULL, *folder = NULL;
208 char *maildir, buf[100], **argp;
210 struct msgs_array msgs = { 0, 0, NULL };
211 struct msgs *mp = NULL;
218 setlocale(LC_ALL, "");
220 invo_name = r1bindex (argv[0], '/');
222 /* read user profile/context */
225 arguments = getarguments (invo_name, argc, argv, 1);
231 while ((cp = *argp++)) {
233 switch (smatch (++cp, switches)) {
235 ambigsw (cp, switches);
238 adios (NULL, "-%s unknown", cp);
241 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
243 print_help (buf, switches, 1);
246 print_version(invo_name);
264 icachesw = &rcachesw;
267 icachesw = &wcachesw;
269 if (!(cp = *argp++) || *cp == '-')
270 adios (NULL, "missing argument to %s", argp[-2]);
271 switch (*icachesw = smatch (cp, caches)) {
273 ambigsw (cp, caches);
276 adios (NULL, "%s unknown", cp);
339 if (!(cp = *argp++) || *cp == '-')
340 adios (NULL, "missing argument to %s", argp[-2]);
342 adios (NULL, "too many parts (starting with %s), %d max",
348 if (!(cp = *argp++) || *cp == '-')
349 adios (NULL, "missing argument to %s", argp[-2]);
351 adios (NULL, "too many types (starting with %s), %d max",
357 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
358 adios (NULL, "missing argument to %s", argp[-2]);
359 file = *cp == '-' ? cp : path (cp, TFILE);
363 if (!(cp = *argp++) || *cp == '-')
364 adios (NULL, "missing argument to %s", argp[-2]);
367 formsw = getcpy (etcpath (cp));
371 * Switches for moreproc/mhlproc
374 if (!(progsw = *argp++) || *progsw == '-')
375 adios (NULL, "missing argument to %s", argp[-2]);
383 if (!(cp = *argp++) || *cp == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
388 * Switches for mhbuild
420 if (*cp == '+' || *cp == '@') {
422 adios (NULL, "only one folder at a time!");
424 folder = pluspath (cp);
426 app_msgarg(&msgs, cp);
429 /* null terminate the list of acceptable parts/types */
435 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
442 * Check if we've specified an additional profile
444 if ((cp = getenv ("MHN"))) {
445 if ((fp = fopen (cp, "r"))) {
446 readconfig ((struct node **) 0, fp, cp, 0);
449 admonish ("", "unable to read $MHN profile (%s)", cp);
454 * Read the standard profile setup
456 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
457 readconfig ((struct node **) 0, fp, cp, 0);
461 /* Check for public cache location */
462 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
465 /* Check for private cache location */
466 if (!(cache_private = context_find (nmhprivcache)))
467 cache_private = ".cache";
468 cache_private = getcpy (m_maildir (cache_private));
471 * Cache the current directory before we do any chdirs()'s.
473 cwd = getcpy (pwd());
476 * Check for storage directory. If specified,
477 * then store temporary files there. Else we
478 * store them in standard nmh directory.
480 if ((cp = context_find (nmhstorage)) && *cp)
481 tmp = concat (cp, "/", invo_name, NULL);
483 tmp = add (m_maildir (invo_name), NULL);
485 if (!context_find ("path"))
486 free (path ("./", TFOLDER));
489 * Process a mhn composition file (mhn -build)
495 if (showsw || storesw || cachesw)
496 adios (NULL, "cannot use -build with -show, -store, -cache");
498 adios (NULL, "need to specify a %s composition file", invo_name);
500 adios (NULL, "only one %s composition file at a time", invo_name);
503 vec[vecp++] = "mhbuild";
506 vec[vecp++] = "-ebcdicsafe";
507 else if (ebcdicsw == -1)
508 vec[vecp++] = "-noebcdicsafe";
511 vec[vecp++] = "-rfc934mode";
512 else if (rfc934sw == -1)
513 vec[vecp++] = "-norfc934mode";
515 vec[vecp++] = msgs.msgs[0];
518 execvp ("mhbuild", vec);
519 fprintf (stderr, "unable to exec ");
524 * Process a mhn composition file (old MH style)
526 if (msgs.size == 1 && !folder && !npart && !cachesw
527 && !showsw && !storesw && !ntype && !file
528 && (cp = getenv ("mhdraft"))
529 && strcmp (cp, msgs.msgs[0]) == 0) {
535 vec[vecp++] = "mhbuild";
538 vec[vecp++] = "-ebcdicsafe";
539 else if (ebcdicsw == -1)
540 vec[vecp++] = "-noebcdicsafe";
543 vec[vecp++] = "-rfc934mode";
544 else if (rfc934sw == -1)
545 vec[vecp++] = "-norfc934mode";
550 execvp ("mhbuild", vec);
551 fprintf (stderr, "unable to exec ");
555 if (file && msgs.size)
556 adios (NULL, "cannot specify msg and file at same time!");
559 * check if message is coming from file
562 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
563 adios (NULL, "out of memory");
566 if ((ct = parse_mime (file)))
570 * message(s) are coming from a folder
573 app_msgarg(&msgs, "cur");
575 folder = getfolder (1);
576 maildir = m_maildir (folder);
578 if (chdir (maildir) == NOTOK)
579 adios (maildir, "unable to change directory to");
581 /* read folder and create message structure */
582 if (!(mp = folder_read (folder)))
583 adios (NULL, "unable to read folder %s", folder);
585 /* check for empty folder */
587 adios (NULL, "no messages in %s", folder);
589 /* parse all the message ranges/sequences and set SELECTED */
590 for (msgnum = 0; msgnum < msgs.size; msgnum++)
591 if (!m_convert (mp, msgs.msgs[msgnum]))
593 seq_setprev (mp); /* set the previous-sequence */
595 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
596 adios (NULL, "out of memory");
599 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
600 if (is_selected(mp, msgnum)) {
603 msgnam = m_name (msgnum);
604 if ((ct = parse_mime (msgnam)))
614 * You can't give more than one of these flags
617 if (showsw + listsw + storesw + cachesw > 1)
618 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
620 /* If no action is specified, assume -show */
621 if (!listsw && !showsw && !storesw && !cachesw)
625 SIGNAL (SIGQUIT, quitser);
626 SIGNAL (SIGPIPE, pipeser);
629 * Get the associated umask for the relevant contents.
631 for (ctp = cts; *ctp; ctp++) {
635 if (type_ok (ct, 1) && !ct->c_umask) {
636 if (stat (ct->c_file, &st) != NOTOK)
637 ct->c_umask = ~(st.st_mode & 0777);
639 ct->c_umask = ~m_gmprot();
644 * List the message content
647 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
650 * Store the message content
653 store_all_messages (cts);
656 * Cache the message content
659 cache_all_messages (cts);
662 * Show the message content
665 show_all_messages (cts);
667 /* Now free all the structures for the content */
668 for (ctp = cts; *ctp; ctp++)
674 /* If reading from a folder, do some updating */
676 context_replace (pfolder, folder);/* update current folder */
677 seq_setcur (mp, mp->hghsel); /* update current message */
678 seq_save (mp); /* synchronize sequences */
679 context_save (); /* save the context file */
693 fprintf (stderr, "\n");