]>
diplodocus.org Git - nmh/blob - uip/show.c
b35dcd83726dffe6142ba7f2e7dcaad74940be95
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/getfolder.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
23 #include "sbr/m_maildir.h"
25 #define SHOW_SWITCHES \
26 X("checkmime", 0, CHECKMIMESW) \
27 X("nocheckmime", 0, NOCHECKMIMESW) \
28 X("header", 0, HEADSW) \
29 X("noheader", 0, NHEADSW) \
30 X("form formfile", 0, FORMSW) \
31 X("moreproc program", 0, PROGSW) \
32 X("nomoreproc", 0, NPROGSW) \
33 X("length lines", 0, LENSW) \
34 X("width columns", 0, WIDTHSW) \
35 X("showproc program", 0, SHOWSW) \
36 X("showmimeproc program", 0, SHOWMIMESW) \
37 X("noshowproc", 0, NSHOWSW) \
38 X("draft", 0, DRFTSW) \
39 X("file file", -4, FILESW) /* interface from showfile */ \
40 X("fmtproc program", 0, FMTPROCSW) \
41 X("nofmtproc", 0, NFMTPROCSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 * switches for mhlproc \
47 X("concat", 0, CONCATSW) \
48 X("noconcat", 0, NCONCATSW) \
50 * switches for mhshow \
52 X("part number", 0, PARTSW) \
53 X("type content", 0, TYPESW) \
54 X("prefer content", 0, PREFERSW) \
55 X("markform file", 0, MARKFORMSW) \
56 X("rcache policy", 0, RCACHESW) \
57 X("wcache policy", 0, WCACHESW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(SHOW
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
70 static int is_nontext(char *);
78 main (int argc
, char **argv
)
83 bool checkmime
= true;
85 int isdf
= 0, mode
= SHOW
, msgnum
;
86 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
87 char buf
[BUFSIZ
], **argp
, **arguments
;
88 struct msgs
*mp
= NULL
;
89 struct msgs_array msgs
= { 0, 0, NULL
};
90 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
92 if (nmh_init(argv
[0], true, true)) { return 1; }
94 if (!strcasecmp (invo_name
, "next")) {
96 } else if (!strcasecmp (invo_name
, "prev")) {
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
111 goto non_mhl_switches
;
118 /* mhl can't handle these, so keep them separate. */
119 app_msgarg(&non_mhl_vec
, --cp
);
125 app_msgarg(&vec
, --cp
);
129 snprintf (buf
, sizeof(buf
),
130 "%s [+folder] %s[switches] [switches for showproc]",
131 invo_name
, mode
== SHOW
? "[msgs] ": "");
132 print_help (buf
, switches
, 1);
135 print_version(invo_name
);
140 die("only one file at a time!");
145 die( "usage: %s [+folder] [switches] [switches for showproc]",
151 die("only one file at a time!");
152 if (!(cp
= *argp
++) || *cp
== '-')
153 die("missing argument to %s", argp
[-2]);
154 file
= path (cp
, TFILE
);
158 app_msgarg(&vec
, --cp
);
159 if (!(cp
= *argp
++) || *cp
== '-')
160 die("missing argument to %s", argp
[-2]);
161 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
174 app_msgarg(&vec
, --cp
);
175 if (!(cp
= *argp
++) || *cp
== '-')
176 die("missing argument to %s", argp
[-2]);
177 app_msgarg(&vec
, cp
);
181 if (!(showproc
= *argp
++) || *showproc
== '-')
182 die("missing argument to %s", argp
[-2]);
190 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
191 die("missing argument to %s", argp
[-2]);
202 if (*cp
== '+' || *cp
== '@') {
204 die("only one folder at a time!");
205 folder
= pluspath (cp
);
209 app_msgarg(&msgs
, cp
);
213 if (!context_find ("path"))
214 free (path ("./", TFOLDER
));
216 if (draftsw
|| file
) {
218 die("only one file at a time!");
220 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
222 app_msgarg(&vec
, file
);
230 app_msgarg(&msgs
, "next");
233 app_msgarg(&msgs
, "prev");
236 app_msgarg(&msgs
, "cur");
242 folder
= getfolder (1);
243 maildir
= m_maildir (folder
);
245 if (chdir (maildir
) == NOTOK
)
246 adios (maildir
, "unable to change directory to");
248 /* read folder and create message structure */
249 if (!(mp
= folder_read (folder
, 1)))
250 die("unable to read folder %s", folder
);
252 /* check for empty folder */
254 die("no messages in %s", folder
);
256 /* parse all the message ranges/sequences and set SELECTED */
257 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
258 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
262 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
263 * since we will use that as a tag to know which messages
264 * to remove from the "unseen" sequence.
266 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
267 if (is_selected(mp
, msgnum
))
268 set_unseen (mp
, msgnum
);
270 seq_setprev (mp
); /* set the Previous-Sequence */
271 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
273 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
274 if (is_selected(mp
, msgnum
))
275 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
277 seq_setcur (mp
, mp
->hghsel
); /* update current message */
278 seq_save (mp
); /* synchronize sequences */
279 context_replace (pfolder
, folder
); /* update current folder */
280 context_save (); /* save the context file */
285 * Decide which "proc" to use
290 /* check if any messages are non-text MIME messages */
291 if (! mime
&& checkmime
) {
292 if (!draftsw
&& !file
) {
293 /* loop through selected messages and check for MIME */
294 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
295 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
300 /* check the file or draft for MIME */
301 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
313 if (folder
&& !draftsw
&& !file
)
314 setenv("mhfolder", folder
, 1);
316 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
318 if (headersw
&& vec
.size
== 1)
319 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
321 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
323 if (headersw
&& vec
.size
== 1)
324 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
326 /* If "mhl", then run it internally */
327 argsplit_insert(&vec
, "mhl", &program
);
328 app_msgarg(&vec
, NULL
);
329 mhl (vec
.size
, vec
.msgs
);
336 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
337 if (draftsw
|| file
) {
338 /* Insert the switch before the filename. */
339 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
340 vec
.msgs
[vec
.size
- 2] = *mp
;
342 app_msgarg(&vec
, *mp
);
346 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
347 /* Add "-file" if showing file or draft, */
348 if (draftsw
|| file
) {
349 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
350 vec
.msgs
[vec
.size
- 2] = "-file";
352 /* and add -show for backward compatibility */
353 app_msgarg(&vec
, "-show");
354 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
355 /* If "mhshow", add "-file" if showing file or draft. */
356 if (draftsw
|| file
) {
357 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
358 vec
.msgs
[vec
.size
- 2] = "-file";
361 if (headersw
&& vec
.size
== 1)
362 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
368 argsplit_insert(&vec
, proc
, &program
);
369 app_msgarg(&vec
, NULL
);
370 execvp (program
, vec
.msgs
);
371 adios (proc
, "unable to exec");
372 return 0; /* dead code to satisfy the compiler */
377 * Check if a message or file contains any non-text parts
380 is_nontext (char *msgnam
)
384 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
386 m_getfld_state_t gstate
;
388 if ((fp
= fopen (msgnam
, "r")) == NULL
)
390 gstate
= m_getfld_state_init(fp
);
392 int bufsz
= sizeof buf
;
393 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
397 * Check Content-Type field
399 if (!strcasecmp (name
, TYPE_FIELD
)) {
403 cp
= mh_xstrdup(buf
);
404 while (state
== FLDPLUS
) {
406 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
413 for (; isspace ((unsigned char) *bp
); bp
++)
418 for (bp
++, i
= 0;;) {
448 for (dp
= bp
; istoken (*dp
); dp
++)
455 if ((result
= (strcasecmp (bp
, "plain") != 0)))
458 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
461 if ((result
= !uprf (dp
, "charset")))
463 dp
+= LEN("charset");
464 while (isspace ((unsigned char) *dp
))
468 while (isspace ((unsigned char) *dp
))
471 if ((bp
= strchr(++dp
, '"')))
474 for (bp
= dp
; *bp
; bp
++)
475 if (!istoken (*bp
)) {
481 /* Default character set */
484 /* Check the character set */
485 result
= !check_charset (dp
, strlen (dp
));
487 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
498 m_getfld_state_destroy (&gstate
);
505 * Check Content-Transfer-Encoding field
507 if (!strcasecmp (name
, ENCODING_FIELD
)) {
508 cp
= mh_xstrdup(buf
);
509 while (state
== FLDPLUS
) {
511 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
514 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
516 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
519 result
= (strcasecmp (bp
, "7bit")
520 && strcasecmp (bp
, "8bit")
521 && strcasecmp (bp
, "binary"));
526 m_getfld_state_destroy (&gstate
);
533 * Just skip the rest of this header
534 * field and go to next one.
536 while (state
== FLDPLUS
) {
538 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
543 * We've passed the message header,
544 * so message is just text.
548 m_getfld_state_destroy (&gstate
);