]>
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>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #define MHN_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("cache", 0, CACHESW) \
25 X("nocache", 0, NCACHESW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("headers", 0, HEADSW) \
29 X("noheaders", 0, NHEADSW) \
30 X("list", 0, LISTSW) \
31 X("nolist", 0, NLISTSW) \
32 X("pause", 0, PAUSESW) \
33 X("nopause", 0, NPAUSESW) \
34 X("realsize", 0, SIZESW) \
35 X("norealsize", 0, NSIZESW) \
36 X("serialonly", 0, SERIALSW) \
37 X("noserialonly", 0, NSERIALSW) \
38 X("show", 0, SHOWSW) \
39 X("noshow", 0, NSHOWSW) \
40 X("store", 0, STORESW) \
41 X("nostore", 0, NSTORESW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
55 X("debug", -5, DEBUGSW) \
57 * switches for moreproc/mhlproc \
59 X("moreproc program", -4, PROGSW) \
60 X("nomoreproc", -3, NPROGSW) \
61 X("length lines", -4, LENSW) \
62 X("width columns", -4, WIDTHSW) \
64 * switches for mhbuild \
66 X("build", -5, BUILDSW) \
67 X("nobuild", -7, NBUILDSW) \
68 X("rfc934mode", -10, RFC934SW) \
69 X("norfc934mode", -12, NRFC934SW) \
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(MHN
);
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(MHN
, switches
);
81 extern char *tmp
; /* directory to place temp files */
86 extern char *cache_public
;
87 extern char *cache_private
;
94 extern int nomore
; /* flags for moreproc/header display */
99 extern char *cwd
; /* cache current working directory */
104 extern char *parts
[NPARTS
+ 1];
105 extern char *types
[NTYPES
+ 1];
112 * variables for mhbuild (mhn -build)
114 static int buildsw
= 0;
115 static int rfc934sw
= 0;
118 * what action to take?
120 static int cachesw
= 0;
121 static int listsw
= 0;
122 static int showsw
= 0;
123 static int storesw
= 0;
125 #define quitser pipeser
128 CT
parse_mime (char *);
131 int part_ok (CT
, int);
132 int type_ok (CT
, int);
133 void flush_errors (void);
136 void show_all_messages (CT
*);
139 void list_all_messages (CT
*, int, int, int, int);
142 void store_all_messages (CT
*);
145 void cache_all_messages (CT
*);
149 void freects_done (int) NORETURN
;
154 static void pipeser (int);
158 main (int argc
, char **argv
)
160 int sizesw
= 1, headsw
= 1;
161 int msgnum
, *icachesw
;
162 char *cp
, *file
= NULL
, *folder
= NULL
;
163 char *maildir
, buf
[100], **argp
;
165 struct msgs_array msgs
= { 0, 0, NULL
};
166 struct msgs
*mp
= NULL
;
173 setlocale(LC_ALL
, "");
175 invo_name
= r1bindex (argv
[0], '/');
177 /* read user profile/context */
180 arguments
= getarguments (invo_name
, argc
, argv
, 1);
186 while ((cp
= *argp
++)) {
188 switch (smatch (++cp
, switches
)) {
190 ambigsw (cp
, switches
);
193 adios (NULL
, "-%s unknown", cp
);
196 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
198 print_help (buf
, switches
, 1);
201 print_version(invo_name
);
219 icachesw
= &rcachesw
;
222 icachesw
= &wcachesw
;
224 if (!(cp
= *argp
++) || *cp
== '-')
225 adios (NULL
, "missing argument to %s", argp
[-2]);
226 switch (*icachesw
= smatch (cp
, caches
)) {
228 ambigsw (cp
, caches
);
231 adios (NULL
, "%s unknown", cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
297 adios (NULL
, "too many parts (starting with %s), %d max",
303 if (!(cp
= *argp
++) || *cp
== '-')
304 adios (NULL
, "missing argument to %s", argp
[-2]);
306 adios (NULL
, "too many types (starting with %s), %d max",
312 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
313 adios (NULL
, "missing argument to %s", argp
[-2]);
314 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
318 if (!(cp
= *argp
++) || *cp
== '-')
319 adios (NULL
, "missing argument to %s", argp
[-2]);
322 formsw
= getcpy (etcpath (cp
));
326 * Switches for moreproc/mhlproc
329 if (!(progsw
= *argp
++) || *progsw
== '-')
330 adios (NULL
, "missing argument to %s", argp
[-2]);
338 if (!(cp
= *argp
++) || *cp
== '-')
339 adios (NULL
, "missing argument to %s", argp
[-2]);
343 * Switches for mhbuild
369 if (*cp
== '+' || *cp
== '@') {
371 adios (NULL
, "only one folder at a time!");
373 folder
= pluspath (cp
);
375 app_msgarg(&msgs
, cp
);
378 /* null terminate the list of acceptable parts/types */
383 * Check if we've specified an additional profile
385 if ((cp
= getenv ("MHN"))) {
386 if ((fp
= fopen (cp
, "r"))) {
387 readconfig ((struct node
**) 0, fp
, cp
, 0);
390 admonish ("", "unable to read $MHN profile (%s)", cp
);
395 * Read the standard profile setup
397 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
398 readconfig ((struct node
**) 0, fp
, cp
, 0);
402 /* Check for public cache location */
403 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
406 /* Check for private cache location */
407 if (!(cache_private
= context_find (nmhprivcache
)))
408 cache_private
= ".cache";
409 cache_private
= getcpy (m_maildir (cache_private
));
412 * Cache the current directory before we do any chdirs()'s.
414 cwd
= getcpy (pwd());
417 * Check for storage directory. If specified,
418 * then store temporary files there. Else we
419 * store them in standard nmh directory.
421 if ((cp
= context_find (nmhstorage
)) && *cp
)
422 tmp
= concat (cp
, "/", invo_name
, NULL
);
424 tmp
= add (m_maildir (invo_name
), NULL
);
426 if (!context_find ("path"))
427 free (path ("./", TFOLDER
));
430 * Process a mhn composition file (mhn -build)
436 if (showsw
|| storesw
|| cachesw
)
437 adios (NULL
, "cannot use -build with -show, -store, -cache");
439 adios (NULL
, "need to specify a %s composition file", invo_name
);
441 adios (NULL
, "only one %s composition file at a time", invo_name
);
444 vec
[vecp
++] = "mhbuild";
447 vec
[vecp
++] = "-rfc934mode";
448 else if (rfc934sw
== -1)
449 vec
[vecp
++] = "-norfc934mode";
451 vec
[vecp
++] = msgs
.msgs
[0];
454 execvp ("mhbuild", vec
);
455 fprintf (stderr
, "unable to exec ");
460 * Process a mhn composition file (old MH style)
462 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
463 && !showsw
&& !storesw
&& !ntype
&& !file
464 && (cp
= getenv ("mhdraft"))
465 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
471 vec
[vecp
++] = "mhbuild";
474 vec
[vecp
++] = "-rfc934mode";
475 else if (rfc934sw
== -1)
476 vec
[vecp
++] = "-norfc934mode";
481 execvp ("mhbuild", vec
);
482 fprintf (stderr
, "unable to exec ");
486 if (file
&& msgs
.size
)
487 adios (NULL
, "cannot specify msg and file at same time!");
490 * check if message is coming from file
493 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
494 adios (NULL
, "out of memory");
497 if ((ct
= parse_mime (file
)))
501 * message(s) are coming from a folder
504 app_msgarg(&msgs
, "cur");
506 folder
= getfolder (1);
507 maildir
= m_maildir (folder
);
509 if (chdir (maildir
) == NOTOK
)
510 adios (maildir
, "unable to change directory to");
512 /* read folder and create message structure */
513 if (!(mp
= folder_read (folder
, 1)))
514 adios (NULL
, "unable to read folder %s", folder
);
516 /* check for empty folder */
518 adios (NULL
, "no messages in %s", folder
);
520 /* parse all the message ranges/sequences and set SELECTED */
521 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
522 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
524 seq_setprev (mp
); /* set the previous-sequence */
526 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
527 adios (NULL
, "out of memory");
530 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
531 if (is_selected(mp
, msgnum
)) {
534 msgnam
= m_name (msgnum
);
535 if ((ct
= parse_mime (msgnam
)))
545 * You can't give more than one of these flags
548 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
549 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
551 /* If no action is specified, assume -show */
552 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
556 SIGNAL (SIGQUIT
, quitser
);
557 SIGNAL (SIGPIPE
, pipeser
);
560 * Get the associated umask for the relevant contents.
562 for (ctp
= cts
; *ctp
; ctp
++) {
566 if (type_ok (ct
, 1) && !ct
->c_umask
) {
567 if (stat (ct
->c_file
, &st
) != NOTOK
)
568 ct
->c_umask
= ~(st
.st_mode
& 0777);
570 ct
->c_umask
= ~m_gmprot();
575 * List the message content
578 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
);
581 * Store the message content
584 store_all_messages (cts
);
587 * Cache the message content
590 cache_all_messages (cts
);
593 * Show the message content
596 show_all_messages (cts
);
598 /* Now free all the structures for the content */
599 for (ctp
= cts
; *ctp
; ctp
++)
605 /* If reading from a folder, do some updating */
607 context_replace (pfolder
, folder
);/* update current folder */
608 seq_setcur (mp
, mp
->hghsel
); /* update current message */
609 seq_save (mp
); /* synchronize sequences */
610 context_save (); /* save the context file */
623 fprintf (stderr
, "\n");