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