]>
diplodocus.org Git - nmh/blob - uip/mhn.c
d127925183991375c1bbd8f4f39fbc3917d5451e
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_save.h"
10 #include "sbr/smatch.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/readconfig.h"
18 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/error.h"
24 #include "h/signals.h"
28 #include "h/fmt_scan.h"
30 #include "h/mhparse.h"
31 #include "h/mhcachesbr.h"
35 #include "sbr/m_maildir.h"
37 #include "mhshowsbr.h"
39 #define MHN_SWITCHES \
40 X("auto", 0, AUTOSW) \
41 X("noauto", 0, NAUTOSW) \
42 X("cache", 0, CACHESW) \
43 X("nocache", 0, NCACHESW) \
44 X("check", 0, CHECKSW) \
45 X("nocheck", 0, NCHECKSW) \
46 X("headers", 0, HEADSW) \
47 X("noheaders", 0, NHEADSW) \
48 X("list", 0, LISTSW) \
49 X("nolist", 0, NLISTSW) \
50 X("realsize", 0, SIZESW) \
51 X("norealsize", 0, NSIZESW) \
52 X("show", 0, SHOWSW) \
53 X("noshow", 0, NSHOWSW) \
54 X("store", 0, STORESW) \
55 X("nostore", 0, NSTORESW) \
56 X("verbose", 0, VERBSW) \
57 X("noverbose", 0, NVERBSW) \
58 X("file file", 0, FILESW) \
59 X("form formfile", 0, FORMSW) \
60 X("part number", 0, PARTSW) \
61 X("type content", 0, TYPESW) \
62 X("rcache policy", 0, RCACHESW) \
63 X("wcache policy", 0, WCACHESW) \
64 X("version", 0, VERSIONSW) \
65 X("help", 0, HELPSW) \
69 X("debug", -5, DEBUGSW) \
71 * switches for moreproc/mhlproc \
73 X("moreproc program", -4, PROGSW) \
74 X("nomoreproc", -3, NPROGSW) \
75 X("length lines", -4, LENSW) \
76 X("width columns", -4, WIDTHSW) \
78 * switches for mhbuild \
80 X("build", -5, BUILDSW) \
81 X("nobuild", -7, NBUILDSW) \
82 X("rfc934mode", 0, RFC934SW) \
83 X("norfc934mode", 0, NRFC934SW) \
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(MHN
);
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(MHN
, switches
);
98 * variables for mhbuild (mhn -build)
101 static int rfc934sw
= 0;
104 * what action to take?
111 #define quitser pipeser
116 static void pipeser (int);
120 main (int argc
, char **argv
)
125 int msgnum
, *icachesw
;
126 char *cp
, *file
= NULL
, *folder
= NULL
;
127 char *maildir
, buf
[100], **argp
;
130 struct msgs_array msgs
= { 0, 0, NULL
};
131 struct msgs
*mp
= NULL
;
136 if (nmh_init(argv
[0], true, true)) { return 1; }
138 set_done(freects_done
);
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
146 while ((cp
= *argp
++)) {
148 switch (smatch (++cp
, switches
)) {
150 ambigsw (cp
, switches
);
153 die("-%s unknown", cp
);
156 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
158 print_help (buf
, switches
, 1);
161 print_version(invo_name
);
179 icachesw
= &rcachesw
;
182 icachesw
= &wcachesw
;
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 switch (*icachesw
= smatch (cp
, cache_policy
)) {
188 ambigsw (cp
, cache_policy
);
191 die("%s unknown", cp
);
240 if (!(cp
= *argp
++) || *cp
== '-')
241 die("missing argument to %s", argp
[-2]);
243 die("too many parts (starting with %s), %d max",
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
252 die("too many types (starting with %s), %d max",
258 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
259 die("missing argument to %s", argp
[-2]);
260 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
264 if (!(cp
= *argp
++) || *cp
== '-')
265 die("missing argument to %s", argp
[-2]);
267 formsw
= mh_xstrdup(etcpath(cp
));
271 * Switches for moreproc/mhlproc
274 if (!(progsw
= *argp
++) || *progsw
== '-')
275 die("missing argument to %s", argp
[-2]);
283 if (!(cp
= *argp
++) || *cp
== '-')
284 die("missing argument to %s", argp
[-2]);
288 * Switches for mhbuild
314 if (*cp
== '+' || *cp
== '@') {
316 die("only one folder at a time!");
317 folder
= pluspath (cp
);
319 app_msgarg(&msgs
, cp
);
322 /* null terminate the list of acceptable parts/types */
327 * Check if we've specified an additional profile
329 if ((cp
= getenv ("MHN"))) {
330 if ((fp
= fopen (cp
, "r"))) {
331 readconfig(NULL
, fp
, cp
, 0);
334 admonish ("", "unable to read $MHN profile (%s)", cp
);
339 * Read the standard profile setup
341 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
342 readconfig(NULL
, fp
, cp
, 0);
346 /* Check for public cache location */
347 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
350 /* Check for private cache location */
351 if (!(cache_private
= context_find (nmhprivcache
)))
352 cache_private
= ".cache";
353 cache_private
= mh_xstrdup(m_maildir(cache_private
));
356 * Cache the current directory before we do any chdirs()'s.
358 cwd
= mh_xstrdup(pwd());
360 if (!context_find ("path"))
361 free (path ("./", TFOLDER
));
364 * Process a mhn composition file (mhn -build)
370 if (showsw
|| storesw
|| cachesw
)
371 die("cannot use -build with -show, -store, -cache");
373 die("need to specify a %s composition file", invo_name
);
375 die("only one %s composition file at a time", invo_name
);
378 vec
[vecp
++] = "mhbuild";
381 vec
[vecp
++] = "-rfc934mode";
382 else if (rfc934sw
== -1)
383 vec
[vecp
++] = "-norfc934mode";
385 vec
[vecp
++] = msgs
.msgs
[0];
388 execvp ("mhbuild", vec
);
389 fprintf (stderr
, "unable to exec ");
394 * Process a mhn composition file (old MH style)
396 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
397 && !showsw
&& !storesw
&& !ntype
&& !file
398 && (cp
= getenv ("mhdraft"))
399 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
405 vec
[vecp
++] = "mhbuild";
408 vec
[vecp
++] = "-rfc934mode";
409 else if (rfc934sw
== -1)
410 vec
[vecp
++] = "-norfc934mode";
415 execvp ("mhbuild", vec
);
416 fprintf (stderr
, "unable to exec ");
420 if (file
&& msgs
.size
)
421 die("cannot specify msg and file at same time!");
424 * check if message is coming from file
427 cts
= mh_xcalloc(2, sizeof *cts
);
430 if ((ct
= parse_mime (file
)))
434 * message(s) are coming from a folder
437 app_msgarg(&msgs
, "cur");
439 folder
= getfolder (1);
440 maildir
= m_maildir (folder
);
442 if (chdir (maildir
) == NOTOK
)
443 adios (maildir
, "unable to change directory to");
445 /* read folder and create message structure */
446 if (!(mp
= folder_read (folder
, 1)))
447 die("unable to read folder %s", folder
);
449 /* check for empty folder */
451 die("no messages in %s", folder
);
453 /* parse all the message ranges/sequences and set SELECTED */
454 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
455 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
457 seq_setprev (mp
); /* set the previous-sequence */
459 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
462 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
463 if (is_selected(mp
, msgnum
)) {
466 msgnam
= m_name (msgnum
);
467 if ((ct
= parse_mime (msgnam
)))
477 * You can't give more than one of these flags
480 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
481 die("can only use one of -show, -list, -store, -cache at same time");
483 /* If no action is specified, assume -show */
484 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
488 SIGNAL (SIGQUIT
, quitser
);
489 SIGNAL (SIGPIPE
, pipeser
);
492 * Get the associated umask for the relevant contents.
494 for (ctp
= cts
; *ctp
; ctp
++) {
498 if (type_ok (ct
, 1) && !ct
->c_umask
) {
499 if (stat (ct
->c_file
, &st
) != NOTOK
)
500 ct
->c_umask
= ~(st
.st_mode
& 0777);
502 ct
->c_umask
= ~m_gmprot();
507 * List the message content
510 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
513 * Store the message content
516 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
517 store_all_messages (info
);
518 mhstoreinfo_free (info
);
521 /* If reading from a folder, do some updating */
523 context_replace (pfolder
, folder
);/* update current folder */
524 seq_setcur (mp
, mp
->hghsel
); /* update current message */
525 seq_save (mp
); /* synchronize sequences */
526 context_save (); /* save the context file */
530 * Cache the message content
533 cache_all_messages (cts
);
536 * Show the message content
539 show_all_messages (cts
, 0, 0, 0);
541 /* Now free all the structures for the content */
542 for (ctp
= cts
; *ctp
; ctp
++)
558 fprintf (stderr
, "\n");