]>
diplodocus.org Git - nmh/blob - uip/mhn.c
c333e06438bb52c8b016d3fcfb9009c3dd7058e3
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
13 #include "h/signals.h"
17 #include "h/fmt_scan.h"
19 #include "h/mhparse.h"
20 #include "h/mhcachesbr.h"
24 #include "sbr/m_maildir.h"
26 #include "mhshowsbr.h"
28 #define MHN_SWITCHES \
29 X("auto", 0, AUTOSW) \
30 X("noauto", 0, NAUTOSW) \
31 X("cache", 0, CACHESW) \
32 X("nocache", 0, NCACHESW) \
33 X("check", 0, CHECKSW) \
34 X("nocheck", 0, NCHECKSW) \
35 X("headers", 0, HEADSW) \
36 X("noheaders", 0, NHEADSW) \
37 X("list", 0, LISTSW) \
38 X("nolist", 0, NLISTSW) \
39 X("realsize", 0, SIZESW) \
40 X("norealsize", 0, NSIZESW) \
41 X("show", 0, SHOWSW) \
42 X("noshow", 0, NSHOWSW) \
43 X("store", 0, STORESW) \
44 X("nostore", 0, NSTORESW) \
45 X("verbose", 0, VERBSW) \
46 X("noverbose", 0, NVERBSW) \
47 X("file file", 0, FILESW) \
48 X("form formfile", 0, FORMSW) \
49 X("part number", 0, PARTSW) \
50 X("type content", 0, TYPESW) \
51 X("rcache policy", 0, RCACHESW) \
52 X("wcache policy", 0, WCACHESW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
58 X("debug", -5, DEBUGSW) \
60 * switches for moreproc/mhlproc \
62 X("moreproc program", -4, PROGSW) \
63 X("nomoreproc", -3, NPROGSW) \
64 X("length lines", -4, LENSW) \
65 X("width columns", -4, WIDTHSW) \
67 * switches for mhbuild \
69 X("build", -5, BUILDSW) \
70 X("nobuild", -7, NBUILDSW) \
71 X("rfc934mode", 0, RFC934SW) \
72 X("norfc934mode", 0, NRFC934SW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(MHN
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(MHN
, switches
);
87 * variables for mhbuild (mhn -build)
90 static int rfc934sw
= 0;
93 * what action to take?
100 #define quitser pipeser
105 static void pipeser (int);
109 main (int argc
, char **argv
)
114 int msgnum
, *icachesw
;
115 char *cp
, *file
= NULL
, *folder
= NULL
;
116 char *maildir
, buf
[100], **argp
;
119 struct msgs_array msgs
= { 0, 0, NULL
};
120 struct msgs
*mp
= NULL
;
125 if (nmh_init(argv
[0], true, true)) { return 1; }
127 set_done(freects_done
);
129 arguments
= getarguments (invo_name
, argc
, argv
, 1);
135 while ((cp
= *argp
++)) {
137 switch (smatch (++cp
, switches
)) {
139 ambigsw (cp
, switches
);
142 die("-%s unknown", cp
);
145 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
147 print_help (buf
, switches
, 1);
150 print_version(invo_name
);
168 icachesw
= &rcachesw
;
171 icachesw
= &wcachesw
;
173 if (!(cp
= *argp
++) || *cp
== '-')
174 die("missing argument to %s", argp
[-2]);
175 switch (*icachesw
= smatch (cp
, cache_policy
)) {
177 ambigsw (cp
, cache_policy
);
180 die("%s unknown", cp
);
229 if (!(cp
= *argp
++) || *cp
== '-')
230 die("missing argument to %s", argp
[-2]);
232 die("too many parts (starting with %s), %d max",
238 if (!(cp
= *argp
++) || *cp
== '-')
239 die("missing argument to %s", argp
[-2]);
241 die("too many types (starting with %s), %d max",
247 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
248 die("missing argument to %s", argp
[-2]);
249 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 die("missing argument to %s", argp
[-2]);
256 formsw
= mh_xstrdup(etcpath(cp
));
260 * Switches for moreproc/mhlproc
263 if (!(progsw
= *argp
++) || *progsw
== '-')
264 die("missing argument to %s", argp
[-2]);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 die("missing argument to %s", argp
[-2]);
277 * Switches for mhbuild
303 if (*cp
== '+' || *cp
== '@') {
305 die("only one folder at a time!");
306 folder
= pluspath (cp
);
308 app_msgarg(&msgs
, cp
);
311 /* null terminate the list of acceptable parts/types */
316 * Check if we've specified an additional profile
318 if ((cp
= getenv ("MHN"))) {
319 if ((fp
= fopen (cp
, "r"))) {
320 readconfig(NULL
, fp
, cp
, 0);
323 admonish ("", "unable to read $MHN profile (%s)", cp
);
328 * Read the standard profile setup
330 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
331 readconfig(NULL
, fp
, cp
, 0);
335 /* Check for public cache location */
336 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
339 /* Check for private cache location */
340 if (!(cache_private
= context_find (nmhprivcache
)))
341 cache_private
= ".cache";
342 cache_private
= mh_xstrdup(m_maildir(cache_private
));
345 * Cache the current directory before we do any chdirs()'s.
347 cwd
= mh_xstrdup(pwd());
349 if (!context_find ("path"))
350 free (path ("./", TFOLDER
));
353 * Process a mhn composition file (mhn -build)
359 if (showsw
|| storesw
|| cachesw
)
360 die("cannot use -build with -show, -store, -cache");
362 die("need to specify a %s composition file", invo_name
);
364 die("only one %s composition file at a time", invo_name
);
367 vec
[vecp
++] = "mhbuild";
370 vec
[vecp
++] = "-rfc934mode";
371 else if (rfc934sw
== -1)
372 vec
[vecp
++] = "-norfc934mode";
374 vec
[vecp
++] = msgs
.msgs
[0];
377 execvp ("mhbuild", vec
);
378 fprintf (stderr
, "unable to exec ");
383 * Process a mhn composition file (old MH style)
385 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
386 && !showsw
&& !storesw
&& !ntype
&& !file
387 && (cp
= getenv ("mhdraft"))
388 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
394 vec
[vecp
++] = "mhbuild";
397 vec
[vecp
++] = "-rfc934mode";
398 else if (rfc934sw
== -1)
399 vec
[vecp
++] = "-norfc934mode";
404 execvp ("mhbuild", vec
);
405 fprintf (stderr
, "unable to exec ");
409 if (file
&& msgs
.size
)
410 die("cannot specify msg and file at same time!");
413 * check if message is coming from file
416 cts
= mh_xcalloc(2, sizeof *cts
);
419 if ((ct
= parse_mime (file
)))
423 * message(s) are coming from a folder
426 app_msgarg(&msgs
, "cur");
428 folder
= getfolder (1);
429 maildir
= m_maildir (folder
);
431 if (chdir (maildir
) == NOTOK
)
432 adios (maildir
, "unable to change directory to");
434 /* read folder and create message structure */
435 if (!(mp
= folder_read (folder
, 1)))
436 die("unable to read folder %s", folder
);
438 /* check for empty folder */
440 die("no messages in %s", folder
);
442 /* parse all the message ranges/sequences and set SELECTED */
443 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
444 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
446 seq_setprev (mp
); /* set the previous-sequence */
448 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
451 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
452 if (is_selected(mp
, msgnum
)) {
455 msgnam
= m_name (msgnum
);
456 if ((ct
= parse_mime (msgnam
)))
466 * You can't give more than one of these flags
469 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
470 die("can only use one of -show, -list, -store, -cache at same time");
472 /* If no action is specified, assume -show */
473 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
477 SIGNAL (SIGQUIT
, quitser
);
478 SIGNAL (SIGPIPE
, pipeser
);
481 * Get the associated umask for the relevant contents.
483 for (ctp
= cts
; *ctp
; ctp
++) {
487 if (type_ok (ct
, 1) && !ct
->c_umask
) {
488 if (stat (ct
->c_file
, &st
) != NOTOK
)
489 ct
->c_umask
= ~(st
.st_mode
& 0777);
491 ct
->c_umask
= ~m_gmprot();
496 * List the message content
499 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
502 * Store the message content
505 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
506 store_all_messages (info
);
507 mhstoreinfo_free (info
);
510 /* If reading from a folder, do some updating */
512 context_replace (pfolder
, folder
);/* update current folder */
513 seq_setcur (mp
, mp
->hghsel
); /* update current message */
514 seq_save (mp
); /* synchronize sequences */
515 context_save (); /* save the context file */
519 * Cache the message content
522 cache_all_messages (cts
);
525 * Show the message content
528 show_all_messages (cts
, 0, 0, 0);
530 /* Now free all the structures for the content */
531 for (ctp
= cts
; *ctp
; ctp
++)
547 fprintf (stderr
, "\n");