]>
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
*);
150 void free_content (CT
);
152 void freects_done (int) NORETURN
;
157 static void pipeser (int);
161 main (int argc
, char **argv
)
163 int sizesw
= 1, headsw
= 1;
164 int msgnum
, *icachesw
;
165 char *cp
, *file
= NULL
, *folder
= NULL
;
166 char *maildir
, buf
[100], **argp
;
168 struct msgs_array msgs
= { 0, 0, NULL
};
169 struct msgs
*mp
= NULL
;
176 setlocale(LC_ALL
, "");
178 invo_name
= r1bindex (argv
[0], '/');
180 /* read user profile/context */
183 arguments
= getarguments (invo_name
, argc
, argv
, 1);
189 while ((cp
= *argp
++)) {
191 switch (smatch (++cp
, switches
)) {
193 ambigsw (cp
, switches
);
196 adios (NULL
, "-%s unknown", cp
);
199 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
201 print_help (buf
, switches
, 1);
204 print_version(invo_name
);
222 icachesw
= &rcachesw
;
225 icachesw
= &wcachesw
;
227 if (!(cp
= *argp
++) || *cp
== '-')
228 adios (NULL
, "missing argument to %s", argp
[-2]);
229 switch (*icachesw
= smatch (cp
, caches
)) {
231 ambigsw (cp
, caches
);
234 adios (NULL
, "%s unknown", cp
);
297 if (!(cp
= *argp
++) || *cp
== '-')
298 adios (NULL
, "missing argument to %s", argp
[-2]);
300 adios (NULL
, "too many parts (starting with %s), %d max",
306 if (!(cp
= *argp
++) || *cp
== '-')
307 adios (NULL
, "missing argument to %s", argp
[-2]);
309 adios (NULL
, "too many types (starting with %s), %d max",
315 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
316 adios (NULL
, "missing argument to %s", argp
[-2]);
317 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
321 if (!(cp
= *argp
++) || *cp
== '-')
322 adios (NULL
, "missing argument to %s", argp
[-2]);
325 formsw
= getcpy (etcpath (cp
));
329 * Switches for moreproc/mhlproc
332 if (!(progsw
= *argp
++) || *progsw
== '-')
333 adios (NULL
, "missing argument to %s", argp
[-2]);
341 if (!(cp
= *argp
++) || *cp
== '-')
342 adios (NULL
, "missing argument to %s", argp
[-2]);
346 * Switches for mhbuild
372 if (*cp
== '+' || *cp
== '@') {
374 adios (NULL
, "only one folder at a time!");
376 folder
= pluspath (cp
);
378 app_msgarg(&msgs
, cp
);
381 /* null terminate the list of acceptable parts/types */
386 * Check if we've specified an additional profile
388 if ((cp
= getenv ("MHN"))) {
389 if ((fp
= fopen (cp
, "r"))) {
390 readconfig ((struct node
**) 0, fp
, cp
, 0);
393 admonish ("", "unable to read $MHN profile (%s)", cp
);
398 * Read the standard profile setup
400 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
401 readconfig ((struct node
**) 0, fp
, cp
, 0);
405 /* Check for public cache location */
406 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
409 /* Check for private cache location */
410 if (!(cache_private
= context_find (nmhprivcache
)))
411 cache_private
= ".cache";
412 cache_private
= getcpy (m_maildir (cache_private
));
415 * Cache the current directory before we do any chdirs()'s.
417 cwd
= getcpy (pwd());
420 * Check for storage directory. If specified,
421 * then store temporary files there. Else we
422 * store them in standard nmh directory.
424 if ((cp
= context_find (nmhstorage
)) && *cp
)
425 tmp
= concat (cp
, "/", invo_name
, NULL
);
427 tmp
= add (m_maildir (invo_name
), NULL
);
429 if (!context_find ("path"))
430 free (path ("./", TFOLDER
));
433 * Process a mhn composition file (mhn -build)
439 if (showsw
|| storesw
|| cachesw
)
440 adios (NULL
, "cannot use -build with -show, -store, -cache");
442 adios (NULL
, "need to specify a %s composition file", invo_name
);
444 adios (NULL
, "only one %s composition file at a time", invo_name
);
447 vec
[vecp
++] = "mhbuild";
450 vec
[vecp
++] = "-rfc934mode";
451 else if (rfc934sw
== -1)
452 vec
[vecp
++] = "-norfc934mode";
454 vec
[vecp
++] = msgs
.msgs
[0];
457 execvp ("mhbuild", vec
);
458 fprintf (stderr
, "unable to exec ");
463 * Process a mhn composition file (old MH style)
465 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
466 && !showsw
&& !storesw
&& !ntype
&& !file
467 && (cp
= getenv ("mhdraft"))
468 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
474 vec
[vecp
++] = "mhbuild";
477 vec
[vecp
++] = "-rfc934mode";
478 else if (rfc934sw
== -1)
479 vec
[vecp
++] = "-norfc934mode";
484 execvp ("mhbuild", vec
);
485 fprintf (stderr
, "unable to exec ");
489 if (file
&& msgs
.size
)
490 adios (NULL
, "cannot specify msg and file at same time!");
493 * check if message is coming from file
496 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
497 adios (NULL
, "out of memory");
500 if ((ct
= parse_mime (file
)))
504 * message(s) are coming from a folder
507 app_msgarg(&msgs
, "cur");
509 folder
= getfolder (1);
510 maildir
= m_maildir (folder
);
512 if (chdir (maildir
) == NOTOK
)
513 adios (maildir
, "unable to change directory to");
515 /* read folder and create message structure */
516 if (!(mp
= folder_read (folder
)))
517 adios (NULL
, "unable to read folder %s", folder
);
519 /* check for empty folder */
521 adios (NULL
, "no messages in %s", folder
);
523 /* parse all the message ranges/sequences and set SELECTED */
524 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
525 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
527 seq_setprev (mp
); /* set the previous-sequence */
529 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
530 adios (NULL
, "out of memory");
533 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
534 if (is_selected(mp
, msgnum
)) {
537 msgnam
= m_name (msgnum
);
538 if ((ct
= parse_mime (msgnam
)))
548 * You can't give more than one of these flags
551 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
552 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
554 /* If no action is specified, assume -show */
555 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
559 SIGNAL (SIGQUIT
, quitser
);
560 SIGNAL (SIGPIPE
, pipeser
);
563 * Get the associated umask for the relevant contents.
565 for (ctp
= cts
; *ctp
; ctp
++) {
569 if (type_ok (ct
, 1) && !ct
->c_umask
) {
570 if (stat (ct
->c_file
, &st
) != NOTOK
)
571 ct
->c_umask
= ~(st
.st_mode
& 0777);
573 ct
->c_umask
= ~m_gmprot();
578 * List the message content
581 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
584 * Store the message content
587 store_all_messages (cts
);
590 * Cache the message content
593 cache_all_messages (cts
);
596 * Show the message content
599 show_all_messages (cts
);
601 /* Now free all the structures for the content */
602 for (ctp
= cts
; *ctp
; ctp
++)
608 /* If reading from a folder, do some updating */
610 context_replace (pfolder
, folder
);/* update current folder */
611 seq_setcur (mp
, mp
->hghsel
); /* update current message */
612 seq_save (mp
); /* synchronize sequences */
613 context_save (); /* save the context file */
627 fprintf (stderr
, "\n");