]>
diplodocus.org Git - nmh/blob - uip/mhn.c
3 * mhn.c -- display, list, cache, or store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.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("pause", 0, PAUSESW) \
35 X("nopause", 0, NPAUSESW) \
36 X("realsize", 0, SIZESW) \
37 X("norealsize", 0, NSIZESW) \
38 X("serialonly", 0, SERIALSW) \
39 X("noserialonly", 0, NSERIALSW) \
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", -10, RFC934SW) \
71 X("norfc934mode", -12, 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
);
83 extern char *tmp
; /* directory to place temp files */
88 extern char *cache_public
;
89 extern char *cache_private
;
96 extern int nomore
; /* flags for moreproc/header display */
101 extern char *cwd
; /* cache current working directory */
106 extern char *parts
[NPARTS
+ 1];
107 extern char *types
[NTYPES
+ 1];
114 * variables for mhbuild (mhn -build)
116 static int buildsw
= 0;
117 static int rfc934sw
= 0;
120 * what action to take?
122 static int cachesw
= 0;
123 static int listsw
= 0;
124 static int showsw
= 0;
125 static int storesw
= 0;
127 #define quitser pipeser
130 CT
parse_mime (char *);
133 int part_ok (CT
, int);
134 int type_ok (CT
, int);
135 void flush_errors (void);
138 void show_all_messages (CT
*);
141 void list_all_messages (CT
*, int, int, int, int);
144 void store_all_messages (CT
*);
147 void cache_all_messages (CT
*);
151 void freects_done (int) NORETURN
;
156 static void pipeser (int);
160 main (int argc
, char **argv
)
162 int sizesw
= 1, headsw
= 1;
163 int msgnum
, *icachesw
;
164 char *cp
, *file
= NULL
, *folder
= NULL
;
165 char *maildir
, buf
[100], **argp
;
167 struct msgs_array msgs
= { 0, 0, NULL
};
168 struct msgs
*mp
= NULL
;
175 setlocale(LC_ALL
, "");
177 invo_name
= r1bindex (argv
[0], '/');
179 /* read user profile/context */
182 arguments
= getarguments (invo_name
, argc
, argv
, 1);
188 while ((cp
= *argp
++)) {
190 switch (smatch (++cp
, switches
)) {
192 ambigsw (cp
, switches
);
195 adios (NULL
, "-%s unknown", cp
);
198 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
200 print_help (buf
, switches
, 1);
203 print_version(invo_name
);
221 icachesw
= &rcachesw
;
224 icachesw
= &wcachesw
;
226 if (!(cp
= *argp
++) || *cp
== '-')
227 adios (NULL
, "missing argument to %s", argp
[-2]);
228 switch (*icachesw
= smatch (cp
, caches
)) {
230 ambigsw (cp
, caches
);
233 adios (NULL
, "%s unknown", cp
);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 adios (NULL
, "missing argument to %s", argp
[-2]);
299 adios (NULL
, "too many parts (starting with %s), %d max",
305 if (!(cp
= *argp
++) || *cp
== '-')
306 adios (NULL
, "missing argument to %s", argp
[-2]);
308 adios (NULL
, "too many types (starting with %s), %d max",
314 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
315 adios (NULL
, "missing argument to %s", argp
[-2]);
316 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
320 if (!(cp
= *argp
++) || *cp
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
324 formsw
= getcpy (etcpath (cp
));
328 * Switches for moreproc/mhlproc
331 if (!(progsw
= *argp
++) || *progsw
== '-')
332 adios (NULL
, "missing argument to %s", argp
[-2]);
340 if (!(cp
= *argp
++) || *cp
== '-')
341 adios (NULL
, "missing argument to %s", argp
[-2]);
345 * Switches for mhbuild
371 if (*cp
== '+' || *cp
== '@') {
373 adios (NULL
, "only one folder at a time!");
375 folder
= pluspath (cp
);
377 app_msgarg(&msgs
, cp
);
380 /* null terminate the list of acceptable parts/types */
385 * Check if we've specified an additional profile
387 if ((cp
= getenv ("MHN"))) {
388 if ((fp
= fopen (cp
, "r"))) {
389 readconfig ((struct node
**) 0, fp
, cp
, 0);
392 admonish ("", "unable to read $MHN profile (%s)", cp
);
397 * Read the standard profile setup
399 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
400 readconfig ((struct node
**) 0, fp
, cp
, 0);
404 /* Check for public cache location */
405 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
408 /* Check for private cache location */
409 if (!(cache_private
= context_find (nmhprivcache
)))
410 cache_private
= ".cache";
411 cache_private
= getcpy (m_maildir (cache_private
));
414 * Cache the current directory before we do any chdirs()'s.
416 cwd
= getcpy (pwd());
419 * Check for storage directory. If specified,
420 * then store temporary files there. Else we
421 * store them in standard nmh directory.
423 if ((cp
= context_find (nmhstorage
)) && *cp
)
424 tmp
= concat (cp
, "/", invo_name
, NULL
);
426 tmp
= add (m_maildir (invo_name
), NULL
);
428 if (!context_find ("path"))
429 free (path ("./", TFOLDER
));
432 * Process a mhn composition file (mhn -build)
438 if (showsw
|| storesw
|| cachesw
)
439 adios (NULL
, "cannot use -build with -show, -store, -cache");
441 adios (NULL
, "need to specify a %s composition file", invo_name
);
443 adios (NULL
, "only one %s composition file at a time", invo_name
);
446 vec
[vecp
++] = "mhbuild";
449 vec
[vecp
++] = "-rfc934mode";
450 else if (rfc934sw
== -1)
451 vec
[vecp
++] = "-norfc934mode";
453 vec
[vecp
++] = msgs
.msgs
[0];
456 execvp ("mhbuild", vec
);
457 fprintf (stderr
, "unable to exec ");
462 * Process a mhn composition file (old MH style)
464 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
465 && !showsw
&& !storesw
&& !ntype
&& !file
466 && (cp
= getenv ("mhdraft"))
467 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
473 vec
[vecp
++] = "mhbuild";
476 vec
[vecp
++] = "-rfc934mode";
477 else if (rfc934sw
== -1)
478 vec
[vecp
++] = "-norfc934mode";
483 execvp ("mhbuild", vec
);
484 fprintf (stderr
, "unable to exec ");
488 if (file
&& msgs
.size
)
489 adios (NULL
, "cannot specify msg and file at same time!");
492 * check if message is coming from file
495 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
496 adios (NULL
, "out of memory");
499 if ((ct
= parse_mime (file
)))
503 * message(s) are coming from a folder
506 app_msgarg(&msgs
, "cur");
508 folder
= getfolder (1);
509 maildir
= m_maildir (folder
);
511 if (chdir (maildir
) == NOTOK
)
512 adios (maildir
, "unable to change directory to");
514 /* read folder and create message structure */
515 if (!(mp
= folder_read (folder
)))
516 adios (NULL
, "unable to read folder %s", folder
);
518 /* check for empty folder */
520 adios (NULL
, "no messages in %s", folder
);
522 /* parse all the message ranges/sequences and set SELECTED */
523 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
524 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
526 seq_setprev (mp
); /* set the previous-sequence */
528 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
529 adios (NULL
, "out of memory");
532 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
533 if (is_selected(mp
, msgnum
)) {
536 msgnam
= m_name (msgnum
);
537 if ((ct
= parse_mime (msgnam
)))
547 * You can't give more than one of these flags
550 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
551 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
553 /* If no action is specified, assume -show */
554 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
558 SIGNAL (SIGQUIT
, quitser
);
559 SIGNAL (SIGPIPE
, pipeser
);
562 * Get the associated umask for the relevant contents.
564 for (ctp
= cts
; *ctp
; ctp
++) {
568 if (type_ok (ct
, 1) && !ct
->c_umask
) {
569 if (stat (ct
->c_file
, &st
) != NOTOK
)
570 ct
->c_umask
= ~(st
.st_mode
& 0777);
572 ct
->c_umask
= ~m_gmprot();
577 * List the message content
580 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
583 * Store the message content
586 store_all_messages (cts
);
589 * Cache the message content
592 cache_all_messages (cts
);
595 * Show the message content
598 show_all_messages (cts
);
600 /* Now free all the structures for the content */
601 for (ctp
= cts
; *ctp
; ctp
++)
607 /* If reading from a folder, do some updating */
609 context_replace (pfolder
, folder
);/* update current folder */
610 seq_setcur (mp
, mp
->hghsel
); /* update current message */
611 seq_save (mp
); /* synchronize sequences */
612 context_save (); /* save the context file */
625 fprintf (stderr
, "\n");