]>
diplodocus.org Git - nmh/blob - uip/show.c
c94757a613d70ff22329397850503706b500fdd6
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/context_save.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/arglist.h"
17 #include "sbr/error.h"
21 #include "sbr/m_maildir.h"
23 #define SHOW_SWITCHES \
24 X("checkmime", 0, CHECKMIMESW) \
25 X("nocheckmime", 0, NOCHECKMIMESW) \
26 X("header", 0, HEADSW) \
27 X("noheader", 0, NHEADSW) \
28 X("form formfile", 0, FORMSW) \
29 X("moreproc program", 0, PROGSW) \
30 X("nomoreproc", 0, NPROGSW) \
31 X("length lines", 0, LENSW) \
32 X("width columns", 0, WIDTHSW) \
33 X("showproc program", 0, SHOWSW) \
34 X("showmimeproc program", 0, SHOWMIMESW) \
35 X("noshowproc", 0, NSHOWSW) \
36 X("draft", 0, DRFTSW) \
37 X("file file", -4, FILESW) /* interface from showfile */ \
38 X("fmtproc program", 0, FMTPROCSW) \
39 X("nofmtproc", 0, NFMTPROCSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 * switches for mhlproc \
45 X("concat", 0, CONCATSW) \
46 X("noconcat", 0, NCONCATSW) \
48 * switches for mhshow \
50 X("part number", 0, PARTSW) \
51 X("type content", 0, TYPESW) \
52 X("prefer content", 0, PREFERSW) \
53 X("markform file", 0, MARKFORMSW) \
54 X("rcache policy", 0, RCACHESW) \
55 X("wcache policy", 0, WCACHESW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(SHOW
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
68 static int is_nontext(char *);
76 main (int argc
, char **argv
)
81 bool checkmime
= true;
83 int isdf
= 0, mode
= SHOW
, msgnum
;
84 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
85 char buf
[BUFSIZ
], **argp
, **arguments
;
86 struct msgs
*mp
= NULL
;
87 struct msgs_array msgs
= { 0, 0, NULL
};
88 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
90 if (nmh_init(argv
[0], true, true)) { return 1; }
92 if (!strcasecmp (invo_name
, "next")) {
94 } else if (!strcasecmp (invo_name
, "prev")) {
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
109 goto non_mhl_switches
;
116 /* mhl can't handle these, so keep them separate. */
117 app_msgarg(&non_mhl_vec
, --cp
);
123 app_msgarg(&vec
, --cp
);
127 snprintf (buf
, sizeof(buf
),
128 "%s [+folder] %s[switches] [switches for showproc]",
129 invo_name
, mode
== SHOW
? "[msgs] ": "");
130 print_help (buf
, switches
, 1);
133 print_version(invo_name
);
138 die("only one file at a time!");
143 die( "usage: %s [+folder] [switches] [switches for showproc]",
149 die("only one file at a time!");
150 if (!(cp
= *argp
++) || *cp
== '-')
151 die("missing argument to %s", argp
[-2]);
152 file
= path (cp
, TFILE
);
156 app_msgarg(&vec
, --cp
);
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
159 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
172 app_msgarg(&vec
, --cp
);
173 if (!(cp
= *argp
++) || *cp
== '-')
174 die("missing argument to %s", argp
[-2]);
175 app_msgarg(&vec
, cp
);
179 if (!(showproc
= *argp
++) || *showproc
== '-')
180 die("missing argument to %s", argp
[-2]);
188 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
189 die("missing argument to %s", argp
[-2]);
200 if (*cp
== '+' || *cp
== '@') {
202 die("only one folder at a time!");
203 folder
= pluspath (cp
);
207 app_msgarg(&msgs
, cp
);
211 if (!context_find ("path"))
212 free (path ("./", TFOLDER
));
214 if (draftsw
|| file
) {
216 die("only one file at a time!");
218 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
220 app_msgarg(&vec
, file
);
228 app_msgarg(&msgs
, "next");
231 app_msgarg(&msgs
, "prev");
234 app_msgarg(&msgs
, "cur");
240 folder
= getfolder (1);
241 maildir
= m_maildir (folder
);
243 if (chdir (maildir
) == NOTOK
)
244 adios (maildir
, "unable to change directory to");
246 /* read folder and create message structure */
247 if (!(mp
= folder_read (folder
, 1)))
248 die("unable to read folder %s", folder
);
250 /* check for empty folder */
252 die("no messages in %s", folder
);
254 /* parse all the message ranges/sequences and set SELECTED */
255 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
256 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
260 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
261 * since we will use that as a tag to know which messages
262 * to remove from the "unseen" sequence.
264 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
265 if (is_selected(mp
, msgnum
))
266 set_unseen (mp
, msgnum
);
268 seq_setprev (mp
); /* set the Previous-Sequence */
269 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
271 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
272 if (is_selected(mp
, msgnum
))
273 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
275 seq_setcur (mp
, mp
->hghsel
); /* update current message */
276 seq_save (mp
); /* synchronize sequences */
277 context_replace (pfolder
, folder
); /* update current folder */
278 context_save (); /* save the context file */
283 * Decide which "proc" to use
288 /* check if any messages are non-text MIME messages */
289 if (! mime
&& checkmime
) {
290 if (!draftsw
&& !file
) {
291 /* loop through selected messages and check for MIME */
292 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
293 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
298 /* check the file or draft for MIME */
299 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
311 if (folder
&& !draftsw
&& !file
)
312 setenv("mhfolder", folder
, 1);
314 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
316 if (headersw
&& vec
.size
== 1)
317 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
319 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
321 if (headersw
&& vec
.size
== 1)
322 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
324 /* If "mhl", then run it internally */
325 argsplit_insert(&vec
, "mhl", &program
);
326 app_msgarg(&vec
, NULL
);
327 mhl (vec
.size
, vec
.msgs
);
334 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
335 if (draftsw
|| file
) {
336 /* Insert the switch before the filename. */
337 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
338 vec
.msgs
[vec
.size
- 2] = *mp
;
340 app_msgarg(&vec
, *mp
);
344 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
345 /* Add "-file" if showing file or draft, */
346 if (draftsw
|| file
) {
347 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
348 vec
.msgs
[vec
.size
- 2] = "-file";
350 /* and add -show for backward compatibility */
351 app_msgarg(&vec
, "-show");
352 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
353 /* If "mhshow", add "-file" if showing file or draft. */
354 if (draftsw
|| file
) {
355 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
356 vec
.msgs
[vec
.size
- 2] = "-file";
359 if (headersw
&& vec
.size
== 1)
360 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
366 argsplit_insert(&vec
, proc
, &program
);
367 app_msgarg(&vec
, NULL
);
368 execvp (program
, vec
.msgs
);
369 adios (proc
, "unable to exec");
370 return 0; /* dead code to satisfy the compiler */
375 * Check if a message or file contains any non-text parts
378 is_nontext (char *msgnam
)
382 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
384 m_getfld_state_t gstate
;
386 if ((fp
= fopen (msgnam
, "r")) == NULL
)
388 gstate
= m_getfld_state_init(fp
);
390 int bufsz
= sizeof buf
;
391 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
395 * Check Content-Type field
397 if (!strcasecmp (name
, TYPE_FIELD
)) {
401 cp
= mh_xstrdup(buf
);
402 while (state
== FLDPLUS
) {
404 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
411 for (; isspace ((unsigned char) *bp
); bp
++)
416 for (bp
++, i
= 0;;) {
446 for (dp
= bp
; istoken (*dp
); dp
++)
453 if ((result
= (strcasecmp (bp
, "plain") != 0)))
456 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
459 if ((result
= !uprf (dp
, "charset")))
461 dp
+= LEN("charset");
462 while (isspace ((unsigned char) *dp
))
466 while (isspace ((unsigned char) *dp
))
469 if ((bp
= strchr(++dp
, '"')))
472 for (bp
= dp
; *bp
; bp
++)
473 if (!istoken (*bp
)) {
479 /* Default character set */
482 /* Check the character set */
483 result
= !check_charset (dp
, strlen (dp
));
485 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
496 m_getfld_state_destroy (&gstate
);
503 * Check Content-Transfer-Encoding field
505 if (!strcasecmp (name
, ENCODING_FIELD
)) {
506 cp
= mh_xstrdup(buf
);
507 while (state
== FLDPLUS
) {
509 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
512 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
514 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
517 result
= (strcasecmp (bp
, "7bit")
518 && strcasecmp (bp
, "8bit")
519 && strcasecmp (bp
, "binary"));
524 m_getfld_state_destroy (&gstate
);
531 * Just skip the rest of this header
532 * field and go to next one.
534 while (state
== FLDPLUS
) {
536 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
541 * We've passed the message header,
542 * so message is just text.
546 m_getfld_state_destroy (&gstate
);