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