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