]> diplodocus.org Git - nmh/blob - uip/show.c
A few more bugs I didn't catch earlier.
[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 static struct swit switches[] = {
15 #define CHECKMIMESW 0
16 { "checkmime", 0 },
17 #define NOCHECKMIMESW 1
18 { "nocheckmime", 0 },
19 #define HEADSW 2
20 { "header", 0 },
21 #define NHEADSW 3
22 { "noheader", 0 },
23 #define FORMSW 4
24 { "form formfile", 0 },
25 #define PROGSW 5
26 { "moreproc program", 0 },
27 #define NPROGSW 6
28 { "nomoreproc", 0 },
29 #define LENSW 7
30 { "length lines", 0 },
31 #define WIDTHSW 8
32 { "width columns", 0 },
33 #define SHOWSW 9
34 { "showproc program", 0 },
35 #define SHOWMIMESW 10
36 { "showmimeproc program", 0 },
37 #define NSHOWSW 11
38 { "noshowproc", 0 },
39 #define DRFTSW 12
40 { "draft", 0 },
41 #define FILESW 13
42 { "file file", -4 }, /* interface from showfile */
43 #define VERSIONSW 14
44 { "version", 0 },
45 #define HELPSW 15
46 { "help", 0 },
47 { NULL, 0 }
48 };
49
50 /*
51 * static prototypes
52 */
53 static int is_nontext(char *);
54
55 #define SHOW 0
56 #define NEXT 1
57 #define PREV 2
58
59
60 int
61 main (int argc, char **argv)
62 {
63 int draftsw = 0, headersw = 1, msgp = 0;
64 int nshow = 0, checkmime = 1, mime;
65 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
66 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
67 char buf[BUFSIZ], **argp, **arguments;
68 char *msgs[MAXARGS], *vec[MAXARGS];
69 struct msgs *mp = NULL;
70
71 #ifdef LOCALE
72 setlocale(LC_ALL, "");
73 #endif
74 invo_name = r1bindex (argv[0], '/');
75
76 /* read user profile/context */
77 context_read();
78
79 if (!mh_strcasecmp (invo_name, "next")) {
80 mode = NEXT;
81 } else if (!mh_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 case UNKWNSW:
94 case NPROGSW:
95 vec[vecp++] = --cp;
96 continue;
97
98 case HELPSW:
99 snprintf (buf, sizeof(buf),
100 "%s [+folder] %s[switches] [switches for showproc]",
101 invo_name, mode == SHOW ? "[msgs] ": "");
102 print_help (buf, switches, 1);
103 done (1);
104 case VERSIONSW:
105 print_version(invo_name);
106 done (1);
107
108 case DRFTSW:
109 if (file)
110 adios (NULL, "only one file at a time!");
111 draftsw++;
112 if (mode == SHOW)
113 continue;
114 usage:
115 adios (NULL,
116 "usage: %s [+folder] [switches] [switches for showproc]",
117 invo_name);
118 case FILESW:
119 if (mode != SHOW)
120 goto usage;
121 if (draftsw || file)
122 adios (NULL, "only one file at a time!");
123 if (!(cp = *argp++) || *cp == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
125 file = path (cp, TFILE);
126 continue;
127
128 case HEADSW:
129 headersw++;
130 continue;
131 case NHEADSW:
132 headersw = 0;
133 continue;
134
135 case FORMSW:
136 vec[vecp++] = --cp;
137 if (!(cp = *argp++) || *cp == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
139 vec[vecp++] = getcpy (etcpath(cp));
140 continue;
141
142 case PROGSW:
143 case LENSW:
144 case WIDTHSW:
145 vec[vecp++] = --cp;
146 if (!(cp = *argp++) || *cp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
148 vec[vecp++] = cp;
149 continue;
150
151 case SHOWSW:
152 if (!(showproc = *argp++) || *showproc == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
154 nshow = 0;
155 continue;
156 case NSHOWSW:
157 nshow++;
158 continue;
159
160 case SHOWMIMESW:
161 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 nshow = 0;
164 continue;
165 case CHECKMIMESW:
166 checkmime++;
167 continue;
168 case NOCHECKMIMESW:
169 checkmime = 0;
170 continue;
171 }
172 }
173 if (*cp == '+' || *cp == '@') {
174 if (folder)
175 adios (NULL, "only one folder at a time!");
176 else
177 folder = pluspath (cp);
178 } else {
179 if (mode != SHOW)
180 goto usage;
181 else
182 msgs[msgp++] = cp;
183 }
184 }
185 procp = vecp;
186
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER));
189
190 if (draftsw || file) {
191 if (msgp)
192 adios (NULL, "only one file at a time!");
193 vec[vecp++] = draftsw
194 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
195 : file;
196 goto go_to_it;
197 }
198
199 #ifdef WHATNOW
200 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
201 draftsw++;
202 vec[vecp++] = cp;
203 goto go_to_it;
204 }
205 #endif /* WHATNOW */
206
207 if (!msgp) {
208 switch (mode) {
209 case NEXT:
210 msgs[msgp++] = "next";
211 break;
212 case PREV:
213 msgs[msgp++] = "prev";
214 break;
215 default:
216 msgs[msgp++] = "cur";
217 break;
218 }
219 }
220
221 if (!folder)
222 folder = getfolder (1);
223 maildir = m_maildir (folder);
224
225 if (chdir (maildir) == NOTOK)
226 adios (maildir, "unable to change directory to");
227
228 /* read folder and create message structure */
229 if (!(mp = folder_read (folder)))
230 adios (NULL, "unable to read folder %s", folder);
231
232 /* check for empty folder */
233 if (mp->nummsg == 0)
234 adios (NULL, "no messages in %s", folder);
235
236 /* parse all the message ranges/sequences and set SELECTED */
237 for (msgnum = 0; msgnum < msgp; msgnum++)
238 if (!m_convert (mp, msgs[msgnum]))
239 done (1);
240
241 /*
242 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
243 * since we will use that as a tag to know which messages
244 * to remove from the "unseen" sequence.
245 */
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
247 if (is_selected(mp, msgnum))
248 set_unseen (mp, msgnum);
249
250 seq_setprev (mp); /* set the Previous-Sequence */
251 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
252
253 if (mp->numsel > MAXARGS - 2)
254 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
255
256 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
257 if (is_selected(mp, msgnum))
258 vec[vecp++] = getcpy (m_name (msgnum));
259
260 seq_setcur (mp, mp->hghsel); /* update current message */
261 seq_save (mp); /* synchronize sequences */
262 context_replace (pfolder, folder); /* update current folder */
263 context_save (); /* save the context file */
264
265 if (headersw && vecp == 2)
266 printf ("(Message %s:%s)\n", folder, vec[1]);
267
268 go_to_it: ;
269 fflush (stdout);
270
271 vec[vecp] = NULL;
272
273 /*
274 * Decide which "proc" to use
275 */
276 mime = 0;
277 if (nshow) {
278 proc = catproc;
279 } else {
280 /* check if any messages are non-text MIME messages */
281 if (checkmime && !getenv ("NOMHNPROC")) {
282 if (!draftsw && !file) {
283 /* loop through selected messages and check for MIME */
284 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
285 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
286 mime = 1;
287 break;
288 }
289 } else {
290 /* check the file or draft for MIME */
291 if (is_nontext (vec[vecp - 1]))
292 mime = 1;
293 }
294 }
295
296 /* Set the "proc" */
297 if (mime)
298 proc = showmimeproc;
299 else
300 proc = showproc;
301 }
302
303 if (folder && !draftsw && !file)
304 m_putenv ("mhfolder", folder);
305
306 /*
307 * For backward compatibility, if the "proc" is mhn,
308 * then add "-show" option. Add "-file" if showing
309 * file or draft.
310 */
311 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
312 if (draftsw || file) {
313 vec[vecp] = vec[vecp - 1];
314 vec[vecp - 1] = "-file";
315 vecp++;
316 }
317 vec[vecp++] = "-show";
318 vec[vecp] = NULL;
319 }
320
321 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
322 */
323 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
324 vec[vecp] = vec[vecp - 1];
325 vec[vecp - 1] = "-file";
326 vec[++vecp] = NULL;
327 }
328
329 /*
330 * If "proc" is mhl, then run it internally
331 * rather than exec'ing it.
332 */
333 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
334 vec[0] = "mhl";
335 mhl (vecp, vec);
336 done (0);
337 }
338
339 /*
340 * If you are not using a nmh command as your "proc", then
341 * add the path to the message names. Currently, we are just
342 * checking for mhn here, since we've already taken care of mhl.
343 */
344 if (!strcmp (r1bindex (proc, '/'), "mhl")
345 && !draftsw
346 && !file
347 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
348 mp->foldpath = concat (mp->foldpath, "/", NULL);
349 cp = ssequal (maildir, mp->foldpath)
350 ? mp->foldpath + strlen (maildir)
351 : mp->foldpath;
352 for (msgnum = procp; msgnum < vecp; msgnum++)
353 vec[msgnum] = concat (cp, vec[msgnum], NULL);
354 }
355
356 vec[0] = r1bindex (proc, '/');
357 execvp (proc, vec);
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 unsigned char *bp, *dp;
371 char *cp;
372 char buf[BUFSIZ], name[NAMESZ];
373 FILE *fp;
374
375 if ((fp = fopen (msgnam, "r")) == NULL)
376 return 0;
377
378 for (state = FLD;;) {
379 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
380 case FLD:
381 case FLDPLUS:
382 case FLDEOF:
383 /*
384 * Check Content-Type field
385 */
386 if (!mh_strcasecmp (name, TYPE_FIELD)) {
387 int passno;
388 char c;
389
390 cp = add (buf, NULL);
391 while (state == FLDPLUS) {
392 state = m_getfld (state, name, buf, sizeof(buf), fp);
393 cp = add (buf, cp);
394 }
395 bp = cp;
396 passno = 1;
397
398 again:
399 for (; isspace (*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 = (mh_strcasecmp (bp, "plain") != 0)))
442 goto out;
443 *dp = c;
444 for (dp++; isspace (*dp); dp++)
445 continue;
446 if (*dp) {
447 if ((result = !uprf (dp, "charset")))
448 goto out;
449 dp += sizeof("charset") - 1;
450 while (isspace (*dp))
451 dp++;
452 if (*dp++ != '=')
453 goto invalid;
454 while (isspace (*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 = (mh_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 return result;
485 }
486 break;
487 }
488
489 /*
490 * Check Content-Transfer-Encoding field
491 */
492 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
493 cp = add (buf, NULL);
494 while (state == FLDPLUS) {
495 state = m_getfld (state, name, buf, sizeof(buf), fp);
496 cp = add (buf, cp);
497 }
498 for (bp = cp; isspace (*bp); bp++)
499 continue;
500 for (dp = bp; istoken (*dp); dp++)
501 continue;
502 *dp = '\0';
503 result = (mh_strcasecmp (bp, "7bit")
504 && mh_strcasecmp (bp, "8bit")
505 && mh_strcasecmp (bp, "binary"));
506
507 free (cp);
508 if (result) {
509 fclose (fp);
510 return result;
511 }
512 break;
513 }
514
515 /*
516 * Just skip the rest of this header
517 * field and go to next one.
518 */
519 while (state == FLDPLUS)
520 state = m_getfld (state, name, buf, sizeof(buf), fp);
521 break;
522
523 /*
524 * We've passed the message header,
525 * so message is just text.
526 */
527 default:
528 fclose (fp);
529 return 0;
530 }
531 }
532 }