]>
diplodocus.org Git - nmh/blob - uip/mhn.c
7afdd85ab5dab7ffd84fe77f14b5fdf3e94df8c6
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_help.h"
10 #include "sbr/error.h"
12 #include "h/signals.h"
16 #include "h/fmt_scan.h"
18 #include "h/mhparse.h"
19 #include "h/mhcachesbr.h"
23 #include "sbr/m_maildir.h"
25 #include "mhshowsbr.h"
27 #define MHN_SWITCHES \
28 X("auto", 0, AUTOSW) \
29 X("noauto", 0, NAUTOSW) \
30 X("cache", 0, CACHESW) \
31 X("nocache", 0, NCACHESW) \
32 X("check", 0, CHECKSW) \
33 X("nocheck", 0, NCHECKSW) \
34 X("headers", 0, HEADSW) \
35 X("noheaders", 0, NHEADSW) \
36 X("list", 0, LISTSW) \
37 X("nolist", 0, NLISTSW) \
38 X("realsize", 0, SIZESW) \
39 X("norealsize", 0, NSIZESW) \
40 X("show", 0, SHOWSW) \
41 X("noshow", 0, NSHOWSW) \
42 X("store", 0, STORESW) \
43 X("nostore", 0, NSTORESW) \
44 X("verbose", 0, VERBSW) \
45 X("noverbose", 0, NVERBSW) \
46 X("file file", 0, FILESW) \
47 X("form formfile", 0, FORMSW) \
48 X("part number", 0, PARTSW) \
49 X("type content", 0, TYPESW) \
50 X("rcache policy", 0, RCACHESW) \
51 X("wcache policy", 0, WCACHESW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
57 X("debug", -5, DEBUGSW) \
59 * switches for moreproc/mhlproc \
61 X("moreproc program", -4, PROGSW) \
62 X("nomoreproc", -3, NPROGSW) \
63 X("length lines", -4, LENSW) \
64 X("width columns", -4, WIDTHSW) \
66 * switches for mhbuild \
68 X("build", -5, BUILDSW) \
69 X("nobuild", -7, NBUILDSW) \
70 X("rfc934mode", 0, RFC934SW) \
71 X("norfc934mode", 0, NRFC934SW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(MHN
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(MHN
, switches
);
86 * variables for mhbuild (mhn -build)
89 static int rfc934sw
= 0;
92 * what action to take?
99 #define quitser pipeser
104 static void pipeser (int);
108 main (int argc
, char **argv
)
113 int msgnum
, *icachesw
;
114 char *cp
, *file
= NULL
, *folder
= NULL
;
115 char *maildir
, buf
[100], **argp
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
119 struct msgs
*mp
= NULL
;
124 if (nmh_init(argv
[0], true, true)) { return 1; }
126 set_done(freects_done
);
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
134 while ((cp
= *argp
++)) {
136 switch (smatch (++cp
, switches
)) {
138 ambigsw (cp
, switches
);
141 die("-%s unknown", cp
);
144 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
146 print_help (buf
, switches
, 1);
149 print_version(invo_name
);
167 icachesw
= &rcachesw
;
170 icachesw
= &wcachesw
;
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
174 switch (*icachesw
= smatch (cp
, cache_policy
)) {
176 ambigsw (cp
, cache_policy
);
179 die("%s unknown", cp
);
228 if (!(cp
= *argp
++) || *cp
== '-')
229 die("missing argument to %s", argp
[-2]);
231 die("too many parts (starting with %s), %d max",
237 if (!(cp
= *argp
++) || *cp
== '-')
238 die("missing argument to %s", argp
[-2]);
240 die("too many types (starting with %s), %d max",
246 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
247 die("missing argument to %s", argp
[-2]);
248 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 die("missing argument to %s", argp
[-2]);
255 formsw
= mh_xstrdup(etcpath(cp
));
259 * Switches for moreproc/mhlproc
262 if (!(progsw
= *argp
++) || *progsw
== '-')
263 die("missing argument to %s", argp
[-2]);
271 if (!(cp
= *argp
++) || *cp
== '-')
272 die("missing argument to %s", argp
[-2]);
276 * Switches for mhbuild
302 if (*cp
== '+' || *cp
== '@') {
304 die("only one folder at a time!");
305 folder
= pluspath (cp
);
307 app_msgarg(&msgs
, cp
);
310 /* null terminate the list of acceptable parts/types */
315 * Check if we've specified an additional profile
317 if ((cp
= getenv ("MHN"))) {
318 if ((fp
= fopen (cp
, "r"))) {
319 readconfig(NULL
, fp
, cp
, 0);
322 admonish ("", "unable to read $MHN profile (%s)", cp
);
327 * Read the standard profile setup
329 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
330 readconfig(NULL
, fp
, cp
, 0);
334 /* Check for public cache location */
335 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
338 /* Check for private cache location */
339 if (!(cache_private
= context_find (nmhprivcache
)))
340 cache_private
= ".cache";
341 cache_private
= mh_xstrdup(m_maildir(cache_private
));
344 * Cache the current directory before we do any chdirs()'s.
346 cwd
= mh_xstrdup(pwd());
348 if (!context_find ("path"))
349 free (path ("./", TFOLDER
));
352 * Process a mhn composition file (mhn -build)
358 if (showsw
|| storesw
|| cachesw
)
359 die("cannot use -build with -show, -store, -cache");
361 die("need to specify a %s composition file", invo_name
);
363 die("only one %s composition file at a time", invo_name
);
366 vec
[vecp
++] = "mhbuild";
369 vec
[vecp
++] = "-rfc934mode";
370 else if (rfc934sw
== -1)
371 vec
[vecp
++] = "-norfc934mode";
373 vec
[vecp
++] = msgs
.msgs
[0];
376 execvp ("mhbuild", vec
);
377 fprintf (stderr
, "unable to exec ");
382 * Process a mhn composition file (old MH style)
384 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
385 && !showsw
&& !storesw
&& !ntype
&& !file
386 && (cp
= getenv ("mhdraft"))
387 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
393 vec
[vecp
++] = "mhbuild";
396 vec
[vecp
++] = "-rfc934mode";
397 else if (rfc934sw
== -1)
398 vec
[vecp
++] = "-norfc934mode";
403 execvp ("mhbuild", vec
);
404 fprintf (stderr
, "unable to exec ");
408 if (file
&& msgs
.size
)
409 die("cannot specify msg and file at same time!");
412 * check if message is coming from file
415 cts
= mh_xcalloc(2, sizeof *cts
);
418 if ((ct
= parse_mime (file
)))
422 * message(s) are coming from a folder
425 app_msgarg(&msgs
, "cur");
427 folder
= getfolder (1);
428 maildir
= m_maildir (folder
);
430 if (chdir (maildir
) == NOTOK
)
431 adios (maildir
, "unable to change directory to");
433 /* read folder and create message structure */
434 if (!(mp
= folder_read (folder
, 1)))
435 die("unable to read folder %s", folder
);
437 /* check for empty folder */
439 die("no messages in %s", folder
);
441 /* parse all the message ranges/sequences and set SELECTED */
442 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
443 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
445 seq_setprev (mp
); /* set the previous-sequence */
447 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
450 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
451 if (is_selected(mp
, msgnum
)) {
454 msgnam
= m_name (msgnum
);
455 if ((ct
= parse_mime (msgnam
)))
465 * You can't give more than one of these flags
468 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
469 die("can only use one of -show, -list, -store, -cache at same time");
471 /* If no action is specified, assume -show */
472 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
476 SIGNAL (SIGQUIT
, quitser
);
477 SIGNAL (SIGPIPE
, pipeser
);
480 * Get the associated umask for the relevant contents.
482 for (ctp
= cts
; *ctp
; ctp
++) {
486 if (type_ok (ct
, 1) && !ct
->c_umask
) {
487 if (stat (ct
->c_file
, &st
) != NOTOK
)
488 ct
->c_umask
= ~(st
.st_mode
& 0777);
490 ct
->c_umask
= ~m_gmprot();
495 * List the message content
498 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
501 * Store the message content
504 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
505 store_all_messages (info
);
506 mhstoreinfo_free (info
);
509 /* If reading from a folder, do some updating */
511 context_replace (pfolder
, folder
);/* update current folder */
512 seq_setcur (mp
, mp
->hghsel
); /* update current message */
513 seq_save (mp
); /* synchronize sequences */
514 context_save (); /* save the context file */
518 * Cache the message content
521 cache_all_messages (cts
);
524 * Show the message content
527 show_all_messages (cts
, 0, 0, 0);
529 /* Now free all the structures for the content */
530 for (ctp
= cts
; *ctp
; ctp
++)
546 fprintf (stderr
, "\n");