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