Removed dead code within never set define WHATNOW.
[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         if (folder && !file)
254                 m_putenv("mhfolder", folder);
255
256         /* Special-cased because mhshow takes msg not files args. */
257         if (strcmp(mhbasename(proc), "mhshow")==0) {
258                 if (file) {
259                         vec[vecp] = vec[vecp - 1];
260                         vec[vecp - 1] = "-file";
261                         vec[++vecp] = NULL;
262                 }
263                 vec[vecp++] = concat("+", folder, NULL);
264                 vec[vecp] = NULL;
265                 vec[0] = mhbasename(proc);
266                 execvp(proc, vec);
267                 adios(proc, "unable to exec");
268         }
269
270         /* Add the path to the message names. */
271         if (!file) {
272                 for (msgnum = procp; msgnum < vecp; msgnum++) {
273                         vec[msgnum] = concat(mp->foldpath, "/",
274                                         vec[msgnum], NULL);
275                 }
276         }
277
278         vec[0] = mhbasename(proc);
279         execvp(proc, vec);
280         adios(proc, "unable to exec");
281         return 0;  /* dead code to satisfy the compiler */
282 }
283
284 /*
285 ** Check if a message or file contains any non-text parts
286 */
287 static int
288 is_nontext(char *msgnam)
289 {
290         int result, state;
291         unsigned char *bp, *dp;
292         char *cp;
293         char buf[BUFSIZ], name[NAMESZ];
294         FILE *fp;
295
296         if ((fp = fopen(msgnam, "r")) == NULL)
297                 return 0;
298
299         for (state = FLD;;) {
300                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
301                 case FLD:
302                 case FLDPLUS:
303                 case FLDEOF:
304                         /*
305                         ** Check Content-Type field
306                         */
307                         if (!mh_strcasecmp(name, TYPE_FIELD)) {
308                                 int passno = 1;
309                                 char c;
310
311                                 cp = getcpy(buf);
312                                 while (state == FLDPLUS) {
313                                         state = m_getfld(state, name, buf,
314                                                         sizeof(buf), fp);
315                                         cp = add(buf, cp);
316                                 }
317                                 bp = cp;
318
319 again:
320                                 for (; isspace(*bp); bp++)
321                                         continue;
322                                 if (*bp == '(') {
323                                         int i;
324
325                                         for (bp++, i = 0;;) {
326                                                 switch (*bp++) {
327                                                 case '\0':
328 invalid:
329                                                         result = 0;
330                                                         goto out;
331                                                 case '\\':
332                                                         if (*bp++ == '\0')
333                                                                 goto invalid;
334                                                         continue;
335                                                 case '(':
336                                                         i++;
337                                                         /* and fall... */
338                                                 default:
339                                                         continue;
340                                                 case ')':
341                                                         if (--i < 0)
342                                                                 break;
343                                                         continue;
344                                                 }
345                                                 break;
346                                         }
347                                 }
348                                 if (passno == 2) {
349                                         if (*bp != '/')
350                                                 goto invalid;
351                                         bp++;
352                                         passno = 3;
353                                         goto again;
354                                 }
355                                 for (dp = bp; istoken(*dp); dp++)
356                                         continue;
357                                 c = *dp;
358                                 *dp = '\0';
359                                 if (!*bp)
360                                         goto invalid;
361                                 if (passno == 1) {
362                                         if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
363                                                 *dp = c;
364                                                 bp = dp;
365                                                 passno = 2;
366                                                 goto again;
367                                         }
368                                 } else {
369                                         if ((result = (mh_strcasecmp(bp,
370                                                         "plain") != 0)))
371                                                 goto out;
372                                         *dp = c;
373                                         for (dp++; isspace(*dp); dp++)
374                                                 continue;
375                                         if (*dp) {
376                                                 if ((result = !uprf(dp,
377                                                                 "charset")))
378                                                         goto out;
379                                                 dp += sizeof("charset") - 1;
380                                                 while (isspace(*dp))
381                                                         dp++;
382                                                 if (*dp++ != '=')
383                                                         goto invalid;
384                                                 while (isspace(*dp))
385                                                         dp++;
386                                                 if (*dp == '"') {
387                                                         if ((bp = strchr(++dp, '"')))
388                                                                 *bp = '\0';
389                                                 } else {
390                                                         for (bp = dp; *bp; bp++)
391                                                                 if (!istoken(*bp)) {
392                                                                         *bp = '\0';
393                                                                         break;
394                                                                 }
395                                                 }
396                                         } else {
397                                                 /* Default character set */
398                                                 dp = "US-ASCII";
399                                         }
400                                         /* Check the character set */
401                                         result = !check_charset(dp, strlen(dp));
402                                 }
403 out:
404                                 free(cp);
405                                 if (result) {
406                                         fclose(fp);
407                                         return result;
408                                 }
409                                 break;
410                         }
411
412                         /*
413                         ** Check Content-Transfer-Encoding field
414                         */
415                         if (!mh_strcasecmp(name, ENCODING_FIELD)) {
416                                 cp = getcpy(buf);
417                                 while (state == FLDPLUS) {
418                                         state = m_getfld(state, name, buf,
419                                                         sizeof(buf), fp);
420                                         cp = add(buf, cp);
421                                 }
422                                 for (bp = cp; isspace(*bp); bp++)
423                                         continue;
424                                 for (dp = bp; istoken(*dp); dp++)
425                                         continue;
426                                 *dp = '\0';
427                                 result = (mh_strcasecmp(bp, "7bit")
428                                            && mh_strcasecmp(bp, "8bit")
429                                            && mh_strcasecmp(bp, "binary"));
430
431                                 free(cp);
432                                 if (result) {
433                                         fclose(fp);
434                                         return result;
435                                 }
436                                 break;
437                         }
438
439                         /*
440                         ** Just skip the rest of this header
441                         ** field and go to next one.
442                         */
443                         while (state == FLDPLUS)
444                                 state = m_getfld(state, name, buf, sizeof(buf),
445                                                 fp);
446                         break;
447
448                         /*
449                         ** We've passed the message header,
450                         ** so message is just text.
451                         */
452                 default:
453                         fclose(fp);
454                         return 0;
455                 }
456         }
457 }