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