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