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