]>
diplodocus.org Git - nmh/blob - uip/show.c
3e708dfb9f3df63928f0f6bd7803444150cd68ea
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/check_charset.h"
11 #include "sbr/m_draft.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/arglist.h"
23 #include "sbr/error.h"
27 #include "sbr/m_maildir.h"
29 #define SHOW_SWITCHES \
30 X("checkmime", 0, CHECKMIMESW) \
31 X("nocheckmime", 0, NOCHECKMIMESW) \
32 X("header", 0, HEADSW) \
33 X("noheader", 0, NHEADSW) \
34 X("form formfile", 0, FORMSW) \
35 X("moreproc program", 0, PROGSW) \
36 X("nomoreproc", 0, NPROGSW) \
37 X("length lines", 0, LENSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("showproc program", 0, SHOWSW) \
40 X("showmimeproc program", 0, SHOWMIMESW) \
41 X("noshowproc", 0, NSHOWSW) \
42 X("draft", 0, DRFTSW) \
43 X("file file", -4, FILESW) /* interface from showfile */ \
44 X("fmtproc program", 0, FMTPROCSW) \
45 X("nofmtproc", 0, NFMTPROCSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
49 * switches for mhlproc \
51 X("concat", 0, CONCATSW) \
52 X("noconcat", 0, NCONCATSW) \
54 * switches for mhshow \
56 X("part number", 0, PARTSW) \
57 X("type content", 0, TYPESW) \
58 X("prefer content", 0, PREFERSW) \
59 X("markform file", 0, MARKFORMSW) \
60 X("rcache policy", 0, RCACHESW) \
61 X("wcache policy", 0, WCACHESW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(SHOW
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
74 static int is_nontext(char *);
82 main (int argc
, char **argv
)
87 bool checkmime
= true;
89 int isdf
= 0, mode
= SHOW
, msgnum
;
90 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
91 char buf
[BUFSIZ
], **argp
, **arguments
;
92 struct msgs
*mp
= NULL
;
93 struct msgs_array msgs
= { 0, 0, NULL
};
94 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
96 if (nmh_init(argv
[0], true, true)) { return 1; }
98 if (!strcasecmp (invo_name
, "next")) {
100 } else if (!strcasecmp (invo_name
, "prev")) {
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
115 goto non_mhl_switches
;
122 /* mhl can't handle these, so keep them separate. */
123 app_msgarg(&non_mhl_vec
, --cp
);
129 app_msgarg(&vec
, --cp
);
133 snprintf (buf
, sizeof(buf
),
134 "%s [+folder] %s[switches] [switches for showproc]",
135 invo_name
, mode
== SHOW
? "[msgs] ": "");
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
144 die("only one file at a time!");
149 die( "usage: %s [+folder] [switches] [switches for showproc]",
155 die("only one file at a time!");
156 if (!(cp
= *argp
++) || *cp
== '-')
157 die("missing argument to %s", argp
[-2]);
158 file
= path (cp
, TFILE
);
162 app_msgarg(&vec
, --cp
);
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
165 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
178 app_msgarg(&vec
, --cp
);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
181 app_msgarg(&vec
, cp
);
185 if (!(showproc
= *argp
++) || *showproc
== '-')
186 die("missing argument to %s", argp
[-2]);
194 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
195 die("missing argument to %s", argp
[-2]);
206 if (*cp
== '+' || *cp
== '@') {
208 die("only one folder at a time!");
209 folder
= pluspath (cp
);
213 app_msgarg(&msgs
, cp
);
217 if (!context_find ("path"))
218 free (path ("./", TFOLDER
));
220 if (draftsw
|| file
) {
222 die("only one file at a time!");
224 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
226 app_msgarg(&vec
, file
);
234 app_msgarg(&msgs
, "next");
237 app_msgarg(&msgs
, "prev");
240 app_msgarg(&msgs
, "cur");
246 folder
= getfolder (1);
247 maildir
= m_maildir (folder
);
249 if (chdir (maildir
) == NOTOK
)
250 adios (maildir
, "unable to change directory to");
252 /* read folder and create message structure */
253 if (!(mp
= folder_read (folder
, 1)))
254 die("unable to read folder %s", folder
);
256 /* check for empty folder */
258 die("no messages in %s", folder
);
260 /* parse all the message ranges/sequences and set SELECTED */
261 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
262 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
266 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
267 * since we will use that as a tag to know which messages
268 * to remove from the "unseen" sequence.
270 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
271 if (is_selected(mp
, msgnum
))
272 set_unseen (mp
, msgnum
);
274 seq_setprev (mp
); /* set the Previous-Sequence */
275 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
277 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
278 if (is_selected(mp
, msgnum
))
279 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
281 seq_setcur (mp
, mp
->hghsel
); /* update current message */
282 seq_save (mp
); /* synchronize sequences */
283 context_replace (pfolder
, folder
); /* update current folder */
284 context_save (); /* save the context file */
289 * Decide which "proc" to use
294 /* check if any messages are non-text MIME messages */
295 if (! mime
&& checkmime
) {
296 if (!draftsw
&& !file
) {
297 /* loop through selected messages and check for MIME */
298 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
299 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
304 /* check the file or draft for MIME */
305 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
317 if (folder
&& !draftsw
&& !file
)
318 setenv("mhfolder", folder
, 1);
320 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
322 if (headersw
&& vec
.size
== 1)
323 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
325 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
327 if (headersw
&& vec
.size
== 1)
328 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
330 /* If "mhl", then run it internally */
331 argsplit_insert(&vec
, "mhl", &program
);
332 app_msgarg(&vec
, NULL
);
333 mhl (vec
.size
, vec
.msgs
);
340 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
341 if (draftsw
|| file
) {
342 /* Insert the switch before the filename. */
343 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
344 vec
.msgs
[vec
.size
- 2] = *mp
;
346 app_msgarg(&vec
, *mp
);
350 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
351 /* Add "-file" if showing file or draft, */
352 if (draftsw
|| file
) {
353 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
354 vec
.msgs
[vec
.size
- 2] = "-file";
356 /* and add -show for backward compatibility */
357 app_msgarg(&vec
, "-show");
358 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
359 /* If "mhshow", add "-file" if showing file or draft. */
360 if (draftsw
|| file
) {
361 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
362 vec
.msgs
[vec
.size
- 2] = "-file";
365 if (headersw
&& vec
.size
== 1)
366 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
372 argsplit_insert(&vec
, proc
, &program
);
373 app_msgarg(&vec
, NULL
);
374 execvp (program
, vec
.msgs
);
375 adios (proc
, "unable to exec");
376 return 0; /* dead code to satisfy the compiler */
381 * Check if a message or file contains any non-text parts
384 is_nontext (char *msgnam
)
388 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
390 m_getfld_state_t gstate
;
392 if ((fp
= fopen (msgnam
, "r")) == NULL
)
394 gstate
= m_getfld_state_init(fp
);
396 int bufsz
= sizeof buf
;
397 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
401 * Check Content-Type field
403 if (!strcasecmp (name
, TYPE_FIELD
)) {
407 cp
= mh_xstrdup(buf
);
408 while (state
== FLDPLUS
) {
410 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
417 for (; isspace ((unsigned char) *bp
); bp
++)
422 for (bp
++, i
= 0;;) {
452 for (dp
= bp
; istoken (*dp
); dp
++)
459 if ((result
= (strcasecmp (bp
, "plain") != 0)))
462 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
465 if ((result
= !uprf (dp
, "charset")))
467 dp
+= LEN("charset");
468 while (isspace ((unsigned char) *dp
))
472 while (isspace ((unsigned char) *dp
))
475 if ((bp
= strchr(++dp
, '"')))
478 for (bp
= dp
; *bp
; bp
++)
479 if (!istoken (*bp
)) {
485 /* Default character set */
488 /* Check the character set */
489 result
= !check_charset (dp
, strlen (dp
));
491 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
502 m_getfld_state_destroy (&gstate
);
509 * Check Content-Transfer-Encoding field
511 if (!strcasecmp (name
, ENCODING_FIELD
)) {
512 cp
= mh_xstrdup(buf
);
513 while (state
== FLDPLUS
) {
515 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
518 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
520 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
523 result
= (strcasecmp (bp
, "7bit")
524 && strcasecmp (bp
, "8bit")
525 && strcasecmp (bp
, "binary"));
530 m_getfld_state_destroy (&gstate
);
537 * Just skip the rest of this header
538 * field and go to next one.
540 while (state
== FLDPLUS
) {
542 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
547 * We've passed the message header,
548 * so message is just text.
552 m_getfld_state_destroy (&gstate
);