Remove RCS keywords, since they no longer work after git migration.
[mmh] / uip / mhshow.c
1
2 /*
3  * mhshow.c -- display 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 CHECKSW                 0
29     { "check", 0 },
30 #define NCHECKSW                1
31     { "nocheck", 0 },
32 #define PAUSESW                 2
33     { "pause", 0 },
34 #define NPAUSESW                3
35     { "nopause", 0 },
36 #define SERIALSW                4
37     { "serialonly", 0 },
38 #define NSERIALSW               5
39     { "noserialonly", 0 },
40 #define VERBSW                  6
41     { "verbose", 0 },
42 #define NVERBSW                 7
43     { "noverbose", 0 },
44 #define FILESW                  8       /* interface from show */
45     { "file file", 0 },
46 #define FORMSW                  9
47     { "form formfile", 0 },
48 #define PARTSW                 10
49     { "part number", 0 },
50 #define TYPESW                 11
51     { "type content", 0 },
52 #define RCACHESW               12
53     { "rcache policy", 0 },
54 #define WCACHESW               13
55     { "wcache policy", 0 },
56 #define VERSIONSW              14
57     { "version", 0 },
58 #define HELPSW                 15
59     { "help", 0 },
60
61 /*
62  * switches for moreproc/mhlproc
63  */
64 #define PROGSW                 16
65     { "moreproc program", -4 },
66 #define NPROGSW                17
67     { "nomoreproc", -3 },
68 #define LENSW                  18
69     { "length lines", -4 },
70 #define WIDTHSW                19
71     { "width columns", -4 },
72
73 /*
74  * switches for debugging
75  */
76 #define DEBUGSW                20
77     { "debug", -5 },
78     { NULL, 0 }
79 };
80
81
82 /* mhparse.c */
83 extern char *tmp;       /* directory to place temp files */
84
85 /* mhcachesbr.c */
86 extern int rcachesw;
87 extern int wcachesw;
88 extern char *cache_public;
89 extern char *cache_private;
90
91 /* mhshowsbr.c */
92 extern int pausesw;
93 extern int serialsw;
94 extern char *progsw;
95 extern int nolist;
96 extern int nomore;      /* flags for moreproc/header display */
97 extern char *formsw;
98
99 /* mhmisc.c */
100 extern int npart;
101 extern int ntype;
102 extern char *parts[NPARTS + 1];
103 extern char *types[NTYPES + 1];
104 extern int userrs;
105
106 int debugsw = 0;
107 int verbosw = 0;
108
109 #define quitser pipeser
110
111 /* mhparse.c */
112 CT parse_mime (char *);
113
114 /* mhmisc.c */
115 int part_ok (CT, int);
116 int type_ok (CT, int);
117 void set_endian (void);
118 void flush_errors (void);
119
120 /* mhshowsbr.c */
121 void show_all_messages (CT *);
122
123 /* mhfree.c */
124 void free_content (CT);
125 extern CT *cts;
126 void freects_done (int) NORETURN;
127
128 /*
129  * static prototypes
130  */
131 static RETSIGTYPE pipeser (int);
132
133
134 int
135 main (int argc, char **argv)
136 {
137     int msgnum, *icachesw;
138     char *cp, *file = NULL, *folder = NULL;
139     char *maildir, buf[100], **argp;
140     char **arguments;
141     struct msgs_array msgs = { 0, 0, NULL };
142     struct msgs *mp = NULL;
143     CT ct, *ctp;
144     FILE *fp;
145
146     done=freects_done;
147
148 #ifdef LOCALE
149     setlocale(LC_ALL, "");
150 #endif
151     invo_name = r1bindex (argv[0], '/');
152
153     /* read user profile/context */
154     context_read();
155
156     arguments = getarguments (invo_name, argc, argv, 1);
157     argp = arguments;
158
159     /*
160      * Parse arguments
161      */
162     while ((cp = *argp++)) {
163         if (*cp == '-') {
164             switch (smatch (++cp, switches)) {
165             case AMBIGSW: 
166                 ambigsw (cp, switches);
167                 done (1);
168             case UNKWNSW: 
169                 adios (NULL, "-%s unknown", cp);
170
171             case HELPSW: 
172                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
173                         invo_name);
174                 print_help (buf, switches, 1);
175                 done (1);
176             case VERSIONSW:
177                 print_version(invo_name);
178                 done (1);
179
180             case RCACHESW:
181                 icachesw = &rcachesw;
182                 goto do_cache;
183             case WCACHESW:
184                 icachesw = &wcachesw;
185 do_cache:
186                 if (!(cp = *argp++) || *cp == '-')
187                     adios (NULL, "missing argument to %s", argp[-2]);
188                 switch (*icachesw = smatch (cp, caches)) {
189                 case AMBIGSW:
190                     ambigsw (cp, caches);
191                     done (1);
192                 case UNKWNSW:
193                     adios (NULL, "%s unknown", cp);
194                 default:
195                     break;
196                 }
197                 continue;
198
199             case CHECKSW:
200                 checksw++;
201                 continue;
202             case NCHECKSW:
203                 checksw = 0;
204                 continue;
205
206             case PAUSESW:
207                 pausesw = 1;
208                 continue;
209             case NPAUSESW:
210                 pausesw = 0;
211                 continue;
212
213             case SERIALSW:
214                 serialsw = 1;
215                 continue;
216             case NSERIALSW:
217                 serialsw = 0;
218                 continue;
219
220             case PARTSW:
221                 if (!(cp = *argp++) || *cp == '-')
222                     adios (NULL, "missing argument to %s", argp[-2]);
223                 if (npart >= NPARTS)
224                     adios (NULL, "too many parts (starting with %s), %d max",
225                            cp, NPARTS);
226                 parts[npart++] = cp;
227                 continue;
228
229             case TYPESW:
230                 if (!(cp = *argp++) || *cp == '-')
231                     adios (NULL, "missing argument to %s", argp[-2]);
232                 if (ntype >= NTYPES)
233                     adios (NULL, "too many types (starting with %s), %d max",
234                            cp, NTYPES);
235                 types[ntype++] = cp;
236                 continue;
237
238             case FILESW:
239                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
240                     adios (NULL, "missing argument to %s", argp[-2]);
241                 file = *cp == '-' ? cp : path (cp, TFILE);
242                 continue;
243
244             case FORMSW:
245                 if (!(cp = *argp++) || *cp == '-')
246                     adios (NULL, "missing argument to %s", argp[-2]);
247                 if (formsw)
248                     free (formsw);
249                 formsw = getcpy (etcpath (cp));
250                 continue;
251
252             /*
253              * Switches for moreproc/mhlproc
254              */
255             case PROGSW:
256                 if (!(progsw = *argp++) || *progsw == '-')
257                     adios (NULL, "missing argument to %s", argp[-2]);
258                 continue;
259             case NPROGSW:
260                 nomore++;
261                 continue;
262
263             case LENSW:
264             case WIDTHSW:
265                 if (!(cp = *argp++) || *cp == '-')
266                     adios (NULL, "missing argument to %s", argp[-2]);
267                 continue;
268
269             case VERBSW: 
270                 verbosw = 1;
271                 continue;
272             case NVERBSW: 
273                 verbosw = 0;
274                 continue;
275             case DEBUGSW:
276                 debugsw = 1;
277                 continue;
278             }
279         }
280         if (*cp == '+' || *cp == '@') {
281             if (folder)
282                 adios (NULL, "only one folder at a time!");
283             else
284                 folder = pluspath (cp);
285         } else
286                 app_msgarg(&msgs, cp);
287     }
288
289     /* null terminate the list of acceptable parts/types */
290     parts[npart] = NULL;
291     types[ntype] = NULL;
292
293     set_endian ();
294
295     if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1")) {
296         nolist  = 1;
297         pausesw = 0;
298     }
299
300     /*
301      * Check if we've specified an additional profile
302      */
303     if ((cp = getenv ("MHSHOW"))) {
304         if ((fp = fopen (cp, "r"))) {
305             readconfig ((struct node **) 0, fp, cp, 0);
306             fclose (fp);
307         } else {
308             admonish ("", "unable to read $MHSHOW profile (%s)", cp);
309         }
310     }
311
312     /*
313      * Read the standard profile setup
314      */
315     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
316         readconfig ((struct node **) 0, fp, cp, 0);
317         fclose (fp);
318     }
319
320     /* Check for public cache location */
321     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
322         cache_public = NULL;
323
324     /* Check for private cache location */
325     if (!(cache_private = context_find (nmhprivcache)))
326         cache_private = ".cache";
327     cache_private = getcpy (m_maildir (cache_private));
328
329     /*
330      * Check for storage directory.  If specified,
331      * then store temporary files there.  Else we
332      * store them in standard nmh directory.
333      */
334     if ((cp = context_find (nmhstorage)) && *cp)
335         tmp = concat (cp, "/", invo_name, NULL);
336     else
337         tmp = add (m_maildir (invo_name), NULL);
338
339     if (!context_find ("path"))
340         free (path ("./", TFOLDER));
341
342     if (file && msgs.size)
343         adios (NULL, "cannot specify msg and file at same time!");
344
345     /*
346      * check if message is coming from file
347      */
348     if (file) {
349         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
350             adios (NULL, "out of memory");
351         ctp = cts;
352
353         if ((ct = parse_mime (file)));
354             *ctp++ = ct;
355     } else {
356         /*
357          * message(s) are coming from a folder
358          */
359         if (!msgs.size)
360             app_msgarg(&msgs, "cur");
361         if (!folder)
362             folder = getfolder (1);
363         maildir = m_maildir (folder);
364
365         if (chdir (maildir) == NOTOK)
366             adios (maildir, "unable to change directory to");
367
368         /* read folder and create message structure */
369         if (!(mp = folder_read (folder)))
370             adios (NULL, "unable to read folder %s", folder);
371
372         /* check for empty folder */
373         if (mp->nummsg == 0)
374             adios (NULL, "no messages in %s", folder);
375
376         /* parse all the message ranges/sequences and set SELECTED */
377         for (msgnum = 0; msgnum < msgs.size; msgnum++)
378             if (!m_convert (mp, msgs.msgs[msgnum]))
379                 done (1);
380
381         /*
382          * Set the SELECT_UNSEEN bit for all the SELECTED messages,
383          * since we will use that as a tag to know which messages
384          * to remove from the "unseen" sequence.
385          */
386         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
387             if (is_selected(mp, msgnum))
388                 set_unseen (mp, msgnum);
389
390         seq_setprev (mp);       /* set the Previous-Sequence */
391         seq_setunseen (mp, 1);  /* unset the Unseen-Sequence */
392
393         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
394             adios (NULL, "out of memory");
395         ctp = cts;
396
397         /*
398          * Parse all the SELECTED messages.
399          */
400         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
401             if (is_selected(mp, msgnum)) {
402                 char *msgnam;
403
404                 msgnam = m_name (msgnum);
405                 if ((ct = parse_mime (msgnam)))
406                     *ctp++ = ct;
407             }
408         }
409     }
410
411     if (!*cts)
412         done (1);
413
414     userrs = 1;
415     SIGNAL (SIGQUIT, quitser);
416     SIGNAL (SIGPIPE, pipeser);
417
418     /*
419      * Get the associated umask for the relevant contents.
420      */
421     for (ctp = cts; *ctp; ctp++) {
422         struct stat st;
423
424         ct = *ctp;
425         if (type_ok (ct, 1) && !ct->c_umask) {
426             if (stat (ct->c_file, &st) != NOTOK)
427                 ct->c_umask = ~(st.st_mode & 0777);
428             else
429                 ct->c_umask = ~m_gmprot();
430         }
431     }
432
433     /*
434      * Show the message content
435      */
436     show_all_messages (cts);
437
438     /* Now free all the structures for the content */
439     for (ctp = cts; *ctp; ctp++)
440         free_content (*ctp);
441
442     free ((char *) cts);
443     cts = NULL;
444
445     /* If reading from a folder, do some updating */
446     if (mp) {
447         context_replace (pfolder, folder);/* update current folder  */
448         seq_setcur (mp, mp->hghsel);      /* update current message */
449         seq_save (mp);                    /* synchronize sequences  */
450         context_save ();                  /* save the context file  */
451     }
452
453     done (0);
454     return 1;
455 }
456
457
458 static RETSIGTYPE
459 pipeser (int i)
460 {
461     if (i == SIGQUIT) {
462         unlink ("core");
463         fflush (stdout);
464         fprintf (stderr, "\n");
465         fflush (stderr);
466     }
467
468     done (1);
469     /* NOTREACHED */
470 }