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