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