]>
diplodocus.org Git - nmh/blob - uip/mhn.c
c6f861a0c1940ce4841dbff0bee6331fc2ecb78c
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/getarguments.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_replace.h"
19 #include "sbr/context_find.h"
20 #include "sbr/readconfig.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
27 #include "h/signals.h"
31 #include "h/fmt_scan.h"
33 #include "h/mhparse.h"
34 #include "h/mhcachesbr.h"
38 #include "sbr/m_maildir.h"
40 #include "mhshowsbr.h"
42 #define MHN_SWITCHES \
43 X("auto", 0, AUTOSW) \
44 X("noauto", 0, NAUTOSW) \
45 X("cache", 0, CACHESW) \
46 X("nocache", 0, NCACHESW) \
47 X("check", 0, CHECKSW) \
48 X("nocheck", 0, NCHECKSW) \
49 X("headers", 0, HEADSW) \
50 X("noheaders", 0, NHEADSW) \
51 X("list", 0, LISTSW) \
52 X("nolist", 0, NLISTSW) \
53 X("realsize", 0, SIZESW) \
54 X("norealsize", 0, NSIZESW) \
55 X("show", 0, SHOWSW) \
56 X("noshow", 0, NSHOWSW) \
57 X("store", 0, STORESW) \
58 X("nostore", 0, NSTORESW) \
59 X("verbose", 0, VERBSW) \
60 X("noverbose", 0, NVERBSW) \
61 X("file file", 0, FILESW) \
62 X("form formfile", 0, FORMSW) \
63 X("part number", 0, PARTSW) \
64 X("type content", 0, TYPESW) \
65 X("rcache policy", 0, RCACHESW) \
66 X("wcache policy", 0, WCACHESW) \
67 X("version", 0, VERSIONSW) \
68 X("help", 0, HELPSW) \
72 X("debug", -5, DEBUGSW) \
74 * switches for moreproc/mhlproc \
76 X("moreproc program", -4, PROGSW) \
77 X("nomoreproc", -3, NPROGSW) \
78 X("length lines", -4, LENSW) \
79 X("width columns", -4, WIDTHSW) \
81 * switches for mhbuild \
83 X("build", -5, BUILDSW) \
84 X("nobuild", -7, NBUILDSW) \
85 X("rfc934mode", 0, RFC934SW) \
86 X("norfc934mode", 0, NRFC934SW) \
88 #define X(sw, minchars, id) id,
89 DEFINE_SWITCH_ENUM(MHN
);
92 #define X(sw, minchars, id) { sw, minchars, id },
93 DEFINE_SWITCH_ARRAY(MHN
, switches
);
101 * variables for mhbuild (mhn -build)
104 static int rfc934sw
= 0;
107 * what action to take?
114 #define quitser pipeser
119 static void pipeser (int);
123 main (int argc
, char **argv
)
128 int msgnum
, *icachesw
;
129 char *cp
, *file
= NULL
, *folder
= NULL
;
130 char *maildir
, buf
[100], **argp
;
133 struct msgs_array msgs
= { 0, 0, NULL
};
134 struct msgs
*mp
= NULL
;
139 if (nmh_init(argv
[0], true, true)) { return 1; }
141 set_done(freects_done
);
143 arguments
= getarguments (invo_name
, argc
, argv
, 1);
149 while ((cp
= *argp
++)) {
151 switch (smatch (++cp
, switches
)) {
153 ambigsw (cp
, switches
);
156 die("-%s unknown", cp
);
159 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
161 print_help (buf
, switches
, 1);
164 print_version(invo_name
);
182 icachesw
= &rcachesw
;
185 icachesw
= &wcachesw
;
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 switch (*icachesw
= smatch (cp
, cache_policy
)) {
191 ambigsw (cp
, cache_policy
);
194 die("%s unknown", cp
);
243 if (!(cp
= *argp
++) || *cp
== '-')
244 die("missing argument to %s", argp
[-2]);
246 die("too many parts (starting with %s), %d max",
252 if (!(cp
= *argp
++) || *cp
== '-')
253 die("missing argument to %s", argp
[-2]);
255 die("too many types (starting with %s), %d max",
261 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
262 die("missing argument to %s", argp
[-2]);
263 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
267 if (!(cp
= *argp
++) || *cp
== '-')
268 die("missing argument to %s", argp
[-2]);
270 formsw
= mh_xstrdup(etcpath(cp
));
274 * Switches for moreproc/mhlproc
277 if (!(progsw
= *argp
++) || *progsw
== '-')
278 die("missing argument to %s", argp
[-2]);
286 if (!(cp
= *argp
++) || *cp
== '-')
287 die("missing argument to %s", argp
[-2]);
291 * Switches for mhbuild
317 if (*cp
== '+' || *cp
== '@') {
319 die("only one folder at a time!");
320 folder
= pluspath (cp
);
322 app_msgarg(&msgs
, cp
);
325 /* null terminate the list of acceptable parts/types */
330 * Check if we've specified an additional profile
332 if ((cp
= getenv ("MHN"))) {
333 if ((fp
= fopen (cp
, "r"))) {
334 readconfig(NULL
, fp
, cp
, 0);
337 admonish ("", "unable to read $MHN profile (%s)", cp
);
342 * Read the standard profile setup
344 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
345 readconfig(NULL
, fp
, cp
, 0);
349 /* Check for public cache location */
350 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
353 /* Check for private cache location */
354 if (!(cache_private
= context_find (nmhprivcache
)))
355 cache_private
= ".cache";
356 cache_private
= mh_xstrdup(m_maildir(cache_private
));
359 * Cache the current directory before we do any chdirs()'s.
361 cwd
= mh_xstrdup(pwd());
363 if (!context_find ("path"))
364 free (path ("./", TFOLDER
));
367 * Process a mhn composition file (mhn -build)
373 if (showsw
|| storesw
|| cachesw
)
374 die("cannot use -build with -show, -store, -cache");
376 die("need to specify a %s composition file", invo_name
);
378 die("only one %s composition file at a time", invo_name
);
381 vec
[vecp
++] = "mhbuild";
384 vec
[vecp
++] = "-rfc934mode";
385 else if (rfc934sw
== -1)
386 vec
[vecp
++] = "-norfc934mode";
388 vec
[vecp
++] = msgs
.msgs
[0];
391 execvp ("mhbuild", vec
);
392 fprintf (stderr
, "unable to exec ");
397 * Process a mhn composition file (old MH style)
399 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
400 && !showsw
&& !storesw
&& !ntype
&& !file
401 && (cp
= getenv ("mhdraft"))
402 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
408 vec
[vecp
++] = "mhbuild";
411 vec
[vecp
++] = "-rfc934mode";
412 else if (rfc934sw
== -1)
413 vec
[vecp
++] = "-norfc934mode";
418 execvp ("mhbuild", vec
);
419 fprintf (stderr
, "unable to exec ");
423 if (file
&& msgs
.size
)
424 die("cannot specify msg and file at same time!");
427 * check if message is coming from file
430 cts
= mh_xcalloc(2, sizeof *cts
);
433 if ((ct
= parse_mime (file
)))
437 * message(s) are coming from a folder
440 app_msgarg(&msgs
, "cur");
442 folder
= getfolder (1);
443 maildir
= m_maildir (folder
);
445 if (chdir (maildir
) == NOTOK
)
446 adios (maildir
, "unable to change directory to");
448 /* read folder and create message structure */
449 if (!(mp
= folder_read (folder
, 1)))
450 die("unable to read folder %s", folder
);
452 /* check for empty folder */
454 die("no messages in %s", folder
);
456 /* parse all the message ranges/sequences and set SELECTED */
457 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
458 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
460 seq_setprev (mp
); /* set the previous-sequence */
462 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
465 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
466 if (is_selected(mp
, msgnum
)) {
469 msgnam
= m_name (msgnum
);
470 if ((ct
= parse_mime (msgnam
)))
480 * You can't give more than one of these flags
483 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
484 die("can only use one of -show, -list, -store, -cache at same time");
486 /* If no action is specified, assume -show */
487 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
491 SIGNAL (SIGQUIT
, quitser
);
492 SIGNAL (SIGPIPE
, pipeser
);
495 * Get the associated umask for the relevant contents.
497 for (ctp
= cts
; *ctp
; ctp
++) {
501 if (type_ok (ct
, 1) && !ct
->c_umask
) {
502 if (stat (ct
->c_file
, &st
) != NOTOK
)
503 ct
->c_umask
= ~(st
.st_mode
& 0777);
505 ct
->c_umask
= ~m_gmprot();
510 * List the message content
513 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
516 * Store the message content
519 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
520 store_all_messages (info
);
521 mhstoreinfo_free (info
);
524 /* If reading from a folder, do some updating */
526 context_replace (pfolder
, folder
);/* update current folder */
527 seq_setcur (mp
, mp
->hghsel
); /* update current message */
528 seq_save (mp
); /* synchronize sequences */
529 context_save (); /* save the context file */
533 * Cache the message content
536 cache_all_messages (cts
);
539 * Show the message content
542 show_all_messages (cts
, 0, 0, 0);
544 /* Now free all the structures for the content */
545 for (ctp
= cts
; *ctp
; ctp
++)
561 fprintf (stderr
, "\n");