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