]>
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>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
19 #define MHN_SWITCHES \
20 X("auto", 0, AUTOSW) \
21 X("noauto", 0, NAUTOSW) \
22 X("cache", 0, CACHESW) \
23 X("nocache", 0, NCACHESW) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("list", 0, LISTSW) \
29 X("nolist", 0, NLISTSW) \
30 X("realsize", 0, SIZESW) \
31 X("norealsize", 0, NSIZESW) \
32 X("show", 0, SHOWSW) \
33 X("noshow", 0, NSHOWSW) \
34 X("store", 0, STORESW) \
35 X("nostore", 0, NSTORESW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("file file", 0, FILESW) \
39 X("form formfile", 0, FORMSW) \
40 X("part number", 0, PARTSW) \
41 X("type content", 0, TYPESW) \
42 X("rcache policy", 0, RCACHESW) \
43 X("wcache policy", 0, WCACHESW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
49 X("debug", -5, DEBUGSW) \
51 * switches for moreproc/mhlproc \
53 X("moreproc program", -4, PROGSW) \
54 X("nomoreproc", -3, NPROGSW) \
55 X("length lines", -4, LENSW) \
56 X("width columns", -4, WIDTHSW) \
58 * switches for mhbuild \
60 X("build", -5, BUILDSW) \
61 X("nobuild", -7, NBUILDSW) \
62 X("rfc934mode", 0, RFC934SW) \
63 X("norfc934mode", 0, NRFC934SW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(MHN
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(MHN
, switches
);
77 extern char *cache_public
;
78 extern char *cache_private
;
83 extern int nomore
; /* flags for moreproc/header display */
89 extern char *parts
[NPARTS
+ 1];
90 extern char *types
[NTYPES
+ 1];
97 * variables for mhbuild (mhn -build)
99 static int buildsw
= 0;
100 static int rfc934sw
= 0;
103 * what action to take?
105 static int cachesw
= 0;
106 static int listsw
= 0;
107 static int showsw
= 0;
108 static int storesw
= 0;
110 #define quitser pipeser
113 CT
parse_mime (char *);
117 int type_ok (CT
, int);
118 void flush_errors (void);
121 void cache_all_messages (CT
*);
125 void freects_done (int) NORETURN
;
130 static void pipeser (int);
134 main (int argc
, char **argv
)
136 int sizesw
= 1, headsw
= 1, autosw
= 0;
137 int msgnum
, *icachesw
;
138 char *cp
, *file
= NULL
, *folder
= NULL
;
139 char *maildir
, buf
[100], **argp
;
142 struct msgs_array msgs
= { 0, 0, NULL
};
143 struct msgs
*mp
= NULL
;
148 if (nmh_init(argv
[0], 1)) { return 1; }
152 arguments
= getarguments (invo_name
, argc
, argv
, 1);
158 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, switches
)) {
162 ambigsw (cp
, switches
);
165 adios (NULL
, "-%s unknown", cp
);
168 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
170 print_help (buf
, switches
, 1);
173 print_version(invo_name
);
191 icachesw
= &rcachesw
;
194 icachesw
= &wcachesw
;
196 if (!(cp
= *argp
++) || *cp
== '-')
197 adios (NULL
, "missing argument to %s", argp
[-2]);
198 switch (*icachesw
= smatch (cp
, caches
)) {
200 ambigsw (cp
, caches
);
203 adios (NULL
, "%s unknown", cp
);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 adios (NULL
, "missing argument to %s", argp
[-2]);
255 adios (NULL
, "too many parts (starting with %s), %d max",
261 if (!(cp
= *argp
++) || *cp
== '-')
262 adios (NULL
, "missing argument to %s", argp
[-2]);
264 adios (NULL
, "too many types (starting with %s), %d max",
270 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
271 adios (NULL
, "missing argument to %s", argp
[-2]);
272 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
276 if (!(cp
= *argp
++) || *cp
== '-')
277 adios (NULL
, "missing argument to %s", argp
[-2]);
279 formsw
= getcpy (etcpath (cp
));
283 * Switches for moreproc/mhlproc
286 if (!(progsw
= *argp
++) || *progsw
== '-')
287 adios (NULL
, "missing argument to %s", argp
[-2]);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 adios (NULL
, "missing argument to %s", argp
[-2]);
300 * Switches for mhbuild
326 if (*cp
== '+' || *cp
== '@') {
328 adios (NULL
, "only one folder at a time!");
330 folder
= pluspath (cp
);
332 app_msgarg(&msgs
, cp
);
335 /* null terminate the list of acceptable parts/types */
340 * Check if we've specified an additional profile
342 if ((cp
= getenv ("MHN"))) {
343 if ((fp
= fopen (cp
, "r"))) {
344 readconfig ((struct node
**) 0, fp
, cp
, 0);
347 admonish ("", "unable to read $MHN profile (%s)", cp
);
352 * Read the standard profile setup
354 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
355 readconfig ((struct node
**) 0, fp
, cp
, 0);
359 /* Check for public cache location */
360 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
363 /* Check for private cache location */
364 if (!(cache_private
= context_find (nmhprivcache
)))
365 cache_private
= ".cache";
366 cache_private
= getcpy (m_maildir (cache_private
));
369 * Cache the current directory before we do any chdirs()'s.
371 cwd
= mh_xstrdup(pwd());
373 if (!context_find ("path"))
374 free (path ("./", TFOLDER
));
377 * Process a mhn composition file (mhn -build)
383 if (showsw
|| storesw
|| cachesw
)
384 adios (NULL
, "cannot use -build with -show, -store, -cache");
386 adios (NULL
, "need to specify a %s composition file", invo_name
);
388 adios (NULL
, "only one %s composition file at a time", invo_name
);
391 vec
[vecp
++] = "mhbuild";
394 vec
[vecp
++] = "-rfc934mode";
395 else if (rfc934sw
== -1)
396 vec
[vecp
++] = "-norfc934mode";
398 vec
[vecp
++] = msgs
.msgs
[0];
401 execvp ("mhbuild", vec
);
402 fprintf (stderr
, "unable to exec ");
407 * Process a mhn composition file (old MH style)
409 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
410 && !showsw
&& !storesw
&& !ntype
&& !file
411 && (cp
= getenv ("mhdraft"))
412 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
418 vec
[vecp
++] = "mhbuild";
421 vec
[vecp
++] = "-rfc934mode";
422 else if (rfc934sw
== -1)
423 vec
[vecp
++] = "-norfc934mode";
428 execvp ("mhbuild", vec
);
429 fprintf (stderr
, "unable to exec ");
433 if (file
&& msgs
.size
)
434 adios (NULL
, "cannot specify msg and file at same time!");
437 * check if message is coming from file
440 cts
= mh_xcalloc(2, sizeof *cts
);
443 if ((ct
= parse_mime (file
)))
447 * message(s) are coming from a folder
450 app_msgarg(&msgs
, "cur");
452 folder
= getfolder (1);
453 maildir
= m_maildir (folder
);
455 if (chdir (maildir
) == NOTOK
)
456 adios (maildir
, "unable to change directory to");
458 /* read folder and create message structure */
459 if (!(mp
= folder_read (folder
, 1)))
460 adios (NULL
, "unable to read folder %s", folder
);
462 /* check for empty folder */
464 adios (NULL
, "no messages in %s", folder
);
466 /* parse all the message ranges/sequences and set SELECTED */
467 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
468 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
470 seq_setprev (mp
); /* set the previous-sequence */
472 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
475 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
476 if (is_selected(mp
, msgnum
)) {
479 msgnam
= m_name (msgnum
);
480 if ((ct
= parse_mime (msgnam
)))
490 * You can't give more than one of these flags
493 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
494 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
496 /* If no action is specified, assume -show */
497 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
501 SIGNAL (SIGQUIT
, quitser
);
502 SIGNAL (SIGPIPE
, pipeser
);
505 * Get the associated umask for the relevant contents.
507 for (ctp
= cts
; *ctp
; ctp
++) {
511 if (type_ok (ct
, 1) && !ct
->c_umask
) {
512 if (stat (ct
->c_file
, &st
) != NOTOK
)
513 ct
->c_umask
= ~(st
.st_mode
& 0777);
515 ct
->c_umask
= ~m_gmprot();
520 * List the message content
523 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
526 * Store the message content
529 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
530 store_all_messages (info
);
531 mhstoreinfo_free (info
);
534 /* If reading from a folder, do some updating */
536 context_replace (pfolder
, folder
);/* update current folder */
537 seq_setcur (mp
, mp
->hghsel
); /* update current message */
538 seq_save (mp
); /* synchronize sequences */
539 context_save (); /* save the context file */
543 * Cache the message content
546 cache_all_messages (cts
);
549 * Show the message content
552 show_all_messages (cts
, 0, 0, 0);
554 /* Now free all the structures for the content */
555 for (ctp
= cts
; *ctp
; ctp
++)
571 fprintf (stderr
, "\n");