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