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