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