]>
diplodocus.org Git - nmh/blob - uip/mhn.c
14378fb031a416434f9fe60447c61c1ec8e2a169
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_setcur.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/readconfig.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
25 #include "h/signals.h"
29 #include "h/fmt_scan.h"
31 #include "h/mhparse.h"
32 #include "h/mhcachesbr.h"
36 #include "sbr/m_maildir.h"
38 #include "mhshowsbr.h"
40 #define MHN_SWITCHES \
41 X("auto", 0, AUTOSW) \
42 X("noauto", 0, NAUTOSW) \
43 X("cache", 0, CACHESW) \
44 X("nocache", 0, NCACHESW) \
45 X("check", 0, CHECKSW) \
46 X("nocheck", 0, NCHECKSW) \
47 X("headers", 0, HEADSW) \
48 X("noheaders", 0, NHEADSW) \
49 X("list", 0, LISTSW) \
50 X("nolist", 0, NLISTSW) \
51 X("realsize", 0, SIZESW) \
52 X("norealsize", 0, NSIZESW) \
53 X("show", 0, SHOWSW) \
54 X("noshow", 0, NSHOWSW) \
55 X("store", 0, STORESW) \
56 X("nostore", 0, NSTORESW) \
57 X("verbose", 0, VERBSW) \
58 X("noverbose", 0, NVERBSW) \
59 X("file file", 0, FILESW) \
60 X("form formfile", 0, FORMSW) \
61 X("part number", 0, PARTSW) \
62 X("type content", 0, TYPESW) \
63 X("rcache policy", 0, RCACHESW) \
64 X("wcache policy", 0, WCACHESW) \
65 X("version", 0, VERSIONSW) \
66 X("help", 0, HELPSW) \
70 X("debug", -5, DEBUGSW) \
72 * switches for moreproc/mhlproc \
74 X("moreproc program", -4, PROGSW) \
75 X("nomoreproc", -3, NPROGSW) \
76 X("length lines", -4, LENSW) \
77 X("width columns", -4, WIDTHSW) \
79 * switches for mhbuild \
81 X("build", -5, BUILDSW) \
82 X("nobuild", -7, NBUILDSW) \
83 X("rfc934mode", 0, RFC934SW) \
84 X("norfc934mode", 0, NRFC934SW) \
86 #define X(sw, minchars, id) id,
87 DEFINE_SWITCH_ENUM(MHN
);
90 #define X(sw, minchars, id) { sw, minchars, id },
91 DEFINE_SWITCH_ARRAY(MHN
, switches
);
99 * variables for mhbuild (mhn -build)
102 static int rfc934sw
= 0;
105 * what action to take?
112 #define quitser pipeser
117 static void pipeser (int);
121 main (int argc
, char **argv
)
126 int msgnum
, *icachesw
;
127 char *cp
, *file
= NULL
, *folder
= NULL
;
128 char *maildir
, buf
[100], **argp
;
131 struct msgs_array msgs
= { 0, 0, NULL
};
132 struct msgs
*mp
= NULL
;
137 if (nmh_init(argv
[0], true, true)) { return 1; }
139 set_done(freects_done
);
141 arguments
= getarguments (invo_name
, argc
, argv
, 1);
147 while ((cp
= *argp
++)) {
149 switch (smatch (++cp
, switches
)) {
151 ambigsw (cp
, switches
);
154 die("-%s unknown", cp
);
157 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
159 print_help (buf
, switches
, 1);
162 print_version(invo_name
);
180 icachesw
= &rcachesw
;
183 icachesw
= &wcachesw
;
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 switch (*icachesw
= smatch (cp
, cache_policy
)) {
189 ambigsw (cp
, cache_policy
);
192 die("%s unknown", cp
);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 die("missing argument to %s", argp
[-2]);
244 die("too many parts (starting with %s), %d max",
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
253 die("too many types (starting with %s), %d max",
259 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
260 die("missing argument to %s", argp
[-2]);
261 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
268 formsw
= mh_xstrdup(etcpath(cp
));
272 * Switches for moreproc/mhlproc
275 if (!(progsw
= *argp
++) || *progsw
== '-')
276 die("missing argument to %s", argp
[-2]);
284 if (!(cp
= *argp
++) || *cp
== '-')
285 die("missing argument to %s", argp
[-2]);
289 * Switches for mhbuild
315 if (*cp
== '+' || *cp
== '@') {
317 die("only one folder at a time!");
318 folder
= pluspath (cp
);
320 app_msgarg(&msgs
, cp
);
323 /* null terminate the list of acceptable parts/types */
328 * Check if we've specified an additional profile
330 if ((cp
= getenv ("MHN"))) {
331 if ((fp
= fopen (cp
, "r"))) {
332 readconfig(NULL
, fp
, cp
, 0);
335 admonish ("", "unable to read $MHN profile (%s)", cp
);
340 * Read the standard profile setup
342 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
343 readconfig(NULL
, fp
, cp
, 0);
347 /* Check for public cache location */
348 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
351 /* Check for private cache location */
352 if (!(cache_private
= context_find (nmhprivcache
)))
353 cache_private
= ".cache";
354 cache_private
= mh_xstrdup(m_maildir(cache_private
));
357 * Cache the current directory before we do any chdirs()'s.
359 cwd
= mh_xstrdup(pwd());
361 if (!context_find ("path"))
362 free (path ("./", TFOLDER
));
365 * Process a mhn composition file (mhn -build)
371 if (showsw
|| storesw
|| cachesw
)
372 die("cannot use -build with -show, -store, -cache");
374 die("need to specify a %s composition file", invo_name
);
376 die("only one %s composition file at a time", invo_name
);
379 vec
[vecp
++] = "mhbuild";
382 vec
[vecp
++] = "-rfc934mode";
383 else if (rfc934sw
== -1)
384 vec
[vecp
++] = "-norfc934mode";
386 vec
[vecp
++] = msgs
.msgs
[0];
389 execvp ("mhbuild", vec
);
390 fprintf (stderr
, "unable to exec ");
395 * Process a mhn composition file (old MH style)
397 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
398 && !showsw
&& !storesw
&& !ntype
&& !file
399 && (cp
= getenv ("mhdraft"))
400 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
406 vec
[vecp
++] = "mhbuild";
409 vec
[vecp
++] = "-rfc934mode";
410 else if (rfc934sw
== -1)
411 vec
[vecp
++] = "-norfc934mode";
416 execvp ("mhbuild", vec
);
417 fprintf (stderr
, "unable to exec ");
421 if (file
&& msgs
.size
)
422 die("cannot specify msg and file at same time!");
425 * check if message is coming from file
428 cts
= mh_xcalloc(2, sizeof *cts
);
431 if ((ct
= parse_mime (file
)))
435 * message(s) are coming from a folder
438 app_msgarg(&msgs
, "cur");
440 folder
= getfolder (1);
441 maildir
= m_maildir (folder
);
443 if (chdir (maildir
) == NOTOK
)
444 adios (maildir
, "unable to change directory to");
446 /* read folder and create message structure */
447 if (!(mp
= folder_read (folder
, 1)))
448 die("unable to read folder %s", folder
);
450 /* check for empty folder */
452 die("no messages in %s", folder
);
454 /* parse all the message ranges/sequences and set SELECTED */
455 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
456 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
458 seq_setprev (mp
); /* set the previous-sequence */
460 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
463 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
464 if (is_selected(mp
, msgnum
)) {
467 msgnam
= m_name (msgnum
);
468 if ((ct
= parse_mime (msgnam
)))
478 * You can't give more than one of these flags
481 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
482 die("can only use one of -show, -list, -store, -cache at same time");
484 /* If no action is specified, assume -show */
485 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
489 SIGNAL (SIGQUIT
, quitser
);
490 SIGNAL (SIGPIPE
, pipeser
);
493 * Get the associated umask for the relevant contents.
495 for (ctp
= cts
; *ctp
; ctp
++) {
499 if (type_ok (ct
, 1) && !ct
->c_umask
) {
500 if (stat (ct
->c_file
, &st
) != NOTOK
)
501 ct
->c_umask
= ~(st
.st_mode
& 0777);
503 ct
->c_umask
= ~m_gmprot();
508 * List the message content
511 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
514 * Store the message content
517 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
518 store_all_messages (info
);
519 mhstoreinfo_free (info
);
522 /* If reading from a folder, do some updating */
524 context_replace (pfolder
, folder
);/* update current folder */
525 seq_setcur (mp
, mp
->hghsel
); /* update current message */
526 seq_save (mp
); /* synchronize sequences */
527 context_save (); /* save the context file */
531 * Cache the message content
534 cache_all_messages (cts
);
537 * Show the message content
540 show_all_messages (cts
, 0, 0, 0);
542 /* Now free all the structures for the content */
543 for (ctp
= cts
; *ctp
; ctp
++)
559 fprintf (stderr
, "\n");