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