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