]>
diplodocus.org Git - nmh/blob - uip/show.c
1 /* show.c -- show/list messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include "sbr/m_name.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/seq_setunseen.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/smatch.h"
18 #include "sbr/r1bindex.h"
20 #include "sbr/check_charset.h"
21 #include "sbr/m_draft.h"
22 #include "sbr/m_convert.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/ambigsw.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/arglist.h"
33 #include "sbr/error.h"
37 #include "sbr/m_maildir.h"
39 #define SHOW_SWITCHES \
40 X("checkmime", 0, CHECKMIMESW) \
41 X("nocheckmime", 0, NOCHECKMIMESW) \
42 X("header", 0, HEADSW) \
43 X("noheader", 0, NHEADSW) \
44 X("form formfile", 0, FORMSW) \
45 X("moreproc program", 0, PROGSW) \
46 X("nomoreproc", 0, NPROGSW) \
47 X("length lines", 0, LENSW) \
48 X("width columns", 0, WIDTHSW) \
49 X("showproc program", 0, SHOWSW) \
50 X("showmimeproc program", 0, SHOWMIMESW) \
51 X("noshowproc", 0, NSHOWSW) \
52 X("draft", 0, DRFTSW) \
53 X("file file", -4, FILESW) /* interface from showfile */ \
54 X("fmtproc program", 0, FMTPROCSW) \
55 X("nofmtproc", 0, NFMTPROCSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
59 * switches for mhlproc \
61 X("concat", 0, CONCATSW) \
62 X("noconcat", 0, NCONCATSW) \
64 * switches for mhshow \
66 X("part number", 0, PARTSW) \
67 X("type content", 0, TYPESW) \
68 X("prefer content", 0, PREFERSW) \
69 X("markform file", 0, MARKFORMSW) \
70 X("rcache policy", 0, RCACHESW) \
71 X("wcache policy", 0, WCACHESW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(SHOW
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
84 static int is_nontext(char *);
92 main (int argc
, char **argv
)
97 bool checkmime
= true;
99 int isdf
= 0, mode
= SHOW
, msgnum
;
100 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
101 char buf
[BUFSIZ
], **argp
, **arguments
;
102 struct msgs
*mp
= NULL
;
103 struct msgs_array msgs
= { 0, 0, NULL
};
104 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
106 if (nmh_init(argv
[0], true, true)) { return 1; }
108 if (!strcasecmp (invo_name
, "next")) {
110 } else if (!strcasecmp (invo_name
, "prev")) {
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
118 switch (smatch (++cp
, switches
)) {
120 ambigsw (cp
, switches
);
125 goto non_mhl_switches
;
132 /* mhl can't handle these, so keep them separate. */
133 app_msgarg(&non_mhl_vec
, --cp
);
139 app_msgarg(&vec
, --cp
);
143 snprintf (buf
, sizeof(buf
),
144 "%s [+folder] %s[switches] [switches for showproc]",
145 invo_name
, mode
== SHOW
? "[msgs] ": "");
146 print_help (buf
, switches
, 1);
149 print_version(invo_name
);
154 die("only one file at a time!");
159 die( "usage: %s [+folder] [switches] [switches for showproc]",
165 die("only one file at a time!");
166 if (!(cp
= *argp
++) || *cp
== '-')
167 die("missing argument to %s", argp
[-2]);
168 file
= path (cp
, TFILE
);
172 app_msgarg(&vec
, --cp
);
173 if (!(cp
= *argp
++) || *cp
== '-')
174 die("missing argument to %s", argp
[-2]);
175 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
188 app_msgarg(&vec
, --cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 app_msgarg(&vec
, cp
);
195 if (!(showproc
= *argp
++) || *showproc
== '-')
196 die("missing argument to %s", argp
[-2]);
204 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
205 die("missing argument to %s", argp
[-2]);
216 if (*cp
== '+' || *cp
== '@') {
218 die("only one folder at a time!");
219 folder
= pluspath (cp
);
223 app_msgarg(&msgs
, cp
);
227 if (!context_find ("path"))
228 free (path ("./", TFOLDER
));
230 if (draftsw
|| file
) {
232 die("only one file at a time!");
234 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
236 app_msgarg(&vec
, file
);
244 app_msgarg(&msgs
, "next");
247 app_msgarg(&msgs
, "prev");
250 app_msgarg(&msgs
, "cur");
256 folder
= getfolder (1);
257 maildir
= m_maildir (folder
);
259 if (chdir (maildir
) == NOTOK
)
260 adios (maildir
, "unable to change directory to");
262 /* read folder and create message structure */
263 if (!(mp
= folder_read (folder
, 1)))
264 die("unable to read folder %s", folder
);
266 /* check for empty folder */
268 die("no messages in %s", folder
);
270 /* parse all the message ranges/sequences and set SELECTED */
271 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
272 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
276 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
277 * since we will use that as a tag to know which messages
278 * to remove from the "unseen" sequence.
280 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
281 if (is_selected(mp
, msgnum
))
282 set_unseen (mp
, msgnum
);
284 seq_setprev (mp
); /* set the Previous-Sequence */
285 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
287 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
288 if (is_selected(mp
, msgnum
))
289 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
291 seq_setcur (mp
, mp
->hghsel
); /* update current message */
292 seq_save (mp
); /* synchronize sequences */
293 context_replace (pfolder
, folder
); /* update current folder */
294 context_save (); /* save the context file */
299 * Decide which "proc" to use
304 /* check if any messages are non-text MIME messages */
305 if (! mime
&& checkmime
) {
306 if (!draftsw
&& !file
) {
307 /* loop through selected messages and check for MIME */
308 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
309 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
314 /* check the file or draft for MIME */
315 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
327 if (folder
&& !draftsw
&& !file
)
328 setenv("mhfolder", folder
, 1);
330 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
332 if (headersw
&& vec
.size
== 1)
333 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
335 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
337 if (headersw
&& vec
.size
== 1)
338 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
340 /* If "mhl", then run it internally */
341 argsplit_insert(&vec
, "mhl", &program
);
342 app_msgarg(&vec
, NULL
);
343 mhl (vec
.size
, vec
.msgs
);
350 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
351 if (draftsw
|| file
) {
352 /* Insert the switch before the filename. */
353 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
354 vec
.msgs
[vec
.size
- 2] = *mp
;
356 app_msgarg(&vec
, *mp
);
360 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
361 /* Add "-file" if showing file or draft, */
362 if (draftsw
|| file
) {
363 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
364 vec
.msgs
[vec
.size
- 2] = "-file";
366 /* and add -show for backward compatibility */
367 app_msgarg(&vec
, "-show");
368 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
369 /* If "mhshow", add "-file" if showing file or draft. */
370 if (draftsw
|| file
) {
371 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
372 vec
.msgs
[vec
.size
- 2] = "-file";
375 if (headersw
&& vec
.size
== 1)
376 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
382 argsplit_insert(&vec
, proc
, &program
);
383 app_msgarg(&vec
, NULL
);
384 execvp (program
, vec
.msgs
);
385 adios (proc
, "unable to exec");
386 return 0; /* dead code to satisfy the compiler */
391 * Check if a message or file contains any non-text parts
394 is_nontext (char *msgnam
)
398 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
400 m_getfld_state_t gstate
;
402 if ((fp
= fopen (msgnam
, "r")) == NULL
)
404 gstate
= m_getfld_state_init(fp
);
406 int bufsz
= sizeof buf
;
407 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
411 * Check Content-Type field
413 if (!strcasecmp (name
, TYPE_FIELD
)) {
417 cp
= mh_xstrdup(buf
);
418 while (state
== FLDPLUS
) {
420 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
427 for (; isspace ((unsigned char) *bp
); bp
++)
432 for (bp
++, i
= 0;;) {
462 for (dp
= bp
; istoken (*dp
); dp
++)
469 if ((result
= (strcasecmp (bp
, "plain") != 0)))
472 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
475 if ((result
= !uprf (dp
, "charset")))
477 dp
+= LEN("charset");
478 while (isspace ((unsigned char) *dp
))
482 while (isspace ((unsigned char) *dp
))
485 if ((bp
= strchr(++dp
, '"')))
488 for (bp
= dp
; *bp
; bp
++)
489 if (!istoken (*bp
)) {
495 /* Default character set */
498 /* Check the character set */
499 result
= !check_charset (dp
, strlen (dp
));
501 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
512 m_getfld_state_destroy (&gstate
);
519 * Check Content-Transfer-Encoding field
521 if (!strcasecmp (name
, ENCODING_FIELD
)) {
522 cp
= mh_xstrdup(buf
);
523 while (state
== FLDPLUS
) {
525 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
528 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
530 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
533 result
= (strcasecmp (bp
, "7bit")
534 && strcasecmp (bp
, "8bit")
535 && strcasecmp (bp
, "binary"));
540 m_getfld_state_destroy (&gstate
);
547 * Just skip the rest of this header
548 * field and go to next one.
550 while (state
== FLDPLUS
) {
552 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
557 * We've passed the message header,
558 * so message is just text.
562 m_getfld_state_destroy (&gstate
);