]> diplodocus.org Git - nmh/blob - uip/show.c
Added notes about the configure change.
[nmh] / uip / show.c
1
2 /*
3 * show.c -- show/list messages
4 *
5 * $Id$
6 */
7
8 #include <h/mh.h>
9 #include <h/mime.h>
10
11 static struct swit switches[] = {
12 #define CHECKMIMESW 0
13 { "checkmime", 0 },
14 #define NOCHECKMIMESW 1
15 { "nocheckmime", 0 },
16 #define HEADSW 2
17 { "header", 0 },
18 #define NHEADSW 3
19 { "noheader", 0 },
20 #define FORMSW 4
21 { "form formfile", 0 },
22 #define PROGSW 5
23 { "moreproc program", 0 },
24 #define NPROGSW 6
25 { "nomoreproc", 0 },
26 #define LENSW 7
27 { "length lines", 0 },
28 #define WIDTHSW 8
29 { "width columns", 0 },
30 #define SHOWSW 9
31 { "showproc program", 0 },
32 #define SHOWMIMESW 10
33 { "showmimeproc program", 0 },
34 #define NSHOWSW 11
35 { "noshowproc", 0 },
36 #define DRFTSW 12
37 { "draft", 0 },
38 #define FILESW 13
39 { "file file", -4 }, /* interface from showfile */
40 #define VERSIONSW 14
41 { "version", 0 },
42 #define HELPSW 15
43 { "help", 0 },
44 { NULL, 0 }
45 };
46
47 /*
48 * static prototypes
49 */
50 static int is_nontext(char *);
51
52 #define SHOW 0
53 #define NEXT 1
54 #define PREV 2
55
56
57 int
58 main (int argc, char **argv)
59 {
60 int draftsw = 0, headersw = 1, msgp = 0;
61 int nshow = 0, checkmime = 1, mime;
62 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
63 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
64 char buf[BUFSIZ], **argp, **arguments;
65 char *msgs[MAXARGS], *vec[MAXARGS];
66 struct msgs *mp;
67
68 #ifdef LOCALE
69 setlocale(LC_ALL, "");
70 #endif
71 invo_name = r1bindex (argv[0], '/');
72
73 /* read user profile/context */
74 context_read();
75
76 if (!strcasecmp (invo_name, "next")) {
77 mode = NEXT;
78 } else if (!strcasecmp (invo_name, "prev")) {
79 mode = PREV;
80 }
81 arguments = getarguments (invo_name, argc, argv, 1);
82 argp = arguments;
83
84 while ((cp = *argp++)) {
85 if (*cp == '-') {
86 switch (smatch (++cp, switches)) {
87 case AMBIGSW:
88 ambigsw (cp, switches);
89 done (1);
90 case UNKWNSW:
91 case NPROGSW:
92 vec[vecp++] = --cp;
93 continue;
94
95 case HELPSW:
96 snprintf (buf, sizeof(buf),
97 "%s [+folder] %s[switches] [switches for showproc]",
98 invo_name, mode == SHOW ? "[msgs] ": "");
99 print_help (buf, switches, 1);
100 done (1);
101 case VERSIONSW:
102 print_version(invo_name);
103 done (1);
104
105 case DRFTSW:
106 if (file)
107 adios (NULL, "only one file at a time!");
108 draftsw++;
109 if (mode == SHOW)
110 continue;
111 usage:
112 adios (NULL,
113 "usage: %s [+folder] [switches] [switches for showproc]",
114 invo_name);
115 case FILESW:
116 if (mode != SHOW)
117 goto usage;
118 if (draftsw || file)
119 adios (NULL, "only one file at a time!");
120 if (!(cp = *argp++) || *cp == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
122 file = path (cp, TFILE);
123 continue;
124
125 case HEADSW:
126 headersw++;
127 continue;
128 case NHEADSW:
129 headersw = 0;
130 continue;
131
132 case FORMSW:
133 vec[vecp++] = --cp;
134 if (!(cp = *argp++) || *cp == '-')
135 adios (NULL, "missing argument to %s", argp[-2]);
136 vec[vecp++] = getcpy (etcpath(cp));
137 continue;
138
139 case PROGSW:
140 case LENSW:
141 case WIDTHSW:
142 vec[vecp++] = --cp;
143 if (!(cp = *argp++) || *cp == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
145 vec[vecp++] = 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 = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
175 } else {
176 if (mode != SHOW)
177 goto usage;
178 else
179 msgs[msgp++] = cp;
180 }
181 }
182 procp = vecp;
183
184 if (!context_find ("path"))
185 free (path ("./", TFOLDER));
186
187 if (draftsw || file) {
188 if (msgp)
189 adios (NULL, "only one file at a time!");
190 vec[vecp++] = draftsw
191 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
192 : file;
193 goto go_to_it;
194 }
195
196 #ifdef WHATNOW
197 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
198 draftsw++;
199 vec[vecp++] = cp;
200 goto go_to_it;
201 }
202 #endif /* WHATNOW */
203
204 if (!msgp) {
205 switch (mode) {
206 case NEXT:
207 msgs[msgp++] = "next";
208 break;
209 case PREV:
210 msgs[msgp++] = "prev";
211 break;
212 default:
213 msgs[msgp++] = "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 < msgp; msgnum++)
235 if (!m_convert (mp, 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 if (mp->numsel > MAXARGS - 2)
251 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
252
253 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
254 if (is_selected(mp, msgnum))
255 vec[vecp++] = getcpy (m_name (msgnum));
256
257 seq_setcur (mp, mp->hghsel); /* update current message */
258 seq_save (mp); /* synchronize sequences */
259 context_replace (pfolder, folder); /* update current folder */
260 context_save (); /* save the context file */
261
262 if (headersw && vecp == 2)
263 printf ("(Message %s:%s)\n", folder, vec[1]);
264
265 go_to_it: ;
266 fflush (stdout);
267
268 vec[vecp] = NULL;
269
270 /*
271 * Decide which "proc" to use
272 */
273 mime = 0;
274 if (nshow) {
275 proc = catproc;
276 } else {
277 /* check if any messages are non-text MIME messages */
278 if (checkmime && !getenv ("NOMHNPROC")) {
279 if (!draftsw && !file) {
280 /* loop through selected messages and check for MIME */
281 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
282 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
283 mime = 1;
284 break;
285 }
286 } else {
287 /* check the file or draft for MIME */
288 if (is_nontext (vec[vecp - 1]))
289 mime = 1;
290 }
291 }
292
293 /* Set the "proc" */
294 if (mime)
295 proc = showmimeproc;
296 else
297 proc = showproc;
298 }
299
300 if (folder && !draftsw && !file)
301 m_putenv ("mhfolder", folder);
302
303 /*
304 * For backward compatibility, if the "proc" is mhn,
305 * then add "-show" option. Add "-file" if showing
306 * file or draft.
307 */
308 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
309 if (draftsw || file) {
310 vec[vecp] = vec[vecp - 1];
311 vec[vecp - 1] = "-file";
312 vecp++;
313 }
314 vec[vecp++] = "-show";
315 vec[vecp] = NULL;
316 }
317
318 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
319 */
320 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
321 vec[vecp] = vec[vecp - 1];
322 vec[vecp - 1] = "-file";
323 vec[++vecp] = NULL;
324 }
325
326 /*
327 * If "proc" is mhl, then run it internally
328 * rather than exec'ing it.
329 */
330 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
331 vec[0] = "mhl";
332 mhl (vecp, vec);
333 done (0);
334 }
335
336 /*
337 * If you are not using a nmh command as your "proc", then
338 * add the path to the message names. Currently, we are just
339 * checking for mhn here, since we've already taken care of mhl.
340 */
341 if (!strcmp (r1bindex (proc, '/'), "mhl")
342 && !draftsw
343 && !file
344 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
345 mp->foldpath = concat (mp->foldpath, "/", NULL);
346 cp = ssequal (maildir, mp->foldpath)
347 ? mp->foldpath + strlen (maildir)
348 : mp->foldpath;
349 for (msgnum = procp; msgnum < vecp; msgnum++)
350 vec[msgnum] = concat (cp, vec[msgnum], NULL);
351 }
352
353 vec[0] = r1bindex (proc, '/');
354 execvp (proc, vec);
355 adios (proc, "unable to exec");
356 }
357
358 /*
359 * Cheat: we are loaded with adrparse, which wants a routine called
360 * OfficialName(). We call adrparse:getm() with the correct arguments
361 * to prevent OfficialName() from being called. Hence, the following
362 * is to keep the loader happy.
363 */
364
365 char *
366 OfficialName (char *name)
367 {
368 return name;
369 }
370
371
372 /*
373 * Check if a message or file contains any non-text parts
374 */
375 static int
376 is_nontext (char *msgnam)
377 {
378 int result, state;
379 char *bp, *cp, *dp;
380 char buf[BUFSIZ], name[NAMESZ];
381 FILE *fp;
382
383 if ((fp = fopen (msgnam, "r")) == NULL)
384 return 0;
385
386 for (state = FLD;;) {
387 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
388 case FLD:
389 case FLDPLUS:
390 case FLDEOF:
391 /*
392 * Check Content-Type field
393 */
394 if (!strcasecmp (name, TYPE_FIELD)) {
395 int passno;
396 char c;
397
398 cp = add (buf, NULL);
399 while (state == FLDPLUS) {
400 state = m_getfld (state, name, buf, sizeof(buf), fp);
401 cp = add (buf, cp);
402 }
403 bp = cp;
404 passno = 1;
405
406 again:
407 for (; isspace (*bp); bp++)
408 continue;
409 if (*bp == '(') {
410 int i;
411
412 for (bp++, i = 0;;) {
413 switch (*bp++) {
414 case '\0':
415 invalid:
416 result = 0;
417 goto out;
418 case '\\':
419 if (*bp++ == '\0')
420 goto invalid;
421 continue;
422 case '(':
423 i++;
424 /* and fall... */
425 default:
426 continue;
427 case ')':
428 if (--i < 0)
429 break;
430 continue;
431 }
432 break;
433 }
434 }
435 if (passno == 2) {
436 if (*bp != '/')
437 goto invalid;
438 bp++;
439 passno = 3;
440 goto again;
441 }
442 for (dp = bp; istoken (*dp); dp++)
443 continue;
444 c = *dp;
445 *dp = '\0';
446 if (!*bp)
447 goto invalid;
448 if (passno > 1) {
449 if ((result = (strcasecmp (bp, "plain") != 0)))
450 goto out;
451 *dp = c;
452 for (dp++; isspace (*dp); dp++)
453 continue;
454 if (*dp) {
455 if ((result = !uprf (dp, "charset")))
456 goto out;
457 dp += sizeof("charset") - 1;
458 while (isspace (*dp))
459 dp++;
460 if (*dp++ != '=')
461 goto invalid;
462 while (isspace (*dp))
463 dp++;
464 if (*dp == '"') {
465 if ((bp = strchr(++dp, '"')))
466 *bp = '\0';
467 } else {
468 for (bp = dp; *bp; bp++)
469 if (isspace (*bp)) {
470 *bp = '\0';
471 break;
472 }
473 }
474 } else {
475 /* Default character set */
476 dp = "US-ASCII";
477 }
478 /* Check the character set */
479 result = !check_charset (dp, strlen (dp));
480 } else {
481 if (!(result = (strcasecmp (bp, "text") != 0))) {
482 *dp = c;
483 bp = dp;
484 passno = 2;
485 goto again;
486 }
487 }
488 out:
489 free (cp);
490 if (result) {
491 fclose (fp);
492 return result;
493 }
494 break;
495 }
496
497 /*
498 * Check Content-Transfer-Encoding field
499 */
500 if (!strcasecmp (name, ENCODING_FIELD)) {
501 cp = add (buf, NULL);
502 while (state == FLDPLUS) {
503 state = m_getfld (state, name, buf, sizeof(buf), fp);
504 cp = add (buf, cp);
505 }
506 for (bp = cp; isspace (*bp); bp++)
507 continue;
508 for (dp = bp; istoken (*dp); dp++)
509 continue;
510 *dp = '\0';
511 result = (strcasecmp (bp, "7bit")
512 && strcasecmp (bp, "8bit")
513 && strcasecmp (bp, "binary"));
514
515 free (cp);
516 if (result) {
517 fclose (fp);
518 return result;
519 }
520 break;
521 }
522
523 /*
524 * Just skip the rest of this header
525 * field and go to next one.
526 */
527 while (state == FLDPLUS)
528 state = m_getfld (state, name, buf, sizeof(buf), fp);
529 break;
530
531 /*
532 * We've passed the message header,
533 * so message is just text.
534 */
535 default:
536 fclose (fp);
537 return 0;
538 }
539 }
540 }