]>
diplodocus.org Git - nmh/blob - uip/show.c
004a2921891766e6792d841cf8aa9d731dbabc15
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/m_name.h"
10 #include "sbr/m_getfld.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setunseen.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/r1bindex.h"
19 #include "sbr/check_charset.h"
20 #include "sbr/m_draft.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/getfolder.h"
23 #include "sbr/folder_read.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_find.h"
27 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/arglist.h"
32 #include "sbr/error.h"
36 #include "sbr/m_maildir.h"
38 #define SHOW_SWITCHES \
39 X("checkmime", 0, CHECKMIMESW) \
40 X("nocheckmime", 0, NOCHECKMIMESW) \
41 X("header", 0, HEADSW) \
42 X("noheader", 0, NHEADSW) \
43 X("form formfile", 0, FORMSW) \
44 X("moreproc program", 0, PROGSW) \
45 X("nomoreproc", 0, NPROGSW) \
46 X("length lines", 0, LENSW) \
47 X("width columns", 0, WIDTHSW) \
48 X("showproc program", 0, SHOWSW) \
49 X("showmimeproc program", 0, SHOWMIMESW) \
50 X("noshowproc", 0, NSHOWSW) \
51 X("draft", 0, DRFTSW) \
52 X("file file", -4, FILESW) /* interface from showfile */ \
53 X("fmtproc program", 0, FMTPROCSW) \
54 X("nofmtproc", 0, NFMTPROCSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
58 * switches for mhlproc \
60 X("concat", 0, CONCATSW) \
61 X("noconcat", 0, NCONCATSW) \
63 * switches for mhshow \
65 X("part number", 0, PARTSW) \
66 X("type content", 0, TYPESW) \
67 X("prefer content", 0, PREFERSW) \
68 X("markform file", 0, MARKFORMSW) \
69 X("rcache policy", 0, RCACHESW) \
70 X("wcache policy", 0, WCACHESW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(SHOW
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
83 static int is_nontext(char *);
91 main (int argc
, char **argv
)
96 bool checkmime
= true;
98 int isdf
= 0, mode
= SHOW
, msgnum
;
99 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
100 char buf
[BUFSIZ
], **argp
, **arguments
;
101 struct msgs
*mp
= NULL
;
102 struct msgs_array msgs
= { 0, 0, NULL
};
103 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
105 if (nmh_init(argv
[0], true, true)) { return 1; }
107 if (!strcasecmp (invo_name
, "next")) {
109 } else if (!strcasecmp (invo_name
, "prev")) {
112 arguments
= getarguments (invo_name
, argc
, argv
, 1);
115 while ((cp
= *argp
++)) {
117 switch (smatch (++cp
, switches
)) {
119 ambigsw (cp
, switches
);
124 goto non_mhl_switches
;
131 /* mhl can't handle these, so keep them separate. */
132 app_msgarg(&non_mhl_vec
, --cp
);
138 app_msgarg(&vec
, --cp
);
142 snprintf (buf
, sizeof(buf
),
143 "%s [+folder] %s[switches] [switches for showproc]",
144 invo_name
, mode
== SHOW
? "[msgs] ": "");
145 print_help (buf
, switches
, 1);
148 print_version(invo_name
);
153 die("only one file at a time!");
158 die( "usage: %s [+folder] [switches] [switches for showproc]",
164 die("only one file at a time!");
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
167 file
= path (cp
, TFILE
);
171 app_msgarg(&vec
, --cp
);
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
174 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
187 app_msgarg(&vec
, --cp
);
188 if (!(cp
= *argp
++) || *cp
== '-')
189 die("missing argument to %s", argp
[-2]);
190 app_msgarg(&vec
, cp
);
194 if (!(showproc
= *argp
++) || *showproc
== '-')
195 die("missing argument to %s", argp
[-2]);
203 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
204 die("missing argument to %s", argp
[-2]);
215 if (*cp
== '+' || *cp
== '@') {
217 die("only one folder at a time!");
218 folder
= pluspath (cp
);
222 app_msgarg(&msgs
, cp
);
226 if (!context_find ("path"))
227 free (path ("./", TFOLDER
));
229 if (draftsw
|| file
) {
231 die("only one file at a time!");
233 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
235 app_msgarg(&vec
, file
);
243 app_msgarg(&msgs
, "next");
246 app_msgarg(&msgs
, "prev");
249 app_msgarg(&msgs
, "cur");
255 folder
= getfolder (1);
256 maildir
= m_maildir (folder
);
258 if (chdir (maildir
) == NOTOK
)
259 adios (maildir
, "unable to change directory to");
261 /* read folder and create message structure */
262 if (!(mp
= folder_read (folder
, 1)))
263 die("unable to read folder %s", folder
);
265 /* check for empty folder */
267 die("no messages in %s", folder
);
269 /* parse all the message ranges/sequences and set SELECTED */
270 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
271 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
275 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
276 * since we will use that as a tag to know which messages
277 * to remove from the "unseen" sequence.
279 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
280 if (is_selected(mp
, msgnum
))
281 set_unseen (mp
, msgnum
);
283 seq_setprev (mp
); /* set the Previous-Sequence */
284 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
286 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
287 if (is_selected(mp
, msgnum
))
288 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
290 seq_setcur (mp
, mp
->hghsel
); /* update current message */
291 seq_save (mp
); /* synchronize sequences */
292 context_replace (pfolder
, folder
); /* update current folder */
293 context_save (); /* save the context file */
298 * Decide which "proc" to use
303 /* check if any messages are non-text MIME messages */
304 if (! mime
&& checkmime
) {
305 if (!draftsw
&& !file
) {
306 /* loop through selected messages and check for MIME */
307 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
308 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
313 /* check the file or draft for MIME */
314 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
326 if (folder
&& !draftsw
&& !file
)
327 setenv("mhfolder", folder
, 1);
329 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
331 if (headersw
&& vec
.size
== 1)
332 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
334 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
336 if (headersw
&& vec
.size
== 1)
337 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
339 /* If "mhl", then run it internally */
340 argsplit_insert(&vec
, "mhl", &program
);
341 app_msgarg(&vec
, NULL
);
342 mhl (vec
.size
, vec
.msgs
);
349 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
350 if (draftsw
|| file
) {
351 /* Insert the switch before the filename. */
352 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
353 vec
.msgs
[vec
.size
- 2] = *mp
;
355 app_msgarg(&vec
, *mp
);
359 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
360 /* 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";
365 /* and add -show for backward compatibility */
366 app_msgarg(&vec
, "-show");
367 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
368 /* If "mhshow", add "-file" if showing file or draft. */
369 if (draftsw
|| file
) {
370 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
371 vec
.msgs
[vec
.size
- 2] = "-file";
374 if (headersw
&& vec
.size
== 1)
375 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
381 argsplit_insert(&vec
, proc
, &program
);
382 app_msgarg(&vec
, NULL
);
383 execvp (program
, vec
.msgs
);
384 adios (proc
, "unable to exec");
385 return 0; /* dead code to satisfy the compiler */
390 * Check if a message or file contains any non-text parts
393 is_nontext (char *msgnam
)
397 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
399 m_getfld_state_t gstate
;
401 if ((fp
= fopen (msgnam
, "r")) == NULL
)
403 gstate
= m_getfld_state_init(fp
);
405 int bufsz
= sizeof buf
;
406 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
410 * Check Content-Type field
412 if (!strcasecmp (name
, TYPE_FIELD
)) {
416 cp
= mh_xstrdup(buf
);
417 while (state
== FLDPLUS
) {
419 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
426 for (; isspace ((unsigned char) *bp
); bp
++)
431 for (bp
++, i
= 0;;) {
461 for (dp
= bp
; istoken (*dp
); dp
++)
468 if ((result
= (strcasecmp (bp
, "plain") != 0)))
471 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
474 if ((result
= !uprf (dp
, "charset")))
476 dp
+= LEN("charset");
477 while (isspace ((unsigned char) *dp
))
481 while (isspace ((unsigned char) *dp
))
484 if ((bp
= strchr(++dp
, '"')))
487 for (bp
= dp
; *bp
; bp
++)
488 if (!istoken (*bp
)) {
494 /* Default character set */
497 /* Check the character set */
498 result
= !check_charset (dp
, strlen (dp
));
500 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
511 m_getfld_state_destroy (&gstate
);
518 * Check Content-Transfer-Encoding field
520 if (!strcasecmp (name
, ENCODING_FIELD
)) {
521 cp
= mh_xstrdup(buf
);
522 while (state
== FLDPLUS
) {
524 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
527 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
529 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
532 result
= (strcasecmp (bp
, "7bit")
533 && strcasecmp (bp
, "8bit")
534 && strcasecmp (bp
, "binary"));
539 m_getfld_state_destroy (&gstate
);
546 * Just skip the rest of this header
547 * field and go to next one.
549 while (state
== FLDPLUS
) {
551 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
556 * We've passed the message header,
557 * so message is just text.
561 m_getfld_state_destroy (&gstate
);