860929303a31082b1c2d376795d418b335875f28
[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         if (mime)
276                 proc = showmimeproc;
277         else
278                 proc = showproc;
279
280         if (folder && !file)
281                 m_putenv("mhfolder", folder);
282
283         /* If the "proc" is "mhshow", add "-file" if showing file.  */
284         if (strcmp(mhbasename(proc), "mhshow") == 0 && file ) {
285            vec[vecp] = vec[vecp - 1];
286            vec[vecp - 1] = "-file";
287            vec[++vecp] = NULL;
288         }
289
290         /* Add the path to the message names. */
291         if (!file) {
292                 for (msgnum = procp; msgnum < vecp; msgnum++) {
293                         vec[msgnum] = concat(mp->foldpath, "/",
294                                         vec[msgnum], NULL);
295                 }
296         }
297
298         vec[0] = mhbasename(proc);
299         execvp(proc, vec);
300         adios(proc, "unable to exec");
301         return 0;  /* dead code to satisfy the compiler */
302 }
303
304 /*
305 ** Check if a message or file contains any non-text parts
306 */
307 static int
308 is_nontext(char *msgnam)
309 {
310         int result, state;
311         unsigned char *bp, *dp;
312         char *cp;
313         char buf[BUFSIZ], name[NAMESZ];
314         FILE *fp;
315
316         if ((fp = fopen(msgnam, "r")) == NULL)
317                 return 0;
318
319         for (state = FLD;;) {
320                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
321                 case FLD:
322                 case FLDPLUS:
323                 case FLDEOF:
324                         /*
325                         ** Check Content-Type field
326                         */
327                         if (!mh_strcasecmp(name, TYPE_FIELD)) {
328                                 int passno;
329                                 char c;
330
331                                 cp = getcpy(buf);
332                                 while (state == FLDPLUS) {
333                                         state = m_getfld(state, name, buf,
334                                                         sizeof(buf), fp);
335                                         cp = add(buf, cp);
336                                 }
337                                 bp = cp;
338                                 passno = 1;
339
340 again:
341                                 for (; isspace(*bp); bp++)
342                                         continue;
343                                 if (*bp == '(') {
344                                         int i;
345
346                                         for (bp++, i = 0;;) {
347                                                 switch (*bp++) {
348                                                 case '\0':
349 invalid:
350                                                         result = 0;
351                                                         goto out;
352                                                 case '\\':
353                                                         if (*bp++ == '\0')
354                                                                 goto invalid;
355                                                         continue;
356                                                 case '(':
357                                                         i++;
358                                                         /* and fall... */
359                                                 default:
360                                                         continue;
361                                                 case ')':
362                                                         if (--i < 0)
363                                                                 break;
364                                                         continue;
365                                                 }
366                                                 break;
367                                         }
368                                 }
369                                 if (passno == 2) {
370                                         if (*bp != '/')
371                                                 goto invalid;
372                                         bp++;
373                                         passno = 3;
374                                         goto again;
375                                 }
376                                 for (dp = bp; istoken(*dp); dp++)
377                                         continue;
378                                 c = *dp;
379                                 *dp = '\0';
380                                 if (!*bp)
381                                         goto invalid;
382                                 if (passno > 1) {
383                                         if ((result = (mh_strcasecmp(bp,
384                                                         "plain") != 0)))
385                                                 goto out;
386                                         *dp = c;
387                                         for (dp++; isspace(*dp); dp++)
388                                                 continue;
389                                         if (*dp) {
390                                                 if ((result = !uprf(dp,
391                                                                 "charset")))
392                                                         goto out;
393                                                 dp += sizeof("charset") - 1;
394                                                 while (isspace(*dp))
395                                                         dp++;
396                                                 if (*dp++ != '=')
397                                                         goto invalid;
398                                                 while (isspace(*dp))
399                                                         dp++;
400                                                 if (*dp == '"') {
401                                                         if ((bp = strchr(++dp, '"')))
402                                                                 *bp = '\0';
403                                                 } else {
404                                                         for (bp = dp; *bp; bp++)
405                                                                 if (!istoken(*bp)) {
406                                                                         *bp = '\0';
407                                                                         break;
408                                                                 }
409                                                 }
410                                         } else {
411                                                 /* Default character set */
412                                                 dp = "US-ASCII";
413                                         }
414                                         /* Check the character set */
415                                         result = !check_charset(dp, strlen(dp));
416                                 } else {
417                                         if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
418                                                 *dp = c;
419                                                 bp = dp;
420                                                 passno = 2;
421                                                 goto again;
422                                         }
423                                 }
424 out:
425                                 free(cp);
426                                 if (result) {
427                                         fclose(fp);
428                                         return result;
429                                 }
430                                 break;
431                         }
432
433                         /*
434                         ** Check Content-Transfer-Encoding field
435                         */
436                         if (!mh_strcasecmp(name, ENCODING_FIELD)) {
437                                 cp = getcpy(buf);
438                                 while (state == FLDPLUS) {
439                                         state = m_getfld(state, name, buf,
440                                                         sizeof(buf), fp);
441                                         cp = add(buf, cp);
442                                 }
443                                 for (bp = cp; isspace(*bp); bp++)
444                                         continue;
445                                 for (dp = bp; istoken(*dp); dp++)
446                                         continue;
447                                 *dp = '\0';
448                                 result = (mh_strcasecmp(bp, "7bit")
449                                            && mh_strcasecmp(bp, "8bit")
450                                            && mh_strcasecmp(bp, "binary"));
451
452                                 free(cp);
453                                 if (result) {
454                                         fclose(fp);
455                                         return result;
456                                 }
457                                 break;
458                         }
459
460                         /*
461                         ** Just skip the rest of this header
462                         ** field and go to next one.
463                         */
464                         while (state == FLDPLUS)
465                                 state = m_getfld(state, name, buf, sizeof(buf),
466                                                 fp);
467                         break;
468
469                         /*
470                         ** We've passed the message header,
471                         ** so message is just text.
472                         */
473                 default:
474                         fclose(fp);
475                         return 0;
476                 }
477         }
478 }