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];
160 /* The list of top-level contents to display */
164 * variables for mhbuild (mhn -build)
166 static int buildsw = 0;
167 static int ebcdicsw = 0;
168 static int rfc934sw = 0;
171 * what action to take?
173 static int cachesw = 0;
174 static int listsw = 0;
175 static int showsw = 0;
176 static int storesw = 0;
178 #define quitser pipeser
181 CT parse_mime (char *);
184 int part_ok (CT, int);
185 int type_ok (CT, int);
186 void set_endian (void);
187 void flush_errors (void);
190 void show_all_messages (CT *);
193 void list_all_messages (CT *, int, int, int, int);
196 void store_all_messages (CT *);
199 void cache_all_messages (CT *);
202 void free_content (CT);
207 static RETSIGTYPE pipeser (int);
208 static int freectp_done (int);
212 main (int argc, char **argv)
214 int sizesw = 1, headsw = 1;
215 int msgnum, *icachesw;
216 char *cp, *file = NULL, *folder = NULL;
217 char *maildir, buf[100], **argp;
219 struct msgs_array msgs = { 0, 0, NULL };
220 struct msgs *mp = NULL;
227 setlocale(LC_ALL, "");
229 invo_name = r1bindex (argv[0], '/');
231 /* read user profile/context */
234 arguments = getarguments (invo_name, argc, argv, 1);
240 while ((cp = *argp++)) {
242 switch (smatch (++cp, switches)) {
244 ambigsw (cp, switches);
247 adios (NULL, "-%s unknown", cp);
250 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
252 print_help (buf, switches, 1);
255 print_version(invo_name);
273 icachesw = &rcachesw;
276 icachesw = &wcachesw;
278 if (!(cp = *argp++) || *cp == '-')
279 adios (NULL, "missing argument to %s", argp[-2]);
280 switch (*icachesw = smatch (cp, caches)) {
282 ambigsw (cp, caches);
285 adios (NULL, "%s unknown", cp);
348 if (!(cp = *argp++) || *cp == '-')
349 adios (NULL, "missing argument to %s", argp[-2]);
351 adios (NULL, "too many parts (starting with %s), %d max",
357 if (!(cp = *argp++) || *cp == '-')
358 adios (NULL, "missing argument to %s", argp[-2]);
360 adios (NULL, "too many types (starting with %s), %d max",
366 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
367 adios (NULL, "missing argument to %s", argp[-2]);
368 file = *cp == '-' ? cp : path (cp, TFILE);
372 if (!(cp = *argp++) || *cp == '-')
373 adios (NULL, "missing argument to %s", argp[-2]);
376 formsw = getcpy (etcpath (cp));
380 * Switches for moreproc/mhlproc
383 if (!(progsw = *argp++) || *progsw == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
392 if (!(cp = *argp++) || *cp == '-')
393 adios (NULL, "missing argument to %s", argp[-2]);
397 * Switches for mhbuild
429 if (*cp == '+' || *cp == '@') {
431 adios (NULL, "only one folder at a time!");
433 folder = pluspath (cp);
435 app_msgarg(&msgs, cp);
438 /* null terminate the list of acceptable parts/types */
444 if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
451 * Check if we've specified an additional profile
453 if ((cp = getenv ("MHN"))) {
454 if ((fp = fopen (cp, "r"))) {
455 readconfig ((struct node **) 0, fp, cp, 0);
458 admonish ("", "unable to read $MHN profile (%s)", cp);
463 * Read the standard profile setup
465 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
466 readconfig ((struct node **) 0, fp, cp, 0);
470 /* Check for public cache location */
471 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
474 /* Check for private cache location */
475 if (!(cache_private = context_find (nmhprivcache)))
476 cache_private = ".cache";
477 cache_private = getcpy (m_maildir (cache_private));
480 * Cache the current directory before we do any chdirs()'s.
482 cwd = getcpy (pwd());
485 * Check for storage directory. If specified,
486 * then store temporary files there. Else we
487 * store them in standard nmh directory.
489 if ((cp = context_find (nmhstorage)) && *cp)
490 tmp = concat (cp, "/", invo_name, NULL);
492 tmp = add (m_maildir (invo_name), NULL);
494 if (!context_find ("path"))
495 free (path ("./", TFOLDER));
498 * Process a mhn composition file (mhn -build)
504 if (showsw || storesw || cachesw)
505 adios (NULL, "cannot use -build with -show, -store, -cache");
507 adios (NULL, "need to specify a %s composition file", invo_name);
509 adios (NULL, "only one %s composition file at a time", invo_name);
512 vec[vecp++] = "mhbuild";
515 vec[vecp++] = "-ebcdicsafe";
516 else if (ebcdicsw == -1)
517 vec[vecp++] = "-noebcdicsafe";
520 vec[vecp++] = "-rfc934mode";
521 else if (rfc934sw == -1)
522 vec[vecp++] = "-norfc934mode";
524 vec[vecp++] = msgs.msgs[0];
527 execvp ("mhbuild", vec);
528 fprintf (stderr, "unable to exec ");
533 * Process a mhn composition file (old MH style)
535 if (msgs.size == 1 && !folder && !npart && !cachesw
536 && !showsw && !storesw && !ntype && !file
537 && (cp = getenv ("mhdraft"))
538 && strcmp (cp, msgs.msgs[0]) == 0) {
544 vec[vecp++] = "mhbuild";
547 vec[vecp++] = "-ebcdicsafe";
548 else if (ebcdicsw == -1)
549 vec[vecp++] = "-noebcdicsafe";
552 vec[vecp++] = "-rfc934mode";
553 else if (rfc934sw == -1)
554 vec[vecp++] = "-norfc934mode";
559 execvp ("mhbuild", vec);
560 fprintf (stderr, "unable to exec ");
564 if (file && msgs.size)
565 adios (NULL, "cannot specify msg and file at same time!");
568 * check if message is coming from file
571 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
572 adios (NULL, "out of memory");
575 if ((ct = parse_mime (file)));
579 * message(s) are coming from a folder
582 app_msgarg(&msgs, "cur");
584 folder = getfolder (1);
585 maildir = m_maildir (folder);
587 if (chdir (maildir) == NOTOK)
588 adios (maildir, "unable to change directory to");
590 /* read folder and create message structure */
591 if (!(mp = folder_read (folder)))
592 adios (NULL, "unable to read folder %s", folder);
594 /* check for empty folder */
596 adios (NULL, "no messages in %s", folder);
598 /* parse all the message ranges/sequences and set SELECTED */
599 for (msgnum = 0; msgnum < msgs.size; msgnum++)
600 if (!m_convert (mp, msgs.msgs[msgnum]))
602 seq_setprev (mp); /* set the previous-sequence */
604 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
605 adios (NULL, "out of memory");
608 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
609 if (is_selected(mp, msgnum)) {
612 msgnam = m_name (msgnum);
613 if ((ct = parse_mime (msgnam)))
623 * You can't give more than one of these flags
626 if (showsw + listsw + storesw + cachesw > 1)
627 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
629 /* If no action is specified, assume -show */
630 if (!listsw && !showsw && !storesw && !cachesw)
634 SIGNAL (SIGQUIT, quitser);
635 SIGNAL (SIGPIPE, pipeser);
638 * Get the associated umask for the relevant contents.
640 for (ctp = cts; *ctp; ctp++) {
644 if (type_ok (ct, 1) && !ct->c_umask) {
645 if (stat (ct->c_file, &st) != NOTOK)
646 ct->c_umask = ~(st.st_mode & 0777);
648 ct->c_umask = ~m_gmprot();
653 * List the message content
656 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
659 * Store the message content
662 store_all_messages (cts);
665 * Cache the message content
668 cache_all_messages (cts);
671 * Show the message content
674 show_all_messages (cts);
676 /* Now free all the structures for the content */
677 for (ctp = cts; *ctp; ctp++)
683 /* If reading from a folder, do some updating */
685 context_replace (pfolder, folder);/* update current folder */
686 seq_setcur (mp, mp->hghsel); /* update current message */
687 seq_save (mp); /* synchronize sequences */
688 context_save (); /* save the context file */
701 fprintf (stderr, "\n");
711 freectp_done (int status)
720 return 1; /* dead code to satisfy the compiler */