]>
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>
20 #define MHN_SWITCHES \
21 X("auto", 0, AUTOSW) \
22 X("noauto", 0, NAUTOSW) \
23 X("cache", 0, CACHESW) \
24 X("nocache", 0, NCACHESW) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("headers", 0, HEADSW) \
28 X("noheaders", 0, NHEADSW) \
29 X("list", 0, LISTSW) \
30 X("nolist", 0, NLISTSW) \
31 X("realsize", 0, SIZESW) \
32 X("norealsize", 0, NSIZESW) \
33 X("show", 0, SHOWSW) \
34 X("noshow", 0, NSHOWSW) \
35 X("store", 0, STORESW) \
36 X("nostore", 0, NSTORESW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("file file", 0, FILESW) \
40 X("form formfile", 0, FORMSW) \
41 X("part number", 0, PARTSW) \
42 X("type content", 0, TYPESW) \
43 X("rcache policy", 0, RCACHESW) \
44 X("wcache policy", 0, WCACHESW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
50 X("debug", -5, DEBUGSW) \
52 * switches for moreproc/mhlproc \
54 X("moreproc program", -4, PROGSW) \
55 X("nomoreproc", -3, NPROGSW) \
56 X("length lines", -4, LENSW) \
57 X("width columns", -4, WIDTHSW) \
59 * switches for mhbuild \
61 X("build", -5, BUILDSW) \
62 X("nobuild", -7, NBUILDSW) \
63 X("rfc934mode", 0, RFC934SW) \
64 X("norfc934mode", 0, NRFC934SW) \
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(MHN
);
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(MHN
, switches
);
78 extern char *cache_public
;
79 extern char *cache_private
;
84 extern int nomore
; /* flags for moreproc/header display */
90 extern char *parts
[NPARTS
+ 1];
91 extern char *types
[NTYPES
+ 1];
98 * variables for mhbuild (mhn -build)
100 static int buildsw
= 0;
101 static int rfc934sw
= 0;
104 * what action to take?
106 static int cachesw
= 0;
107 static int listsw
= 0;
108 static int showsw
= 0;
109 static int storesw
= 0;
111 #define quitser pipeser
114 CT
parse_mime (char *);
118 int type_ok (CT
, int);
119 void flush_errors (void);
122 void cache_all_messages (CT
*);
127 static void pipeser (int);
131 main (int argc
, char **argv
)
133 int sizesw
= 1, headsw
= 1, autosw
= 0;
134 int msgnum
, *icachesw
;
135 char *cp
, *file
= NULL
, *folder
= NULL
;
136 char *maildir
, buf
[100], **argp
;
139 struct msgs_array msgs
= { 0, 0, NULL
};
140 struct msgs
*mp
= NULL
;
145 if (nmh_init(argv
[0], 1)) { return 1; }
149 arguments
= getarguments (invo_name
, argc
, argv
, 1);
155 while ((cp
= *argp
++)) {
157 switch (smatch (++cp
, switches
)) {
159 ambigsw (cp
, switches
);
162 adios (NULL
, "-%s unknown", cp
);
165 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
167 print_help (buf
, switches
, 1);
170 print_version(invo_name
);
188 icachesw
= &rcachesw
;
191 icachesw
= &wcachesw
;
193 if (!(cp
= *argp
++) || *cp
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
195 switch (*icachesw
= smatch (cp
, caches
)) {
197 ambigsw (cp
, caches
);
200 adios (NULL
, "%s unknown", cp
);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
252 adios (NULL
, "too many parts (starting with %s), %d max",
258 if (!(cp
= *argp
++) || *cp
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
261 adios (NULL
, "too many types (starting with %s), %d max",
267 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
268 adios (NULL
, "missing argument to %s", argp
[-2]);
269 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 adios (NULL
, "missing argument to %s", argp
[-2]);
276 formsw
= getcpy (etcpath (cp
));
280 * Switches for moreproc/mhlproc
283 if (!(progsw
= *argp
++) || *progsw
== '-')
284 adios (NULL
, "missing argument to %s", argp
[-2]);
292 if (!(cp
= *argp
++) || *cp
== '-')
293 adios (NULL
, "missing argument to %s", argp
[-2]);
297 * Switches for mhbuild
323 if (*cp
== '+' || *cp
== '@') {
325 adios (NULL
, "only one folder at a time!");
327 folder
= pluspath (cp
);
329 app_msgarg(&msgs
, cp
);
332 /* null terminate the list of acceptable parts/types */
337 * Check if we've specified an additional profile
339 if ((cp
= getenv ("MHN"))) {
340 if ((fp
= fopen (cp
, "r"))) {
341 readconfig ((struct node
**) 0, fp
, cp
, 0);
344 admonish ("", "unable to read $MHN profile (%s)", cp
);
349 * Read the standard profile setup
351 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
352 readconfig ((struct node
**) 0, fp
, cp
, 0);
356 /* Check for public cache location */
357 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
360 /* Check for private cache location */
361 if (!(cache_private
= context_find (nmhprivcache
)))
362 cache_private
= ".cache";
363 cache_private
= getcpy (m_maildir (cache_private
));
366 * Cache the current directory before we do any chdirs()'s.
368 cwd
= mh_xstrdup(pwd());
370 if (!context_find ("path"))
371 free (path ("./", TFOLDER
));
374 * Process a mhn composition file (mhn -build)
380 if (showsw
|| storesw
|| cachesw
)
381 adios (NULL
, "cannot use -build with -show, -store, -cache");
383 adios (NULL
, "need to specify a %s composition file", invo_name
);
385 adios (NULL
, "only one %s composition file at a time", invo_name
);
388 vec
[vecp
++] = "mhbuild";
391 vec
[vecp
++] = "-rfc934mode";
392 else if (rfc934sw
== -1)
393 vec
[vecp
++] = "-norfc934mode";
395 vec
[vecp
++] = msgs
.msgs
[0];
398 execvp ("mhbuild", vec
);
399 fprintf (stderr
, "unable to exec ");
404 * Process a mhn composition file (old MH style)
406 if (msgs
.size
== 1 && !folder
&& !npart
&& !cachesw
407 && !showsw
&& !storesw
&& !ntype
&& !file
408 && (cp
= getenv ("mhdraft"))
409 && strcmp (cp
, msgs
.msgs
[0]) == 0) {
415 vec
[vecp
++] = "mhbuild";
418 vec
[vecp
++] = "-rfc934mode";
419 else if (rfc934sw
== -1)
420 vec
[vecp
++] = "-norfc934mode";
425 execvp ("mhbuild", vec
);
426 fprintf (stderr
, "unable to exec ");
430 if (file
&& msgs
.size
)
431 adios (NULL
, "cannot specify msg and file at same time!");
434 * check if message is coming from file
437 cts
= mh_xcalloc(2, sizeof *cts
);
440 if ((ct
= parse_mime (file
)))
444 * message(s) are coming from a folder
447 app_msgarg(&msgs
, "cur");
449 folder
= getfolder (1);
450 maildir
= m_maildir (folder
);
452 if (chdir (maildir
) == NOTOK
)
453 adios (maildir
, "unable to change directory to");
455 /* read folder and create message structure */
456 if (!(mp
= folder_read (folder
, 1)))
457 adios (NULL
, "unable to read folder %s", folder
);
459 /* check for empty folder */
461 adios (NULL
, "no messages in %s", folder
);
463 /* parse all the message ranges/sequences and set SELECTED */
464 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
465 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
467 seq_setprev (mp
); /* set the previous-sequence */
469 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
472 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
473 if (is_selected(mp
, msgnum
)) {
476 msgnam
= m_name (msgnum
);
477 if ((ct
= parse_mime (msgnam
)))
487 * You can't give more than one of these flags
490 if (showsw
+ listsw
+ storesw
+ cachesw
> 1)
491 adios (NULL
, "can only use one of -show, -list, -store, -cache at same time");
493 /* If no action is specified, assume -show */
494 if (!listsw
&& !showsw
&& !storesw
&& !cachesw
)
498 SIGNAL (SIGQUIT
, quitser
);
499 SIGNAL (SIGPIPE
, pipeser
);
502 * Get the associated umask for the relevant contents.
504 for (ctp
= cts
; *ctp
; ctp
++) {
508 if (type_ok (ct
, 1) && !ct
->c_umask
) {
509 if (stat (ct
->c_file
, &st
) != NOTOK
)
510 ct
->c_umask
= ~(st
.st_mode
& 0777);
512 ct
->c_umask
= ~m_gmprot();
517 * List the message content
520 list_all_messages (cts
, headsw
, sizesw
, verbosw
, debugsw
, 0);
523 * Store the message content
526 info
= mhstoreinfo_create (cts
, cwd
, "always", autosw
, verbosw
);;
527 store_all_messages (info
);
528 mhstoreinfo_free (info
);
531 /* If reading from a folder, do some updating */
533 context_replace (pfolder
, folder
);/* update current folder */
534 seq_setcur (mp
, mp
->hghsel
); /* update current message */
535 seq_save (mp
); /* synchronize sequences */
536 context_save (); /* save the context file */
540 * Cache the message content
543 cache_all_messages (cts
);
546 * Show the message content
549 show_all_messages (cts
, 0, 0, 0);
551 /* Now free all the structures for the content */
552 for (ctp
= cts
; *ctp
; ctp
++)
568 fprintf (stderr
, "\n");