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