]> diplodocus.org Git - nmh/blob - uip/show.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[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 };
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 /* Use showmimeproc if one of these switches was
89 specified because mhl doesn't understand them. */
90 mime = 1;
91 /* fall thru */
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, '/'), "mhn") == 0) {
294 /* Add "-file" if showing file or draft, */
295 if (draftsw || file) {
296 app_msgarg(&vec, vec.msgs[vec.size - 1]);
297 vec.msgs[vec.size - 2] = "-file";
298 }
299 /* and add -show for backward compatibility */
300 app_msgarg(&vec, "-show");
301 } else if (strcmp (r1bindex (proc, '/'), "mhshow") == 0) {
302 /* If "mhshow", add "-file" if showing file or draft. */
303 if (draftsw || file) {
304 app_msgarg(&vec, vec.msgs[vec.size - 1]);
305 vec.msgs[vec.size - 2] = "-file";
306 }
307 } else if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
308 /* If "mhl", then run it internally */
309 argsplit_insert(&vec, "mhl", &program);
310 app_msgarg(&vec, NULL);
311 mhl (vec.size, vec.msgs);
312 done (0);
313 }
314
315 argsplit_insert(&vec, proc, &program);
316 app_msgarg(&vec, NULL);
317 execvp (program, vec.msgs);
318 adios (proc, "unable to exec");
319 return 0; /* dead code to satisfy the compiler */
320 }
321
322
323 /*
324 * Check if a message or file contains any non-text parts
325 */
326 static int
327 is_nontext (char *msgnam)
328 {
329 int result, state;
330 char *bp, *dp, *cp;
331 char buf[BUFSIZ], name[NAMESZ];
332 FILE *fp;
333 m_getfld_state_t gstate = 0;
334
335 if ((fp = fopen (msgnam, "r")) == NULL)
336 return 0;
337
338 for (;;) {
339 int bufsz = sizeof buf;
340 switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
341 case FLD:
342 case FLDPLUS:
343 /*
344 * Check Content-Type field
345 */
346 if (!strcasecmp (name, TYPE_FIELD)) {
347 int passno;
348 char c;
349
350 cp = add (buf, NULL);
351 while (state == FLDPLUS) {
352 bufsz = sizeof buf;
353 state = m_getfld (&gstate, name, buf, &bufsz, fp);
354 cp = add (buf, cp);
355 }
356 bp = cp;
357 passno = 1;
358
359 again:
360 for (; isspace ((unsigned char) *bp); bp++)
361 continue;
362 if (*bp == '(') {
363 int i;
364
365 for (bp++, i = 0;;) {
366 switch (*bp++) {
367 case '\0':
368 invalid:
369 result = 0;
370 goto out;
371 case '\\':
372 if (*bp++ == '\0')
373 goto invalid;
374 continue;
375 case '(':
376 i++;
377 /* and fall... */
378 default:
379 continue;
380 case ')':
381 if (--i < 0)
382 break;
383 continue;
384 }
385 break;
386 }
387 }
388 if (passno == 2) {
389 if (*bp != '/')
390 goto invalid;
391 bp++;
392 passno = 3;
393 goto again;
394 }
395 for (dp = bp; istoken (*dp); dp++)
396 continue;
397 c = *dp;
398 *dp = '\0';
399 if (!*bp)
400 goto invalid;
401 if (passno > 1) {
402 if ((result = (strcasecmp (bp, "plain") != 0)))
403 goto out;
404 *dp = c;
405 for (dp++; isspace ((unsigned char) *dp); dp++)
406 continue;
407 if (*dp) {
408 if ((result = !uprf (dp, "charset")))
409 goto out;
410 dp += sizeof("charset") - 1;
411 while (isspace ((unsigned char) *dp))
412 dp++;
413 if (*dp++ != '=')
414 goto invalid;
415 while (isspace ((unsigned char) *dp))
416 dp++;
417 if (*dp == '"') {
418 if ((bp = strchr(++dp, '"')))
419 *bp = '\0';
420 } else {
421 for (bp = dp; *bp; bp++)
422 if (!istoken (*bp)) {
423 *bp = '\0';
424 break;
425 }
426 }
427 } else {
428 /* Default character set */
429 dp = "US-ASCII";
430 }
431 /* Check the character set */
432 result = !check_charset (dp, strlen (dp));
433 } else {
434 if (!(result = (strcasecmp (bp, "text") != 0))) {
435 *dp = c;
436 bp = dp;
437 passno = 2;
438 goto again;
439 }
440 }
441 out:
442 free (cp);
443 if (result) {
444 fclose (fp);
445 m_getfld_state_destroy (&gstate);
446 return result;
447 }
448 break;
449 }
450
451 /*
452 * Check Content-Transfer-Encoding field
453 */
454 if (!strcasecmp (name, ENCODING_FIELD)) {
455 cp = add (buf, NULL);
456 while (state == FLDPLUS) {
457 bufsz = sizeof buf;
458 state = m_getfld (&gstate, name, buf, &bufsz, fp);
459 cp = add (buf, cp);
460 }
461 for (bp = cp; isspace ((unsigned char) *bp); bp++)
462 continue;
463 for (dp = bp; istoken ((unsigned char) *dp); dp++)
464 continue;
465 *dp = '\0';
466 result = (strcasecmp (bp, "7bit")
467 && strcasecmp (bp, "8bit")
468 && strcasecmp (bp, "binary"));
469
470 free (cp);
471 if (result) {
472 fclose (fp);
473 m_getfld_state_destroy (&gstate);
474 return result;
475 }
476 break;
477 }
478
479 /*
480 * Just skip the rest of this header
481 * field and go to next one.
482 */
483 while (state == FLDPLUS) {
484 bufsz = sizeof buf;
485 state = m_getfld (&gstate, name, buf, &bufsz, fp);
486 }
487 break;
488
489 /*
490 * We've passed the message header,
491 * so message is just text.
492 */
493 default:
494 fclose (fp);
495 m_getfld_state_destroy (&gstate);
496 return 0;
497 }
498 }
499 }