]>
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.
10 #include <h/signals.h>
14 #include <h/fmt_scan.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
21 #include "sbr/m_maildir.h"
23 #include "mhshowsbr.h"
25 #define MHN_SWITCHES \
26 X("auto", 0, AUTOSW) \
27 X("noauto", 0, NAUTOSW) \
28 X("cache", 0, CACHESW) \
29 X("nocache", 0, NCACHESW) \
30 X("check", 0, CHECKSW) \
31 X("nocheck", 0, NCHECKSW) \
32 X("headers", 0, HEADSW) \
33 X("noheaders", 0, NHEADSW) \
34 X("list", 0, LISTSW) \
35 X("nolist", 0, NLISTSW) \
36 X("realsize", 0, SIZESW) \
37 X("norealsize", 0, NSIZESW) \
38 X("show", 0, SHOWSW) \
39 X("noshow", 0, NSHOWSW) \
40 X("store", 0, STORESW) \
41 X("nostore", 0, NSTORESW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
55 X("debug", -5, DEBUGSW) \
57 * switches for moreproc/mhlproc \
59 X("moreproc program", -4, PROGSW) \
60 X("nomoreproc", -3, NPROGSW) \
61 X("length lines", -4, LENSW) \
62 X("width columns", -4, WIDTHSW) \
64 * switches for mhbuild \
66 X("build", -5, BUILDSW) \
67 X("nobuild", -7, NBUILDSW) \
68 X("rfc934mode", 0, RFC934SW) \
69 X("norfc934mode", 0, NRFC934SW) \
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(MHN
);
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(MHN
, switches
);
84 * variables for mhbuild (mhn -build)
87 static int rfc934sw
= 0;
90 * what action to take?
97 #define quitser pipeser
102 static void pipeser (int);
106 main (int argc
, char **argv
)
111 int msgnum
, *icachesw
;
112 char *cp
, *file
= NULL
, *folder
= NULL
;
113 char *maildir
, buf
[100], **argp
;
116 struct msgs_array msgs
= { 0, 0, NULL
};
117 struct msgs
*mp
= NULL
;
122 if (nmh_init(argv
[0], true, true)) { return 1; }
124 set_done(freects_done
);
126 arguments
= getarguments (invo_name
, argc
, argv
, 1);
132 while ((cp
= *argp
++)) {
134 switch (smatch (++cp
, switches
)) {
136 ambigsw (cp
, switches
);
139 die("-%s unknown", cp
);
142 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
144 print_help (buf
, switches
, 1);
147 print_version(invo_name
);
165 icachesw
= &rcachesw
;
168 icachesw
= &wcachesw
;
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
172 switch (*icachesw
= smatch (cp
, cache_policy
)) {
174 ambigsw (cp
, cache_policy
);
177 die("%s unknown", cp
);
226 if (!(cp
= *argp
++) || *cp
== '-')
227 die("missing argument to %s", argp
[-2]);
229 die("too many parts (starting with %s), %d max",
235 if (!(cp
= *argp
++) || *cp
== '-')
236 die("missing argument to %s", argp
[-2]);
238 die("too many types (starting with %s), %d max",
244 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
245 die("missing argument to %s", argp
[-2]);
246 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
253 formsw
= mh_xstrdup(etcpath(cp
));
257 * Switches for moreproc/mhlproc
260 if (!(progsw
= *argp
++) || *progsw
== '-')
261 die("missing argument to %s", argp
[-2]);
269 if (!(cp
= *argp
++) || *cp
== '-')
270 die("missing argument to %s", argp
[-2]);
274 * Switches for mhbuild
300 if (*cp
== '+' || *cp
== '@') {
302 die("only one folder at a time!");
303 folder
= pluspath (cp
);
305 app_msgarg(&msgs
, cp
);
308 /* null terminate the list of acceptable parts/types */
313 * Check if we've specified an additional profile
315 if ((cp
= getenv ("MHN"))) {
316 if ((fp
= fopen (cp
, "r"))) {
317 readconfig(NULL
, fp
, cp
, 0);
320 admonish ("", "unable to read $MHN profile (%s)", cp
);
325 * Read the standard profile setup
327 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
328 readconfig(NULL
, fp
, cp
, 0);
332 /* Check for public cache location */
333 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
336 /* Check for private cache location */
337 if (!(cache_private
= context_find (nmhprivcache
)))
338 cache_private
= ".cache";
339 cache_private
= mh_xstrdup(m_maildir(cache_private
));
342 * Cache the current directory before we do any chdirs()'s.
344 cwd
= mh_xstrdup(pwd());
346 if (!context_find ("path"))
347 free (path ("./", TFOLDER
));
350 * Process a mhn composition file (mhn -build)
356 if (showsw
|| storesw
|| cachesw
)
357 die("cannot use -build with -show, -store, -cache");
359 die("need to specify a %s composition file", invo_name
);
361 die("only one %s composition file at a time", invo_name
);
364 vec
[vecp
++] = "mhbuild";
367 vec
[vecp
++] = "-rfc934mode";
368 else if (rfc934sw
== -1)
369 vec
[vecp
++] = "-norfc934mode";
371 vec
[vecp
++] = msgs
.msgs
[0];
374 execvp ("mhbuild", vec
);
375 fprintf (stderr
, "unable to exec ");
380 * Process a mhn composition file (old MH style)
382 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
383 && !showsw
&& !storesw
&& !ntype
&& !file
384 && (cp
= getenv ("mhdraft"))
385 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
391 vec
[vecp
++] = "mhbuild";
394 vec
[vecp
++] = "-rfc934mode";
395 else if (rfc934sw
== -1)
396 vec
[vecp
++] = "-norfc934mode";
401 execvp ("mhbuild", vec
);
402 fprintf (stderr
, "unable to exec ");
406 if (file
&& msgs
.size
)
407 die("cannot specify msg and file at same time!");
410 * check if message is coming from file
413 cts
= mh_xcalloc(2, sizeof *cts
);
416 if ((ct
= parse_mime (file
)))
420 * message(s) are coming from a folder
423 app_msgarg(&msgs
, "cur");
425 folder
= getfolder (1);
426 maildir
= m_maildir (folder
);
428 if (chdir (maildir
) == NOTOK
)
429 adios (maildir
, "unable to change directory to");
431 /* read folder and create message structure */
432 if (!(mp
= folder_read (folder
, 1)))
433 die("unable to read folder %s", folder
);
435 /* check for empty folder */
437 die("no messages in %s", folder
);
439 /* parse all the message ranges/sequences and set SELECTED */
440 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
441 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
443 seq_setprev (mp
); /* set the previous-sequence */
445 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
448 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
449 if (is_selected(mp
, msgnum
)) {
452 msgnam
= m_name (msgnum
);
453 if ((ct
= parse_mime (msgnam
)))
463 * You can't give more than one of these flags
466 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
467 die("can only use one of -show, -list, -store, -cache at same time");
469 /* If no action is specified, assume -show */
470 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
474 SIGNAL (SIGQUIT
, quitser
);
475 SIGNAL (SIGPIPE
, pipeser
);
478 * Get the associated umask for the relevant contents.
480 for (ctp
= cts
; *ctp
; ctp
++) {
484 if (type_ok (ct
, 1) && !ct
->c_umask
) {
485 if (stat (ct
->c_file
, &st
) != NOTOK
)
486 ct
->c_umask
= ~(st
.st_mode
& 0777);
488 ct
->c_umask
= ~m_gmprot();
493 * List the message content
496 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
499 * Store the message content
502 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
503 store_all_messages (info
);
504 mhstoreinfo_free (info
);
507 /* If reading from a folder, do some updating */
509 context_replace (pfolder
, folder
);/* update current folder */
510 seq_setcur (mp
, mp
->hghsel
); /* update current message */
511 seq_save (mp
); /* synchronize sequences */
512 context_save (); /* save the context file */
516 * Cache the message content
519 cache_all_messages (cts
);
522 * Show the message content
525 show_all_messages (cts
, 0, 0, 0);
527 /* Now free all the structures for the content */
528 for (ctp
= cts
; *ctp
; ctp
++)
544 fprintf (stderr
, "\n");