]>
diplodocus.org Git - nmh/blob - uip/mhn.c
49c1c83fc076633917732442149d62db875ca3bf
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/readconfig.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "h/signals.h"
20 #include "h/fmt_scan.h"
22 #include "h/mhparse.h"
23 #include "h/mhcachesbr.h"
27 #include "sbr/m_maildir.h"
29 #include "mhshowsbr.h"
31 #define MHN_SWITCHES \
32 X("auto", 0, AUTOSW) \
33 X("noauto", 0, NAUTOSW) \
34 X("cache", 0, CACHESW) \
35 X("nocache", 0, NCACHESW) \
36 X("check", 0, CHECKSW) \
37 X("nocheck", 0, NCHECKSW) \
38 X("headers", 0, HEADSW) \
39 X("noheaders", 0, NHEADSW) \
40 X("list", 0, LISTSW) \
41 X("nolist", 0, NLISTSW) \
42 X("realsize", 0, SIZESW) \
43 X("norealsize", 0, NSIZESW) \
44 X("show", 0, SHOWSW) \
45 X("noshow", 0, NSHOWSW) \
46 X("store", 0, STORESW) \
47 X("nostore", 0, NSTORESW) \
48 X("verbose", 0, VERBSW) \
49 X("noverbose", 0, NVERBSW) \
50 X("file file", 0, FILESW) \
51 X("form formfile", 0, FORMSW) \
52 X("part number", 0, PARTSW) \
53 X("type content", 0, TYPESW) \
54 X("rcache policy", 0, RCACHESW) \
55 X("wcache policy", 0, WCACHESW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
61 X("debug", -5, DEBUGSW) \
63 * switches for moreproc/mhlproc \
65 X("moreproc program", -4, PROGSW) \
66 X("nomoreproc", -3, NPROGSW) \
67 X("length lines", -4, LENSW) \
68 X("width columns", -4, WIDTHSW) \
70 * switches for mhbuild \
72 X("build", -5, BUILDSW) \
73 X("nobuild", -7, NBUILDSW) \
74 X("rfc934mode", 0, RFC934SW) \
75 X("norfc934mode", 0, NRFC934SW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(MHN
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(MHN
, switches
);
90 * variables for mhbuild (mhn -build)
93 static int rfc934sw
= 0;
96 * what action to take?
103 #define quitser pipeser
108 static void pipeser (int);
112 main (int argc
, char **argv
)
117 int msgnum
, *icachesw
;
118 char *cp
, *file
= NULL
, *folder
= NULL
;
119 char *maildir
, buf
[100], **argp
;
122 struct msgs_array msgs
= { 0, 0, NULL
};
123 struct msgs
*mp
= NULL
;
128 if (nmh_init(argv
[0], true, true)) { return 1; }
130 set_done(freects_done
);
132 arguments
= getarguments (invo_name
, argc
, argv
, 1);
138 while ((cp
= *argp
++)) {
140 switch (smatch (++cp
, switches
)) {
142 ambigsw (cp
, switches
);
145 die("-%s unknown", cp
);
148 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
150 print_help (buf
, switches
, 1);
153 print_version(invo_name
);
171 icachesw
= &rcachesw
;
174 icachesw
= &wcachesw
;
176 if (!(cp
= *argp
++) || *cp
== '-')
177 die("missing argument to %s", argp
[-2]);
178 switch (*icachesw
= smatch (cp
, cache_policy
)) {
180 ambigsw (cp
, cache_policy
);
183 die("%s unknown", cp
);
232 if (!(cp
= *argp
++) || *cp
== '-')
233 die("missing argument to %s", argp
[-2]);
235 die("too many parts (starting with %s), %d max",
241 if (!(cp
= *argp
++) || *cp
== '-')
242 die("missing argument to %s", argp
[-2]);
244 die("too many types (starting with %s), %d max",
250 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
251 die("missing argument to %s", argp
[-2]);
252 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 die("missing argument to %s", argp
[-2]);
259 formsw
= mh_xstrdup(etcpath(cp
));
263 * Switches for moreproc/mhlproc
266 if (!(progsw
= *argp
++) || *progsw
== '-')
267 die("missing argument to %s", argp
[-2]);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 die("missing argument to %s", argp
[-2]);
280 * Switches for mhbuild
306 if (*cp
== '+' || *cp
== '@') {
308 die("only one folder at a time!");
309 folder
= pluspath (cp
);
311 app_msgarg(&msgs
, cp
);
314 /* null terminate the list of acceptable parts/types */
319 * Check if we've specified an additional profile
321 if ((cp
= getenv ("MHN"))) {
322 if ((fp
= fopen (cp
, "r"))) {
323 readconfig(NULL
, fp
, cp
, 0);
326 admonish ("", "unable to read $MHN profile (%s)", cp
);
331 * Read the standard profile setup
333 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
334 readconfig(NULL
, fp
, cp
, 0);
338 /* Check for public cache location */
339 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
342 /* Check for private cache location */
343 if (!(cache_private
= context_find (nmhprivcache
)))
344 cache_private
= ".cache";
345 cache_private
= mh_xstrdup(m_maildir(cache_private
));
348 * Cache the current directory before we do any chdirs()'s.
350 cwd
= mh_xstrdup(pwd());
352 if (!context_find ("path"))
353 free (path ("./", TFOLDER
));
356 * Process a mhn composition file (mhn -build)
362 if (showsw
|| storesw
|| cachesw
)
363 die("cannot use -build with -show, -store, -cache");
365 die("need to specify a %s composition file", invo_name
);
367 die("only one %s composition file at a time", invo_name
);
370 vec
[vecp
++] = "mhbuild";
373 vec
[vecp
++] = "-rfc934mode";
374 else if (rfc934sw
== -1)
375 vec
[vecp
++] = "-norfc934mode";
377 vec
[vecp
++] = msgs
.msgs
[0];
380 execvp ("mhbuild", vec
);
381 fprintf (stderr
, "unable to exec ");
386 * Process a mhn composition file (old MH style)
388 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
389 && !showsw
&& !storesw
&& !ntype
&& !file
390 && (cp
= getenv ("mhdraft"))
391 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
397 vec
[vecp
++] = "mhbuild";
400 vec
[vecp
++] = "-rfc934mode";
401 else if (rfc934sw
== -1)
402 vec
[vecp
++] = "-norfc934mode";
407 execvp ("mhbuild", vec
);
408 fprintf (stderr
, "unable to exec ");
412 if (file
&& msgs
.size
)
413 die("cannot specify msg and file at same time!");
416 * check if message is coming from file
419 cts
= mh_xcalloc(2, sizeof *cts
);
422 if ((ct
= parse_mime (file
)))
426 * message(s) are coming from a folder
429 app_msgarg(&msgs
, "cur");
431 folder
= getfolder (1);
432 maildir
= m_maildir (folder
);
434 if (chdir (maildir
) == NOTOK
)
435 adios (maildir
, "unable to change directory to");
437 /* read folder and create message structure */
438 if (!(mp
= folder_read (folder
, 1)))
439 die("unable to read folder %s", folder
);
441 /* check for empty folder */
443 die("no messages in %s", folder
);
445 /* parse all the message ranges/sequences and set SELECTED */
446 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
447 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
449 seq_setprev (mp
); /* set the previous-sequence */
451 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
454 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
455 if (is_selected(mp
, msgnum
)) {
458 msgnam
= m_name (msgnum
);
459 if ((ct
= parse_mime (msgnam
)))
469 * You can't give more than one of these flags
472 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
473 die("can only use one of -show, -list, -store, -cache at same time");
475 /* If no action is specified, assume -show */
476 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
480 SIGNAL (SIGQUIT
, quitser
);
481 SIGNAL (SIGPIPE
, pipeser
);
484 * Get the associated umask for the relevant contents.
486 for (ctp
= cts
; *ctp
; ctp
++) {
490 if (type_ok (ct
, 1) && !ct
->c_umask
) {
491 if (stat (ct
->c_file
, &st
) != NOTOK
)
492 ct
->c_umask
= ~(st
.st_mode
& 0777);
494 ct
->c_umask
= ~m_gmprot();
499 * List the message content
502 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
505 * Store the message content
508 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
509 store_all_messages (info
);
510 mhstoreinfo_free (info
);
513 /* If reading from a folder, do some updating */
515 context_replace (pfolder
, folder
);/* update current folder */
516 seq_setcur (mp
, mp
->hghsel
); /* update current message */
517 seq_save (mp
); /* synchronize sequences */
518 context_save (); /* save the context file */
522 * Cache the message content
525 cache_all_messages (cts
);
528 * Show the message content
531 show_all_messages (cts
, 0, 0, 0);
533 /* Now free all the structures for the content */
534 for (ctp
= cts
; *ctp
; ctp
++)
550 fprintf (stderr
, "\n");