]>
diplodocus.org Git - nmh/blob - uip/mhn.c
1 /* mhn.c -- display, list, cache, or store the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/signals.h"
18 #include "h/fmt_scan.h"
20 #include "h/mhparse.h"
21 #include "h/mhcachesbr.h"
25 #include "sbr/m_maildir.h"
27 #include "mhshowsbr.h"
29 #define MHN_SWITCHES \
30 X("auto", 0, AUTOSW) \
31 X("noauto", 0, NAUTOSW) \
32 X("cache", 0, CACHESW) \
33 X("nocache", 0, NCACHESW) \
34 X("check", 0, CHECKSW) \
35 X("nocheck", 0, NCHECKSW) \
36 X("headers", 0, HEADSW) \
37 X("noheaders", 0, NHEADSW) \
38 X("list", 0, LISTSW) \
39 X("nolist", 0, NLISTSW) \
40 X("realsize", 0, SIZESW) \
41 X("norealsize", 0, NSIZESW) \
42 X("show", 0, SHOWSW) \
43 X("noshow", 0, NSHOWSW) \
44 X("store", 0, STORESW) \
45 X("nostore", 0, NSTORESW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("file file", 0, FILESW) \
49 X("form formfile", 0, FORMSW) \
50 X("part number", 0, PARTSW) \
51 X("type content", 0, TYPESW) \
52 X("rcache policy", 0, RCACHESW) \
53 X("wcache policy", 0, WCACHESW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
59 X("debug", -5, DEBUGSW) \
61 * switches for moreproc/mhlproc \
63 X("moreproc program", -4, PROGSW) \
64 X("nomoreproc", -3, NPROGSW) \
65 X("length lines", -4, LENSW) \
66 X("width columns", -4, WIDTHSW) \
68 * switches for mhbuild \
70 X("build", -5, BUILDSW) \
71 X("nobuild", -7, NBUILDSW) \
72 X("rfc934mode", 0, RFC934SW) \
73 X("norfc934mode", 0, NRFC934SW) \
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(MHN
);
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(MHN
, switches
);
88 * variables for mhbuild (mhn -build)
91 static int rfc934sw
= 0;
94 * what action to take?
101 #define quitser pipeser
106 static void pipeser (int);
110 main (int argc
, char **argv
)
115 int msgnum
, *icachesw
;
116 char *cp
, *file
= NULL
, *folder
= NULL
;
117 char *maildir
, buf
[100], **argp
;
120 struct msgs_array msgs
= { 0, 0, NULL
};
121 struct msgs
*mp
= NULL
;
126 if (nmh_init(argv
[0], true, true)) { return 1; }
128 set_done(freects_done
);
130 arguments
= getarguments (invo_name
, argc
, argv
, 1);
136 while ((cp
= *argp
++)) {
138 switch (smatch (++cp
, switches
)) {
140 ambigsw (cp
, switches
);
143 die("-%s unknown", cp
);
146 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
169 icachesw
= &rcachesw
;
172 icachesw
= &wcachesw
;
174 if (!(cp
= *argp
++) || *cp
== '-')
175 die("missing argument to %s", argp
[-2]);
176 switch (*icachesw
= smatch (cp
, cache_policy
)) {
178 ambigsw (cp
, cache_policy
);
181 die("%s unknown", cp
);
230 if (!(cp
= *argp
++) || *cp
== '-')
231 die("missing argument to %s", argp
[-2]);
233 die("too many parts (starting with %s), %d max",
239 if (!(cp
= *argp
++) || *cp
== '-')
240 die("missing argument to %s", argp
[-2]);
242 die("too many types (starting with %s), %d max",
248 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
249 die("missing argument to %s", argp
[-2]);
250 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
257 formsw
= mh_xstrdup(etcpath(cp
));
261 * Switches for moreproc/mhlproc
264 if (!(progsw
= *argp
++) || *progsw
== '-')
265 die("missing argument to %s", argp
[-2]);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
278 * Switches for mhbuild
304 if (*cp
== '+' || *cp
== '@') {
306 die("only one folder at a time!");
307 folder
= pluspath (cp
);
309 app_msgarg(&msgs
, cp
);
312 /* null terminate the list of acceptable parts/types */
317 * Check if we've specified an additional profile
319 if ((cp
= getenv ("MHN"))) {
320 if ((fp
= fopen (cp
, "r"))) {
321 readconfig(NULL
, fp
, cp
, 0);
324 admonish ("", "unable to read $MHN profile (%s)", cp
);
329 * Read the standard profile setup
331 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
332 readconfig(NULL
, fp
, cp
, 0);
336 /* Check for public cache location */
337 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
340 /* Check for private cache location */
341 if (!(cache_private
= context_find (nmhprivcache
)))
342 cache_private
= ".cache";
343 cache_private
= mh_xstrdup(m_maildir(cache_private
));
346 * Cache the current directory before we do any chdirs()'s.
348 cwd
= mh_xstrdup(pwd());
350 if (!context_find ("path"))
351 free (path ("./", TFOLDER
));
354 * Process a mhn composition file (mhn -build)
360 if (showsw
|| storesw
|| cachesw
)
361 die("cannot use -build with -show, -store, -cache");
363 die("need to specify a %s composition file", invo_name
);
365 die("only one %s composition file at a time", invo_name
);
368 vec
[vecp
++] = "mhbuild";
371 vec
[vecp
++] = "-rfc934mode";
372 else if (rfc934sw
== -1)
373 vec
[vecp
++] = "-norfc934mode";
375 vec
[vecp
++] = msgs
.msgs
[0];
378 execvp ("mhbuild", vec
);
379 fprintf (stderr
, "unable to exec ");
384 * Process a mhn composition file (old MH style)
386 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
387 && !showsw
&& !storesw
&& !ntype
&& !file
388 && (cp
= getenv ("mhdraft"))
389 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
395 vec
[vecp
++] = "mhbuild";
398 vec
[vecp
++] = "-rfc934mode";
399 else if (rfc934sw
== -1)
400 vec
[vecp
++] = "-norfc934mode";
405 execvp ("mhbuild", vec
);
406 fprintf (stderr
, "unable to exec ");
410 if (file
&& msgs
.size
)
411 die("cannot specify msg and file at same time!");
414 * check if message is coming from file
417 cts
= mh_xcalloc(2, sizeof *cts
);
420 if ((ct
= parse_mime (file
)))
424 * message(s) are coming from a folder
427 app_msgarg(&msgs
, "cur");
429 folder
= getfolder (1);
430 maildir
= m_maildir (folder
);
432 if (chdir (maildir
) == NOTOK
)
433 adios (maildir
, "unable to change directory to");
435 /* read folder and create message structure */
436 if (!(mp
= folder_read (folder
, 1)))
437 die("unable to read folder %s", folder
);
439 /* check for empty folder */
441 die("no messages in %s", folder
);
443 /* parse all the message ranges/sequences and set SELECTED */
444 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
445 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
447 seq_setprev (mp
); /* set the previous-sequence */
449 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
452 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
453 if (is_selected(mp
, msgnum
)) {
456 msgnam
= m_name (msgnum
);
457 if ((ct
= parse_mime (msgnam
)))
467 * You can't give more than one of these flags
470 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
471 die("can only use one of -show, -list, -store, -cache at same time");
473 /* If no action is specified, assume -show */
474 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
478 SIGNAL (SIGQUIT
, quitser
);
479 SIGNAL (SIGPIPE
, pipeser
);
482 * Get the associated umask for the relevant contents.
484 for (ctp
= cts
; *ctp
; ctp
++) {
488 if (type_ok (ct
, 1) && !ct
->c_umask
) {
489 if (stat (ct
->c_file
, &st
) != NOTOK
)
490 ct
->c_umask
= ~(st
.st_mode
& 0777);
492 ct
->c_umask
= ~m_gmprot();
497 * List the message content
500 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
503 * Store the message content
506 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
507 store_all_messages (info
);
508 mhstoreinfo_free (info
);
511 /* If reading from a folder, do some updating */
513 context_replace (pfolder
, folder
);/* update current folder */
514 seq_setcur (mp
, mp
->hghsel
); /* update current message */
515 seq_save (mp
); /* synchronize sequences */
516 context_save (); /* save the context file */
520 * Cache the message content
523 cache_all_messages (cts
);
526 * Show the message content
529 show_all_messages (cts
, 0, 0, 0);
531 /* Now free all the structures for the content */
532 for (ctp
= cts
; *ctp
; ctp
++)
548 fprintf (stderr
, "\n");