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