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