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