]>
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>
14 #include <h/fmt_scan.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 #include "sbr/m_maildir.h"
22 #include "mhshowsbr.h"
24 #define MHN_SWITCHES \
25 X("auto", 0, AUTOSW) \
26 X("noauto", 0, NAUTOSW) \
27 X("cache", 0, CACHESW) \
28 X("nocache", 0, NCACHESW) \
29 X("check", 0, CHECKSW) \
30 X("nocheck", 0, NCHECKSW) \
31 X("headers", 0, HEADSW) \
32 X("noheaders", 0, NHEADSW) \
33 X("list", 0, LISTSW) \
34 X("nolist", 0, NLISTSW) \
35 X("realsize", 0, SIZESW) \
36 X("norealsize", 0, NSIZESW) \
37 X("show", 0, SHOWSW) \
38 X("noshow", 0, NSHOWSW) \
39 X("store", 0, STORESW) \
40 X("nostore", 0, NSTORESW) \
41 X("verbose", 0, VERBSW) \
42 X("noverbose", 0, NVERBSW) \
43 X("file file", 0, FILESW) \
44 X("form formfile", 0, FORMSW) \
45 X("part number", 0, PARTSW) \
46 X("type content", 0, TYPESW) \
47 X("rcache policy", 0, RCACHESW) \
48 X("wcache policy", 0, WCACHESW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
54 X("debug", -5, DEBUGSW) \
56 * switches for moreproc/mhlproc \
58 X("moreproc program", -4, PROGSW) \
59 X("nomoreproc", -3, NPROGSW) \
60 X("length lines", -4, LENSW) \
61 X("width columns", -4, WIDTHSW) \
63 * switches for mhbuild \
65 X("build", -5, BUILDSW) \
66 X("nobuild", -7, NBUILDSW) \
67 X("rfc934mode", 0, RFC934SW) \
68 X("norfc934mode", 0, NRFC934SW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(MHN
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(MHN
, switches
);
83 * variables for mhbuild (mhn -build)
85 static int buildsw
= 0;
86 static int rfc934sw
= 0;
89 * what action to take?
91 static int cachesw
= 0;
92 static int listsw
= 0;
93 static int showsw
= 0;
94 static int storesw
= 0;
96 #define quitser pipeser
101 static void pipeser (int);
105 main (int argc
, char **argv
)
107 int sizesw
= 1, headsw
= 1, autosw
= 0;
108 int msgnum
, *icachesw
;
109 char *cp
, *file
= NULL
, *folder
= NULL
;
110 char *maildir
, buf
[100], **argp
;
113 struct msgs_array msgs
= { 0, 0, NULL
};
114 struct msgs
*mp
= NULL
;
119 if (nmh_init(argv
[0], 1)) { return 1; }
123 arguments
= getarguments (invo_name
, argc
, argv
, 1);
129 while ((cp
= *argp
++)) {
131 switch (smatch (++cp
, switches
)) {
133 ambigsw (cp
, switches
);
136 adios (NULL
, "-%s unknown", cp
);
139 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
141 print_help (buf
, switches
, 1);
144 print_version(invo_name
);
162 icachesw
= &rcachesw
;
165 icachesw
= &wcachesw
;
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
169 switch (*icachesw
= smatch (cp
, cache_policy
)) {
171 ambigsw (cp
, cache_policy
);
174 adios (NULL
, "%s unknown", cp
);
223 if (!(cp
= *argp
++) || *cp
== '-')
224 adios (NULL
, "missing argument to %s", argp
[-2]);
226 adios (NULL
, "too many parts (starting with %s), %d max",
232 if (!(cp
= *argp
++) || *cp
== '-')
233 adios (NULL
, "missing argument to %s", argp
[-2]);
235 adios (NULL
, "too many types (starting with %s), %d max",
241 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
242 adios (NULL
, "missing argument to %s", argp
[-2]);
243 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
247 if (!(cp
= *argp
++) || *cp
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
250 formsw
= getcpy (etcpath (cp
));
254 * Switches for moreproc/mhlproc
257 if (!(progsw
= *argp
++) || *progsw
== '-')
258 adios (NULL
, "missing argument to %s", argp
[-2]);
266 if (!(cp
= *argp
++) || *cp
== '-')
267 adios (NULL
, "missing argument to %s", argp
[-2]);
271 * Switches for mhbuild
297 if (*cp
== '+' || *cp
== '@') {
299 adios (NULL
, "only one folder at a time!");
300 folder
= pluspath (cp
);
302 app_msgarg(&msgs
, cp
);
305 /* null terminate the list of acceptable parts/types */
310 * Check if we've specified an additional profile
312 if ((cp
= getenv ("MHN"))) {
313 if ((fp
= fopen (cp
, "r"))) {
314 readconfig(NULL
, fp
, cp
, 0);
317 admonish ("", "unable to read $MHN profile (%s)", cp
);
322 * Read the standard profile setup
324 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
325 readconfig(NULL
, fp
, cp
, 0);
329 /* Check for public cache location */
330 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
333 /* Check for private cache location */
334 if (!(cache_private
= context_find (nmhprivcache
)))
335 cache_private
= ".cache";
336 cache_private
= getcpy (m_maildir (cache_private
));
339 * Cache the current directory before we do any chdirs()'s.
341 cwd
= mh_xstrdup(pwd());
343 if (!context_find ("path"))
344 free (path ("./", TFOLDER
));
347 * Process a mhn composition file (mhn -build)
353 if (showsw
|| storesw
|| cachesw
)
354 adios (NULL
, "cannot use -build with -show, -store, -cache");
356 adios (NULL
, "need to specify a %s composition file", invo_name
);
358 adios (NULL
, "only one %s composition file at a time", invo_name
);
361 vec
[vecp
++] = "mhbuild";
364 vec
[vecp
++] = "-rfc934mode";
365 else if (rfc934sw
== -1)
366 vec
[vecp
++] = "-norfc934mode";
368 vec
[vecp
++] = msgs
.msgs
[0];
371 execvp ("mhbuild", vec
);
372 fprintf (stderr
, "unable to exec ");
377 * Process a mhn composition file (old MH style)
379 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
380 && !showsw
&& !storesw
&& !ntype
&& !file
381 && (cp
= getenv ("mhdraft"))
382 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
388 vec
[vecp
++] = "mhbuild";
391 vec
[vecp
++] = "-rfc934mode";
392 else if (rfc934sw
== -1)
393 vec
[vecp
++] = "-norfc934mode";
398 execvp ("mhbuild", vec
);
399 fprintf (stderr
, "unable to exec ");
403 if (file
&& msgs
.size
)
404 adios (NULL
, "cannot specify msg and file at same time!");
407 * check if message is coming from file
410 cts
= mh_xcalloc(2, sizeof *cts
);
413 if ((ct
= parse_mime (file
)))
417 * message(s) are coming from a folder
420 app_msgarg(&msgs
, "cur");
422 folder
= getfolder (1);
423 maildir
= m_maildir (folder
);
425 if (chdir (maildir
) == NOTOK
)
426 adios (maildir
, "unable to change directory to");
428 /* read folder and create message structure */
429 if (!(mp
= folder_read (folder
, 1)))
430 adios (NULL
, "unable to read folder %s", folder
);
432 /* check for empty folder */
434 adios (NULL
, "no messages in %s", folder
);
436 /* parse all the message ranges/sequences and set SELECTED */
437 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
438 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
440 seq_setprev (mp
); /* set the previous-sequence */
442 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
445 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
446 if (is_selected(mp
, msgnum
)) {
449 msgnam
= m_name (msgnum
);
450 if ((ct
= parse_mime (msgnam
)))
460 * You can't give more than one of these flags
463 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
464 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
466 /* If no action is specified, assume -show */
467 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
471 SIGNAL (SIGQUIT
, quitser
);
472 SIGNAL (SIGPIPE
, pipeser
);
475 * Get the associated umask for the relevant contents.
477 for (ctp
= cts
; *ctp
; ctp
++) {
481 if (type_ok (ct
, 1) && !ct
->c_umask
) {
482 if (stat (ct
->c_file
, &st
) != NOTOK
)
483 ct
->c_umask
= ~(st
.st_mode
& 0777);
485 ct
->c_umask
= ~m_gmprot();
490 * List the message content
493 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
496 * Store the message content
499 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
500 store_all_messages (info
);
501 mhstoreinfo_free (info
);
504 /* If reading from a folder, do some updating */
506 context_replace (pfolder
, folder
);/* update current folder */
507 seq_setcur (mp
, mp
->hghsel
); /* update current message */
508 seq_save (mp
); /* synchronize sequences */
509 context_save (); /* save the context file */
513 * Cache the message content
516 cache_all_messages (cts
);
519 * Show the message content
522 show_all_messages (cts
, 0, 0, 0);
524 /* Now free all the structures for the content */
525 for (ctp
= cts
; *ctp
; ctp
++)
541 fprintf (stderr
, "\n");