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