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