]> diplodocus.org Git - nmh/blob - uip/show.c
Formatting cleanup.
[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 FMTPROCSW 14
44 { "fmtproc program", 0 },
45 #define NFMTPROCSW 15
46 { "nofmtproc", 0 },
47 #define VERSIONSW 16
48 { "version", 0 },
49 #define HELPSW 17
50 { "help", 0 },
51 { NULL, 0 }
52 };
53
54 /*
55 * static prototypes
56 */
57 static int is_nontext(char *);
58
59 #define SHOW 0
60 #define NEXT 1
61 #define PREV 2
62
63
64 int
65 main (int argc, char **argv)
66 {
67 int draftsw = 0, headersw = 1, msgp = 0;
68 int nshow = 0, checkmime = 1, mime;
69 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
70 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
71 char buf[BUFSIZ], **argp, **arguments;
72 char *msgs[MAXARGS], *vec[MAXARGS];
73 struct msgs *mp = NULL;
74
75 #ifdef LOCALE
76 setlocale(LC_ALL, "");
77 #endif
78 invo_name = r1bindex (argv[0], '/');
79
80 /* read user profile/context */
81 context_read();
82
83 if (!mh_strcasecmp (invo_name, "next")) {
84 mode = NEXT;
85 } else if (!mh_strcasecmp (invo_name, "prev")) {
86 mode = PREV;
87 }
88 arguments = getarguments (invo_name, argc, argv, 1);
89 argp = arguments;
90
91 while ((cp = *argp++)) {
92 if (*cp == '-') {
93 switch (smatch (++cp, switches)) {
94 case AMBIGSW:
95 ambigsw (cp, switches);
96 done (1);
97 case UNKWNSW:
98 case NPROGSW:
99 case NFMTPROCSW:
100 vec[vecp++] = --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 (1);
109 case VERSIONSW:
110 print_version(invo_name);
111 done (1);
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 HEADSW:
134 headersw++;
135 continue;
136 case NHEADSW:
137 headersw = 0;
138 continue;
139
140 case FORMSW:
141 vec[vecp++] = --cp;
142 if (!(cp = *argp++) || *cp == '-')
143 adios (NULL, "missing argument to %s", argp[-2]);
144 vec[vecp++] = getcpy (etcpath(cp));
145 continue;
146
147 case PROGSW:
148 case LENSW:
149 case WIDTHSW:
150 case FMTPROCSW:
151 vec[vecp++] = --cp;
152 if (!(cp = *argp++) || *cp == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
154 vec[vecp++] = cp;
155 continue;
156
157 case SHOWSW:
158 if (!(showproc = *argp++) || *showproc == '-')
159 adios (NULL, "missing argument to %s", argp[-2]);
160 nshow = 0;
161 continue;
162 case NSHOWSW:
163 nshow++;
164 continue;
165
166 case SHOWMIMESW:
167 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
168 adios (NULL, "missing argument to %s", argp[-2]);
169 nshow = 0;
170 continue;
171 case CHECKMIMESW:
172 checkmime++;
173 continue;
174 case NOCHECKMIMESW:
175 checkmime = 0;
176 continue;
177 }
178 }
179 if (*cp == '+' || *cp == '@') {
180 if (folder)
181 adios (NULL, "only one folder at a time!");
182 else
183 folder = pluspath (cp);
184 } else {
185 if (mode != SHOW)
186 goto usage;
187 else
188 msgs[msgp++] = cp;
189 }
190 }
191 procp = vecp;
192
193 if (!context_find ("path"))
194 free (path ("./", TFOLDER));
195
196 if (draftsw || file) {
197 if (msgp)
198 adios (NULL, "only one file at a time!");
199 vec[vecp++] = draftsw
200 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
201 : file;
202 goto go_to_it;
203 }
204
205 #ifdef WHATNOW
206 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
207 draftsw++;
208 vec[vecp++] = cp;
209 goto go_to_it;
210 }
211 #endif /* WHATNOW */
212
213 if (!msgp) {
214 switch (mode) {
215 case NEXT:
216 msgs[msgp++] = "next";
217 break;
218 case PREV:
219 msgs[msgp++] = "prev";
220 break;
221 default:
222 msgs[msgp++] = "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)))
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 < msgp; msgnum++)
244 if (!m_convert (mp, 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 if (mp->numsel > MAXARGS - 2)
260 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
261
262 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
263 if (is_selected(mp, msgnum))
264 vec[vecp++] = getcpy (m_name (msgnum));
265
266 seq_setcur (mp, mp->hghsel); /* update current message */
267 seq_save (mp); /* synchronize sequences */
268 context_replace (pfolder, folder); /* update current folder */
269 context_save (); /* save the context file */
270
271 if (headersw && vecp == 2)
272 printf ("(Message %s:%s)\n", folder, vec[1]);
273
274 go_to_it: ;
275 fflush (stdout);
276
277 vec[vecp] = NULL;
278
279 /*
280 * Decide which "proc" to use
281 */
282 mime = 0;
283 if (nshow) {
284 proc = catproc;
285 } else {
286 /* check if any messages are non-text MIME messages */
287 if (checkmime && !getenv ("NOMHNPROC")) {
288 if (!draftsw && !file) {
289 /* loop through selected messages and check for MIME */
290 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
291 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
292 mime = 1;
293 break;
294 }
295 } else {
296 /* check the file or draft for MIME */
297 if (is_nontext (vec[vecp - 1]))
298 mime = 1;
299 }
300 }
301
302 /* Set the "proc" */
303 if (mime)
304 proc = showmimeproc;
305 else
306 proc = showproc;
307 }
308
309 if (folder && !draftsw && !file)
310 m_putenv ("mhfolder", folder);
311
312 /*
313 * For backward compatibility, if the "proc" is mhn,
314 * then add "-show" option. Add "-file" if showing
315 * file or draft.
316 */
317 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
318 if (draftsw || file) {
319 vec[vecp] = vec[vecp - 1];
320 vec[vecp - 1] = "-file";
321 vecp++;
322 }
323 vec[vecp++] = "-show";
324 vec[vecp] = NULL;
325 }
326
327 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
328 */
329 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
330 vec[vecp] = vec[vecp - 1];
331 vec[vecp - 1] = "-file";
332 vec[++vecp] = NULL;
333 }
334
335 /*
336 * If "proc" is mhl, then run it internally
337 * rather than exec'ing it.
338 */
339 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
340 vec[0] = "mhl";
341 mhl (vecp, vec);
342 done (0);
343 }
344
345 /*
346 * If you are not using a nmh command as your "proc", then
347 * add the path to the message names. Currently, we are just
348 * checking for mhn here, since we've already taken care of mhl.
349 */
350 if (!strcmp (r1bindex (proc, '/'), "mhl")
351 && !draftsw
352 && !file
353 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
354 mp->foldpath = concat (mp->foldpath, "/", NULL);
355 cp = ssequal (maildir, mp->foldpath)
356 ? mp->foldpath + strlen (maildir)
357 : mp->foldpath;
358 for (msgnum = procp; msgnum < vecp; msgnum++)
359 vec[msgnum] = concat (cp, vec[msgnum], NULL);
360 }
361
362 vec[0] = r1bindex (proc, '/');
363 execvp (proc, vec);
364 adios (proc, "unable to exec");
365 return 0; /* dead code to satisfy the compiler */
366 }
367
368
369 /*
370 * Check if a message or file contains any non-text parts
371 */
372 static int
373 is_nontext (char *msgnam)
374 {
375 int result, state;
376 unsigned char *bp, *dp;
377 char *cp;
378 char buf[BUFSIZ], name[NAMESZ];
379 FILE *fp;
380
381 if ((fp = fopen (msgnam, "r")) == NULL)
382 return 0;
383
384 for (state = FLD;;) {
385 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
386 case FLD:
387 case FLDPLUS:
388 case FLDEOF:
389 /*
390 * Check Content-Type field
391 */
392 if (!mh_strcasecmp (name, TYPE_FIELD)) {
393 int passno;
394 char c;
395
396 cp = add (buf, NULL);
397 while (state == FLDPLUS) {
398 state = m_getfld (state, name, buf, sizeof(buf), fp);
399 cp = add (buf, cp);
400 }
401 bp = cp;
402 passno = 1;
403
404 again:
405 for (; isspace (*bp); bp++)
406 continue;
407 if (*bp == '(') {
408 int i;
409
410 for (bp++, i = 0;;) {
411 switch (*bp++) {
412 case '\0':
413 invalid:
414 result = 0;
415 goto out;
416 case '\\':
417 if (*bp++ == '\0')
418 goto invalid;
419 continue;
420 case '(':
421 i++;
422 /* and fall... */
423 default:
424 continue;
425 case ')':
426 if (--i < 0)
427 break;
428 continue;
429 }
430 break;
431 }
432 }
433 if (passno == 2) {
434 if (*bp != '/')
435 goto invalid;
436 bp++;
437 passno = 3;
438 goto again;
439 }
440 for (dp = bp; istoken (*dp); dp++)
441 continue;
442 c = *dp;
443 *dp = '\0';
444 if (!*bp)
445 goto invalid;
446 if (passno > 1) {
447 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
448 goto out;
449 *dp = c;
450 for (dp++; isspace (*dp); dp++)
451 continue;
452 if (*dp) {
453 if ((result = !uprf (dp, "charset")))
454 goto out;
455 dp += sizeof("charset") - 1;
456 while (isspace (*dp))
457 dp++;
458 if (*dp++ != '=')
459 goto invalid;
460 while (isspace (*dp))
461 dp++;
462 if (*dp == '"') {
463 if ((bp = strchr(++dp, '"')))
464 *bp = '\0';
465 } else {
466 for (bp = dp; *bp; bp++)
467 if (!istoken (*bp)) {
468 *bp = '\0';
469 break;
470 }
471 }
472 } else {
473 /* Default character set */
474 dp = "US-ASCII";
475 }
476 /* Check the character set */
477 result = !check_charset (dp, strlen (dp));
478 } else {
479 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
480 *dp = c;
481 bp = dp;
482 passno = 2;
483 goto again;
484 }
485 }
486 out:
487 free (cp);
488 if (result) {
489 fclose (fp);
490 return result;
491 }
492 break;
493 }
494
495 /*
496 * Check Content-Transfer-Encoding field
497 */
498 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
499 cp = add (buf, NULL);
500 while (state == FLDPLUS) {
501 state = m_getfld (state, name, buf, sizeof(buf), fp);
502 cp = add (buf, cp);
503 }
504 for (bp = cp; isspace (*bp); bp++)
505 continue;
506 for (dp = bp; istoken (*dp); dp++)
507 continue;
508 *dp = '\0';
509 result = (mh_strcasecmp (bp, "7bit")
510 && mh_strcasecmp (bp, "8bit")
511 && mh_strcasecmp (bp, "binary"));
512
513 free (cp);
514 if (result) {
515 fclose (fp);
516 return result;
517 }
518 break;
519 }
520
521 /*
522 * Just skip the rest of this header
523 * field and go to next one.
524 */
525 while (state == FLDPLUS)
526 state = m_getfld (state, name, buf, sizeof(buf), fp);
527 break;
528
529 /*
530 * We've passed the message header,
531 * so message is just text.
532 */
533 default:
534 fclose (fp);
535 return 0;
536 }
537 }
538 }