]>
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/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/readconfig.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
20 #include "h/signals.h"
24 #include "h/fmt_scan.h"
26 #include "h/mhparse.h"
27 #include "h/mhcachesbr.h"
31 #include "sbr/m_maildir.h"
33 #include "mhshowsbr.h"
35 #define MHN_SWITCHES \
36 X("auto", 0, AUTOSW) \
37 X("noauto", 0, NAUTOSW) \
38 X("cache", 0, CACHESW) \
39 X("nocache", 0, NCACHESW) \
40 X("check", 0, CHECKSW) \
41 X("nocheck", 0, NCHECKSW) \
42 X("headers", 0, HEADSW) \
43 X("noheaders", 0, NHEADSW) \
44 X("list", 0, LISTSW) \
45 X("nolist", 0, NLISTSW) \
46 X("realsize", 0, SIZESW) \
47 X("norealsize", 0, NSIZESW) \
48 X("show", 0, SHOWSW) \
49 X("noshow", 0, NSHOWSW) \
50 X("store", 0, STORESW) \
51 X("nostore", 0, NSTORESW) \
52 X("verbose", 0, VERBSW) \
53 X("noverbose", 0, NVERBSW) \
54 X("file file", 0, FILESW) \
55 X("form formfile", 0, FORMSW) \
56 X("part number", 0, PARTSW) \
57 X("type content", 0, TYPESW) \
58 X("rcache policy", 0, RCACHESW) \
59 X("wcache policy", 0, WCACHESW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
65 X("debug", -5, DEBUGSW) \
67 * switches for moreproc/mhlproc \
69 X("moreproc program", -4, PROGSW) \
70 X("nomoreproc", -3, NPROGSW) \
71 X("length lines", -4, LENSW) \
72 X("width columns", -4, WIDTHSW) \
74 * switches for mhbuild \
76 X("build", -5, BUILDSW) \
77 X("nobuild", -7, NBUILDSW) \
78 X("rfc934mode", 0, RFC934SW) \
79 X("norfc934mode", 0, NRFC934SW) \
81 #define X(sw, minchars, id) id,
82 DEFINE_SWITCH_ENUM(MHN
);
85 #define X(sw, minchars, id) { sw, minchars, id },
86 DEFINE_SWITCH_ARRAY(MHN
, switches
);
94 * variables for mhbuild (mhn -build)
97 static int rfc934sw
= 0;
100 * what action to take?
107 #define quitser pipeser
112 static void pipeser (int);
116 main (int argc
, char **argv
)
121 int msgnum
, *icachesw
;
122 char *cp
, *file
= NULL
, *folder
= NULL
;
123 char *maildir
, buf
[100], **argp
;
126 struct msgs_array msgs
= { 0, 0, NULL
};
127 struct msgs
*mp
= NULL
;
132 if (nmh_init(argv
[0], true, true)) { return 1; }
134 set_done(freects_done
);
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 while ((cp
= *argp
++)) {
144 switch (smatch (++cp
, switches
)) {
146 ambigsw (cp
, switches
);
149 die("-%s unknown", cp
);
152 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
154 print_help (buf
, switches
, 1);
157 print_version(invo_name
);
175 icachesw
= &rcachesw
;
178 icachesw
= &wcachesw
;
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 switch (*icachesw
= smatch (cp
, cache_policy
)) {
184 ambigsw (cp
, cache_policy
);
187 die("%s unknown", cp
);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 die("missing argument to %s", argp
[-2]);
239 die("too many parts (starting with %s), %d max",
245 if (!(cp
= *argp
++) || *cp
== '-')
246 die("missing argument to %s", argp
[-2]);
248 die("too many types (starting with %s), %d max",
254 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
255 die("missing argument to %s", argp
[-2]);
256 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
260 if (!(cp
= *argp
++) || *cp
== '-')
261 die("missing argument to %s", argp
[-2]);
263 formsw
= mh_xstrdup(etcpath(cp
));
267 * Switches for moreproc/mhlproc
270 if (!(progsw
= *argp
++) || *progsw
== '-')
271 die("missing argument to %s", argp
[-2]);
279 if (!(cp
= *argp
++) || *cp
== '-')
280 die("missing argument to %s", argp
[-2]);
284 * Switches for mhbuild
310 if (*cp
== '+' || *cp
== '@') {
312 die("only one folder at a time!");
313 folder
= pluspath (cp
);
315 app_msgarg(&msgs
, cp
);
318 /* null terminate the list of acceptable parts/types */
323 * Check if we've specified an additional profile
325 if ((cp
= getenv ("MHN"))) {
326 if ((fp
= fopen (cp
, "r"))) {
327 readconfig(NULL
, fp
, cp
, 0);
330 admonish ("", "unable to read $MHN profile (%s)", cp
);
335 * Read the standard profile setup
337 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
338 readconfig(NULL
, fp
, cp
, 0);
342 /* Check for public cache location */
343 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
346 /* Check for private cache location */
347 if (!(cache_private
= context_find (nmhprivcache
)))
348 cache_private
= ".cache";
349 cache_private
= mh_xstrdup(m_maildir(cache_private
));
352 * Cache the current directory before we do any chdirs()'s.
354 cwd
= mh_xstrdup(pwd());
356 if (!context_find ("path"))
357 free (path ("./", TFOLDER
));
360 * Process a mhn composition file (mhn -build)
366 if (showsw
|| storesw
|| cachesw
)
367 die("cannot use -build with -show, -store, -cache");
369 die("need to specify a %s composition file", invo_name
);
371 die("only one %s composition file at a time", invo_name
);
374 vec
[vecp
++] = "mhbuild";
377 vec
[vecp
++] = "-rfc934mode";
378 else if (rfc934sw
== -1)
379 vec
[vecp
++] = "-norfc934mode";
381 vec
[vecp
++] = msgs
.msgs
[0];
384 execvp ("mhbuild", vec
);
385 fprintf (stderr
, "unable to exec ");
390 * Process a mhn composition file (old MH style)
392 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
393 && !showsw
&& !storesw
&& !ntype
&& !file
394 && (cp
= getenv ("mhdraft"))
395 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
401 vec
[vecp
++] = "mhbuild";
404 vec
[vecp
++] = "-rfc934mode";
405 else if (rfc934sw
== -1)
406 vec
[vecp
++] = "-norfc934mode";
411 execvp ("mhbuild", vec
);
412 fprintf (stderr
, "unable to exec ");
416 if (file
&& msgs
.size
)
417 die("cannot specify msg and file at same time!");
420 * check if message is coming from file
423 cts
= mh_xcalloc(2, sizeof *cts
);
426 if ((ct
= parse_mime (file
)))
430 * message(s) are coming from a folder
433 app_msgarg(&msgs
, "cur");
435 folder
= getfolder (1);
436 maildir
= m_maildir (folder
);
438 if (chdir (maildir
) == NOTOK
)
439 adios (maildir
, "unable to change directory to");
441 /* read folder and create message structure */
442 if (!(mp
= folder_read (folder
, 1)))
443 die("unable to read folder %s", folder
);
445 /* check for empty folder */
447 die("no messages in %s", folder
);
449 /* parse all the message ranges/sequences and set SELECTED */
450 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
451 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
453 seq_setprev (mp
); /* set the previous-sequence */
455 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
458 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
459 if (is_selected(mp
, msgnum
)) {
462 msgnam
= m_name (msgnum
);
463 if ((ct
= parse_mime (msgnam
)))
473 * You can't give more than one of these flags
476 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
477 die("can only use one of -show, -list, -store, -cache at same time");
479 /* If no action is specified, assume -show */
480 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
484 SIGNAL (SIGQUIT
, quitser
);
485 SIGNAL (SIGPIPE
, pipeser
);
488 * Get the associated umask for the relevant contents.
490 for (ctp
= cts
; *ctp
; ctp
++) {
494 if (type_ok (ct
, 1) && !ct
->c_umask
) {
495 if (stat (ct
->c_file
, &st
) != NOTOK
)
496 ct
->c_umask
= ~(st
.st_mode
& 0777);
498 ct
->c_umask
= ~m_gmprot();
503 * List the message content
506 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
509 * Store the message content
512 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
513 store_all_messages (info
);
514 mhstoreinfo_free (info
);
517 /* If reading from a folder, do some updating */
519 context_replace (pfolder
, folder
);/* update current folder */
520 seq_setcur (mp
, mp
->hghsel
); /* update current message */
521 seq_save (mp
); /* synchronize sequences */
522 context_save (); /* save the context file */
526 * Cache the message content
529 cache_all_messages (cts
);
532 * Show the message content
535 show_all_messages (cts
, 0, 0, 0);
537 /* Now free all the structures for the content */
538 for (ctp
= cts
; *ctp
; ctp
++)
554 fprintf (stderr
, "\n");