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