]> diplodocus.org Git - nmh/blob - uip/mhn.c
read_yes_or_no_if_tty.c: Move interface to own file.
[nmh] / uip / mhn.c
1 /* mhn.c -- display, list, cache, or store the contents of MIME messages
2 *
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.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/readconfig.h"
20 #include "sbr/ambigsw.h"
21 #include "sbr/path.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
25 #include <fcntl.h>
26 #include "h/signals.h"
27 #include "h/md5.h"
28 #include "h/mts.h"
29 #include "h/tws.h"
30 #include "h/fmt_scan.h"
31 #include "h/mime.h"
32 #include "h/mhparse.h"
33 #include "h/mhcachesbr.h"
34 #include "h/done.h"
35 #include "h/utils.h"
36 #include "mhmisc.h"
37 #include "sbr/m_maildir.h"
38 #include "mhfree.h"
39 #include "mhshowsbr.h"
40
41 #define MHN_SWITCHES \
42 X("auto", 0, AUTOSW) \
43 X("noauto", 0, NAUTOSW) \
44 X("cache", 0, CACHESW) \
45 X("nocache", 0, NCACHESW) \
46 X("check", 0, CHECKSW) \
47 X("nocheck", 0, NCHECKSW) \
48 X("headers", 0, HEADSW) \
49 X("noheaders", 0, NHEADSW) \
50 X("list", 0, LISTSW) \
51 X("nolist", 0, NLISTSW) \
52 X("realsize", 0, SIZESW) \
53 X("norealsize", 0, NSIZESW) \
54 X("show", 0, SHOWSW) \
55 X("noshow", 0, NSHOWSW) \
56 X("store", 0, STORESW) \
57 X("nostore", 0, NSTORESW) \
58 X("verbose", 0, VERBSW) \
59 X("noverbose", 0, NVERBSW) \
60 X("file file", 0, FILESW) \
61 X("form formfile", 0, FORMSW) \
62 X("part number", 0, PARTSW) \
63 X("type content", 0, TYPESW) \
64 X("rcache policy", 0, RCACHESW) \
65 X("wcache policy", 0, WCACHESW) \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
68 /* \
69 * for debugging \
70 */ \
71 X("debug", -5, DEBUGSW) \
72 /* \
73 * switches for moreproc/mhlproc \
74 */ \
75 X("moreproc program", -4, PROGSW) \
76 X("nomoreproc", -3, NPROGSW) \
77 X("length lines", -4, LENSW) \
78 X("width columns", -4, WIDTHSW) \
79 /* \
80 * switches for mhbuild \
81 */ \
82 X("build", -5, BUILDSW) \
83 X("nobuild", -7, NBUILDSW) \
84 X("rfc934mode", 0, RFC934SW) \
85 X("norfc934mode", 0, NRFC934SW) \
86
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(MHN);
89 #undef X
90
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(MHN, switches);
93 #undef X
94
95
96 int debugsw = 0;
97 int verbosw = 0;
98
99 /*
100 * variables for mhbuild (mhn -build)
101 */
102 static bool buildsw;
103 static int rfc934sw = 0;
104
105 /*
106 * what action to take?
107 */
108 static bool cachesw;
109 static bool listsw;
110 static bool showsw;
111 static bool storesw;
112
113 #define quitser pipeser
114
115 /*
116 * static prototypes
117 */
118 static void pipeser (int);
119
120
121 int
122 main (int argc, char **argv)
123 {
124 bool sizesw = true;
125 bool headsw = true;
126 bool autosw = false;
127 int msgnum, *icachesw;
128 char *cp, *file = NULL, *folder = NULL;
129 char *maildir, buf[100], **argp;
130 char **arguments;
131 char *cwd;
132 struct msgs_array msgs = { 0, 0, NULL };
133 struct msgs *mp = NULL;
134 CT ct, *ctp;
135 FILE *fp;
136 mhstoreinfo_t info;
137
138 if (nmh_init(argv[0], true, true)) { return 1; }
139
140 set_done(freects_done);
141
142 arguments = getarguments (invo_name, argc, argv, 1);
143 argp = arguments;
144
145 /*
146 * Parse arguments
147 */
148 while ((cp = *argp++)) {
149 if (*cp == '-') {
150 switch (smatch (++cp, switches)) {
151 case AMBIGSW:
152 ambigsw (cp, switches);
153 done (1);
154 case UNKWNSW:
155 die("-%s unknown", cp);
156
157 case HELPSW:
158 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
159 invo_name);
160 print_help (buf, switches, 1);
161 done (0);
162 case VERSIONSW:
163 print_version(invo_name);
164 done (0);
165
166 case AUTOSW:
167 autosw = true;
168 continue;
169 case NAUTOSW:
170 autosw = false;
171 continue;
172
173 case CACHESW:
174 cachesw = true;
175 continue;
176 case NCACHESW:
177 cachesw = false;
178 continue;
179
180 case RCACHESW:
181 icachesw = &rcachesw;
182 goto do_cache;
183 case WCACHESW:
184 icachesw = &wcachesw;
185 do_cache:
186 if (!(cp = *argp++) || *cp == '-')
187 die("missing argument to %s", argp[-2]);
188 switch (*icachesw = smatch (cp, cache_policy)) {
189 case AMBIGSW:
190 ambigsw (cp, cache_policy);
191 done (1);
192 case UNKWNSW:
193 die("%s unknown", cp);
194 default:
195 break;
196 }
197 continue;
198
199 case CHECKSW:
200 checksw++;
201 continue;
202 case NCHECKSW:
203 checksw = 0;
204 continue;
205
206 case HEADSW:
207 headsw = true;
208 continue;
209 case NHEADSW:
210 headsw = false;
211 continue;
212
213 case LISTSW:
214 listsw = true;
215 continue;
216 case NLISTSW:
217 listsw = false;
218 continue;
219
220 case SHOWSW:
221 showsw = true;
222 continue;
223 case NSHOWSW:
224 showsw = false;
225 continue;
226
227 case SIZESW:
228 sizesw = true;
229 continue;
230 case NSIZESW:
231 sizesw = false;
232 continue;
233
234 case STORESW:
235 storesw = true;
236 continue;
237 case NSTORESW:
238 storesw = false;
239 continue;
240
241 case PARTSW:
242 if (!(cp = *argp++) || *cp == '-')
243 die("missing argument to %s", argp[-2]);
244 if (npart >= NPARTS)
245 die("too many parts (starting with %s), %d max",
246 cp, NPARTS);
247 parts[npart++] = cp;
248 continue;
249
250 case TYPESW:
251 if (!(cp = *argp++) || *cp == '-')
252 die("missing argument to %s", argp[-2]);
253 if (ntype >= NTYPES)
254 die("too many types (starting with %s), %d max",
255 cp, NTYPES);
256 types[ntype++] = cp;
257 continue;
258
259 case FILESW:
260 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
261 die("missing argument to %s", argp[-2]);
262 file = *cp == '-' ? cp : path (cp, TFILE);
263 continue;
264
265 case FORMSW:
266 if (!(cp = *argp++) || *cp == '-')
267 die("missing argument to %s", argp[-2]);
268 free(formsw);
269 formsw = mh_xstrdup(etcpath(cp));
270 continue;
271
272 /*
273 * Switches for moreproc/mhlproc
274 */
275 case PROGSW:
276 if (!(progsw = *argp++) || *progsw == '-')
277 die("missing argument to %s", argp[-2]);
278 continue;
279 case NPROGSW:
280 nomore++;
281 continue;
282
283 case LENSW:
284 case WIDTHSW:
285 if (!(cp = *argp++) || *cp == '-')
286 die("missing argument to %s", argp[-2]);
287 continue;
288
289 /*
290 * Switches for mhbuild
291 */
292 case BUILDSW:
293 buildsw = true;
294 continue;
295 case NBUILDSW:
296 buildsw = false;
297 continue;
298 case RFC934SW:
299 rfc934sw = 1;
300 continue;
301 case NRFC934SW:
302 rfc934sw = -1;
303 continue;
304
305 case VERBSW:
306 verbosw = 1;
307 continue;
308 case NVERBSW:
309 verbosw = 0;
310 continue;
311 case DEBUGSW:
312 debugsw = 1;
313 continue;
314 }
315 }
316 if (*cp == '+' || *cp == '@') {
317 if (folder)
318 die("only one folder at a time!");
319 folder = pluspath (cp);
320 } else
321 app_msgarg(&msgs, cp);
322 }
323
324 /* null terminate the list of acceptable parts/types */
325 parts[npart] = NULL;
326 types[ntype] = NULL;
327
328 /*
329 * Check if we've specified an additional profile
330 */
331 if ((cp = getenv ("MHN"))) {
332 if ((fp = fopen (cp, "r"))) {
333 readconfig(NULL, fp, cp, 0);
334 fclose (fp);
335 } else {
336 admonish ("", "unable to read $MHN profile (%s)", cp);
337 }
338 }
339
340 /*
341 * Read the standard profile setup
342 */
343 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
344 readconfig(NULL, fp, cp, 0);
345 fclose (fp);
346 }
347
348 /* Check for public cache location */
349 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
350 cache_public = NULL;
351
352 /* Check for private cache location */
353 if (!(cache_private = context_find (nmhprivcache)))
354 cache_private = ".cache";
355 cache_private = mh_xstrdup(m_maildir(cache_private));
356
357 /*
358 * Cache the current directory before we do any chdirs()'s.
359 */
360 cwd = mh_xstrdup(pwd());
361
362 if (!context_find ("path"))
363 free (path ("./", TFOLDER));
364
365 /*
366 * Process a mhn composition file (mhn -build)
367 */
368 if (buildsw) {
369 char *vec[MAXARGS];
370 int vecp;
371
372 if (showsw || storesw || cachesw)
373 die("cannot use -build with -show, -store, -cache");
374 if (msgs.size < 1)
375 die("need to specify a %s composition file", invo_name);
376 if (msgs.size > 1)
377 die("only one %s composition file at a time", invo_name);
378
379 vecp = 0;
380 vec[vecp++] = "mhbuild";
381
382 if (rfc934sw == 1)
383 vec[vecp++] = "-rfc934mode";
384 else if (rfc934sw == -1)
385 vec[vecp++] = "-norfc934mode";
386
387 vec[vecp++] = msgs.msgs[0];
388 vec[vecp] = NULL;
389
390 execvp ("mhbuild", vec);
391 fprintf (stderr, "unable to exec ");
392 _exit(1);
393 }
394
395 /*
396 * Process a mhn composition file (old MH style)
397 */
398 if (msgs.size == 1 && !folder && !npart && !cachesw
399 && !showsw && !storesw && !ntype && !file
400 && (cp = getenv ("mhdraft"))
401 && strcmp (cp, msgs.msgs[0]) == 0) {
402
403 char *vec[MAXARGS];
404 int vecp;
405
406 vecp = 0;
407 vec[vecp++] = "mhbuild";
408
409 if (rfc934sw == 1)
410 vec[vecp++] = "-rfc934mode";
411 else if (rfc934sw == -1)
412 vec[vecp++] = "-norfc934mode";
413
414 vec[vecp++] = cp;
415 vec[vecp] = NULL;
416
417 execvp ("mhbuild", vec);
418 fprintf (stderr, "unable to exec ");
419 _exit(1);
420 }
421
422 if (file && msgs.size)
423 die("cannot specify msg and file at same time!");
424
425 /*
426 * check if message is coming from file
427 */
428 if (file) {
429 cts = mh_xcalloc(2, sizeof *cts);
430 ctp = cts;
431
432 if ((ct = parse_mime (file)))
433 *ctp++ = ct;
434 } else {
435 /*
436 * message(s) are coming from a folder
437 */
438 if (!msgs.size)
439 app_msgarg(&msgs, "cur");
440 if (!folder)
441 folder = getfolder (1);
442 maildir = m_maildir (folder);
443
444 if (chdir (maildir) == NOTOK)
445 adios (maildir, "unable to change directory to");
446
447 /* read folder and create message structure */
448 if (!(mp = folder_read (folder, 1)))
449 die("unable to read folder %s", folder);
450
451 /* check for empty folder */
452 if (mp->nummsg == 0)
453 die("no messages in %s", folder);
454
455 /* parse all the message ranges/sequences and set SELECTED */
456 for (msgnum = 0; msgnum < msgs.size; msgnum++)
457 if (!m_convert (mp, msgs.msgs[msgnum]))
458 done (1);
459 seq_setprev (mp); /* set the previous-sequence */
460
461 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
462 ctp = cts;
463
464 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
465 if (is_selected(mp, msgnum)) {
466 char *msgnam;
467
468 msgnam = m_name (msgnum);
469 if ((ct = parse_mime (msgnam)))
470 *ctp++ = ct;
471 }
472 }
473 }
474
475 if (!*cts)
476 done (1);
477
478 /*
479 * You can't give more than one of these flags
480 * at a time.
481 */
482 if (showsw + listsw + storesw + cachesw > 1)
483 die("can only use one of -show, -list, -store, -cache at same time");
484
485 /* If no action is specified, assume -show */
486 if (!listsw && !showsw && !storesw && !cachesw)
487 showsw = true;
488
489 userrs = true;
490 SIGNAL (SIGQUIT, quitser);
491 SIGNAL (SIGPIPE, pipeser);
492
493 /*
494 * Get the associated umask for the relevant contents.
495 */
496 for (ctp = cts; *ctp; ctp++) {
497 struct stat st;
498
499 ct = *ctp;
500 if (type_ok (ct, 1) && !ct->c_umask) {
501 if (stat (ct->c_file, &st) != NOTOK)
502 ct->c_umask = ~(st.st_mode & 0777);
503 else
504 ct->c_umask = ~m_gmprot();
505 }
506 }
507
508 /*
509 * List the message content
510 */
511 if (listsw)
512 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, 0);
513
514 /*
515 * Store the message content
516 */
517 if (storesw) {
518 info = mhstoreinfo_create (cts, cwd, "always", autosw, verbosw);;
519 store_all_messages (info);
520 mhstoreinfo_free (info);
521 }
522
523 /* If reading from a folder, do some updating */
524 if (mp) {
525 context_replace (pfolder, folder);/* update current folder */
526 seq_setcur (mp, mp->hghsel); /* update current message */
527 seq_save (mp); /* synchronize sequences */
528 context_save (); /* save the context file */
529 }
530
531 /*
532 * Cache the message content
533 */
534 if (cachesw)
535 cache_all_messages (cts);
536
537 /*
538 * Show the message content
539 */
540 if (showsw)
541 show_all_messages (cts, 0, 0, 0);
542
543 /* Now free all the structures for the content */
544 for (ctp = cts; *ctp; ctp++)
545 free_content (*ctp);
546
547 free (cts);
548 cts = NULL;
549
550 done (0);
551 return 1;
552 }
553
554
555 static void
556 pipeser (int i)
557 {
558 if (i == SIGQUIT) {
559 fflush (stdout);
560 fprintf (stderr, "\n");
561 fflush (stderr);
562 }
563
564 done (1);
565 /* NOTREACHED */
566 }