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