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