Generate a From: header, using localmbox, if the user doesn't provide
[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     set_endian ();
289
290     /*
291      * Check if we've specified an additional profile
292      */
293     if ((cp = getenv ("MHSHOW"))) {
294         if ((fp = fopen (cp, "r"))) {
295             readconfig ((struct node **) 0, fp, cp, 0);
296             fclose (fp);
297         } else {
298             admonish ("", "unable to read $MHSHOW profile (%s)", cp);
299         }
300     }
301
302     /*
303      * Read the standard profile setup
304      */
305     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
306         readconfig ((struct node **) 0, fp, cp, 0);
307         fclose (fp);
308     }
309
310     /* Check for public cache location */
311     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
312         cache_public = NULL;
313
314     /* Check for private cache location */
315     if (!(cache_private = context_find (nmhprivcache)))
316         cache_private = ".cache";
317     cache_private = getcpy (m_maildir (cache_private));
318
319     /*
320      * Check for storage directory.  If specified,
321      * then store temporary files there.  Else we
322      * store them in standard nmh directory.
323      */
324     if ((cp = context_find (nmhstorage)) && *cp)
325         tmp = concat (cp, "/", invo_name, NULL);
326     else
327         tmp = add (m_maildir (invo_name), NULL);
328
329     if (!context_find ("path"))
330         free (path ("./", TFOLDER));
331
332     if (file && msgs.size)
333         adios (NULL, "cannot specify msg and file at same time!");
334
335     /*
336      * check if message is coming from file
337      */
338     if (file) {
339         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
340             adios (NULL, "out of memory");
341         ctp = cts;
342
343         if ((ct = parse_mime (file)))
344             *ctp++ = ct;
345     } else {
346         /*
347          * message(s) are coming from a folder
348          */
349         if (!msgs.size)
350             app_msgarg(&msgs, "cur");
351         if (!folder)
352             folder = getfolder (1);
353         maildir = m_maildir (folder);
354
355         if (chdir (maildir) == NOTOK)
356             adios (maildir, "unable to change directory to");
357
358         /* read folder and create message structure */
359         if (!(mp = folder_read (folder)))
360             adios (NULL, "unable to read folder %s", folder);
361
362         /* check for empty folder */
363         if (mp->nummsg == 0)
364             adios (NULL, "no messages in %s", folder);
365
366         /* parse all the message ranges/sequences and set SELECTED */
367         for (msgnum = 0; msgnum < msgs.size; msgnum++)
368             if (!m_convert (mp, msgs.msgs[msgnum]))
369                 done (1);
370
371         /*
372          * Set the SELECT_UNSEEN bit for all the SELECTED messages,
373          * since we will use that as a tag to know which messages
374          * to remove from the "unseen" sequence.
375          */
376         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
377             if (is_selected(mp, msgnum))
378                 set_unseen (mp, msgnum);
379
380         seq_setprev (mp);       /* set the Previous-Sequence */
381         seq_setunseen (mp, 1);  /* unset the Unseen-Sequence */
382
383         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
384             adios (NULL, "out of memory");
385         ctp = cts;
386
387         /*
388          * Parse all the SELECTED messages.
389          */
390         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
391             if (is_selected(mp, msgnum)) {
392                 char *msgnam;
393
394                 msgnam = m_name (msgnum);
395                 if ((ct = parse_mime (msgnam)))
396                     *ctp++ = ct;
397             }
398         }
399     }
400
401     if (!*cts)
402         done (1);
403
404     userrs = 1;
405     SIGNAL (SIGQUIT, quitser);
406     SIGNAL (SIGPIPE, pipeser);
407
408     /*
409      * Get the associated umask for the relevant contents.
410      */
411     for (ctp = cts; *ctp; ctp++) {
412         struct stat st;
413
414         ct = *ctp;
415         if (type_ok (ct, 1) && !ct->c_umask) {
416             if (stat (ct->c_file, &st) != NOTOK)
417                 ct->c_umask = ~(st.st_mode & 0777);
418             else
419                 ct->c_umask = ~m_gmprot();
420         }
421     }
422
423     /* If reading from a folder, do some updating */
424     if (mp) {
425         context_replace (pfolder, folder);/* update current folder  */
426         seq_setcur (mp, mp->hghsel);      /* update current message */
427         seq_save (mp);                    /* synchronize sequences  */
428         context_save ();                  /* save the context file  */
429     }
430
431     /*
432      * Show the message content
433      */
434     show_all_messages (cts);
435
436     /* Now free all the structures for the content */
437     for (ctp = cts; *ctp; ctp++)
438         free_content (*ctp);
439
440     free ((char *) cts);
441     cts = NULL;
442
443     done (0);
444     return 1;
445 }
446
447
448 static void
449 pipeser (int i)
450 {
451     if (i == SIGQUIT) {
452         unlink ("core");
453         fflush (stdout);
454         fprintf (stderr, "\n");
455         fflush (stderr);
456     }
457
458     done (1);
459     /* NOTREACHED */
460 }