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