1 | |
---|
2 | /* |
---|
3 | * show.c -- show/list messages |
---|
4 | * |
---|
5 | * $Id: show.c,v 1.3 1999-10-08 22:14:32 danw Exp $ |
---|
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 showing multiple messages, default to -nocheckmime. |
---|
185 | * (But just decrement it rather than setting to 0, so user |
---|
186 | * can specify -checkmime to override. |
---|
187 | */ |
---|
188 | if (msgp > 1) |
---|
189 | checkmime--; |
---|
190 | |
---|
191 | if (!context_find ("path")) |
---|
192 | free (path ("./", TFOLDER)); |
---|
193 | |
---|
194 | if (draftsw || file) { |
---|
195 | if (msgp) |
---|
196 | adios (NULL, "only one file at a time!"); |
---|
197 | vec[vecp++] = draftsw |
---|
198 | ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf)) |
---|
199 | : file; |
---|
200 | goto go_to_it; |
---|
201 | } |
---|
202 | |
---|
203 | #ifdef WHATNOW |
---|
204 | if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) { |
---|
205 | draftsw++; |
---|
206 | vec[vecp++] = cp; |
---|
207 | goto go_to_it; |
---|
208 | } |
---|
209 | #endif /* WHATNOW */ |
---|
210 | |
---|
211 | if (!msgp) { |
---|
212 | switch (mode) { |
---|
213 | case NEXT: |
---|
214 | msgs[msgp++] = "next"; |
---|
215 | break; |
---|
216 | case PREV: |
---|
217 | msgs[msgp++] = "prev"; |
---|
218 | break; |
---|
219 | default: |
---|
220 | msgs[msgp++] = "cur"; |
---|
221 | break; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | if (!folder) |
---|
226 | folder = getfolder (1); |
---|
227 | maildir = m_maildir (folder); |
---|
228 | |
---|
229 | if (chdir (maildir) == NOTOK) |
---|
230 | adios (maildir, "unable to change directory to"); |
---|
231 | |
---|
232 | /* read folder and create message structure */ |
---|
233 | if (!(mp = folder_read (folder))) |
---|
234 | adios (NULL, "unable to read folder %s", folder); |
---|
235 | |
---|
236 | /* check for empty folder */ |
---|
237 | if (mp->nummsg == 0) |
---|
238 | adios (NULL, "no messages in %s", folder); |
---|
239 | |
---|
240 | /* parse all the message ranges/sequences and set SELECTED */ |
---|
241 | for (msgnum = 0; msgnum < msgp; msgnum++) |
---|
242 | if (!m_convert (mp, msgs[msgnum])) |
---|
243 | done (1); |
---|
244 | |
---|
245 | /* |
---|
246 | * Set the SELECT_UNSEEN bit for all the SELECTED messages, |
---|
247 | * since we will use that as a tag to know which messages |
---|
248 | * to remove from the "unseen" sequence. |
---|
249 | */ |
---|
250 | for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) |
---|
251 | if (is_selected(mp, msgnum)) |
---|
252 | set_unseen (mp, msgnum); |
---|
253 | |
---|
254 | seq_setprev (mp); /* set the Previous-Sequence */ |
---|
255 | seq_setunseen (mp, 1); /* unset the Unseen-Sequence */ |
---|
256 | |
---|
257 | if (mp->numsel > MAXARGS - 2) |
---|
258 | adios (NULL, "more than %d messages for show exec", MAXARGS - 2); |
---|
259 | |
---|
260 | for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) |
---|
261 | if (is_selected(mp, msgnum)) |
---|
262 | vec[vecp++] = getcpy (m_name (msgnum)); |
---|
263 | |
---|
264 | seq_setcur (mp, mp->hghsel); /* update current message */ |
---|
265 | seq_save (mp); /* synchronize sequences */ |
---|
266 | context_replace (pfolder, folder); /* update current folder */ |
---|
267 | context_save (); /* save the context file */ |
---|
268 | |
---|
269 | if (headersw && vecp == 2) |
---|
270 | printf ("(Message %s:%s)\n", folder, vec[1]); |
---|
271 | |
---|
272 | go_to_it: ; |
---|
273 | fflush (stdout); |
---|
274 | |
---|
275 | vec[vecp] = NULL; |
---|
276 | |
---|
277 | /* |
---|
278 | * Decide which "proc" to use |
---|
279 | */ |
---|
280 | mime = 0; |
---|
281 | if (nshow) { |
---|
282 | proc = catproc; |
---|
283 | } else { |
---|
284 | /* check if any messages are non-text MIME messages */ |
---|
285 | if (checkmime && !getenv ("NOMHNPROC")) { |
---|
286 | if (!draftsw && !file) { |
---|
287 | /* loop through selected messages and check for MIME */ |
---|
288 | for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) |
---|
289 | if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) { |
---|
290 | mime = 1; |
---|
291 | break; |
---|
292 | } |
---|
293 | } else { |
---|
294 | /* check the file or draft for MIME */ |
---|
295 | if (is_nontext (vec[vecp - 1])) |
---|
296 | mime = 1; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | /* Set the "proc" */ |
---|
301 | if (mime) |
---|
302 | proc = showmimeproc; |
---|
303 | else |
---|
304 | proc = showproc; |
---|
305 | } |
---|
306 | |
---|
307 | if (folder && !draftsw && !file) |
---|
308 | m_putenv ("mhfolder", folder); |
---|
309 | |
---|
310 | /* |
---|
311 | * For backward compatibility, if the "proc" is mhn, |
---|
312 | * then add "-show" option. Add "-file" if showing |
---|
313 | * file or draft. |
---|
314 | */ |
---|
315 | if (strcmp (r1bindex (proc, '/'), "mhn") == 0) { |
---|
316 | if (draftsw || file) { |
---|
317 | vec[vecp] = vec[vecp - 1]; |
---|
318 | vec[vecp - 1] = "-file"; |
---|
319 | vecp++; |
---|
320 | } |
---|
321 | vec[vecp++] = "-show"; |
---|
322 | vec[vecp] = NULL; |
---|
323 | } |
---|
324 | |
---|
325 | /* If the "proc" is "mhshow", add "-file" if showing file or draft. |
---|
326 | */ |
---|
327 | if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) { |
---|
328 | vec[vecp] = vec[vecp - 1]; |
---|
329 | vec[vecp - 1] = "-file"; |
---|
330 | vec[++vecp] = NULL; |
---|
331 | } |
---|
332 | |
---|
333 | /* |
---|
334 | * If "proc" is mhl, then run it internally |
---|
335 | * rather than exec'ing it. |
---|
336 | */ |
---|
337 | if (strcmp (r1bindex (proc, '/'), "mhl") == 0) { |
---|
338 | vec[0] = "mhl"; |
---|
339 | mhl (vecp, vec); |
---|
340 | done (0); |
---|
341 | } |
---|
342 | |
---|
343 | /* |
---|
344 | * If you are not using a nmh command as your "proc", then |
---|
345 | * add the path to the message names. Currently, we are just |
---|
346 | * checking for mhn here, since we've already taken care of mhl. |
---|
347 | */ |
---|
348 | if (!strcmp (r1bindex (proc, '/'), "mhl") |
---|
349 | && !draftsw |
---|
350 | && !file |
---|
351 | && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) { |
---|
352 | mp->foldpath = concat (mp->foldpath, "/", NULL); |
---|
353 | cp = ssequal (maildir, mp->foldpath) |
---|
354 | ? mp->foldpath + strlen (maildir) |
---|
355 | : mp->foldpath; |
---|
356 | for (msgnum = procp; msgnum < vecp; msgnum++) |
---|
357 | vec[msgnum] = concat (cp, vec[msgnum], NULL); |
---|
358 | } |
---|
359 | |
---|
360 | vec[0] = r1bindex (proc, '/'); |
---|
361 | execvp (proc, vec); |
---|
362 | adios (proc, "unable to exec"); |
---|
363 | } |
---|
364 | |
---|
365 | /* |
---|
366 | * Cheat: we are loaded with adrparse, which wants a routine called |
---|
367 | * OfficialName(). We call adrparse:getm() with the correct arguments |
---|
368 | * to prevent OfficialName() from being called. Hence, the following |
---|
369 | * is to keep the loader happy. |
---|
370 | */ |
---|
371 | |
---|
372 | char * |
---|
373 | OfficialName (char *name) |
---|
374 | { |
---|
375 | return name; |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | /* |
---|
380 | * Check if a message or file contains any non-text parts |
---|
381 | */ |
---|
382 | static int |
---|
383 | is_nontext (char *msgnam) |
---|
384 | { |
---|
385 | int result, state; |
---|
386 | char *bp, *cp, *dp; |
---|
387 | char buf[BUFSIZ], name[NAMESZ]; |
---|
388 | FILE *fp; |
---|
389 | |
---|
390 | if ((fp = fopen (msgnam, "r")) == NULL) |
---|
391 | return 0; |
---|
392 | |
---|
393 | for (state = FLD;;) { |
---|
394 | switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) { |
---|
395 | case FLD: |
---|
396 | case FLDPLUS: |
---|
397 | case FLDEOF: |
---|
398 | /* |
---|
399 | * Check Content-Type field |
---|
400 | */ |
---|
401 | if (!strcasecmp (name, TYPE_FIELD)) { |
---|
402 | int passno; |
---|
403 | char c; |
---|
404 | |
---|
405 | cp = add (buf, NULL); |
---|
406 | while (state == FLDPLUS) { |
---|
407 | state = m_getfld (state, name, buf, sizeof(buf), fp); |
---|
408 | cp = add (buf, cp); |
---|
409 | } |
---|
410 | bp = cp; |
---|
411 | passno = 1; |
---|
412 | |
---|
413 | again: |
---|
414 | for (; isspace (*bp); bp++) |
---|
415 | continue; |
---|
416 | if (*bp == '(') { |
---|
417 | int i; |
---|
418 | |
---|
419 | for (bp++, i = 0;;) { |
---|
420 | switch (*bp++) { |
---|
421 | case '\0': |
---|
422 | invalid: |
---|
423 | result = 0; |
---|
424 | goto out; |
---|
425 | case '\\': |
---|
426 | if (*bp++ == '\0') |
---|
427 | goto invalid; |
---|
428 | continue; |
---|
429 | case '(': |
---|
430 | i++; |
---|
431 | /* and fall... */ |
---|
432 | default: |
---|
433 | continue; |
---|
434 | case ')': |
---|
435 | if (--i < 0) |
---|
436 | break; |
---|
437 | continue; |
---|
438 | } |
---|
439 | break; |
---|
440 | } |
---|
441 | } |
---|
442 | if (passno == 2) { |
---|
443 | if (*bp != '/') |
---|
444 | goto invalid; |
---|
445 | bp++; |
---|
446 | passno = 3; |
---|
447 | goto again; |
---|
448 | } |
---|
449 | for (dp = bp; istoken (*dp); dp++) |
---|
450 | continue; |
---|
451 | c = *dp; |
---|
452 | *dp = '\0'; |
---|
453 | if (!*bp) |
---|
454 | goto invalid; |
---|
455 | if (passno > 1) { |
---|
456 | if ((result = (strcasecmp (bp, "plain") != 0))) |
---|
457 | goto out; |
---|
458 | *dp = c; |
---|
459 | for (dp++; isspace (*dp); dp++) |
---|
460 | continue; |
---|
461 | if (*dp) { |
---|
462 | if ((result = !uprf (dp, "charset"))) |
---|
463 | goto out; |
---|
464 | dp += sizeof("charset") - 1; |
---|
465 | while (isspace (*dp)) |
---|
466 | dp++; |
---|
467 | if (*dp++ != '=') |
---|
468 | goto invalid; |
---|
469 | while (isspace (*dp)) |
---|
470 | dp++; |
---|
471 | if (*dp == '"') { |
---|
472 | if ((bp = strchr(++dp, '"'))) |
---|
473 | *bp = '\0'; |
---|
474 | } else { |
---|
475 | for (bp = dp; *bp; bp++) |
---|
476 | if (isspace (*bp)) { |
---|
477 | *bp = '\0'; |
---|
478 | break; |
---|
479 | } |
---|
480 | } |
---|
481 | } else { |
---|
482 | /* Default character set */ |
---|
483 | dp = "US-ASCII"; |
---|
484 | } |
---|
485 | /* Check the character set */ |
---|
486 | result = !check_charset (dp, strlen (dp)); |
---|
487 | } else { |
---|
488 | if (!(result = (strcasecmp (bp, "text") != 0))) { |
---|
489 | *dp = c; |
---|
490 | bp = dp; |
---|
491 | passno = 2; |
---|
492 | goto again; |
---|
493 | } |
---|
494 | } |
---|
495 | out: |
---|
496 | free (cp); |
---|
497 | if (result) { |
---|
498 | fclose (fp); |
---|
499 | return result; |
---|
500 | } |
---|
501 | break; |
---|
502 | } |
---|
503 | |
---|
504 | /* |
---|
505 | * Check Content-Transfer-Encoding field |
---|
506 | */ |
---|
507 | if (!strcasecmp (name, ENCODING_FIELD)) { |
---|
508 | cp = add (buf, NULL); |
---|
509 | while (state == FLDPLUS) { |
---|
510 | state = m_getfld (state, name, buf, sizeof(buf), fp); |
---|
511 | cp = add (buf, cp); |
---|
512 | } |
---|
513 | for (bp = cp; isspace (*bp); bp++) |
---|
514 | continue; |
---|
515 | for (dp = bp; istoken (*dp); dp++) |
---|
516 | continue; |
---|
517 | *dp = '\0'; |
---|
518 | result = (strcasecmp (bp, "7bit") |
---|
519 | && strcasecmp (bp, "8bit") |
---|
520 | && strcasecmp (bp, "binary")); |
---|
521 | |
---|
522 | free (cp); |
---|
523 | if (result) { |
---|
524 | fclose (fp); |
---|
525 | return result; |
---|
526 | } |
---|
527 | break; |
---|
528 | } |
---|
529 | |
---|
530 | /* |
---|
531 | * Just skip the rest of this header |
---|
532 | * field and go to next one. |
---|
533 | */ |
---|
534 | while (state == FLDPLUS) |
---|
535 | state = m_getfld (state, name, buf, sizeof(buf), fp); |
---|
536 | break; |
---|
537 | |
---|
538 | /* |
---|
539 | * We've passed the message header, |
---|
540 | * so message is just text. |
---|
541 | */ |
---|
542 | default: |
---|
543 | fclose (fp); |
---|
544 | return 0; |
---|
545 | } |
---|
546 | } |
---|
547 | } |
---|