]>
diplodocus.org Git - nmh/blob - uip/mhn.c
aaf92d9e498e0afa968d6bc007bd526b892aad29
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/smatch.h"
10 #include "sbr/m_convert.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/readconfig.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
23 #include "h/signals.h"
27 #include "h/fmt_scan.h"
29 #include "h/mhparse.h"
30 #include "h/mhcachesbr.h"
34 #include "sbr/m_maildir.h"
36 #include "mhshowsbr.h"
38 #define MHN_SWITCHES \
39 X("auto", 0, AUTOSW) \
40 X("noauto", 0, NAUTOSW) \
41 X("cache", 0, CACHESW) \
42 X("nocache", 0, NCACHESW) \
43 X("check", 0, CHECKSW) \
44 X("nocheck", 0, NCHECKSW) \
45 X("headers", 0, HEADSW) \
46 X("noheaders", 0, NHEADSW) \
47 X("list", 0, LISTSW) \
48 X("nolist", 0, NLISTSW) \
49 X("realsize", 0, SIZESW) \
50 X("norealsize", 0, NSIZESW) \
51 X("show", 0, SHOWSW) \
52 X("noshow", 0, NSHOWSW) \
53 X("store", 0, STORESW) \
54 X("nostore", 0, NSTORESW) \
55 X("verbose", 0, VERBSW) \
56 X("noverbose", 0, NVERBSW) \
57 X("file file", 0, FILESW) \
58 X("form formfile", 0, FORMSW) \
59 X("part number", 0, PARTSW) \
60 X("type content", 0, TYPESW) \
61 X("rcache policy", 0, RCACHESW) \
62 X("wcache policy", 0, WCACHESW) \
63 X("version", 0, VERSIONSW) \
64 X("help", 0, HELPSW) \
68 X("debug", -5, DEBUGSW) \
70 * switches for moreproc/mhlproc \
72 X("moreproc program", -4, PROGSW) \
73 X("nomoreproc", -3, NPROGSW) \
74 X("length lines", -4, LENSW) \
75 X("width columns", -4, WIDTHSW) \
77 * switches for mhbuild \
79 X("build", -5, BUILDSW) \
80 X("nobuild", -7, NBUILDSW) \
81 X("rfc934mode", 0, RFC934SW) \
82 X("norfc934mode", 0, NRFC934SW) \
84 #define X(sw, minchars, id) id,
85 DEFINE_SWITCH_ENUM(MHN
);
88 #define X(sw, minchars, id) { sw, minchars, id },
89 DEFINE_SWITCH_ARRAY(MHN
, switches
);
97 * variables for mhbuild (mhn -build)
100 static int rfc934sw
= 0;
103 * what action to take?
110 #define quitser pipeser
115 static void pipeser (int);
119 main (int argc
, char **argv
)
124 int msgnum
, *icachesw
;
125 char *cp
, *file
= NULL
, *folder
= NULL
;
126 char *maildir
, buf
[100], **argp
;
129 struct msgs_array msgs
= { 0, 0, NULL
};
130 struct msgs
*mp
= NULL
;
135 if (nmh_init(argv
[0], true, true)) { return 1; }
137 set_done(freects_done
);
139 arguments
= getarguments (invo_name
, argc
, argv
, 1);
145 while ((cp
= *argp
++)) {
147 switch (smatch (++cp
, switches
)) {
149 ambigsw (cp
, switches
);
152 die("-%s unknown", cp
);
155 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
157 print_help (buf
, switches
, 1);
160 print_version(invo_name
);
178 icachesw
= &rcachesw
;
181 icachesw
= &wcachesw
;
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 switch (*icachesw
= smatch (cp
, cache_policy
)) {
187 ambigsw (cp
, cache_policy
);
190 die("%s unknown", cp
);
239 if (!(cp
= *argp
++) || *cp
== '-')
240 die("missing argument to %s", argp
[-2]);
242 die("too many parts (starting with %s), %d max",
248 if (!(cp
= *argp
++) || *cp
== '-')
249 die("missing argument to %s", argp
[-2]);
251 die("too many types (starting with %s), %d max",
257 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
258 die("missing argument to %s", argp
[-2]);
259 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
263 if (!(cp
= *argp
++) || *cp
== '-')
264 die("missing argument to %s", argp
[-2]);
266 formsw
= mh_xstrdup(etcpath(cp
));
270 * Switches for moreproc/mhlproc
273 if (!(progsw
= *argp
++) || *progsw
== '-')
274 die("missing argument to %s", argp
[-2]);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 die("missing argument to %s", argp
[-2]);
287 * Switches for mhbuild
313 if (*cp
== '+' || *cp
== '@') {
315 die("only one folder at a time!");
316 folder
= pluspath (cp
);
318 app_msgarg(&msgs
, cp
);
321 /* null terminate the list of acceptable parts/types */
326 * Check if we've specified an additional profile
328 if ((cp
= getenv ("MHN"))) {
329 if ((fp
= fopen (cp
, "r"))) {
330 readconfig(NULL
, fp
, cp
, 0);
333 admonish ("", "unable to read $MHN profile (%s)", cp
);
338 * Read the standard profile setup
340 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
341 readconfig(NULL
, fp
, cp
, 0);
345 /* Check for public cache location */
346 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
349 /* Check for private cache location */
350 if (!(cache_private
= context_find (nmhprivcache
)))
351 cache_private
= ".cache";
352 cache_private
= mh_xstrdup(m_maildir(cache_private
));
355 * Cache the current directory before we do any chdirs()'s.
357 cwd
= mh_xstrdup(pwd());
359 if (!context_find ("path"))
360 free (path ("./", TFOLDER
));
363 * Process a mhn composition file (mhn -build)
369 if (showsw
|| storesw
|| cachesw
)
370 die("cannot use -build with -show, -store, -cache");
372 die("need to specify a %s composition file", invo_name
);
374 die("only one %s composition file at a time", invo_name
);
377 vec
[vecp
++] = "mhbuild";
380 vec
[vecp
++] = "-rfc934mode";
381 else if (rfc934sw
== -1)
382 vec
[vecp
++] = "-norfc934mode";
384 vec
[vecp
++] = msgs
.msgs
[0];
387 execvp ("mhbuild", vec
);
388 fprintf (stderr
, "unable to exec ");
393 * Process a mhn composition file (old MH style)
395 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
396 && !showsw
&& !storesw
&& !ntype
&& !file
397 && (cp
= getenv ("mhdraft"))
398 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
404 vec
[vecp
++] = "mhbuild";
407 vec
[vecp
++] = "-rfc934mode";
408 else if (rfc934sw
== -1)
409 vec
[vecp
++] = "-norfc934mode";
414 execvp ("mhbuild", vec
);
415 fprintf (stderr
, "unable to exec ");
419 if (file
&& msgs
.size
)
420 die("cannot specify msg and file at same time!");
423 * check if message is coming from file
426 cts
= mh_xcalloc(2, sizeof *cts
);
429 if ((ct
= parse_mime (file
)))
433 * message(s) are coming from a folder
436 app_msgarg(&msgs
, "cur");
438 folder
= getfolder (1);
439 maildir
= m_maildir (folder
);
441 if (chdir (maildir
) == NOTOK
)
442 adios (maildir
, "unable to change directory to");
444 /* read folder and create message structure */
445 if (!(mp
= folder_read (folder
, 1)))
446 die("unable to read folder %s", folder
);
448 /* check for empty folder */
450 die("no messages in %s", folder
);
452 /* parse all the message ranges/sequences and set SELECTED */
453 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
454 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
456 seq_setprev (mp
); /* set the previous-sequence */
458 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
461 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
462 if (is_selected(mp
, msgnum
)) {
465 msgnam
= m_name (msgnum
);
466 if ((ct
= parse_mime (msgnam
)))
476 * You can't give more than one of these flags
479 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
480 die("can only use one of -show, -list, -store, -cache at same time");
482 /* If no action is specified, assume -show */
483 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
487 SIGNAL (SIGQUIT
, quitser
);
488 SIGNAL (SIGPIPE
, pipeser
);
491 * Get the associated umask for the relevant contents.
493 for (ctp
= cts
; *ctp
; ctp
++) {
497 if (type_ok (ct
, 1) && !ct
->c_umask
) {
498 if (stat (ct
->c_file
, &st
) != NOTOK
)
499 ct
->c_umask
= ~(st
.st_mode
& 0777);
501 ct
->c_umask
= ~m_gmprot();
506 * List the message content
509 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
512 * Store the message content
515 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
516 store_all_messages (info
);
517 mhstoreinfo_free (info
);
520 /* If reading from a folder, do some updating */
522 context_replace (pfolder
, folder
);/* update current folder */
523 seq_setcur (mp
, mp
->hghsel
); /* update current message */
524 seq_save (mp
); /* synchronize sequences */
525 context_save (); /* save the context file */
529 * Cache the message content
532 cache_all_messages (cts
);
535 * Show the message content
538 show_all_messages (cts
, 0, 0, 0);
540 /* Now free all the structures for the content */
541 for (ctp
= cts
; *ctp
; ctp
++)
557 fprintf (stderr
, "\n");