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