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