]>
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>
19 #include "../sbr/m_maildir.h"
21 #include "mhshowsbr.h"
23 #define MHN_SWITCHES \
24 X("auto", 0, AUTOSW) \
25 X("noauto", 0, NAUTOSW) \
26 X("cache", 0, CACHESW) \
27 X("nocache", 0, NCACHESW) \
28 X("check", 0, CHECKSW) \
29 X("nocheck", 0, NCHECKSW) \
30 X("headers", 0, HEADSW) \
31 X("noheaders", 0, NHEADSW) \
32 X("list", 0, LISTSW) \
33 X("nolist", 0, NLISTSW) \
34 X("realsize", 0, SIZESW) \
35 X("norealsize", 0, NSIZESW) \
36 X("show", 0, SHOWSW) \
37 X("noshow", 0, NSHOWSW) \
38 X("store", 0, STORESW) \
39 X("nostore", 0, NSTORESW) \
40 X("verbose", 0, VERBSW) \
41 X("noverbose", 0, NVERBSW) \
42 X("file file", 0, FILESW) \
43 X("form formfile", 0, FORMSW) \
44 X("part number", 0, PARTSW) \
45 X("type content", 0, TYPESW) \
46 X("rcache policy", 0, RCACHESW) \
47 X("wcache policy", 0, WCACHESW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
53 X("debug", -5, DEBUGSW) \
55 * switches for moreproc/mhlproc \
57 X("moreproc program", -4, PROGSW) \
58 X("nomoreproc", -3, NPROGSW) \
59 X("length lines", -4, LENSW) \
60 X("width columns", -4, WIDTHSW) \
62 * switches for mhbuild \
64 X("build", -5, BUILDSW) \
65 X("nobuild", -7, NBUILDSW) \
66 X("rfc934mode", 0, RFC934SW) \
67 X("norfc934mode", 0, NRFC934SW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MHN
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MHN
, switches
);
81 extern char *parts
[NPARTS
+ 1];
82 extern char *types
[NTYPES
+ 1];
89 * variables for mhbuild (mhn -build)
91 static int buildsw
= 0;
92 static int rfc934sw
= 0;
95 * what action to take?
97 static int cachesw
= 0;
98 static int listsw
= 0;
99 static int showsw
= 0;
100 static int storesw
= 0;
102 #define quitser pipeser
105 CT
parse_mime (char *);
109 int type_ok (CT
, int);
110 void flush_errors (void);
115 static void pipeser (int);
119 main (int argc
, char **argv
)
121 int sizesw
= 1, headsw
= 1, autosw
= 0;
122 int msgnum
, *icachesw
;
123 char *cp
, *file
= NULL
, *folder
= NULL
;
124 char *maildir
, buf
[100], **argp
;
127 struct msgs_array msgs
= { 0, 0, NULL
};
128 struct msgs
*mp
= NULL
;
133 if (nmh_init(argv
[0], 1)) { return 1; }
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 while ((cp
= *argp
++)) {
145 switch (smatch (++cp
, switches
)) {
147 ambigsw (cp
, switches
);
150 adios (NULL
, "-%s unknown", cp
);
153 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
155 print_help (buf
, switches
, 1);
158 print_version(invo_name
);
176 icachesw
= &rcachesw
;
179 icachesw
= &wcachesw
;
181 if (!(cp
= *argp
++) || *cp
== '-')
182 adios (NULL
, "missing argument to %s", argp
[-2]);
183 switch (*icachesw
= smatch (cp
, cache_policy
)) {
185 ambigsw (cp
, cache_policy
);
188 adios (NULL
, "%s unknown", cp
);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 adios (NULL
, "missing argument to %s", argp
[-2]);
240 adios (NULL
, "too many parts (starting with %s), %d max",
246 if (!(cp
= *argp
++) || *cp
== '-')
247 adios (NULL
, "missing argument to %s", argp
[-2]);
249 adios (NULL
, "too many types (starting with %s), %d max",
255 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
256 adios (NULL
, "missing argument to %s", argp
[-2]);
257 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
261 if (!(cp
= *argp
++) || *cp
== '-')
262 adios (NULL
, "missing argument to %s", argp
[-2]);
264 formsw
= getcpy (etcpath (cp
));
268 * Switches for moreproc/mhlproc
271 if (!(progsw
= *argp
++) || *progsw
== '-')
272 adios (NULL
, "missing argument to %s", argp
[-2]);
280 if (!(cp
= *argp
++) || *cp
== '-')
281 adios (NULL
, "missing argument to %s", argp
[-2]);
285 * Switches for mhbuild
311 if (*cp
== '+' || *cp
== '@') {
313 adios (NULL
, "only one folder at a time!");
315 folder
= pluspath (cp
);
317 app_msgarg(&msgs
, cp
);
320 /* null terminate the list of acceptable parts/types */
325 * Check if we've specified an additional profile
327 if ((cp
= getenv ("MHN"))) {
328 if ((fp
= fopen (cp
, "r"))) {
329 readconfig ((struct node
**) 0, fp
, cp
, 0);
332 admonish ("", "unable to read $MHN profile (%s)", cp
);
337 * Read the standard profile setup
339 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
340 readconfig ((struct node
**) 0, fp
, cp
, 0);
344 /* Check for public cache location */
345 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
348 /* Check for private cache location */
349 if (!(cache_private
= context_find (nmhprivcache
)))
350 cache_private
= ".cache";
351 cache_private
= getcpy (m_maildir (cache_private
));
354 * Cache the current directory before we do any chdirs()'s.
356 cwd
= mh_xstrdup(pwd());
358 if (!context_find ("path"))
359 free (path ("./", TFOLDER
));
362 * Process a mhn composition file (mhn -build)
368 if (showsw
|| storesw
|| cachesw
)
369 adios (NULL
, "cannot use -build with -show, -store, -cache");
371 adios (NULL
, "need to specify a %s composition file", invo_name
);
373 adios (NULL
, "only one %s composition file at a time", invo_name
);
376 vec
[vecp
++] = "mhbuild";
379 vec
[vecp
++] = "-rfc934mode";
380 else if (rfc934sw
== -1)
381 vec
[vecp
++] = "-norfc934mode";
383 vec
[vecp
++] = msgs
.msgs
[0];
386 execvp ("mhbuild", vec
);
387 fprintf (stderr
, "unable to exec ");
392 * Process a mhn composition file (old MH style)
394 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
395 && !showsw
&& !storesw
&& !ntype
&& !file
396 && (cp
= getenv ("mhdraft"))
397 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
403 vec
[vecp
++] = "mhbuild";
406 vec
[vecp
++] = "-rfc934mode";
407 else if (rfc934sw
== -1)
408 vec
[vecp
++] = "-norfc934mode";
413 execvp ("mhbuild", vec
);
414 fprintf (stderr
, "unable to exec ");
418 if (file
&& msgs
.size
)
419 adios (NULL
, "cannot specify msg and file at same time!");
422 * check if message is coming from file
425 cts
= mh_xcalloc(2, sizeof *cts
);
428 if ((ct
= parse_mime (file
)))
432 * message(s) are coming from a folder
435 app_msgarg(&msgs
, "cur");
437 folder
= getfolder (1);
438 maildir
= m_maildir (folder
);
440 if (chdir (maildir
) == NOTOK
)
441 adios (maildir
, "unable to change directory to");
443 /* read folder and create message structure */
444 if (!(mp
= folder_read (folder
, 1)))
445 adios (NULL
, "unable to read folder %s", folder
);
447 /* check for empty folder */
449 adios (NULL
, "no messages in %s", folder
);
451 /* parse all the message ranges/sequences and set SELECTED */
452 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
453 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
455 seq_setprev (mp
); /* set the previous-sequence */
457 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
460 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
461 if (is_selected(mp
, msgnum
)) {
464 msgnam
= m_name (msgnum
);
465 if ((ct
= parse_mime (msgnam
)))
475 * You can't give more than one of these flags
478 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
479 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
481 /* If no action is specified, assume -show */
482 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
486 SIGNAL (SIGQUIT
, quitser
);
487 SIGNAL (SIGPIPE
, pipeser
);
490 * Get the associated umask for the relevant contents.
492 for (ctp
= cts
; *ctp
; ctp
++) {
496 if (type_ok (ct
, 1) && !ct
->c_umask
) {
497 if (stat (ct
->c_file
, &st
) != NOTOK
)
498 ct
->c_umask
= ~(st
.st_mode
& 0777);
500 ct
->c_umask
= ~m_gmprot();
505 * List the message content
508 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
511 * Store the message content
514 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
515 store_all_messages (info
);
516 mhstoreinfo_free (info
);
519 /* If reading from a folder, do some updating */
521 context_replace (pfolder
, folder
);/* update current folder */
522 seq_setcur (mp
, mp
->hghsel
); /* update current message */
523 seq_save (mp
); /* synchronize sequences */
524 context_save (); /* save the context file */
528 * Cache the message content
531 cache_all_messages (cts
);
534 * Show the message content
537 show_all_messages (cts
, 0, 0, 0);
539 /* Now free all the structures for the content */
540 for (ctp
= cts
; *ctp
; ctp
++)
556 fprintf (stderr
, "\n");