]>
diplodocus.org Git - nmh/blob - uip/mhn.c
459c8268595a92d7b9da35b0bfd6c460b15a9422
2 * mhn.c -- display, list, cache, or store the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 #define MHN_SWITCHES \
21 X("auto", 0, AUTOSW) \
22 X("noauto", 0, NAUTOSW) \
23 X("cache", 0, CACHESW) \
24 X("nocache", 0, NCACHESW) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("headers", 0, HEADSW) \
28 X("noheaders", 0, NHEADSW) \
29 X("list", 0, LISTSW) \
30 X("nolist", 0, NLISTSW) \
31 X("realsize", 0, SIZESW) \
32 X("norealsize", 0, NSIZESW) \
33 X("show", 0, SHOWSW) \
34 X("noshow", 0, NSHOWSW) \
35 X("store", 0, STORESW) \
36 X("nostore", 0, NSTORESW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("file file", 0, FILESW) \
40 X("form formfile", 0, FORMSW) \
41 X("part number", 0, PARTSW) \
42 X("type content", 0, TYPESW) \
43 X("rcache policy", 0, RCACHESW) \
44 X("wcache policy", 0, WCACHESW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
50 X("debug", -5, DEBUGSW) \
52 * switches for moreproc/mhlproc \
54 X("moreproc program", -4, PROGSW) \
55 X("nomoreproc", -3, NPROGSW) \
56 X("length lines", -4, LENSW) \
57 X("width columns", -4, WIDTHSW) \
59 * switches for mhbuild \
61 X("build", -5, BUILDSW) \
62 X("nobuild", -7, NBUILDSW) \
63 X("rfc934mode", 0, RFC934SW) \
64 X("norfc934mode", 0, NRFC934SW) \
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(MHN
);
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(MHN
, switches
);
78 extern char *cache_public
;
79 extern char *cache_private
;
84 extern int nomore
; /* flags for moreproc/header display */
90 extern char *parts
[NPARTS
+ 1];
91 extern char *types
[NTYPES
+ 1];
98 * variables for mhbuild (mhn -build)
100 static int buildsw
= 0;
101 static int rfc934sw
= 0;
104 * what action to take?
106 static int cachesw
= 0;
107 static int listsw
= 0;
108 static int showsw
= 0;
109 static int storesw
= 0;
111 #define quitser pipeser
114 CT
parse_mime (char *);
118 int type_ok (CT
, int);
119 void flush_errors (void);
122 void cache_all_messages (CT
*);
126 void freects_done (int) NORETURN
;
131 static void pipeser (int);
135 main (int argc
, char **argv
)
137 int sizesw
= 1, headsw
= 1, autosw
= 0;
138 int msgnum
, *icachesw
;
139 char *cp
, *file
= NULL
, *folder
= NULL
;
140 char *maildir
, buf
[100], **argp
;
143 struct msgs_array msgs
= { 0, 0, NULL
};
144 struct msgs
*mp
= NULL
;
149 if (nmh_init(argv
[0], 1)) { return 1; }
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
159 while ((cp
= *argp
++)) {
161 switch (smatch (++cp
, switches
)) {
163 ambigsw (cp
, switches
);
166 adios (NULL
, "-%s unknown", cp
);
169 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
171 print_help (buf
, switches
, 1);
174 print_version(invo_name
);
192 icachesw
= &rcachesw
;
195 icachesw
= &wcachesw
;
197 if (!(cp
= *argp
++) || *cp
== '-')
198 adios (NULL
, "missing argument to %s", argp
[-2]);
199 switch (*icachesw
= smatch (cp
, caches
)) {
201 ambigsw (cp
, caches
);
204 adios (NULL
, "%s unknown", cp
);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
256 adios (NULL
, "too many parts (starting with %s), %d max",
262 if (!(cp
= *argp
++) || *cp
== '-')
263 adios (NULL
, "missing argument to %s", argp
[-2]);
265 adios (NULL
, "too many types (starting with %s), %d max",
271 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
272 adios (NULL
, "missing argument to %s", argp
[-2]);
273 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 adios (NULL
, "missing argument to %s", argp
[-2]);
280 formsw
= getcpy (etcpath (cp
));
284 * Switches for moreproc/mhlproc
287 if (!(progsw
= *argp
++) || *progsw
== '-')
288 adios (NULL
, "missing argument to %s", argp
[-2]);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 adios (NULL
, "missing argument to %s", argp
[-2]);
301 * Switches for mhbuild
327 if (*cp
== '+' || *cp
== '@') {
329 adios (NULL
, "only one folder at a time!");
331 folder
= pluspath (cp
);
333 app_msgarg(&msgs
, cp
);
336 /* null terminate the list of acceptable parts/types */
341 * Check if we've specified an additional profile
343 if ((cp
= getenv ("MHN"))) {
344 if ((fp
= fopen (cp
, "r"))) {
345 readconfig ((struct node
**) 0, fp
, cp
, 0);
348 admonish ("", "unable to read $MHN profile (%s)", cp
);
353 * Read the standard profile setup
355 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
356 readconfig ((struct node
**) 0, fp
, cp
, 0);
360 /* Check for public cache location */
361 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
364 /* Check for private cache location */
365 if (!(cache_private
= context_find (nmhprivcache
)))
366 cache_private
= ".cache";
367 cache_private
= getcpy (m_maildir (cache_private
));
370 * Cache the current directory before we do any chdirs()'s.
372 cwd
= mh_xstrdup(pwd());
374 if (!context_find ("path"))
375 free (path ("./", TFOLDER
));
378 * Process a mhn composition file (mhn -build)
384 if (showsw
|| storesw
|| cachesw
)
385 adios (NULL
, "cannot use -build with -show, -store, -cache");
387 adios (NULL
, "need to specify a %s composition file", invo_name
);
389 adios (NULL
, "only one %s composition file at a time", invo_name
);
392 vec
[vecp
++] = "mhbuild";
395 vec
[vecp
++] = "-rfc934mode";
396 else if (rfc934sw
== -1)
397 vec
[vecp
++] = "-norfc934mode";
399 vec
[vecp
++] = msgs
.msgs
[0];
402 execvp ("mhbuild", vec
);
403 fprintf (stderr
, "unable to exec ");
408 * Process a mhn composition file (old MH style)
410 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
411 && !showsw
&& !storesw
&& !ntype
&& !file
412 && (cp
= getenv ("mhdraft"))
413 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
419 vec
[vecp
++] = "mhbuild";
422 vec
[vecp
++] = "-rfc934mode";
423 else if (rfc934sw
== -1)
424 vec
[vecp
++] = "-norfc934mode";
429 execvp ("mhbuild", vec
);
430 fprintf (stderr
, "unable to exec ");
434 if (file
&& msgs
.size
)
435 adios (NULL
, "cannot specify msg and file at same time!");
438 * check if message is coming from file
441 cts
= mh_xcalloc(2, sizeof *cts
);
444 if ((ct
= parse_mime (file
)))
448 * message(s) are coming from a folder
451 app_msgarg(&msgs
, "cur");
453 folder
= getfolder (1);
454 maildir
= m_maildir (folder
);
456 if (chdir (maildir
) == NOTOK
)
457 adios (maildir
, "unable to change directory to");
459 /* read folder and create message structure */
460 if (!(mp
= folder_read (folder
, 1)))
461 adios (NULL
, "unable to read folder %s", folder
);
463 /* check for empty folder */
465 adios (NULL
, "no messages in %s", folder
);
467 /* parse all the message ranges/sequences and set SELECTED */
468 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
469 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
471 seq_setprev (mp
); /* set the previous-sequence */
473 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
476 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
477 if (is_selected(mp
, msgnum
)) {
480 msgnam
= m_name (msgnum
);
481 if ((ct
= parse_mime (msgnam
)))
491 * You can't give more than one of these flags
494 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
495 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
497 /* If no action is specified, assume -show */
498 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
502 SIGNAL (SIGQUIT
, quitser
);
503 SIGNAL (SIGPIPE
, pipeser
);
506 * Get the associated umask for the relevant contents.
508 for (ctp
= cts
; *ctp
; ctp
++) {
512 if (type_ok (ct
, 1) && !ct
->c_umask
) {
513 if (stat (ct
->c_file
, &st
) != NOTOK
)
514 ct
->c_umask
= ~(st
.st_mode
& 0777);
516 ct
->c_umask
= ~m_gmprot();
521 * List the message content
524 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
527 * Store the message content
530 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
531 store_all_messages (info
);
532 mhstoreinfo_free (info
);
535 /* If reading from a folder, do some updating */
537 context_replace (pfolder
, folder
);/* update current folder */
538 seq_setcur (mp
, mp
->hghsel
); /* update current message */
539 seq_save (mp
); /* synchronize sequences */
540 context_save (); /* save the context file */
544 * Cache the message content
547 cache_all_messages (cts
);
550 * Show the message content
553 show_all_messages (cts
, 0, 0, 0);
555 /* Now free all the structures for the content */
556 for (ctp
= cts
; *ctp
; ctp
++)
572 fprintf (stderr
, "\n");