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