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