]> diplodocus.org Git - nmh/blob - uip/show.c
Removed temporary probes added in commit
[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 #define SHOW_SWITCHES \
15 X("checkmime", 0, CHECKMIMESW) \
16 X("nocheckmime", 0, NOCHECKMIMESW) \
17 X("header", 0, HEADSW) \
18 X("noheader", 0, NHEADSW) \
19 X("form formfile", 0, FORMSW) \
20 X("moreproc program", 0, PROGSW) \
21 X("nomoreproc", 0, NPROGSW) \
22 X("length lines", 0, LENSW) \
23 X("width columns", 0, WIDTHSW) \
24 X("showproc program", 0, SHOWSW) \
25 X("showmimeproc program", 0, SHOWMIMESW) \
26 X("noshowproc", 0, NSHOWSW) \
27 X("draft", 0, DRFTSW) \
28 X("file file", -4, FILESW) /* interface from showfile */ \
29 X("fmtproc program", 0, FMTPROCSW) \
30 X("nofmtproc", 0, NFMTPROCSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
33
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(SHOW);
36 #undef X
37
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(SHOW, switches);
40 #undef X
41
42 /*
43 * static prototypes
44 */
45 static int is_nontext(char *);
46
47 #define SHOW 0
48 #define NEXT 1
49 #define PREV 2
50
51
52 int
53 main (int argc, char **argv)
54 {
55 int draftsw = 0, headersw = 1;
56 int nshow = 0, checkmime = 1, mime;
57 int isdf = 0, mode = SHOW, msgnum;
58 char *cp, *maildir, *file = NULL, *folder = NULL, *proc, *program;
59 char buf[BUFSIZ], **argp, **arguments;
60 struct msgs *mp = NULL;
61 struct msgs_array msgs = { 0, 0, NULL };
62 struct msgs_array vec = { 0, 0, NULL };
63
64 #ifdef LOCALE
65 setlocale(LC_ALL, "");
66 #endif
67 invo_name = r1bindex (argv[0], '/');
68
69 /* read user profile/context */
70 context_read();
71
72 if (!strcasecmp (invo_name, "next")) {
73 mode = NEXT;
74 } else if (!strcasecmp (invo_name, "prev")) {
75 mode = PREV;
76 }
77 arguments = getarguments (invo_name, argc, argv, 1);
78 argp = arguments;
79
80 while ((cp = *argp++)) {
81 if (*cp == '-') {
82 switch (smatch (++cp, switches)) {
83 case AMBIGSW:
84 ambigsw (cp, switches);
85 done (1);
86 case UNKWNSW:
87 case NPROGSW:
88 case NFMTPROCSW:
89 app_msgarg(&vec, --cp);
90 continue;
91
92 case HELPSW:
93 snprintf (buf, sizeof(buf),
94 "%s [+folder] %s[switches] [switches for showproc]",
95 invo_name, mode == SHOW ? "[msgs] ": "");
96 print_help (buf, switches, 1);
97 done (0);
98 case VERSIONSW:
99 print_version(invo_name);
100 done (0);
101
102 case DRFTSW:
103 if (file)
104 adios (NULL, "only one file at a time!");
105 draftsw++;
106 if (mode == SHOW)
107 continue;
108 usage:
109 adios (NULL,
110 "usage: %s [+folder] [switches] [switches for showproc]",
111 invo_name);
112 case FILESW:
113 if (mode != SHOW)
114 goto usage;
115 if (draftsw || file)
116 adios (NULL, "only one file at a time!");
117 if (!(cp = *argp++) || *cp == '-')
118 adios (NULL, "missing argument to %s", argp[-2]);
119 file = path (cp, TFILE);
120 continue;
121
122 case HEADSW:
123 headersw++;
124 continue;
125 case NHEADSW:
126 headersw = 0;
127 continue;
128
129 case FORMSW:
130 app_msgarg(&vec, --cp);
131 if (!(cp = *argp++) || *cp == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 app_msgarg(&vec, getcpy (etcpath(cp)));
134 continue;
135
136 case PROGSW:
137 case LENSW:
138 case WIDTHSW:
139 case FMTPROCSW:
140 app_msgarg(&vec, --cp);
141 if (!(cp = *argp++) || *cp == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
143 app_msgarg(&vec, cp);
144 continue;
145
146 case SHOWSW:
147 if (!(showproc = *argp++) || *showproc == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
149 nshow = 0;
150 continue;
151 case NSHOWSW:
152 nshow++;
153 continue;
154
155 case SHOWMIMESW:
156 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
157 adios (NULL, "missing argument to %s", argp[-2]);
158 nshow = 0;
159 continue;
160 case CHECKMIMESW:
161 checkmime++;
162 continue;
163 case NOCHECKMIMESW:
164 checkmime = 0;
165 continue;
166 }
167 }
168 if (*cp == '+' || *cp == '@') {
169 if (folder)
170 adios (NULL, "only one folder at a time!");
171 else
172 folder = pluspath (cp);
173 } else {
174 if (mode != SHOW)
175 goto usage;
176 else
177 app_msgarg(&msgs, cp);
178 }
179 }
180
181 if (!context_find ("path"))
182 free (path ("./", TFOLDER));
183
184 if (draftsw || file) {
185 if (msgs.size)
186 adios (NULL, "only one file at a time!");
187 if (draftsw)
188 app_msgarg(&vec, getcpy (m_draft (folder, NULL, 1, &isdf)));
189 else
190 app_msgarg(&vec, file);
191 goto go_to_it;
192 }
193
194 #ifdef WHATNOW
195 if (!msgs.size && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
196 draftsw++;
197 app_msgarg(&vec, cp);
198 goto go_to_it;
199 }
200 #endif /* WHATNOW */
201
202 if (!msgs.size) {
203 switch (mode) {
204 case NEXT:
205 app_msgarg(&msgs, "next");
206 break;
207 case PREV:
208 app_msgarg(&msgs, "prev");
209 break;
210 default:
211 app_msgarg(&msgs, "cur");
212 break;
213 }
214 }
215
216 if (!folder)
217 folder = getfolder (1);
218 maildir = m_maildir (folder);
219
220 if (chdir (maildir) == NOTOK)
221 adios (maildir, "unable to change directory to");
222
223 /* read folder and create message structure */
224 if (!(mp = folder_read (folder, 1)))
225 adios (NULL, "unable to read folder %s", folder);
226
227 /* check for empty folder */
228 if (mp->nummsg == 0)
229 adios (NULL, "no messages in %s", folder);
230
231 /* parse all the message ranges/sequences and set SELECTED */
232 for (msgnum = 0; msgnum < msgs.size; msgnum++)
233 if (!m_convert (mp, msgs.msgs[msgnum]))
234 done (1);
235
236 /*
237 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
238 * since we will use that as a tag to know which messages
239 * to remove from the "unseen" sequence.
240 */
241 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
242 if (is_selected(mp, msgnum))
243 set_unseen (mp, msgnum);
244
245 seq_setprev (mp); /* set the Previous-Sequence */
246 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
247
248 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
249 if (is_selected(mp, msgnum))
250 app_msgarg(&vec, getcpy (m_name (msgnum)));
251
252 seq_setcur (mp, mp->hghsel); /* update current message */
253 seq_save (mp); /* synchronize sequences */
254 context_replace (pfolder, folder); /* update current folder */
255 context_save (); /* save the context file */
256
257 if (headersw && vec.size == 1)
258 printf ("(Message %s:%s)\n", folder, vec.msgs[0]);
259
260 go_to_it: ;
261 fflush (stdout);
262
263 /*
264 * Decide which "proc" to use
265 */
266 mime = 0;
267 if (nshow) {
268 proc = catproc;
269 } else {
270 /* check if any messages are non-text MIME messages */
271 if (checkmime) {
272 if (!draftsw && !file) {
273 /* loop through selected messages and check for MIME */
274 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
275 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
276 mime = 1;
277 break;
278 }
279 } else {
280 /* check the file or draft for MIME */
281 if (is_nontext (vec.msgs[vec.size - 1]))
282 mime = 1;
283 }
284 }
285
286 /* Set the "proc" */
287 if (mime)
288 proc = showmimeproc;
289 else
290 proc = showproc;
291 }
292
293 if (folder && !draftsw && !file)
294 m_putenv ("mhfolder", folder);
295
296 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
297 /* Add "-file" if showing file or draft, */
298 if (draftsw || file) {
299 app_msgarg(&vec, vec.msgs[vec.size - 1]);
300 vec.msgs[vec.size - 2] = "-file";
301 }
302 /* and add -show for backward compatibility */
303 app_msgarg(&vec, "-show");
304 } else if (strcmp (r1bindex (proc, '/'), "mhshow") == 0) {
305 /* If "mhshow", add "-file" if showing file or draft. */
306 if (draftsw || file) {
307 app_msgarg(&vec, vec.msgs[vec.size - 1]);
308 vec.msgs[vec.size - 2] = "-file";
309 }
310 } else if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
311 /* If "mhl", then run it internally */
312 argsplit_insert(&vec, "mhl", &program);
313 app_msgarg(&vec, NULL);
314 mhl (vec.size, vec.msgs);
315 done (0);
316 }
317
318 argsplit_insert(&vec, proc, &program);
319 app_msgarg(&vec, NULL);
320 execvp (program, vec.msgs);
321 adios (proc, "unable to exec");
322 return 0; /* dead code to satisfy the compiler */
323 }
324
325
326 /*
327 * Check if a message or file contains any non-text parts
328 */
329 static int
330 is_nontext (char *msgnam)
331 {
332 int result, state;
333 char *bp, *dp, *cp;
334 char buf[BUFSIZ], name[NAMESZ];
335 FILE *fp;
336 m_getfld_state_t gstate = 0;
337
338 if ((fp = fopen (msgnam, "r")) == NULL)
339 return 0;
340
341 for (;;) {
342 int bufsz = sizeof buf;
343 switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
344 case FLD:
345 case FLDPLUS:
346 /*
347 * Check Content-Type field
348 */
349 if (!strcasecmp (name, TYPE_FIELD)) {
350 int passno;
351 char c;
352
353 cp = add (buf, NULL);
354 while (state == FLDPLUS) {
355 bufsz = sizeof buf;
356 state = m_getfld (&gstate, name, buf, &bufsz, fp);
357 cp = add (buf, cp);
358 }
359 bp = cp;
360 passno = 1;
361
362 again:
363 for (; isspace ((unsigned char) *bp); bp++)
364 continue;
365 if (*bp == '(') {
366 int i;
367
368 for (bp++, i = 0;;) {
369 switch (*bp++) {
370 case '\0':
371 invalid:
372 result = 0;
373 goto out;
374 case '\\':
375 if (*bp++ == '\0')
376 goto invalid;
377 continue;
378 case '(':
379 i++;
380 /* and fall... */
381 default:
382 continue;
383 case ')':
384 if (--i < 0)
385 break;
386 continue;
387 }
388 break;
389 }
390 }
391 if (passno == 2) {
392 if (*bp != '/')
393 goto invalid;
394 bp++;
395 passno = 3;
396 goto again;
397 }
398 for (dp = bp; istoken (*dp); dp++)
399 continue;
400 c = *dp;
401 *dp = '\0';
402 if (!*bp)
403 goto invalid;
404 if (passno > 1) {
405 if ((result = (strcasecmp (bp, "plain") != 0)))
406 goto out;
407 *dp = c;
408 for (dp++; isspace ((unsigned char) *dp); dp++)
409 continue;
410 if (*dp) {
411 if ((result = !uprf (dp, "charset")))
412 goto out;
413 dp += sizeof("charset") - 1;
414 while (isspace ((unsigned char) *dp))
415 dp++;
416 if (*dp++ != '=')
417 goto invalid;
418 while (isspace ((unsigned char) *dp))
419 dp++;
420 if (*dp == '"') {
421 if ((bp = strchr(++dp, '"')))
422 *bp = '\0';
423 } else {
424 for (bp = dp; *bp; bp++)
425 if (!istoken (*bp)) {
426 *bp = '\0';
427 break;
428 }
429 }
430 } else {
431 /* Default character set */
432 dp = "US-ASCII";
433 }
434 /* Check the character set */
435 result = !check_charset (dp, strlen (dp));
436 } else {
437 if (!(result = (strcasecmp (bp, "text") != 0))) {
438 *dp = c;
439 bp = dp;
440 passno = 2;
441 goto again;
442 }
443 }
444 out:
445 free (cp);
446 if (result) {
447 fclose (fp);
448 m_getfld_state_destroy (&gstate);
449 return result;
450 }
451 break;
452 }
453
454 /*
455 * Check Content-Transfer-Encoding field
456 */
457 if (!strcasecmp (name, ENCODING_FIELD)) {
458 cp = add (buf, NULL);
459 while (state == FLDPLUS) {
460 bufsz = sizeof buf;
461 state = m_getfld (&gstate, name, buf, &bufsz, fp);
462 cp = add (buf, cp);
463 }
464 for (bp = cp; isspace ((unsigned char) *bp); bp++)
465 continue;
466 for (dp = bp; istoken ((unsigned char) *dp); dp++)
467 continue;
468 *dp = '\0';
469 result = (strcasecmp (bp, "7bit")
470 && strcasecmp (bp, "8bit")
471 && strcasecmp (bp, "binary"));
472
473 free (cp);
474 if (result) {
475 fclose (fp);
476 m_getfld_state_destroy (&gstate);
477 return result;
478 }
479 break;
480 }
481
482 /*
483 * Just skip the rest of this header
484 * field and go to next one.
485 */
486 while (state == FLDPLUS) {
487 bufsz = sizeof buf;
488 state = m_getfld (&gstate, name, buf, &bufsz, fp);
489 }
490 break;
491
492 /*
493 * We've passed the message header,
494 * so message is just text.
495 */
496 default:
497 fclose (fp);
498 m_getfld_state_destroy (&gstate);
499 return 0;
500 }
501 }
502 }