]>
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)
86 static int buildsw
= 0;
87 static int rfc934sw
= 0;
90 * what action to take?
92 static int cachesw
= 0;
93 static int listsw
= 0;
94 static int showsw
= 0;
95 static int storesw
= 0;
97 #define quitser pipeser
102 static void pipeser (int);
106 main (int argc
, char **argv
)
108 int sizesw
= 1, headsw
= 1, autosw
= 0;
109 int msgnum
, *icachesw
;
110 char *cp
, *file
= NULL
, *folder
= NULL
;
111 char *maildir
, buf
[100], **argp
;
114 struct msgs_array msgs
= { 0, 0, NULL
};
115 struct msgs
*mp
= NULL
;
120 if (nmh_init(argv
[0], 1)) { return 1; }
122 set_done(freects_done
);
124 arguments
= getarguments (invo_name
, argc
, argv
, 1);
130 while ((cp
= *argp
++)) {
132 switch (smatch (++cp
, switches
)) {
134 ambigsw (cp
, switches
);
137 adios (NULL
, "-%s unknown", cp
);
140 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
142 print_help (buf
, switches
, 1);
145 print_version(invo_name
);
163 icachesw
= &rcachesw
;
166 icachesw
= &wcachesw
;
168 if (!(cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
170 switch (*icachesw
= smatch (cp
, cache_policy
)) {
172 ambigsw (cp
, cache_policy
);
175 adios (NULL
, "%s unknown", cp
);
224 if (!(cp
= *argp
++) || *cp
== '-')
225 adios (NULL
, "missing argument to %s", argp
[-2]);
227 adios (NULL
, "too many parts (starting with %s), %d max",
233 if (!(cp
= *argp
++) || *cp
== '-')
234 adios (NULL
, "missing argument to %s", argp
[-2]);
236 adios (NULL
, "too many types (starting with %s), %d max",
242 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
243 adios (NULL
, "missing argument to %s", argp
[-2]);
244 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
251 formsw
= getcpy (etcpath (cp
));
255 * Switches for moreproc/mhlproc
258 if (!(progsw
= *argp
++) || *progsw
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
267 if (!(cp
= *argp
++) || *cp
== '-')
268 adios (NULL
, "missing argument to %s", argp
[-2]);
272 * Switches for mhbuild
298 if (*cp
== '+' || *cp
== '@') {
300 adios (NULL
, "only one folder at a time!");
301 folder
= pluspath (cp
);
303 app_msgarg(&msgs
, cp
);
306 /* null terminate the list of acceptable parts/types */
311 * Check if we've specified an additional profile
313 if ((cp
= getenv ("MHN"))) {
314 if ((fp
= fopen (cp
, "r"))) {
315 readconfig(NULL
, fp
, cp
, 0);
318 admonish ("", "unable to read $MHN profile (%s)", cp
);
323 * Read the standard profile setup
325 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
326 readconfig(NULL
, fp
, cp
, 0);
330 /* Check for public cache location */
331 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
334 /* Check for private cache location */
335 if (!(cache_private
= context_find (nmhprivcache
)))
336 cache_private
= ".cache";
337 cache_private
= getcpy (m_maildir (cache_private
));
340 * Cache the current directory before we do any chdirs()'s.
342 cwd
= mh_xstrdup(pwd());
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER
));
348 * Process a mhn composition file (mhn -build)
354 if (showsw
|| storesw
|| cachesw
)
355 adios (NULL
, "cannot use -build with -show, -store, -cache");
357 adios (NULL
, "need to specify a %s composition file", invo_name
);
359 adios (NULL
, "only one %s composition file at a time", invo_name
);
362 vec
[vecp
++] = "mhbuild";
365 vec
[vecp
++] = "-rfc934mode";
366 else if (rfc934sw
== -1)
367 vec
[vecp
++] = "-norfc934mode";
369 vec
[vecp
++] = msgs
.msgs
[0];
372 execvp ("mhbuild", vec
);
373 fprintf (stderr
, "unable to exec ");
378 * Process a mhn composition file (old MH style)
380 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
381 && !showsw
&& !storesw
&& !ntype
&& !file
382 && (cp
= getenv ("mhdraft"))
383 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
389 vec
[vecp
++] = "mhbuild";
392 vec
[vecp
++] = "-rfc934mode";
393 else if (rfc934sw
== -1)
394 vec
[vecp
++] = "-norfc934mode";
399 execvp ("mhbuild", vec
);
400 fprintf (stderr
, "unable to exec ");
404 if (file
&& msgs
.size
)
405 adios (NULL
, "cannot specify msg and file at same time!");
408 * check if message is coming from file
411 cts
= mh_xcalloc(2, sizeof *cts
);
414 if ((ct
= parse_mime (file
)))
418 * message(s) are coming from a folder
421 app_msgarg(&msgs
, "cur");
423 folder
= getfolder (1);
424 maildir
= m_maildir (folder
);
426 if (chdir (maildir
) == NOTOK
)
427 adios (maildir
, "unable to change directory to");
429 /* read folder and create message structure */
430 if (!(mp
= folder_read (folder
, 1)))
431 adios (NULL
, "unable to read folder %s", folder
);
433 /* check for empty folder */
435 adios (NULL
, "no messages in %s", folder
);
437 /* parse all the message ranges/sequences and set SELECTED */
438 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
439 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
441 seq_setprev (mp
); /* set the previous-sequence */
443 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
446 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
447 if (is_selected(mp
, msgnum
)) {
450 msgnam
= m_name (msgnum
);
451 if ((ct
= parse_mime (msgnam
)))
461 * You can't give more than one of these flags
464 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
465 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
467 /* If no action is specified, assume -show */
468 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
472 SIGNAL (SIGQUIT
, quitser
);
473 SIGNAL (SIGPIPE
, pipeser
);
476 * Get the associated umask for the relevant contents.
478 for (ctp
= cts
; *ctp
; ctp
++) {
482 if (type_ok (ct
, 1) && !ct
->c_umask
) {
483 if (stat (ct
->c_file
, &st
) != NOTOK
)
484 ct
->c_umask
= ~(st
.st_mode
& 0777);
486 ct
->c_umask
= ~m_gmprot();
491 * List the message content
494 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
497 * Store the message content
500 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
501 store_all_messages (info
);
502 mhstoreinfo_free (info
);
505 /* If reading from a folder, do some updating */
507 context_replace (pfolder
, folder
);/* update current folder */
508 seq_setcur (mp
, mp
->hghsel
); /* update current message */
509 seq_save (mp
); /* synchronize sequences */
510 context_save (); /* save the context file */
514 * Cache the message content
517 cache_all_messages (cts
);
520 * Show the message content
523 show_all_messages (cts
, 0, 0, 0);
525 /* Now free all the structures for the content */
526 for (ctp
= cts
; *ctp
; ctp
++)
542 fprintf (stderr
, "\n");