Changed msg_style and msg_delim to be file static to m_getfld.c
[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 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 flush_errors (void);
177
178 /* mhshowsbr.c */
179 void show_all_messages (CT *);
180
181 /* mhlistsbr.c */
182 void list_all_messages (CT *, int, int, int, int);
183
184 /* mhstoresbr.c */
185 void store_all_messages (CT *);
186
187 /* mhcachesbr.c */
188 void cache_all_messages (CT *);
189
190 /* mhfree.c */
191 void free_content (CT);
192 extern CT *cts;
193 void freects_done (int) NORETURN;
194
195 /*
196  * static prototypes
197  */
198 static void pipeser (int);
199
200
201 int
202 main (int argc, char **argv)
203 {
204     int sizesw = 1, headsw = 1;
205     int msgnum, *icachesw;
206     char *cp, *file = NULL, *folder = NULL;
207     char *maildir, buf[100], **argp;
208     char **arguments;
209     struct msgs_array msgs = { 0, 0, NULL };
210     struct msgs *mp = NULL;
211     CT ct, *ctp;
212     FILE *fp;
213
214     done=freects_done;
215
216 #ifdef LOCALE
217     setlocale(LC_ALL, "");
218 #endif
219     invo_name = r1bindex (argv[0], '/');
220
221     /* read user profile/context */
222     context_read();
223
224     arguments = getarguments (invo_name, argc, argv, 1);
225     argp = arguments;
226
227     /*
228      * Parse arguments
229      */
230     while ((cp = *argp++)) {
231         if (*cp == '-') {
232             switch (smatch (++cp, switches)) {
233             case AMBIGSW: 
234                 ambigsw (cp, switches);
235                 done (1);
236             case UNKWNSW: 
237                 adios (NULL, "-%s unknown", cp);
238
239             case HELPSW: 
240                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
241                         invo_name);
242                 print_help (buf, switches, 1);
243                 done (0);
244             case VERSIONSW:
245                 print_version(invo_name);
246                 done (0);
247
248             case AUTOSW:
249                 autosw++;
250                 continue;
251             case NAUTOSW:
252                 autosw = 0;
253                 continue;
254
255             case CACHESW:
256                 cachesw++;
257                 continue;
258             case NCACHESW:
259                 cachesw = 0;
260                 continue;
261
262             case RCACHESW:
263                 icachesw = &rcachesw;
264                 goto do_cache;
265             case WCACHESW:
266                 icachesw = &wcachesw;
267 do_cache:
268                 if (!(cp = *argp++) || *cp == '-')
269                     adios (NULL, "missing argument to %s", argp[-2]);
270                 switch (*icachesw = smatch (cp, caches)) {
271                 case AMBIGSW:
272                     ambigsw (cp, caches);
273                     done (1);
274                 case UNKWNSW:
275                     adios (NULL, "%s unknown", cp);
276                 default:
277                     break;
278                 }
279                 continue;
280
281             case CHECKSW:
282                 checksw++;
283                 continue;
284             case NCHECKSW:
285                 checksw = 0;
286                 continue;
287
288             case HEADSW:
289                 headsw = 1;
290                 continue;
291             case NHEADSW:
292                 headsw = 0;
293                 continue;
294
295             case LISTSW:
296                 listsw = 1;
297                 continue;
298             case NLISTSW:
299                 listsw = 0;
300                 continue;
301
302             case PAUSESW:
303                 pausesw = 1;
304                 continue;
305             case NPAUSESW:
306                 pausesw = 0;
307                 continue;
308
309             case SERIALSW:
310                 serialsw = 1;
311                 continue;
312             case NSERIALSW:
313                 serialsw = 0;
314                 continue;
315
316             case SHOWSW:
317                 showsw = 1;
318                 continue;
319             case NSHOWSW:
320                 showsw = 0;
321                 continue;
322
323             case SIZESW:
324                 sizesw = 1;
325                 continue;
326             case NSIZESW:
327                 sizesw = 0;
328                 continue;
329
330             case STORESW:
331                 storesw = 1;
332                 continue;
333             case NSTORESW:
334                 storesw = 0;
335                 continue;
336
337             case PARTSW:
338                 if (!(cp = *argp++) || *cp == '-')
339                     adios (NULL, "missing argument to %s", argp[-2]);
340                 if (npart >= NPARTS)
341                     adios (NULL, "too many parts (starting with %s), %d max",
342                            cp, NPARTS);
343                 parts[npart++] = cp;
344                 continue;
345
346             case TYPESW:
347                 if (!(cp = *argp++) || *cp == '-')
348                     adios (NULL, "missing argument to %s", argp[-2]);
349                 if (ntype >= NTYPES)
350                     adios (NULL, "too many types (starting with %s), %d max",
351                            cp, NTYPES);
352                 types[ntype++] = cp;
353                 continue;
354
355             case FILESW:
356                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
357                     adios (NULL, "missing argument to %s", argp[-2]);
358                 file = *cp == '-' ? cp : path (cp, TFILE);
359                 continue;
360
361             case FORMSW:
362                 if (!(cp = *argp++) || *cp == '-')
363                     adios (NULL, "missing argument to %s", argp[-2]);
364                 if (formsw)
365                     free (formsw);
366                 formsw = getcpy (etcpath (cp));
367                 continue;
368
369             /*
370              * Switches for moreproc/mhlproc
371              */
372             case PROGSW:
373                 if (!(progsw = *argp++) || *progsw == '-')
374                     adios (NULL, "missing argument to %s", argp[-2]);
375                 continue;
376             case NPROGSW:
377                 nomore++;
378                 continue;
379
380             case LENSW:
381             case WIDTHSW:
382                 if (!(cp = *argp++) || *cp == '-')
383                     adios (NULL, "missing argument to %s", argp[-2]);
384                 continue;
385
386             /*
387              * Switches for mhbuild
388              */
389             case BUILDSW:
390                 buildsw = 1;
391                 continue;
392             case NBUILDSW:
393                 buildsw = 0;
394                 continue;
395             case RFC934SW:
396                 rfc934sw = 1;
397                 continue;
398             case NRFC934SW:
399                 rfc934sw = -1;
400                 continue;
401             case EBCDICSW:
402                 ebcdicsw = 1;
403                 continue;
404             case NEBCDICSW:
405                 ebcdicsw = -1;
406                 continue;
407
408             case VERBSW: 
409                 verbosw = 1;
410                 continue;
411             case NVERBSW: 
412                 verbosw = 0;
413                 continue;
414             case DEBUGSW:
415                 debugsw = 1;
416                 continue;
417             }
418         }
419         if (*cp == '+' || *cp == '@') {
420             if (folder)
421                 adios (NULL, "only one folder at a time!");
422             else
423                 folder = pluspath (cp);
424         } else
425                 app_msgarg(&msgs, cp);
426     }
427
428     /* null terminate the list of acceptable parts/types */
429     parts[npart] = NULL;
430     types[ntype] = NULL;
431
432     /*
433      * Check if we've specified an additional profile
434      */
435     if ((cp = getenv ("MHN"))) {
436         if ((fp = fopen (cp, "r"))) {
437             readconfig ((struct node **) 0, fp, cp, 0);
438             fclose (fp);
439         } else {
440             admonish ("", "unable to read $MHN profile (%s)", cp);
441         }
442     }
443
444     /*
445      * Read the standard profile setup
446      */
447     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
448         readconfig ((struct node **) 0, fp, cp, 0);
449         fclose (fp);
450     }
451
452     /* Check for public cache location */
453     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
454         cache_public = NULL;
455
456     /* Check for private cache location */
457     if (!(cache_private = context_find (nmhprivcache)))
458         cache_private = ".cache";
459     cache_private = getcpy (m_maildir (cache_private));
460
461     /*
462      * Cache the current directory before we do any chdirs()'s.
463      */
464     cwd = getcpy (pwd());
465
466     /*
467      * Check for storage directory.  If specified,
468      * then store temporary files there.  Else we
469      * store them in standard nmh directory.
470      */
471     if ((cp = context_find (nmhstorage)) && *cp)
472         tmp = concat (cp, "/", invo_name, NULL);
473     else
474         tmp = add (m_maildir (invo_name), NULL);
475
476     if (!context_find ("path"))
477         free (path ("./", TFOLDER));
478
479     /*
480      * Process a mhn composition file (mhn -build)
481      */
482     if (buildsw) {
483         char *vec[MAXARGS];
484         int vecp;
485
486         if (showsw || storesw || cachesw)
487             adios (NULL, "cannot use -build with -show, -store, -cache");
488         if (msgs.size < 1)
489             adios (NULL, "need to specify a %s composition file", invo_name);
490         if (msgs.size > 1)
491             adios (NULL, "only one %s composition file at a time", invo_name);
492
493         vecp = 0;
494         vec[vecp++] = "mhbuild";
495
496         if (ebcdicsw == 1)
497             vec[vecp++] = "-ebcdicsafe";
498         else if (ebcdicsw == -1)
499             vec[vecp++] = "-noebcdicsafe";
500
501         if (rfc934sw == 1)
502             vec[vecp++] = "-rfc934mode";
503         else if (rfc934sw == -1)
504             vec[vecp++] = "-norfc934mode";
505
506         vec[vecp++] = msgs.msgs[0];
507         vec[vecp] = NULL;
508
509         execvp ("mhbuild", vec);
510         fprintf (stderr, "unable to exec ");
511         _exit (-1);
512     }
513
514     /*
515      * Process a mhn composition file (old MH style)
516      */
517     if (msgs.size == 1 && !folder && !npart && !cachesw
518         && !showsw && !storesw && !ntype && !file
519         && (cp = getenv ("mhdraft"))
520         && strcmp (cp, msgs.msgs[0]) == 0) {
521
522         char *vec[MAXARGS];
523         int vecp;
524
525         vecp = 0;
526         vec[vecp++] = "mhbuild";
527
528         if (ebcdicsw == 1)
529             vec[vecp++] = "-ebcdicsafe";
530         else if (ebcdicsw == -1)
531             vec[vecp++] = "-noebcdicsafe";
532
533         if (rfc934sw == 1)
534             vec[vecp++] = "-rfc934mode";
535         else if (rfc934sw == -1)
536             vec[vecp++] = "-norfc934mode";
537
538         vec[vecp++] = cp;
539         vec[vecp] = NULL;
540
541         execvp ("mhbuild", vec);
542         fprintf (stderr, "unable to exec ");
543         _exit (-1);
544     }
545
546     if (file && msgs.size)
547         adios (NULL, "cannot specify msg and file at same time!");
548
549     /*
550      * check if message is coming from file
551      */
552     if (file) {
553         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
554             adios (NULL, "out of memory");
555         ctp = cts;
556
557         if ((ct = parse_mime (file)))
558             *ctp++ = ct;
559     } else {
560         /*
561          * message(s) are coming from a folder
562          */
563         if (!msgs.size)
564             app_msgarg(&msgs, "cur");
565         if (!folder)
566             folder = getfolder (1);
567         maildir = m_maildir (folder);
568
569         if (chdir (maildir) == NOTOK)
570             adios (maildir, "unable to change directory to");
571
572         /* read folder and create message structure */
573         if (!(mp = folder_read (folder)))
574             adios (NULL, "unable to read folder %s", folder);
575
576         /* check for empty folder */
577         if (mp->nummsg == 0)
578             adios (NULL, "no messages in %s", folder);
579
580         /* parse all the message ranges/sequences and set SELECTED */
581         for (msgnum = 0; msgnum < msgs.size; msgnum++)
582             if (!m_convert (mp, msgs.msgs[msgnum]))
583                 done (1);
584         seq_setprev (mp);       /* set the previous-sequence */
585
586         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
587             adios (NULL, "out of memory");
588         ctp = cts;
589
590         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
591             if (is_selected(mp, msgnum)) {
592                 char *msgnam;
593
594                 msgnam = m_name (msgnum);
595                 if ((ct = parse_mime (msgnam)))
596                     *ctp++ = ct;
597             }
598         }
599     }
600
601     if (!*cts)
602         done (1);
603
604     /*
605      * You can't give more than one of these flags
606      * at a time.
607      */
608     if (showsw + listsw + storesw + cachesw > 1)
609         adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
610
611     /* If no action is specified, assume -show */
612     if (!listsw && !showsw && !storesw && !cachesw)
613         showsw = 1;
614
615     userrs = 1;
616     SIGNAL (SIGQUIT, quitser);
617     SIGNAL (SIGPIPE, pipeser);
618
619     /*
620      * Get the associated umask for the relevant contents.
621      */
622     for (ctp = cts; *ctp; ctp++) {
623         struct stat st;
624
625         ct = *ctp;
626         if (type_ok (ct, 1) && !ct->c_umask) {
627             if (stat (ct->c_file, &st) != NOTOK)
628                 ct->c_umask = ~(st.st_mode & 0777);
629             else
630                 ct->c_umask = ~m_gmprot();
631         }
632     }
633
634     /*
635      * List the message content
636      */
637     if (listsw)
638         list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
639
640     /*
641      * Store the message content
642      */
643     if (storesw)
644         store_all_messages (cts);
645
646     /*
647      * Cache the message content
648      */
649     if (cachesw)
650         cache_all_messages (cts);
651
652     /*
653      * Show the message content
654      */
655     if (showsw)
656         show_all_messages (cts);
657
658     /* Now free all the structures for the content */
659     for (ctp = cts; *ctp; ctp++)
660         free_content (*ctp);
661
662     free ((char *) cts);
663     cts = NULL;
664
665     /* If reading from a folder, do some updating */
666     if (mp) {
667         context_replace (pfolder, folder);/* update current folder  */
668         seq_setcur (mp, mp->hghsel);      /* update current message */
669         seq_save (mp);                    /* synchronize sequences  */
670         context_save ();                  /* save the context file  */
671     }
672
673     done (0);
674     return 1;
675 }
676
677
678 static void
679 pipeser (int i)
680 {
681     if (i == SIGQUIT) {
682         unlink ("core");
683         fflush (stdout);
684         fprintf (stderr, "\n");
685         fflush (stderr);
686     }
687
688     done (1);
689     /* NOTREACHED */
690 }