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