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