]>
diplodocus.org Git - nmh/blob - uip/show.c
3 * show.c -- show/list messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
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 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(SHOW
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
45 static int is_nontext(char *);
53 main (int argc
, char **argv
)
55 int draftsw
= 0, headersw
= 1;
56 int nshow
= 0, checkmime
= 1, mime
;
57 int isdf
= 0, mode
= SHOW
, msgnum
;
58 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
, *proc
, *program
;
59 char buf
[BUFSIZ
], **argp
, **arguments
;
60 struct msgs
*mp
= NULL
;
61 struct msgs_array msgs
= { 0, 0, NULL
};
62 struct msgs_array vec
= { 0, 0, NULL
};
65 setlocale(LC_ALL
, "");
67 invo_name
= r1bindex (argv
[0], '/');
69 /* read user profile/context */
72 if (!strcasecmp (invo_name
, "next")) {
74 } else if (!strcasecmp (invo_name
, "prev")) {
77 arguments
= getarguments (invo_name
, argc
, argv
, 1);
80 while ((cp
= *argp
++)) {
82 switch (smatch (++cp
, switches
)) {
84 ambigsw (cp
, switches
);
89 app_msgarg(&vec
, --cp
);
93 snprintf (buf
, sizeof(buf
),
94 "%s [+folder] %s[switches] [switches for showproc]",
95 invo_name
, mode
== SHOW
? "[msgs] ": "");
96 print_help (buf
, switches
, 1);
99 print_version(invo_name
);
104 adios (NULL
, "only one file at a time!");
110 "usage: %s [+folder] [switches] [switches for showproc]",
116 adios (NULL
, "only one file at a time!");
117 if (!(cp
= *argp
++) || *cp
== '-')
118 adios (NULL
, "missing argument to %s", argp
[-2]);
119 file
= path (cp
, TFILE
);
130 app_msgarg(&vec
, --cp
);
131 if (!(cp
= *argp
++) || *cp
== '-')
132 adios (NULL
, "missing argument to %s", argp
[-2]);
133 app_msgarg(&vec
, getcpy (etcpath(cp
)));
140 app_msgarg(&vec
, --cp
);
141 if (!(cp
= *argp
++) || *cp
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
143 app_msgarg(&vec
, cp
);
147 if (!(showproc
= *argp
++) || *showproc
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
156 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
157 adios (NULL
, "missing argument to %s", argp
[-2]);
168 if (*cp
== '+' || *cp
== '@') {
170 adios (NULL
, "only one folder at a time!");
172 folder
= pluspath (cp
);
177 app_msgarg(&msgs
, cp
);
181 if (!context_find ("path"))
182 free (path ("./", TFOLDER
));
184 if (draftsw
|| file
) {
186 adios (NULL
, "only one file at a time!");
188 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
190 app_msgarg(&vec
, file
);
195 if (!msgs
.size
&& !folder
&& mode
== SHOW
&& (cp
= getenv ("mhdraft")) && *cp
) {
197 app_msgarg(&vec
, cp
);
205 app_msgarg(&msgs
, "next");
208 app_msgarg(&msgs
, "prev");
211 app_msgarg(&msgs
, "cur");
217 folder
= getfolder (1);
218 maildir
= m_maildir (folder
);
220 if (chdir (maildir
) == NOTOK
)
221 adios (maildir
, "unable to change directory to");
223 /* read folder and create message structure */
224 if (!(mp
= folder_read (folder
, 1)))
225 adios (NULL
, "unable to read folder %s", folder
);
227 /* check for empty folder */
229 adios (NULL
, "no messages in %s", folder
);
231 /* parse all the message ranges/sequences and set SELECTED */
232 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
233 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
237 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
238 * since we will use that as a tag to know which messages
239 * to remove from the "unseen" sequence.
241 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
242 if (is_selected(mp
, msgnum
))
243 set_unseen (mp
, msgnum
);
245 seq_setprev (mp
); /* set the Previous-Sequence */
246 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
248 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
249 if (is_selected(mp
, msgnum
))
250 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
252 seq_setcur (mp
, mp
->hghsel
); /* update current message */
253 seq_save (mp
); /* synchronize sequences */
254 context_replace (pfolder
, folder
); /* update current folder */
255 context_save (); /* save the context file */
257 if (headersw
&& vec
.size
== 1)
258 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
264 * Decide which "proc" to use
270 /* check if any messages are non-text MIME messages */
272 if (!draftsw
&& !file
) {
273 /* loop through selected messages and check for MIME */
274 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
275 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
280 /* check the file or draft for MIME */
281 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
293 if (folder
&& !draftsw
&& !file
)
294 m_putenv ("mhfolder", folder
);
296 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
297 /* Add "-file" if showing file or draft, */
298 if (draftsw
|| file
) {
299 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
300 vec
.msgs
[vec
.size
- 2] = "-file";
302 /* and add -show for backward compatibility */
303 app_msgarg(&vec
, "-show");
304 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
305 /* If "mhshow", add "-file" if showing file or draft. */
306 if (draftsw
|| file
) {
307 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
308 vec
.msgs
[vec
.size
- 2] = "-file";
310 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
311 /* If "mhl", then run it internally */
312 argsplit_insert(&vec
, "mhl", &program
);
313 app_msgarg(&vec
, NULL
);
314 mhl (vec
.size
, vec
.msgs
);
318 argsplit_insert(&vec
, proc
, &program
);
319 app_msgarg(&vec
, NULL
);
320 execvp (program
, vec
.msgs
);
321 adios (proc
, "unable to exec");
322 return 0; /* dead code to satisfy the compiler */
327 * Check if a message or file contains any non-text parts
330 is_nontext (char *msgnam
)
334 char buf
[BUFSIZ
], name
[NAMESZ
];
336 m_getfld_state_t gstate
= 0;
338 if ((fp
= fopen (msgnam
, "r")) == NULL
)
342 int bufsz
= sizeof buf
;
343 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
347 * Check Content-Type field
349 if (!strcasecmp (name
, TYPE_FIELD
)) {
353 cp
= add (buf
, NULL
);
354 while (state
== FLDPLUS
) {
356 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
363 for (; isspace ((unsigned char) *bp
); bp
++)
368 for (bp
++, i
= 0;;) {
398 for (dp
= bp
; istoken (*dp
); dp
++)
405 if ((result
= (strcasecmp (bp
, "plain") != 0)))
408 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
411 if ((result
= !uprf (dp
, "charset")))
413 dp
+= sizeof("charset") - 1;
414 while (isspace ((unsigned char) *dp
))
418 while (isspace ((unsigned char) *dp
))
421 if ((bp
= strchr(++dp
, '"')))
424 for (bp
= dp
; *bp
; bp
++)
425 if (!istoken (*bp
)) {
431 /* Default character set */
434 /* Check the character set */
435 result
= !check_charset (dp
, strlen (dp
));
437 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
448 m_getfld_state_destroy (&gstate
);
455 * Check Content-Transfer-Encoding field
457 if (!strcasecmp (name
, ENCODING_FIELD
)) {
458 cp
= add (buf
, NULL
);
459 while (state
== FLDPLUS
) {
461 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
464 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
466 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
469 result
= (strcasecmp (bp
, "7bit")
470 && strcasecmp (bp
, "8bit")
471 && strcasecmp (bp
, "binary"));
476 m_getfld_state_destroy (&gstate
);
483 * Just skip the rest of this header
484 * field and go to next one.
486 while (state
== FLDPLUS
) {
488 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
493 * We've passed the message header,
494 * so message is just text.
498 m_getfld_state_destroy (&gstate
);