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