]>
diplodocus.org Git - nmh/blob - uip/show.c
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.
12 #include "sbr/m_maildir.h"
14 #define SHOW_SWITCHES \
15 X("checkmime", 0, CHECKMIMESW) \
16 X("nocheckmime", 0, NOCHECKMIMESW) \
17 X("header", 0, HEADSW) \
18 X("noheader", 0, NHEADSW) \
19 X("form formfile", 0, FORMSW) \
20 X("moreproc program", 0, PROGSW) \
21 X("nomoreproc", 0, NPROGSW) \
22 X("length lines", 0, LENSW) \
23 X("width columns", 0, WIDTHSW) \
24 X("showproc program", 0, SHOWSW) \
25 X("showmimeproc program", 0, SHOWMIMESW) \
26 X("noshowproc", 0, NSHOWSW) \
27 X("draft", 0, DRFTSW) \
28 X("file file", -4, FILESW) /* interface from showfile */ \
29 X("fmtproc program", 0, FMTPROCSW) \
30 X("nofmtproc", 0, NFMTPROCSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 * switches for mhlproc \
36 X("concat", 0, CONCATSW) \
37 X("noconcat", 0, NCONCATSW) \
39 * switches for mhshow \
41 X("part number", 0, PARTSW) \
42 X("type content", 0, TYPESW) \
43 X("prefer content", 0, PREFERSW) \
44 X("markform file", 0, MARKFORMSW) \
45 X("rcache policy", 0, RCACHESW) \
46 X("wcache policy", 0, WCACHESW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(SHOW
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
59 static int is_nontext(char *);
67 main (int argc
, char **argv
)
72 bool checkmime
= true;
74 int isdf
= 0, mode
= SHOW
, msgnum
;
75 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
76 char buf
[BUFSIZ
], **argp
, **arguments
;
77 struct msgs
*mp
= NULL
;
78 struct msgs_array msgs
= { 0, 0, NULL
};
79 struct msgs_array vec
= { 0, 0, NULL
}, non_mhl_vec
= { 0, 0, NULL
};
81 if (nmh_init(argv
[0], true, true)) { return 1; }
83 if (!strcasecmp (invo_name
, "next")) {
85 } else if (!strcasecmp (invo_name
, "prev")) {
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
91 while ((cp
= *argp
++)) {
93 switch (smatch (++cp
, switches
)) {
95 ambigsw (cp
, switches
);
100 goto non_mhl_switches
;
107 /* mhl can't handle these, so keep them separate. */
108 app_msgarg(&non_mhl_vec
, --cp
);
114 app_msgarg(&vec
, --cp
);
118 snprintf (buf
, sizeof(buf
),
119 "%s [+folder] %s[switches] [switches for showproc]",
120 invo_name
, mode
== SHOW
? "[msgs] ": "");
121 print_help (buf
, switches
, 1);
124 print_version(invo_name
);
129 die("only one file at a time!");
134 die( "usage: %s [+folder] [switches] [switches for showproc]",
140 die("only one file at a time!");
141 if (!(cp
= *argp
++) || *cp
== '-')
142 die("missing argument to %s", argp
[-2]);
143 file
= path (cp
, TFILE
);
147 app_msgarg(&vec
, --cp
);
148 if (!(cp
= *argp
++) || *cp
== '-')
149 die("missing argument to %s", argp
[-2]);
150 app_msgarg(&vec
, mh_xstrdup(etcpath(cp
)));
163 app_msgarg(&vec
, --cp
);
164 if (!(cp
= *argp
++) || *cp
== '-')
165 die("missing argument to %s", argp
[-2]);
166 app_msgarg(&vec
, cp
);
170 if (!(showproc
= *argp
++) || *showproc
== '-')
171 die("missing argument to %s", argp
[-2]);
179 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
180 die("missing argument to %s", argp
[-2]);
191 if (*cp
== '+' || *cp
== '@') {
193 die("only one folder at a time!");
194 folder
= pluspath (cp
);
198 app_msgarg(&msgs
, cp
);
202 if (!context_find ("path"))
203 free (path ("./", TFOLDER
));
205 if (draftsw
|| file
) {
207 die("only one file at a time!");
209 app_msgarg(&vec
, mh_xstrdup(m_draft(folder
, NULL
, 1, &isdf
)));
211 app_msgarg(&vec
, file
);
219 app_msgarg(&msgs
, "next");
222 app_msgarg(&msgs
, "prev");
225 app_msgarg(&msgs
, "cur");
231 folder
= getfolder (1);
232 maildir
= m_maildir (folder
);
234 if (chdir (maildir
) == NOTOK
)
235 adios (maildir
, "unable to change directory to");
237 /* read folder and create message structure */
238 if (!(mp
= folder_read (folder
, 1)))
239 die("unable to read folder %s", folder
);
241 /* check for empty folder */
243 die("no messages in %s", folder
);
245 /* parse all the message ranges/sequences and set SELECTED */
246 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
247 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
251 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
252 * since we will use that as a tag to know which messages
253 * to remove from the "unseen" sequence.
255 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
256 if (is_selected(mp
, msgnum
))
257 set_unseen (mp
, msgnum
);
259 seq_setprev (mp
); /* set the Previous-Sequence */
260 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
262 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
263 if (is_selected(mp
, msgnum
))
264 app_msgarg(&vec
, mh_xstrdup(m_name (msgnum
)));
266 seq_setcur (mp
, mp
->hghsel
); /* update current message */
267 seq_save (mp
); /* synchronize sequences */
268 context_replace (pfolder
, folder
); /* update current folder */
269 context_save (); /* save the context file */
274 * Decide which "proc" to use
279 /* check if any messages are non-text MIME messages */
280 if (! mime
&& checkmime
) {
281 if (!draftsw
&& !file
) {
282 /* loop through selected messages and check for MIME */
283 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
284 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
289 /* check the file or draft for MIME */
290 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
302 if (folder
&& !draftsw
&& !file
)
303 setenv("mhfolder", folder
, 1);
305 if (strcmp (r1bindex (proc
, '/'), "cat") == 0) {
307 if (headersw
&& vec
.size
== 1)
308 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
310 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
312 if (headersw
&& vec
.size
== 1)
313 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
315 /* If "mhl", then run it internally */
316 argsplit_insert(&vec
, "mhl", &program
);
317 app_msgarg(&vec
, NULL
);
318 mhl (vec
.size
, vec
.msgs
);
325 for (i
= 0, mp
= non_mhl_vec
.msgs
; i
< non_mhl_vec
.size
; ++i
, ++mp
) {
326 if (draftsw
|| file
) {
327 /* Insert the switch before the filename. */
328 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
329 vec
.msgs
[vec
.size
- 2] = *mp
;
331 app_msgarg(&vec
, *mp
);
335 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
336 /* Add "-file" if showing file or draft, */
337 if (draftsw
|| file
) {
338 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
339 vec
.msgs
[vec
.size
- 2] = "-file";
341 /* and add -show for backward compatibility */
342 app_msgarg(&vec
, "-show");
343 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
344 /* If "mhshow", add "-file" if showing file or draft. */
345 if (draftsw
|| file
) {
346 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
347 vec
.msgs
[vec
.size
- 2] = "-file";
350 if (headersw
&& vec
.size
== 1)
351 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
357 argsplit_insert(&vec
, proc
, &program
);
358 app_msgarg(&vec
, NULL
);
359 execvp (program
, vec
.msgs
);
360 adios (proc
, "unable to exec");
361 return 0; /* dead code to satisfy the compiler */
366 * Check if a message or file contains any non-text parts
369 is_nontext (char *msgnam
)
373 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
375 m_getfld_state_t gstate
;
377 if ((fp
= fopen (msgnam
, "r")) == NULL
)
379 gstate
= m_getfld_state_init(fp
);
381 int bufsz
= sizeof buf
;
382 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
386 * Check Content-Type field
388 if (!strcasecmp (name
, TYPE_FIELD
)) {
392 cp
= mh_xstrdup(buf
);
393 while (state
== FLDPLUS
) {
395 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
402 for (; isspace ((unsigned char) *bp
); bp
++)
407 for (bp
++, i
= 0;;) {
437 for (dp
= bp
; istoken (*dp
); dp
++)
444 if ((result
= (strcasecmp (bp
, "plain") != 0)))
447 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
450 if ((result
= !uprf (dp
, "charset")))
452 dp
+= LEN("charset");
453 while (isspace ((unsigned char) *dp
))
457 while (isspace ((unsigned char) *dp
))
460 if ((bp
= strchr(++dp
, '"')))
463 for (bp
= dp
; *bp
; bp
++)
464 if (!istoken (*bp
)) {
470 /* Default character set */
473 /* Check the character set */
474 result
= !check_charset (dp
, strlen (dp
));
476 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
487 m_getfld_state_destroy (&gstate
);
494 * Check Content-Transfer-Encoding field
496 if (!strcasecmp (name
, ENCODING_FIELD
)) {
497 cp
= mh_xstrdup(buf
);
498 while (state
== FLDPLUS
) {
500 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
503 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
505 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
508 result
= (strcasecmp (bp
, "7bit")
509 && strcasecmp (bp
, "8bit")
510 && strcasecmp (bp
, "binary"));
515 m_getfld_state_destroy (&gstate
);
522 * Just skip the rest of this header
523 * field and go to next one.
525 while (state
== FLDPLUS
) {
527 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
532 * We've passed the message header,
533 * so message is just text.
537 m_getfld_state_destroy (&gstate
);