]>
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.
9 #include "sbr/seq_setunseen.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/r1bindex.h"
16 #include "sbr/check_charset.h"
17 #include "sbr/m_draft.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/arglist.h"
29 #include "sbr/error.h"
33 #include "sbr/m_maildir.h"
35 #define SHOW_SWITCHES \
36 X("checkmime", 0, CHECKMIMESW) \
37 X("nocheckmime", 0, NOCHECKMIMESW) \
38 X("header", 0, HEADSW) \
39 X("noheader", 0, NHEADSW) \
40 X("form formfile", 0, FORMSW) \
41 X("moreproc program", 0, PROGSW) \
42 X("nomoreproc", 0, NPROGSW) \
43 X("length lines", 0, LENSW) \
44 X("width columns", 0, WIDTHSW) \
45 X("showproc program", 0, SHOWSW) \
46 X("showmimeproc program", 0, SHOWMIMESW) \
47 X("noshowproc", 0, NSHOWSW) \
48 X("draft", 0, DRFTSW) \
49 X("file file", -4, FILESW) /* interface from showfile */ \
50 X("fmtproc program", 0, FMTPROCSW) \
51 X("nofmtproc", 0, NFMTPROCSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
55 * switches for mhlproc \
57 X("concat", 0, CONCATSW) \
58 X("noconcat", 0, NCONCATSW) \
60 * switches for mhshow \
62 X("part number", 0, PARTSW) \
63 X("type content", 0, TYPESW) \
64 X("prefer content", 0, PREFERSW) \
65 X("markform file", 0, MARKFORMSW) \
66 X("rcache policy", 0, RCACHESW) \
67 X("wcache policy", 0, WCACHESW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(SHOW
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
80 static int is_nontext(char *);
88 main (int argc
, char **argv
)
93 bool checkmime
= true;
95 int isdf
= 0, mode
= SHOW
, msgnum
;
96 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
97 char buf
[BUFSIZ
], **argp
, **arguments
;
98 struct msgs
*mp
= NULL
;
99 struct msgs_array msgs
= { 0, 0, NULL
};
100 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
102 if (nmh_init(argv
[0], true, true)) { return 1; }
104 if (!strcasecmp (invo_name
, "next")) {
106 } else if (!strcasecmp (invo_name
, "prev")) {
109 arguments
= getarguments (invo_name
, argc
, argv
, 1);
112 while ((cp
= *argp
++)) {
114 switch (smatch (++cp
, switches
)) {
116 ambigsw (cp
, switches
);
121 goto non_mhl_switches
;
128 /* mhl can't handle these, so keep them separate. */
129 app_msgarg(&non_mhl_vec
, --cp
);
135 app_msgarg(&vec
, --cp
);
139 snprintf (buf
, sizeof(buf
),
140 "%s [+folder] %s[switches] [switches for showproc]",
141 invo_name
, mode
== SHOW
? "[msgs] ": "");
142 print_help (buf
, switches
, 1);
145 print_version(invo_name
);
150 die("only one file at a time!");
155 die( "usage: %s [+folder] [switches] [switches for showproc]",
161 die("only one file at a time!");
162 if (!(cp
= *argp
++) || *cp
== '-')
163 die("missing argument to %s", argp
[-2]);
164 file
= path (cp
, TFILE
);
168 app_msgarg(&vec
, --cp
);
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
184 app_msgarg(&vec
, --cp
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
187 app_msgarg(&vec
, cp
);
191 if (!(showproc
= *argp
++) || *showproc
== '-')
192 die("missing argument to %s", argp
[-2]);
200 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
201 die("missing argument to %s", argp
[-2]);
212 if (*cp
== '+' || *cp
== '@') {
214 die("only one folder at a time!");
215 folder
= pluspath (cp
);
219 app_msgarg(&msgs
, cp
);
223 if (!context_find ("path"))
224 free (path ("./", TFOLDER
));
226 if (draftsw
|| file
) {
228 die("only one file at a time!");
230 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
232 app_msgarg(&vec
, file
);
240 app_msgarg(&msgs
, "next");
243 app_msgarg(&msgs
, "prev");
246 app_msgarg(&msgs
, "cur");
252 folder
= getfolder (1);
253 maildir
= m_maildir (folder
);
255 if (chdir (maildir
) == NOTOK
)
256 adios (maildir
, "unable to change directory to");
258 /* read folder and create message structure */
259 if (!(mp
= folder_read (folder
, 1)))
260 die("unable to read folder %s", folder
);
262 /* check for empty folder */
264 die("no messages in %s", folder
);
266 /* parse all the message ranges/sequences and set SELECTED */
267 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
268 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
272 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
273 * since we will use that as a tag to know which messages
274 * to remove from the "unseen" sequence.
276 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
277 if (is_selected(mp
, msgnum
))
278 set_unseen (mp
, msgnum
);
280 seq_setprev (mp
); /* set the Previous-Sequence */
281 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
283 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
284 if (is_selected(mp
, msgnum
))
285 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
287 seq_setcur (mp
, mp
->hghsel
); /* update current message */
288 seq_save (mp
); /* synchronize sequences */
289 context_replace (pfolder
, folder
); /* update current folder */
290 context_save (); /* save the context file */
295 * Decide which "proc" to use
300 /* check if any messages are non-text MIME messages */
301 if (! mime
&& checkmime
) {
302 if (!draftsw
&& !file
) {
303 /* loop through selected messages and check for MIME */
304 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
305 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
310 /* check the file or draft for MIME */
311 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
323 if (folder
&& !draftsw
&& !file
)
324 setenv("mhfolder", folder
, 1);
326 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
328 if (headersw
&& vec
.size
== 1)
329 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
331 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
333 if (headersw
&& vec
.size
== 1)
334 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
336 /* If "mhl", then run it internally */
337 argsplit_insert(&vec
, "mhl", &program
);
338 app_msgarg(&vec
, NULL
);
339 mhl (vec
.size
, vec
.msgs
);
346 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
347 if (draftsw
|| file
) {
348 /* Insert the switch before the filename. */
349 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
350 vec
.msgs
[vec
.size
- 2] = *mp
;
352 app_msgarg(&vec
, *mp
);
356 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
357 /* Add "-file" if showing file or draft, */
358 if (draftsw
|| file
) {
359 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
360 vec
.msgs
[vec
.size
- 2] = "-file";
362 /* and add -show for backward compatibility */
363 app_msgarg(&vec
, "-show");
364 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
365 /* If "mhshow", add "-file" if showing file or draft. */
366 if (draftsw
|| file
) {
367 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
368 vec
.msgs
[vec
.size
- 2] = "-file";
371 if (headersw
&& vec
.size
== 1)
372 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
378 argsplit_insert(&vec
, proc
, &program
);
379 app_msgarg(&vec
, NULL
);
380 execvp (program
, vec
.msgs
);
381 adios (proc
, "unable to exec");
382 return 0; /* dead code to satisfy the compiler */
387 * Check if a message or file contains any non-text parts
390 is_nontext (char *msgnam
)
394 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
396 m_getfld_state_t gstate
;
398 if ((fp
= fopen (msgnam
, "r")) == NULL
)
400 gstate
= m_getfld_state_init(fp
);
402 int bufsz
= sizeof buf
;
403 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
407 * Check Content-Type field
409 if (!strcasecmp (name
, TYPE_FIELD
)) {
413 cp
= mh_xstrdup(buf
);
414 while (state
== FLDPLUS
) {
416 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
423 for (; isspace ((unsigned char) *bp
); bp
++)
428 for (bp
++, i
= 0;;) {
458 for (dp
= bp
; istoken (*dp
); dp
++)
465 if ((result
= (strcasecmp (bp
, "plain") != 0)))
468 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
471 if ((result
= !uprf (dp
, "charset")))
473 dp
+= LEN("charset");
474 while (isspace ((unsigned char) *dp
))
478 while (isspace ((unsigned char) *dp
))
481 if ((bp
= strchr(++dp
, '"')))
484 for (bp
= dp
; *bp
; bp
++)
485 if (!istoken (*bp
)) {
491 /* Default character set */
494 /* Check the character set */
495 result
= !check_charset (dp
, strlen (dp
));
497 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
508 m_getfld_state_destroy (&gstate
);
515 * Check Content-Transfer-Encoding field
517 if (!strcasecmp (name
, ENCODING_FIELD
)) {
518 cp
= mh_xstrdup(buf
);
519 while (state
== FLDPLUS
) {
521 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
524 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
526 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
529 result
= (strcasecmp (bp
, "7bit")
530 && strcasecmp (bp
, "8bit")
531 && strcasecmp (bp
, "binary"));
536 m_getfld_state_destroy (&gstate
);
543 * Just skip the rest of this header
544 * field and go to next one.
546 while (state
== FLDPLUS
) {
548 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
553 * We've passed the message header,
554 * so message is just text.
558 m_getfld_state_destroy (&gstate
);