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