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