]> diplodocus.org Git - nmh/blob - uip/show.c
Fix this man page for the 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 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 * Cheat: we are loaded with adrparse, which wants a routine called
364 * OfficialName(). We call adrparse:getm() with the correct arguments
365 * to prevent OfficialName() from being called. Hence, the following
366 * is to keep the loader happy.
367 */
368
369 char *
370 OfficialName (char *name)
371 {
372 return name;
373 }
374
375
376 /*
377 * Check if a message or file contains any non-text parts
378 */
379 static int
380 is_nontext (char *msgnam)
381 {
382 int result, state;
383 unsigned char *bp, *dp;
384 char *cp;
385 char buf[BUFSIZ], name[NAMESZ];
386 FILE *fp;
387
388 if ((fp = fopen (msgnam, "r")) == NULL)
389 return 0;
390
391 for (state = FLD;;) {
392 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
393 case FLD:
394 case FLDPLUS:
395 case FLDEOF:
396 /*
397 * Check Content-Type field
398 */
399 if (!mh_strcasecmp (name, TYPE_FIELD)) {
400 int passno;
401 char c;
402
403 cp = add (buf, NULL);
404 while (state == FLDPLUS) {
405 state = m_getfld (state, name, buf, sizeof(buf), fp);
406 cp = add (buf, cp);
407 }
408 bp = cp;
409 passno = 1;
410
411 again:
412 for (; isspace (*bp); bp++)
413 continue;
414 if (*bp == '(') {
415 int i;
416
417 for (bp++, i = 0;;) {
418 switch (*bp++) {
419 case '\0':
420 invalid:
421 result = 0;
422 goto out;
423 case '\\':
424 if (*bp++ == '\0')
425 goto invalid;
426 continue;
427 case '(':
428 i++;
429 /* and fall... */
430 default:
431 continue;
432 case ')':
433 if (--i < 0)
434 break;
435 continue;
436 }
437 break;
438 }
439 }
440 if (passno == 2) {
441 if (*bp != '/')
442 goto invalid;
443 bp++;
444 passno = 3;
445 goto again;
446 }
447 for (dp = bp; istoken (*dp); dp++)
448 continue;
449 c = *dp;
450 *dp = '\0';
451 if (!*bp)
452 goto invalid;
453 if (passno > 1) {
454 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
455 goto out;
456 *dp = c;
457 for (dp++; isspace (*dp); dp++)
458 continue;
459 if (*dp) {
460 if ((result = !uprf (dp, "charset")))
461 goto out;
462 dp += sizeof("charset") - 1;
463 while (isspace (*dp))
464 dp++;
465 if (*dp++ != '=')
466 goto invalid;
467 while (isspace (*dp))
468 dp++;
469 if (*dp == '"') {
470 if ((bp = strchr(++dp, '"')))
471 *bp = '\0';
472 } else {
473 for (bp = dp; *bp; bp++)
474 if (!istoken (*bp)) {
475 *bp = '\0';
476 break;
477 }
478 }
479 } else {
480 /* Default character set */
481 dp = "US-ASCII";
482 }
483 /* Check the character set */
484 result = !check_charset (dp, strlen (dp));
485 } else {
486 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
487 *dp = c;
488 bp = dp;
489 passno = 2;
490 goto again;
491 }
492 }
493 out:
494 free (cp);
495 if (result) {
496 fclose (fp);
497 return result;
498 }
499 break;
500 }
501
502 /*
503 * Check Content-Transfer-Encoding field
504 */
505 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
506 cp = add (buf, NULL);
507 while (state == FLDPLUS) {
508 state = m_getfld (state, name, buf, sizeof(buf), fp);
509 cp = add (buf, cp);
510 }
511 for (bp = cp; isspace (*bp); bp++)
512 continue;
513 for (dp = bp; istoken (*dp); dp++)
514 continue;
515 *dp = '\0';
516 result = (mh_strcasecmp (bp, "7bit")
517 && mh_strcasecmp (bp, "8bit")
518 && mh_strcasecmp (bp, "binary"));
519
520 free (cp);
521 if (result) {
522 fclose (fp);
523 return result;
524 }
525 break;
526 }
527
528 /*
529 * Just skip the rest of this header
530 * field and go to next one.
531 */
532 while (state == FLDPLUS)
533 state = m_getfld (state, name, buf, sizeof(buf), fp);
534 break;
535
536 /*
537 * We've passed the message header,
538 * so message is just text.
539 */
540 default:
541 fclose (fp);
542 return 0;
543 }
544 }
545 }