]>
diplodocus.org Git - nmh/blob - uip/show.c
45bb4f0161c37d3a5630f95ec995a99fa9b49a36
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/r1bindex.h"
11 #include "sbr/check_charset.h"
12 #include "sbr/m_draft.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/arglist.h"
24 #include "sbr/error.h"
28 #include "sbr/m_maildir.h"
30 #define SHOW_SWITCHES \
31 X("checkmime", 0, CHECKMIMESW) \
32 X("nocheckmime", 0, NOCHECKMIMESW) \
33 X("header", 0, HEADSW) \
34 X("noheader", 0, NHEADSW) \
35 X("form formfile", 0, FORMSW) \
36 X("moreproc program", 0, PROGSW) \
37 X("nomoreproc", 0, NPROGSW) \
38 X("length lines", 0, LENSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("showproc program", 0, SHOWSW) \
41 X("showmimeproc program", 0, SHOWMIMESW) \
42 X("noshowproc", 0, NSHOWSW) \
43 X("draft", 0, DRFTSW) \
44 X("file file", -4, FILESW) /* interface from showfile */ \
45 X("fmtproc program", 0, FMTPROCSW) \
46 X("nofmtproc", 0, NFMTPROCSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
50 * switches for mhlproc \
52 X("concat", 0, CONCATSW) \
53 X("noconcat", 0, NCONCATSW) \
55 * switches for mhshow \
57 X("part number", 0, PARTSW) \
58 X("type content", 0, TYPESW) \
59 X("prefer content", 0, PREFERSW) \
60 X("markform file", 0, MARKFORMSW) \
61 X("rcache policy", 0, RCACHESW) \
62 X("wcache policy", 0, WCACHESW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(SHOW
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
75 static int is_nontext(char *);
83 main (int argc
, char **argv
)
88 bool checkmime
= true;
90 int isdf
= 0, mode
= SHOW
, msgnum
;
91 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
92 char buf
[BUFSIZ
], **argp
, **arguments
;
93 struct msgs
*mp
= NULL
;
94 struct msgs_array msgs
= { 0, 0, NULL
};
95 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
97 if (nmh_init(argv
[0], true, true)) { return 1; }
99 if (!strcasecmp (invo_name
, "next")) {
101 } else if (!strcasecmp (invo_name
, "prev")) {
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
107 while ((cp
= *argp
++)) {
109 switch (smatch (++cp
, switches
)) {
111 ambigsw (cp
, switches
);
116 goto non_mhl_switches
;
123 /* mhl can't handle these, so keep them separate. */
124 app_msgarg(&non_mhl_vec
, --cp
);
130 app_msgarg(&vec
, --cp
);
134 snprintf (buf
, sizeof(buf
),
135 "%s [+folder] %s[switches] [switches for showproc]",
136 invo_name
, mode
== SHOW
? "[msgs] ": "");
137 print_help (buf
, switches
, 1);
140 print_version(invo_name
);
145 die("only one file at a time!");
150 die( "usage: %s [+folder] [switches] [switches for showproc]",
156 die("only one file at a time!");
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
159 file
= path (cp
, TFILE
);
163 app_msgarg(&vec
, --cp
);
164 if (!(cp
= *argp
++) || *cp
== '-')
165 die("missing argument to %s", argp
[-2]);
166 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
179 app_msgarg(&vec
, --cp
);
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 app_msgarg(&vec
, cp
);
186 if (!(showproc
= *argp
++) || *showproc
== '-')
187 die("missing argument to %s", argp
[-2]);
195 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
196 die("missing argument to %s", argp
[-2]);
207 if (*cp
== '+' || *cp
== '@') {
209 die("only one folder at a time!");
210 folder
= pluspath (cp
);
214 app_msgarg(&msgs
, cp
);
218 if (!context_find ("path"))
219 free (path ("./", TFOLDER
));
221 if (draftsw
|| file
) {
223 die("only one file at a time!");
225 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
227 app_msgarg(&vec
, file
);
235 app_msgarg(&msgs
, "next");
238 app_msgarg(&msgs
, "prev");
241 app_msgarg(&msgs
, "cur");
247 folder
= getfolder (1);
248 maildir
= m_maildir (folder
);
250 if (chdir (maildir
) == NOTOK
)
251 adios (maildir
, "unable to change directory to");
253 /* read folder and create message structure */
254 if (!(mp
= folder_read (folder
, 1)))
255 die("unable to read folder %s", folder
);
257 /* check for empty folder */
259 die("no messages in %s", folder
);
261 /* parse all the message ranges/sequences and set SELECTED */
262 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
263 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
267 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
268 * since we will use that as a tag to know which messages
269 * to remove from the "unseen" sequence.
271 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
272 if (is_selected(mp
, msgnum
))
273 set_unseen (mp
, msgnum
);
275 seq_setprev (mp
); /* set the Previous-Sequence */
276 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
278 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
279 if (is_selected(mp
, msgnum
))
280 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
282 seq_setcur (mp
, mp
->hghsel
); /* update current message */
283 seq_save (mp
); /* synchronize sequences */
284 context_replace (pfolder
, folder
); /* update current folder */
285 context_save (); /* save the context file */
290 * Decide which "proc" to use
295 /* check if any messages are non-text MIME messages */
296 if (! mime
&& checkmime
) {
297 if (!draftsw
&& !file
) {
298 /* loop through selected messages and check for MIME */
299 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
300 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
305 /* check the file or draft for MIME */
306 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
318 if (folder
&& !draftsw
&& !file
)
319 setenv("mhfolder", folder
, 1);
321 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
323 if (headersw
&& vec
.size
== 1)
324 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
326 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
328 if (headersw
&& vec
.size
== 1)
329 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
331 /* If "mhl", then run it internally */
332 argsplit_insert(&vec
, "mhl", &program
);
333 app_msgarg(&vec
, NULL
);
334 mhl (vec
.size
, vec
.msgs
);
341 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
342 if (draftsw
|| file
) {
343 /* Insert the switch before the filename. */
344 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
345 vec
.msgs
[vec
.size
- 2] = *mp
;
347 app_msgarg(&vec
, *mp
);
351 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
352 /* Add "-file" if showing file or draft, */
353 if (draftsw
|| file
) {
354 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
355 vec
.msgs
[vec
.size
- 2] = "-file";
357 /* and add -show for backward compatibility */
358 app_msgarg(&vec
, "-show");
359 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
360 /* If "mhshow", add "-file" if showing file or draft. */
361 if (draftsw
|| file
) {
362 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
363 vec
.msgs
[vec
.size
- 2] = "-file";
366 if (headersw
&& vec
.size
== 1)
367 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
373 argsplit_insert(&vec
, proc
, &program
);
374 app_msgarg(&vec
, NULL
);
375 execvp (program
, vec
.msgs
);
376 adios (proc
, "unable to exec");
377 return 0; /* dead code to satisfy the compiler */
382 * Check if a message or file contains any non-text parts
385 is_nontext (char *msgnam
)
389 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
391 m_getfld_state_t gstate
;
393 if ((fp
= fopen (msgnam
, "r")) == NULL
)
395 gstate
= m_getfld_state_init(fp
);
397 int bufsz
= sizeof buf
;
398 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
402 * Check Content-Type field
404 if (!strcasecmp (name
, TYPE_FIELD
)) {
408 cp
= mh_xstrdup(buf
);
409 while (state
== FLDPLUS
) {
411 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
418 for (; isspace ((unsigned char) *bp
); bp
++)
423 for (bp
++, i
= 0;;) {
453 for (dp
= bp
; istoken (*dp
); dp
++)
460 if ((result
= (strcasecmp (bp
, "plain") != 0)))
463 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
466 if ((result
= !uprf (dp
, "charset")))
468 dp
+= LEN("charset");
469 while (isspace ((unsigned char) *dp
))
473 while (isspace ((unsigned char) *dp
))
476 if ((bp
= strchr(++dp
, '"')))
479 for (bp
= dp
; *bp
; bp
++)
480 if (!istoken (*bp
)) {
486 /* Default character set */
489 /* Check the character set */
490 result
= !check_charset (dp
, strlen (dp
));
492 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
503 m_getfld_state_destroy (&gstate
);
510 * Check Content-Transfer-Encoding field
512 if (!strcasecmp (name
, ENCODING_FIELD
)) {
513 cp
= mh_xstrdup(buf
);
514 while (state
== FLDPLUS
) {
516 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
519 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
521 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
524 result
= (strcasecmp (bp
, "7bit")
525 && strcasecmp (bp
, "8bit")
526 && strcasecmp (bp
, "binary"));
531 m_getfld_state_destroy (&gstate
);
538 * Just skip the rest of this header
539 * field and go to next one.
541 while (state
== FLDPLUS
) {
543 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
548 * We've passed the message header,
549 * so message is just text.
553 m_getfld_state_destroy (&gstate
);