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