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