]>
diplodocus.org Git - nmh/blob - uip/mhn.c
0475876a952da1c433cc4402065ce575dd50f126
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.
9 #include "sbr/context_find.h"
10 #include "sbr/readconfig.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
17 #include "h/signals.h"
21 #include "h/fmt_scan.h"
23 #include "h/mhparse.h"
24 #include "h/mhcachesbr.h"
28 #include "sbr/m_maildir.h"
30 #include "mhshowsbr.h"
32 #define MHN_SWITCHES \
33 X("auto", 0, AUTOSW) \
34 X("noauto", 0, NAUTOSW) \
35 X("cache", 0, CACHESW) \
36 X("nocache", 0, NCACHESW) \
37 X("check", 0, CHECKSW) \
38 X("nocheck", 0, NCHECKSW) \
39 X("headers", 0, HEADSW) \
40 X("noheaders", 0, NHEADSW) \
41 X("list", 0, LISTSW) \
42 X("nolist", 0, NLISTSW) \
43 X("realsize", 0, SIZESW) \
44 X("norealsize", 0, NSIZESW) \
45 X("show", 0, SHOWSW) \
46 X("noshow", 0, NSHOWSW) \
47 X("store", 0, STORESW) \
48 X("nostore", 0, NSTORESW) \
49 X("verbose", 0, VERBSW) \
50 X("noverbose", 0, NVERBSW) \
51 X("file file", 0, FILESW) \
52 X("form formfile", 0, FORMSW) \
53 X("part number", 0, PARTSW) \
54 X("type content", 0, TYPESW) \
55 X("rcache policy", 0, RCACHESW) \
56 X("wcache policy", 0, WCACHESW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
62 X("debug", -5, DEBUGSW) \
64 * switches for moreproc/mhlproc \
66 X("moreproc program", -4, PROGSW) \
67 X("nomoreproc", -3, NPROGSW) \
68 X("length lines", -4, LENSW) \
69 X("width columns", -4, WIDTHSW) \
71 * switches for mhbuild \
73 X("build", -5, BUILDSW) \
74 X("nobuild", -7, NBUILDSW) \
75 X("rfc934mode", 0, RFC934SW) \
76 X("norfc934mode", 0, NRFC934SW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(MHN
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(MHN
, switches
);
91 * variables for mhbuild (mhn -build)
94 static int rfc934sw
= 0;
97 * what action to take?
104 #define quitser pipeser
109 static void pipeser (int);
113 main (int argc
, char **argv
)
118 int msgnum
, *icachesw
;
119 char *cp
, *file
= NULL
, *folder
= NULL
;
120 char *maildir
, buf
[100], **argp
;
123 struct msgs_array msgs
= { 0, 0, NULL
};
124 struct msgs
*mp
= NULL
;
129 if (nmh_init(argv
[0], true, true)) { return 1; }
131 set_done(freects_done
);
133 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 while ((cp
= *argp
++)) {
141 switch (smatch (++cp
, switches
)) {
143 ambigsw (cp
, switches
);
146 die("-%s unknown", cp
);
149 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
151 print_help (buf
, switches
, 1);
154 print_version(invo_name
);
172 icachesw
= &rcachesw
;
175 icachesw
= &wcachesw
;
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 switch (*icachesw
= smatch (cp
, cache_policy
)) {
181 ambigsw (cp
, cache_policy
);
184 die("%s unknown", cp
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 die("missing argument to %s", argp
[-2]);
236 die("too many parts (starting with %s), %d max",
242 if (!(cp
= *argp
++) || *cp
== '-')
243 die("missing argument to %s", argp
[-2]);
245 die("too many types (starting with %s), %d max",
251 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
252 die("missing argument to %s", argp
[-2]);
253 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
257 if (!(cp
= *argp
++) || *cp
== '-')
258 die("missing argument to %s", argp
[-2]);
260 formsw
= mh_xstrdup(etcpath(cp
));
264 * Switches for moreproc/mhlproc
267 if (!(progsw
= *argp
++) || *progsw
== '-')
268 die("missing argument to %s", argp
[-2]);
276 if (!(cp
= *argp
++) || *cp
== '-')
277 die("missing argument to %s", argp
[-2]);
281 * Switches for mhbuild
307 if (*cp
== '+' || *cp
== '@') {
309 die("only one folder at a time!");
310 folder
= pluspath (cp
);
312 app_msgarg(&msgs
, cp
);
315 /* null terminate the list of acceptable parts/types */
320 * Check if we've specified an additional profile
322 if ((cp
= getenv ("MHN"))) {
323 if ((fp
= fopen (cp
, "r"))) {
324 readconfig(NULL
, fp
, cp
, 0);
327 admonish ("", "unable to read $MHN profile (%s)", cp
);
332 * Read the standard profile setup
334 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
335 readconfig(NULL
, fp
, cp
, 0);
339 /* Check for public cache location */
340 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
343 /* Check for private cache location */
344 if (!(cache_private
= context_find (nmhprivcache
)))
345 cache_private
= ".cache";
346 cache_private
= mh_xstrdup(m_maildir(cache_private
));
349 * Cache the current directory before we do any chdirs()'s.
351 cwd
= mh_xstrdup(pwd());
353 if (!context_find ("path"))
354 free (path ("./", TFOLDER
));
357 * Process a mhn composition file (mhn -build)
363 if (showsw
|| storesw
|| cachesw
)
364 die("cannot use -build with -show, -store, -cache");
366 die("need to specify a %s composition file", invo_name
);
368 die("only one %s composition file at a time", invo_name
);
371 vec
[vecp
++] = "mhbuild";
374 vec
[vecp
++] = "-rfc934mode";
375 else if (rfc934sw
== -1)
376 vec
[vecp
++] = "-norfc934mode";
378 vec
[vecp
++] = msgs
.msgs
[0];
381 execvp ("mhbuild", vec
);
382 fprintf (stderr
, "unable to exec ");
387 * Process a mhn composition file (old MH style)
389 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
390 && !showsw
&& !storesw
&& !ntype
&& !file
391 && (cp
= getenv ("mhdraft"))
392 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
398 vec
[vecp
++] = "mhbuild";
401 vec
[vecp
++] = "-rfc934mode";
402 else if (rfc934sw
== -1)
403 vec
[vecp
++] = "-norfc934mode";
408 execvp ("mhbuild", vec
);
409 fprintf (stderr
, "unable to exec ");
413 if (file
&& msgs
.size
)
414 die("cannot specify msg and file at same time!");
417 * check if message is coming from file
420 cts
= mh_xcalloc(2, sizeof *cts
);
423 if ((ct
= parse_mime (file
)))
427 * message(s) are coming from a folder
430 app_msgarg(&msgs
, "cur");
432 folder
= getfolder (1);
433 maildir
= m_maildir (folder
);
435 if (chdir (maildir
) == NOTOK
)
436 adios (maildir
, "unable to change directory to");
438 /* read folder and create message structure */
439 if (!(mp
= folder_read (folder
, 1)))
440 die("unable to read folder %s", folder
);
442 /* check for empty folder */
444 die("no messages in %s", folder
);
446 /* parse all the message ranges/sequences and set SELECTED */
447 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
448 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
450 seq_setprev (mp
); /* set the previous-sequence */
452 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
455 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
456 if (is_selected(mp
, msgnum
)) {
459 msgnam
= m_name (msgnum
);
460 if ((ct
= parse_mime (msgnam
)))
470 * You can't give more than one of these flags
473 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
474 die("can only use one of -show, -list, -store, -cache at same time");
476 /* If no action is specified, assume -show */
477 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
481 SIGNAL (SIGQUIT
, quitser
);
482 SIGNAL (SIGPIPE
, pipeser
);
485 * Get the associated umask for the relevant contents.
487 for (ctp
= cts
; *ctp
; ctp
++) {
491 if (type_ok (ct
, 1) && !ct
->c_umask
) {
492 if (stat (ct
->c_file
, &st
) != NOTOK
)
493 ct
->c_umask
= ~(st
.st_mode
& 0777);
495 ct
->c_umask
= ~m_gmprot();
500 * List the message content
503 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
506 * Store the message content
509 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
510 store_all_messages (info
);
511 mhstoreinfo_free (info
);
514 /* If reading from a folder, do some updating */
516 context_replace (pfolder
, folder
);/* update current folder */
517 seq_setcur (mp
, mp
->hghsel
); /* update current message */
518 seq_save (mp
); /* synchronize sequences */
519 context_save (); /* save the context file */
523 * Cache the message content
526 cache_all_messages (cts
);
529 * Show the message content
532 show_all_messages (cts
, 0, 0, 0);
534 /* Now free all the structures for the content */
535 for (ctp
= cts
; *ctp
; ctp
++)
551 fprintf (stderr
, "\n");