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