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