4abc3d74cf93a62a3b5856b1c55a3006d1c1a7e3
[mmh] / uip / show.c
1 /*
2 ** show.c -- show/list 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 <h/mime.h>
11 #include <h/utils.h>
12
13 static struct swit switches[] = {
14 #define CHECKMIMESW  0
15         { "checkmime", 0 },
16 #define NOCHECKMIMESW  1
17         { "nocheckmime", 0 },
18 #define HEADSW  2
19         { "header", 0 },
20 #define NHEADSW  3
21         { "noheader", 0 },
22 #define FORMSW  4
23         { "form formfile", 0 },
24 #define SHOWSW  5
25         { "showproc program", 0 },
26 #define SHOWMIMESW  6
27         { "showmimeproc program", 0 },
28 #define FILESW  7
29         { "file file", -4 },  /* interface from whatnow (listproc) */
30 #define VERSIONSW  8
31         { "version", 0 },
32 #define HELPSW  9
33         { "help", 0 },
34         { NULL, 0 }
35 };
36
37 /*
38 ** static prototypes
39 */
40 static int is_nontext(char *);
41
42 #define SHOW  0
43 #define NEXT  1
44 #define PREV  2
45
46 char *showproc = "mhl";
47 char *showmimeproc = "mhshow";
48
49 int
50 main(int argc, char **argv)
51 {
52         int headersw = 1, msgp = 0;
53         int checkmime = 1, mime;
54         int vecp = 1, procp = 1, mode = SHOW, msgnum;
55         char *cp, *file = NULL, *folder = NULL, *proc;
56         char buf[BUFSIZ], **argp, **arguments;
57         char *msgs[MAXARGS], *vec[MAXARGS];
58         struct msgs *mp = NULL;
59
60 #ifdef LOCALE
61         setlocale(LC_ALL, "");
62 #endif
63         invo_name = mhbasename(argv[0]);
64
65         /* read user profile/context */
66         context_read();
67
68         if (!mh_strcasecmp(invo_name, "next")) {
69                 mode = NEXT;
70         } else if (!mh_strcasecmp(invo_name, "prev")) {
71                 mode = PREV;
72         }
73         arguments = getarguments(invo_name, argc, argv, 1);
74         argp = arguments;
75
76         while ((cp = *argp++)) {
77                 if (*cp == '-') {
78                         switch (smatch(++cp, switches)) {
79                         case AMBIGSW:
80                                 ambigsw(cp, switches);
81                                 done(1);
82                         case UNKWNSW:
83                                 vec[vecp++] = --cp;
84                                 continue;
85
86                         case HELPSW:
87                                 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
88                                 print_help(buf, switches, 1);
89                                 done(1);
90                         case VERSIONSW:
91                                 print_version(invo_name);
92                                 done(1);
93
94                         case FILESW:
95                                 if (mode != SHOW)
96 usage:
97                                         adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
98
99                                 if (file)
100                                         adios(NULL, "only one file at a time!");
101                                 if (!(cp = *argp++) || *cp == '-')
102                                         adios(NULL, "missing argument to %s", argp[-2]);
103                                 file = getcpy(expanddir(cp));
104                                 continue;
105
106                         case HEADSW:
107                                 headersw++;
108                                 continue;
109                         case NHEADSW:
110                                 headersw = 0;
111                                 continue;
112
113                         case FORMSW:
114                                 vec[vecp++] = --cp;
115                                 if (!(cp = *argp++) || *cp == '-')
116                                         adios(NULL, "missing argument to %s",
117                                                         argp[-2]);
118                                 vec[vecp++] = getcpy(etcpath(cp));
119                                 continue;
120
121                         case SHOWSW:
122                                 if (!(showproc = *argp++) || *showproc == '-')
123                                         adios(NULL, "missing argument to %s",
124                                                         argp[-2]);
125                                 continue;
126
127                         case SHOWMIMESW:
128                                 if (!(showmimeproc = *argp++) ||
129                                                 *showmimeproc == '-')
130                                         adios(NULL, "missing argument to %s",
131                                                         argp[-2]);
132                                 continue;
133                         case CHECKMIMESW:
134                                 checkmime++;
135                                 continue;
136                         case NOCHECKMIMESW:
137                                 checkmime = 0;
138                                 continue;
139                         }
140                 }
141                 if (*cp == '+' || *cp == '@') {
142                         if (folder)
143                                 adios(NULL, "only one folder at a time!");
144                         else
145                                 folder = getcpy(expandfol(cp));
146                 } else if (mode != SHOW) {
147                         goto usage;
148                 } else {
149                         msgs[msgp++] = cp;
150                 }
151         }
152         procp = vecp;
153
154         if (file) {
155                 if (msgp)
156                         adios(NULL, "only one file at a time!");
157                 vec[vecp++] = file;
158                 goto go_to_it;
159         }
160
161 #ifdef WHATNOW
162         if (!msgp && !folder && mode == SHOW &&
163                         (cp = getenv("mhdraft")) && *cp) {
164                 vec[vecp++] = cp;
165                 goto go_to_it;
166         }
167 #endif /* WHATNOW */
168
169         if (!msgp) {
170                 switch (mode) {
171                 case NEXT:
172                         msgs[msgp++] = seq_next;
173                         break;
174                 case PREV:
175                         msgs[msgp++] = seq_prev;
176                         break;
177                 default:
178                         msgs[msgp++] = seq_cur;
179                         break;
180                 }
181         }
182
183         if (!folder)
184                 folder = getcurfol();
185
186         /* read folder and create message structure */
187         if (!(mp = folder_read(folder)))
188                 adios(NULL, "unable to read folder %s", folder);
189
190         /* check for empty folder */
191         if (mp->nummsg == 0)
192                 adios(NULL, "no messages in %s", folder);
193
194         /* parse all the message ranges/sequences and set SELECTED */
195         for (msgnum = 0; msgnum < msgp; msgnum++)
196                 if (!m_convert(mp, msgs[msgnum]))
197                         done(1);
198
199         /*
200         ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
201         ** since we will use that as a tag to know which messages
202         ** to remove from the "unseen" sequence.
203         */
204         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
205                 if (is_selected(mp, msgnum))
206                         set_unseen(mp, msgnum);
207
208         seq_setprev(mp);  /* set the Previous-Sequence */
209         seq_setunseen(mp, 0);  /* unset unseen seqs for shown msgs */
210
211         if (mp->numsel > MAXARGS - 2)
212                 adios(NULL, "more than %d messages for show exec",
213                                 MAXARGS - 2);
214
215         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
216                 if (is_selected(mp, msgnum))
217                         vec[vecp++] = getcpy(m_name(msgnum));
218
219         seq_setcur(mp, mp->hghsel);  /* update current message  */
220         seq_save(mp);  /* synchronize sequences   */
221         context_replace(curfolder, folder);  /* update current folder   */
222         context_save();  /* save the context file   */
223
224         if (headersw && vecp == 2)
225                 printf("(Message %s:%s)\n", folder, vec[1]);
226
227 go_to_it: ;
228         fflush(stdout);
229
230         vec[vecp] = NULL;
231
232         /*
233         ** Decide which "proc" to use
234         */
235         mime = 0;
236         /* check if any messages are non-text MIME messages */
237         if (checkmime) {
238                 if (file) {
239                         /* check the file for MIME */
240                         if (is_nontext(vec[vecp - 1])) {
241                                 mime = 1;
242                         }
243                 } else {
244                         /*
245                         ** loop through selected messages
246                         ** and check for MIME
247                         */
248                         for (msgnum = mp->lowsel; msgnum <= mp->hghsel;
249                                         msgnum++)
250                                 if (is_selected(mp, msgnum) &&
251                                                 is_nontext(m_name(msgnum))) {
252                                         mime = 1;
253                                         break;
254                                 }
255                 }
256         }
257
258         /* Set the "proc" */
259         proc = (mime) ? showmimeproc : showproc;
260
261         if (folder && !file)
262                 m_putenv("mhfolder", folder);
263
264         /* Special-cased because mhshow takes msg not files args. */
265         if (strcmp(mhbasename(proc), "mhshow")==0) {
266                 if (file) {
267                         vec[vecp] = vec[vecp - 1];
268                         vec[vecp - 1] = "-file";
269                         vec[++vecp] = NULL;
270                 }
271                 vec[vecp++] = concat("+", folder, NULL);
272                 vec[vecp] = NULL;
273                 vec[0] = mhbasename(proc);
274                 execvp(proc, vec);
275                 adios(proc, "unable to exec");
276         }
277
278         /* Add the path to the message names. */
279         if (!file) {
280                 for (msgnum = procp; msgnum < vecp; msgnum++) {
281                         vec[msgnum] = concat(mp->foldpath, "/",
282                                         vec[msgnum], NULL);
283                 }
284         }
285
286         vec[0] = mhbasename(proc);
287         execvp(proc, vec);
288         adios(proc, "unable to exec");
289         return 0;  /* dead code to satisfy the compiler */
290 }
291
292 /*
293 ** Check if a message or file contains any non-text parts
294 */
295 static int
296 is_nontext(char *msgnam)
297 {
298         int result, state;
299         unsigned char *bp, *dp;
300         char *cp;
301         char buf[BUFSIZ], name[NAMESZ];
302         FILE *fp;
303
304         if ((fp = fopen(msgnam, "r")) == NULL)
305                 return 0;
306
307         for (state = FLD;;) {
308                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
309                 case FLD:
310                 case FLDPLUS:
311                 case FLDEOF:
312                         /*
313                         ** Check Content-Type field
314                         */
315                         if (!mh_strcasecmp(name, TYPE_FIELD)) {
316                                 int passno = 1;
317                                 char c;
318
319                                 cp = getcpy(buf);
320                                 while (state == FLDPLUS) {
321                                         state = m_getfld(state, name, buf,
322                                                         sizeof(buf), fp);
323                                         cp = add(buf, cp);
324                                 }
325                                 bp = cp;
326
327 again:
328                                 for (; isspace(*bp); bp++)
329                                         continue;
330                                 if (*bp == '(') {
331                                         int i;
332
333                                         for (bp++, i = 0;;) {
334                                                 switch (*bp++) {
335                                                 case '\0':
336 invalid:
337                                                         result = 0;
338                                                         goto out;
339                                                 case '\\':
340                                                         if (*bp++ == '\0')
341                                                                 goto invalid;
342                                                         continue;
343                                                 case '(':
344                                                         i++;
345                                                         /* and fall... */
346                                                 default:
347                                                         continue;
348                                                 case ')':
349                                                         if (--i < 0)
350                                                                 break;
351                                                         continue;
352                                                 }
353                                                 break;
354                                         }
355                                 }
356                                 if (passno == 2) {
357                                         if (*bp != '/')
358                                                 goto invalid;
359                                         bp++;
360                                         passno = 3;
361                                         goto again;
362                                 }
363                                 for (dp = bp; istoken(*dp); dp++)
364                                         continue;
365                                 c = *dp;
366                                 *dp = '\0';
367                                 if (!*bp)
368                                         goto invalid;
369                                 if (passno == 1) {
370                                         if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
371                                                 *dp = c;
372                                                 bp = dp;
373                                                 passno = 2;
374                                                 goto again;
375                                         }
376                                 } else {
377                                         if ((result = (mh_strcasecmp(bp,
378                                                         "plain") != 0)))
379                                                 goto out;
380                                         *dp = c;
381                                         for (dp++; isspace(*dp); dp++)
382                                                 continue;
383                                         if (*dp) {
384                                                 if ((result = !uprf(dp,
385                                                                 "charset")))
386                                                         goto out;
387                                                 dp += sizeof("charset") - 1;
388                                                 while (isspace(*dp))
389                                                         dp++;
390                                                 if (*dp++ != '=')
391                                                         goto invalid;
392                                                 while (isspace(*dp))
393                                                         dp++;
394                                                 if (*dp == '"') {
395                                                         if ((bp = strchr(++dp, '"')))
396                                                                 *bp = '\0';
397                                                 } else {
398                                                         for (bp = dp; *bp; bp++)
399                                                                 if (!istoken(*bp)) {
400                                                                         *bp = '\0';
401                                                                         break;
402                                                                 }
403                                                 }
404                                         } else {
405                                                 /* Default character set */
406                                                 dp = "US-ASCII";
407                                         }
408                                         /* Check the character set */
409                                         result = !check_charset(dp, strlen(dp));
410                                 }
411 out:
412                                 free(cp);
413                                 if (result) {
414                                         fclose(fp);
415                                         return result;
416                                 }
417                                 break;
418                         }
419
420                         /*
421                         ** Check Content-Transfer-Encoding field
422                         */
423                         if (!mh_strcasecmp(name, ENCODING_FIELD)) {
424                                 cp = getcpy(buf);
425                                 while (state == FLDPLUS) {
426                                         state = m_getfld(state, name, buf,
427                                                         sizeof(buf), fp);
428                                         cp = add(buf, cp);
429                                 }
430                                 for (bp = cp; isspace(*bp); bp++)
431                                         continue;
432                                 for (dp = bp; istoken(*dp); dp++)
433                                         continue;
434                                 *dp = '\0';
435                                 result = (mh_strcasecmp(bp, "7bit")
436                                            && mh_strcasecmp(bp, "8bit")
437                                            && mh_strcasecmp(bp, "binary"));
438
439                                 free(cp);
440                                 if (result) {
441                                         fclose(fp);
442                                         return result;
443                                 }
444                                 break;
445                         }
446
447                         /*
448                         ** Just skip the rest of this header
449                         ** field and go to next one.
450                         */
451                         while (state == FLDPLUS)
452                                 state = m_getfld(state, name, buf, sizeof(buf),
453                                                 fp);
454                         break;
455
456                         /*
457                         ** We've passed the message header,
458                         ** so message is just text.
459                         */
460                 default:
461                         fclose(fp);
462                         return 0;
463                 }
464         }
465 }