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