]>
diplodocus.org Git - nmh/blob - uip/show.c
7573432e63ba6cf146336140278cfe1169535a32
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/arglist.h"
10 #include "sbr/error.h"
14 #include "sbr/m_maildir.h"
16 #define SHOW_SWITCHES \
17 X("checkmime", 0, CHECKMIMESW) \
18 X("nocheckmime", 0, NOCHECKMIMESW) \
19 X("header", 0, HEADSW) \
20 X("noheader", 0, NHEADSW) \
21 X("form formfile", 0, FORMSW) \
22 X("moreproc program", 0, PROGSW) \
23 X("nomoreproc", 0, NPROGSW) \
24 X("length lines", 0, LENSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("showproc program", 0, SHOWSW) \
27 X("showmimeproc program", 0, SHOWMIMESW) \
28 X("noshowproc", 0, NSHOWSW) \
29 X("draft", 0, DRFTSW) \
30 X("file file", -4, FILESW) /* interface from showfile */ \
31 X("fmtproc program", 0, FMTPROCSW) \
32 X("nofmtproc", 0, NFMTPROCSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
36 * switches for mhlproc \
38 X("concat", 0, CONCATSW) \
39 X("noconcat", 0, NCONCATSW) \
41 * switches for mhshow \
43 X("part number", 0, PARTSW) \
44 X("type content", 0, TYPESW) \
45 X("prefer content", 0, PREFERSW) \
46 X("markform file", 0, MARKFORMSW) \
47 X("rcache policy", 0, RCACHESW) \
48 X("wcache policy", 0, WCACHESW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(SHOW
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
61 static int is_nontext(char *);
69 main (int argc
, char **argv
)
74 bool checkmime
= true;
76 int isdf
= 0, mode
= SHOW
, msgnum
;
77 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
78 char buf
[BUFSIZ
], **argp
, **arguments
;
79 struct msgs
*mp
= NULL
;
80 struct msgs_array msgs
= { 0, 0, NULL
};
81 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
83 if (nmh_init(argv
[0], true, true)) { return 1; }
85 if (!strcasecmp (invo_name
, "next")) {
87 } else if (!strcasecmp (invo_name
, "prev")) {
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
93 while ((cp
= *argp
++)) {
95 switch (smatch (++cp
, switches
)) {
97 ambigsw (cp
, switches
);
102 goto non_mhl_switches
;
109 /* mhl can't handle these, so keep them separate. */
110 app_msgarg(&non_mhl_vec
, --cp
);
116 app_msgarg(&vec
, --cp
);
120 snprintf (buf
, sizeof(buf
),
121 "%s [+folder] %s[switches] [switches for showproc]",
122 invo_name
, mode
== SHOW
? "[msgs] ": "");
123 print_help (buf
, switches
, 1);
126 print_version(invo_name
);
131 die("only one file at a time!");
136 die( "usage: %s [+folder] [switches] [switches for showproc]",
142 die("only one file at a time!");
143 if (!(cp
= *argp
++) || *cp
== '-')
144 die("missing argument to %s", argp
[-2]);
145 file
= path (cp
, TFILE
);
149 app_msgarg(&vec
, --cp
);
150 if (!(cp
= *argp
++) || *cp
== '-')
151 die("missing argument to %s", argp
[-2]);
152 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
165 app_msgarg(&vec
, --cp
);
166 if (!(cp
= *argp
++) || *cp
== '-')
167 die("missing argument to %s", argp
[-2]);
168 app_msgarg(&vec
, cp
);
172 if (!(showproc
= *argp
++) || *showproc
== '-')
173 die("missing argument to %s", argp
[-2]);
181 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
182 die("missing argument to %s", argp
[-2]);
193 if (*cp
== '+' || *cp
== '@') {
195 die("only one folder at a time!");
196 folder
= pluspath (cp
);
200 app_msgarg(&msgs
, cp
);
204 if (!context_find ("path"))
205 free (path ("./", TFOLDER
));
207 if (draftsw
|| file
) {
209 die("only one file at a time!");
211 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
213 app_msgarg(&vec
, file
);
221 app_msgarg(&msgs
, "next");
224 app_msgarg(&msgs
, "prev");
227 app_msgarg(&msgs
, "cur");
233 folder
= getfolder (1);
234 maildir
= m_maildir (folder
);
236 if (chdir (maildir
) == NOTOK
)
237 adios (maildir
, "unable to change directory to");
239 /* read folder and create message structure */
240 if (!(mp
= folder_read (folder
, 1)))
241 die("unable to read folder %s", folder
);
243 /* check for empty folder */
245 die("no messages in %s", folder
);
247 /* parse all the message ranges/sequences and set SELECTED */
248 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
249 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
253 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
254 * since we will use that as a tag to know which messages
255 * to remove from the "unseen" sequence.
257 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
258 if (is_selected(mp
, msgnum
))
259 set_unseen (mp
, msgnum
);
261 seq_setprev (mp
); /* set the Previous-Sequence */
262 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
264 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
265 if (is_selected(mp
, msgnum
))
266 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
268 seq_setcur (mp
, mp
->hghsel
); /* update current message */
269 seq_save (mp
); /* synchronize sequences */
270 context_replace (pfolder
, folder
); /* update current folder */
271 context_save (); /* save the context file */
276 * Decide which "proc" to use
281 /* check if any messages are non-text MIME messages */
282 if (! mime
&& checkmime
) {
283 if (!draftsw
&& !file
) {
284 /* loop through selected messages and check for MIME */
285 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
286 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
291 /* check the file or draft for MIME */
292 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
304 if (folder
&& !draftsw
&& !file
)
305 setenv("mhfolder", folder
, 1);
307 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
309 if (headersw
&& vec
.size
== 1)
310 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
312 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
314 if (headersw
&& vec
.size
== 1)
315 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
317 /* If "mhl", then run it internally */
318 argsplit_insert(&vec
, "mhl", &program
);
319 app_msgarg(&vec
, NULL
);
320 mhl (vec
.size
, vec
.msgs
);
327 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
328 if (draftsw
|| file
) {
329 /* Insert the switch before the filename. */
330 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
331 vec
.msgs
[vec
.size
- 2] = *mp
;
333 app_msgarg(&vec
, *mp
);
337 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
338 /* Add "-file" if showing file or draft, */
339 if (draftsw
|| file
) {
340 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
341 vec
.msgs
[vec
.size
- 2] = "-file";
343 /* and add -show for backward compatibility */
344 app_msgarg(&vec
, "-show");
345 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
346 /* If "mhshow", add "-file" if showing file or draft. */
347 if (draftsw
|| file
) {
348 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
349 vec
.msgs
[vec
.size
- 2] = "-file";
352 if (headersw
&& vec
.size
== 1)
353 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
359 argsplit_insert(&vec
, proc
, &program
);
360 app_msgarg(&vec
, NULL
);
361 execvp (program
, vec
.msgs
);
362 adios (proc
, "unable to exec");
363 return 0; /* dead code to satisfy the compiler */
368 * Check if a message or file contains any non-text parts
371 is_nontext (char *msgnam
)
375 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
377 m_getfld_state_t gstate
;
379 if ((fp
= fopen (msgnam
, "r")) == NULL
)
381 gstate
= m_getfld_state_init(fp
);
383 int bufsz
= sizeof buf
;
384 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
388 * Check Content-Type field
390 if (!strcasecmp (name
, TYPE_FIELD
)) {
394 cp
= mh_xstrdup(buf
);
395 while (state
== FLDPLUS
) {
397 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
404 for (; isspace ((unsigned char) *bp
); bp
++)
409 for (bp
++, i
= 0;;) {
439 for (dp
= bp
; istoken (*dp
); dp
++)
446 if ((result
= (strcasecmp (bp
, "plain") != 0)))
449 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
452 if ((result
= !uprf (dp
, "charset")))
454 dp
+= LEN("charset");
455 while (isspace ((unsigned char) *dp
))
459 while (isspace ((unsigned char) *dp
))
462 if ((bp
= strchr(++dp
, '"')))
465 for (bp
= dp
; *bp
; bp
++)
466 if (!istoken (*bp
)) {
472 /* Default character set */
475 /* Check the character set */
476 result
= !check_charset (dp
, strlen (dp
));
478 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
489 m_getfld_state_destroy (&gstate
);
496 * Check Content-Transfer-Encoding field
498 if (!strcasecmp (name
, ENCODING_FIELD
)) {
499 cp
= mh_xstrdup(buf
);
500 while (state
== FLDPLUS
) {
502 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
505 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
507 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
510 result
= (strcasecmp (bp
, "7bit")
511 && strcasecmp (bp
, "8bit")
512 && strcasecmp (bp
, "binary"));
517 m_getfld_state_destroy (&gstate
);
524 * Just skip the rest of this header
525 * field and go to next one.
527 while (state
== FLDPLUS
) {
529 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
534 * We've passed the message header,
535 * so message is just text.
539 m_getfld_state_destroy (&gstate
);