]>
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.
9 #include "sbr/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/readconfig.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
26 #include "h/signals.h"
30 #include "h/fmt_scan.h"
32 #include "h/mhparse.h"
33 #include "h/mhcachesbr.h"
37 #include "sbr/m_maildir.h"
39 #include "mhshowsbr.h"
41 #define MHN_SWITCHES \
42 X("auto", 0, AUTOSW) \
43 X("noauto", 0, NAUTOSW) \
44 X("cache", 0, CACHESW) \
45 X("nocache", 0, NCACHESW) \
46 X("check", 0, CHECKSW) \
47 X("nocheck", 0, NCHECKSW) \
48 X("headers", 0, HEADSW) \
49 X("noheaders", 0, NHEADSW) \
50 X("list", 0, LISTSW) \
51 X("nolist", 0, NLISTSW) \
52 X("realsize", 0, SIZESW) \
53 X("norealsize", 0, NSIZESW) \
54 X("show", 0, SHOWSW) \
55 X("noshow", 0, NSHOWSW) \
56 X("store", 0, STORESW) \
57 X("nostore", 0, NSTORESW) \
58 X("verbose", 0, VERBSW) \
59 X("noverbose", 0, NVERBSW) \
60 X("file file", 0, FILESW) \
61 X("form formfile", 0, FORMSW) \
62 X("part number", 0, PARTSW) \
63 X("type content", 0, TYPESW) \
64 X("rcache policy", 0, RCACHESW) \
65 X("wcache policy", 0, WCACHESW) \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
71 X("debug", -5, DEBUGSW) \
73 * switches for moreproc/mhlproc \
75 X("moreproc program", -4, PROGSW) \
76 X("nomoreproc", -3, NPROGSW) \
77 X("length lines", -4, LENSW) \
78 X("width columns", -4, WIDTHSW) \
80 * switches for mhbuild \
82 X("build", -5, BUILDSW) \
83 X("nobuild", -7, NBUILDSW) \
84 X("rfc934mode", 0, RFC934SW) \
85 X("norfc934mode", 0, NRFC934SW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(MHN
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(MHN
, switches
);
100 * variables for mhbuild (mhn -build)
103 static int rfc934sw
= 0;
106 * what action to take?
113 #define quitser pipeser
118 static void pipeser (int);
122 main (int argc
, char **argv
)
127 int msgnum
, *icachesw
;
128 char *cp
, *file
= NULL
, *folder
= NULL
;
129 char *maildir
, buf
[100], **argp
;
132 struct msgs_array msgs
= { 0, 0, NULL
};
133 struct msgs
*mp
= NULL
;
138 if (nmh_init(argv
[0], true, true)) { return 1; }
140 set_done(freects_done
);
142 arguments
= getarguments (invo_name
, argc
, argv
, 1);
148 while ((cp
= *argp
++)) {
150 switch (smatch (++cp
, switches
)) {
152 ambigsw (cp
, switches
);
155 die("-%s unknown", cp
);
158 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
160 print_help (buf
, switches
, 1);
163 print_version(invo_name
);
181 icachesw
= &rcachesw
;
184 icachesw
= &wcachesw
;
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 switch (*icachesw
= smatch (cp
, cache_policy
)) {
190 ambigsw (cp
, cache_policy
);
193 die("%s unknown", cp
);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 die("missing argument to %s", argp
[-2]);
245 die("too many parts (starting with %s), %d max",
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
254 die("too many types (starting with %s), %d max",
260 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
261 die("missing argument to %s", argp
[-2]);
262 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
266 if (!(cp
= *argp
++) || *cp
== '-')
267 die("missing argument to %s", argp
[-2]);
269 formsw
= mh_xstrdup(etcpath(cp
));
273 * Switches for moreproc/mhlproc
276 if (!(progsw
= *argp
++) || *progsw
== '-')
277 die("missing argument to %s", argp
[-2]);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
290 * Switches for mhbuild
316 if (*cp
== '+' || *cp
== '@') {
318 die("only one folder at a time!");
319 folder
= pluspath (cp
);
321 app_msgarg(&msgs
, cp
);
324 /* null terminate the list of acceptable parts/types */
329 * Check if we've specified an additional profile
331 if ((cp
= getenv ("MHN"))) {
332 if ((fp
= fopen (cp
, "r"))) {
333 readconfig(NULL
, fp
, cp
, 0);
336 admonish ("", "unable to read $MHN profile (%s)", cp
);
341 * Read the standard profile setup
343 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
344 readconfig(NULL
, fp
, cp
, 0);
348 /* Check for public cache location */
349 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
352 /* Check for private cache location */
353 if (!(cache_private
= context_find (nmhprivcache
)))
354 cache_private
= ".cache";
355 cache_private
= mh_xstrdup(m_maildir(cache_private
));
358 * Cache the current directory before we do any chdirs()'s.
360 cwd
= mh_xstrdup(pwd());
362 if (!context_find ("path"))
363 free (path ("./", TFOLDER
));
366 * Process a mhn composition file (mhn -build)
372 if (showsw
|| storesw
|| cachesw
)
373 die("cannot use -build with -show, -store, -cache");
375 die("need to specify a %s composition file", invo_name
);
377 die("only one %s composition file at a time", invo_name
);
380 vec
[vecp
++] = "mhbuild";
383 vec
[vecp
++] = "-rfc934mode";
384 else if (rfc934sw
== -1)
385 vec
[vecp
++] = "-norfc934mode";
387 vec
[vecp
++] = msgs
.msgs
[0];
390 execvp ("mhbuild", vec
);
391 fprintf (stderr
, "unable to exec ");
396 * Process a mhn composition file (old MH style)
398 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
399 && !showsw
&& !storesw
&& !ntype
&& !file
400 && (cp
= getenv ("mhdraft"))
401 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
407 vec
[vecp
++] = "mhbuild";
410 vec
[vecp
++] = "-rfc934mode";
411 else if (rfc934sw
== -1)
412 vec
[vecp
++] = "-norfc934mode";
417 execvp ("mhbuild", vec
);
418 fprintf (stderr
, "unable to exec ");
422 if (file
&& msgs
.size
)
423 die("cannot specify msg and file at same time!");
426 * check if message is coming from file
429 cts
= mh_xcalloc(2, sizeof *cts
);
432 if ((ct
= parse_mime (file
)))
436 * message(s) are coming from a folder
439 app_msgarg(&msgs
, "cur");
441 folder
= getfolder (1);
442 maildir
= m_maildir (folder
);
444 if (chdir (maildir
) == NOTOK
)
445 adios (maildir
, "unable to change directory to");
447 /* read folder and create message structure */
448 if (!(mp
= folder_read (folder
, 1)))
449 die("unable to read folder %s", folder
);
451 /* check for empty folder */
453 die("no messages in %s", folder
);
455 /* parse all the message ranges/sequences and set SELECTED */
456 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
457 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
459 seq_setprev (mp
); /* set the previous-sequence */
461 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
464 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
465 if (is_selected(mp
, msgnum
)) {
468 msgnam
= m_name (msgnum
);
469 if ((ct
= parse_mime (msgnam
)))
479 * You can't give more than one of these flags
482 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
483 die("can only use one of -show, -list, -store, -cache at same time");
485 /* If no action is specified, assume -show */
486 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
490 SIGNAL (SIGQUIT
, quitser
);
491 SIGNAL (SIGPIPE
, pipeser
);
494 * Get the associated umask for the relevant contents.
496 for (ctp
= cts
; *ctp
; ctp
++) {
500 if (type_ok (ct
, 1) && !ct
->c_umask
) {
501 if (stat (ct
->c_file
, &st
) != NOTOK
)
502 ct
->c_umask
= ~(st
.st_mode
& 0777);
504 ct
->c_umask
= ~m_gmprot();
509 * List the message content
512 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
515 * Store the message content
518 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
519 store_all_messages (info
);
520 mhstoreinfo_free (info
);
523 /* If reading from a folder, do some updating */
525 context_replace (pfolder
, folder
);/* update current folder */
526 seq_setcur (mp
, mp
->hghsel
); /* update current message */
527 seq_save (mp
); /* synchronize sequences */
528 context_save (); /* save the context file */
532 * Cache the message content
535 cache_all_messages (cts
);
538 * Show the message content
541 show_all_messages (cts
, 0, 0, 0);
543 /* Now free all the structures for the content */
544 for (ctp
= cts
; *ctp
; ctp
++)
560 fprintf (stderr
, "\n");