38bb4b535597b41b455600bef31305325ec087db
[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         /*
291         ** For backward compatibility, if the "proc" is mhn,
292         ** then add "-show" option.  Add "-file" if showing
293         ** file.
294         */
295         if (strcmp(mhbasename(proc), "mhn") == 0) {
296                 if (file) {
297                         vec[vecp] = vec[vecp - 1];
298                         vec[vecp - 1] = "-file";
299                         vecp++;
300                 }
301                 vec[vecp++] = "-show";
302                 vec[vecp] = NULL;
303         }
304
305         /* If the "proc" is "mhshow", add "-file" if showing file.  */
306         if (strcmp(mhbasename(proc), "mhshow") == 0 && file ) {
307            vec[vecp] = vec[vecp - 1];
308            vec[vecp - 1] = "-file";
309            vec[++vecp] = NULL;
310         }
311
312         /*
313         ** If "proc" is mhl, then run it internally
314         ** rather than exec'ing it.
315         */
316         if (strcmp(mhbasename(proc), "mhl") == 0) {
317                 vec[0] = "mhl";
318                 mhl(vecp, vec);
319                 done(0);
320         }
321
322         /*
323         ** If you are not using a nmh command as your "proc", then
324         ** add the path to the message names.  Currently, we are just
325         ** checking for mhn here, since we've already taken care of mhl.
326         */
327         if (strcmp(mhbasename(proc), "mhl")==0
328                         && !file
329                         && chdir(maildir =
330                         concat(toabsdir("+"), "/", NULL)) != NOTOK) {
331                 mp->foldpath = concat(mp->foldpath, "/", NULL);
332                 cp = (strncmp(mp->foldpath, maildir, strlen(maildir))==0) ?
333                         mp->foldpath + strlen(maildir) :
334                         mp->foldpath;
335                 for (msgnum = procp; msgnum < vecp; msgnum++)
336                         vec[msgnum] = concat(cp, vec[msgnum], NULL);
337         }
338
339         vec[0] = mhbasename(proc);
340         execvp(proc, vec);
341         adios(proc, "unable to exec");
342         return 0;  /* dead code to satisfy the compiler */
343 }
344
345 /*
346 ** Check if a message or file contains any non-text parts
347 */
348 static int
349 is_nontext(char *msgnam)
350 {
351         int result, state;
352         unsigned char *bp, *dp;
353         char *cp;
354         char buf[BUFSIZ], name[NAMESZ];
355         FILE *fp;
356
357         if ((fp = fopen(msgnam, "r")) == NULL)
358                 return 0;
359
360         for (state = FLD;;) {
361                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
362                 case FLD:
363                 case FLDPLUS:
364                 case FLDEOF:
365                         /*
366                         ** Check Content-Type field
367                         */
368                         if (!mh_strcasecmp(name, TYPE_FIELD)) {
369                                 int passno;
370                                 char c;
371
372                                 cp = getcpy(buf);
373                                 while (state == FLDPLUS) {
374                                         state = m_getfld(state, name, buf,
375                                                         sizeof(buf), fp);
376                                         cp = add(buf, cp);
377                                 }
378                                 bp = cp;
379                                 passno = 1;
380
381 again:
382                                 for (; isspace(*bp); bp++)
383                                         continue;
384                                 if (*bp == '(') {
385                                         int i;
386
387                                         for (bp++, i = 0;;) {
388                                                 switch (*bp++) {
389                                                 case '\0':
390 invalid:
391                                                         result = 0;
392                                                         goto out;
393                                                 case '\\':
394                                                         if (*bp++ == '\0')
395                                                                 goto invalid;
396                                                         continue;
397                                                 case '(':
398                                                         i++;
399                                                         /* and fall... */
400                                                 default:
401                                                         continue;
402                                                 case ')':
403                                                         if (--i < 0)
404                                                                 break;
405                                                         continue;
406                                                 }
407                                                 break;
408                                         }
409                                 }
410                                 if (passno == 2) {
411                                         if (*bp != '/')
412                                                 goto invalid;
413                                         bp++;
414                                         passno = 3;
415                                         goto again;
416                                 }
417                                 for (dp = bp; istoken(*dp); dp++)
418                                         continue;
419                                 c = *dp;
420                                 *dp = '\0';
421                                 if (!*bp)
422                                         goto invalid;
423                                 if (passno > 1) {
424                                         if ((result = (mh_strcasecmp(bp,
425                                                         "plain") != 0)))
426                                                 goto out;
427                                         *dp = c;
428                                         for (dp++; isspace(*dp); dp++)
429                                                 continue;
430                                         if (*dp) {
431                                                 if ((result = !uprf(dp,
432                                                                 "charset")))
433                                                         goto out;
434                                                 dp += sizeof("charset") - 1;
435                                                 while (isspace(*dp))
436                                                         dp++;
437                                                 if (*dp++ != '=')
438                                                         goto invalid;
439                                                 while (isspace(*dp))
440                                                         dp++;
441                                                 if (*dp == '"') {
442                                                         if ((bp = strchr(++dp, '"')))
443                                                                 *bp = '\0';
444                                                 } else {
445                                                         for (bp = dp; *bp; bp++)
446                                                                 if (!istoken(*bp)) {
447                                                                         *bp = '\0';
448                                                                         break;
449                                                                 }
450                                                 }
451                                         } else {
452                                                 /* Default character set */
453                                                 dp = "US-ASCII";
454                                         }
455                                         /* Check the character set */
456                                         result = !check_charset(dp, strlen(dp));
457                                 } else {
458                                         if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
459                                                 *dp = c;
460                                                 bp = dp;
461                                                 passno = 2;
462                                                 goto again;
463                                         }
464                                 }
465 out:
466                                 free(cp);
467                                 if (result) {
468                                         fclose(fp);
469                                         return result;
470                                 }
471                                 break;
472                         }
473
474                         /*
475                         ** Check Content-Transfer-Encoding field
476                         */
477                         if (!mh_strcasecmp(name, ENCODING_FIELD)) {
478                                 cp = getcpy(buf);
479                                 while (state == FLDPLUS) {
480                                         state = m_getfld(state, name, buf,
481                                                         sizeof(buf), fp);
482                                         cp = add(buf, cp);
483                                 }
484                                 for (bp = cp; isspace(*bp); bp++)
485                                         continue;
486                                 for (dp = bp; istoken(*dp); dp++)
487                                         continue;
488                                 *dp = '\0';
489                                 result = (mh_strcasecmp(bp, "7bit")
490                                            && mh_strcasecmp(bp, "8bit")
491                                            && mh_strcasecmp(bp, "binary"));
492
493                                 free(cp);
494                                 if (result) {
495                                         fclose(fp);
496                                         return result;
497                                 }
498                                 break;
499                         }
500
501                         /*
502                         ** Just skip the rest of this header
503                         ** field and go to next one.
504                         */
505                         while (state == FLDPLUS)
506                                 state = m_getfld(state, name, buf, sizeof(buf),
507                                                 fp);
508                         break;
509
510                         /*
511                         ** We've passed the message header,
512                         ** so message is just text.
513                         */
514                 default:
515                         fclose(fp);
516                         return 0;
517                 }
518         }
519 }