]>
diplodocus.org Git - nmh/blob - uip/mhn.c
866cdf2c3ae954c207190d73c9b1e710b4d13540
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.
11 #include <h/signals.h>
15 #include <h/fmt_scan.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
22 #include "sbr/m_maildir.h"
24 #include "mhshowsbr.h"
26 #define MHN_SWITCHES \
27 X("auto", 0, AUTOSW) \
28 X("noauto", 0, NAUTOSW) \
29 X("cache", 0, CACHESW) \
30 X("nocache", 0, NCACHESW) \
31 X("check", 0, CHECKSW) \
32 X("nocheck", 0, NCHECKSW) \
33 X("headers", 0, HEADSW) \
34 X("noheaders", 0, NHEADSW) \
35 X("list", 0, LISTSW) \
36 X("nolist", 0, NLISTSW) \
37 X("realsize", 0, SIZESW) \
38 X("norealsize", 0, NSIZESW) \
39 X("show", 0, SHOWSW) \
40 X("noshow", 0, NSHOWSW) \
41 X("store", 0, STORESW) \
42 X("nostore", 0, NSTORESW) \
43 X("verbose", 0, VERBSW) \
44 X("noverbose", 0, NVERBSW) \
45 X("file file", 0, FILESW) \
46 X("form formfile", 0, FORMSW) \
47 X("part number", 0, PARTSW) \
48 X("type content", 0, TYPESW) \
49 X("rcache policy", 0, RCACHESW) \
50 X("wcache policy", 0, WCACHESW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
56 X("debug", -5, DEBUGSW) \
58 * switches for moreproc/mhlproc \
60 X("moreproc program", -4, PROGSW) \
61 X("nomoreproc", -3, NPROGSW) \
62 X("length lines", -4, LENSW) \
63 X("width columns", -4, WIDTHSW) \
65 * switches for mhbuild \
67 X("build", -5, BUILDSW) \
68 X("nobuild", -7, NBUILDSW) \
69 X("rfc934mode", 0, RFC934SW) \
70 X("norfc934mode", 0, NRFC934SW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(MHN
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(MHN
, switches
);
85 * variables for mhbuild (mhn -build)
88 static int rfc934sw
= 0;
91 * what action to take?
98 #define quitser pipeser
103 static void pipeser (int);
107 main (int argc
, char **argv
)
112 int msgnum
, *icachesw
;
113 char *cp
, *file
= NULL
, *folder
= NULL
;
114 char *maildir
, buf
[100], **argp
;
117 struct msgs_array msgs
= { 0, 0, NULL
};
118 struct msgs
*mp
= NULL
;
123 if (nmh_init(argv
[0], true, true)) { return 1; }
125 set_done(freects_done
);
127 arguments
= getarguments (invo_name
, argc
, argv
, 1);
133 while ((cp
= *argp
++)) {
135 switch (smatch (++cp
, switches
)) {
137 ambigsw (cp
, switches
);
140 die("-%s unknown", cp
);
143 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
145 print_help (buf
, switches
, 1);
148 print_version(invo_name
);
166 icachesw
= &rcachesw
;
169 icachesw
= &wcachesw
;
171 if (!(cp
= *argp
++) || *cp
== '-')
172 die("missing argument to %s", argp
[-2]);
173 switch (*icachesw
= smatch (cp
, cache_policy
)) {
175 ambigsw (cp
, cache_policy
);
178 die("%s unknown", cp
);
227 if (!(cp
= *argp
++) || *cp
== '-')
228 die("missing argument to %s", argp
[-2]);
230 die("too many parts (starting with %s), %d max",
236 if (!(cp
= *argp
++) || *cp
== '-')
237 die("missing argument to %s", argp
[-2]);
239 die("too many types (starting with %s), %d max",
245 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
246 die("missing argument to %s", argp
[-2]);
247 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
254 formsw
= mh_xstrdup(etcpath(cp
));
258 * Switches for moreproc/mhlproc
261 if (!(progsw
= *argp
++) || *progsw
== '-')
262 die("missing argument to %s", argp
[-2]);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
275 * Switches for mhbuild
301 if (*cp
== '+' || *cp
== '@') {
303 die("only one folder at a time!");
304 folder
= pluspath (cp
);
306 app_msgarg(&msgs
, cp
);
309 /* null terminate the list of acceptable parts/types */
314 * Check if we've specified an additional profile
316 if ((cp
= getenv ("MHN"))) {
317 if ((fp
= fopen (cp
, "r"))) {
318 readconfig(NULL
, fp
, cp
, 0);
321 admonish ("", "unable to read $MHN profile (%s)", cp
);
326 * Read the standard profile setup
328 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
329 readconfig(NULL
, fp
, cp
, 0);
333 /* Check for public cache location */
334 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
337 /* Check for private cache location */
338 if (!(cache_private
= context_find (nmhprivcache
)))
339 cache_private
= ".cache";
340 cache_private
= mh_xstrdup(m_maildir(cache_private
));
343 * Cache the current directory before we do any chdirs()'s.
345 cwd
= mh_xstrdup(pwd());
347 if (!context_find ("path"))
348 free (path ("./", TFOLDER
));
351 * Process a mhn composition file (mhn -build)
357 if (showsw
|| storesw
|| cachesw
)
358 die("cannot use -build with -show, -store, -cache");
360 die("need to specify a %s composition file", invo_name
);
362 die("only one %s composition file at a time", invo_name
);
365 vec
[vecp
++] = "mhbuild";
368 vec
[vecp
++] = "-rfc934mode";
369 else if (rfc934sw
== -1)
370 vec
[vecp
++] = "-norfc934mode";
372 vec
[vecp
++] = msgs
.msgs
[0];
375 execvp ("mhbuild", vec
);
376 fprintf (stderr
, "unable to exec ");
381 * Process a mhn composition file (old MH style)
383 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
384 && !showsw
&& !storesw
&& !ntype
&& !file
385 && (cp
= getenv ("mhdraft"))
386 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
392 vec
[vecp
++] = "mhbuild";
395 vec
[vecp
++] = "-rfc934mode";
396 else if (rfc934sw
== -1)
397 vec
[vecp
++] = "-norfc934mode";
402 execvp ("mhbuild", vec
);
403 fprintf (stderr
, "unable to exec ");
407 if (file
&& msgs
.size
)
408 die("cannot specify msg and file at same time!");
411 * check if message is coming from file
414 cts
= mh_xcalloc(2, sizeof *cts
);
417 if ((ct
= parse_mime (file
)))
421 * message(s) are coming from a folder
424 app_msgarg(&msgs
, "cur");
426 folder
= getfolder (1);
427 maildir
= m_maildir (folder
);
429 if (chdir (maildir
) == NOTOK
)
430 adios (maildir
, "unable to change directory to");
432 /* read folder and create message structure */
433 if (!(mp
= folder_read (folder
, 1)))
434 die("unable to read folder %s", folder
);
436 /* check for empty folder */
438 die("no messages in %s", folder
);
440 /* parse all the message ranges/sequences and set SELECTED */
441 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
442 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
444 seq_setprev (mp
); /* set the previous-sequence */
446 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
449 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
450 if (is_selected(mp
, msgnum
)) {
453 msgnam
= m_name (msgnum
);
454 if ((ct
= parse_mime (msgnam
)))
464 * You can't give more than one of these flags
467 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
468 die("can only use one of -show, -list, -store, -cache at same time");
470 /* If no action is specified, assume -show */
471 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
475 SIGNAL (SIGQUIT
, quitser
);
476 SIGNAL (SIGPIPE
, pipeser
);
479 * Get the associated umask for the relevant contents.
481 for (ctp
= cts
; *ctp
; ctp
++) {
485 if (type_ok (ct
, 1) && !ct
->c_umask
) {
486 if (stat (ct
->c_file
, &st
) != NOTOK
)
487 ct
->c_umask
= ~(st
.st_mode
& 0777);
489 ct
->c_umask
= ~m_gmprot();
494 * List the message content
497 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
500 * Store the message content
503 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
504 store_all_messages (info
);
505 mhstoreinfo_free (info
);
508 /* If reading from a folder, do some updating */
510 context_replace (pfolder
, folder
);/* update current folder */
511 seq_setcur (mp
, mp
->hghsel
); /* update current message */
512 seq_save (mp
); /* synchronize sequences */
513 context_save (); /* save the context file */
517 * Cache the message content
520 cache_all_messages (cts
);
523 * Show the message content
526 show_all_messages (cts
, 0, 0, 0);
528 /* Now free all the structures for the content */
529 for (ctp
= cts
; *ctp
; ctp
++)
545 fprintf (stderr
, "\n");