]>
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
);
197 app_msgarg(&msgs
, "next");
200 app_msgarg(&msgs
, "prev");
203 app_msgarg(&msgs
, "cur");
209 folder
= getfolder (1);
210 maildir
= m_maildir (folder
);
212 if (chdir (maildir
) == NOTOK
)
213 adios (maildir
, "unable to change directory to");
215 /* read folder and create message structure */
216 if (!(mp
= folder_read (folder
, 1)))
217 adios (NULL
, "unable to read folder %s", folder
);
219 /* check for empty folder */
221 adios (NULL
, "no messages in %s", folder
);
223 /* parse all the message ranges/sequences and set SELECTED */
224 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
225 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
229 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
230 * since we will use that as a tag to know which messages
231 * to remove from the "unseen" sequence.
233 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
234 if (is_selected(mp
, msgnum
))
235 set_unseen (mp
, msgnum
);
237 seq_setprev (mp
); /* set the Previous-Sequence */
238 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
240 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
241 if (is_selected(mp
, msgnum
))
242 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
244 seq_setcur (mp
, mp
->hghsel
); /* update current message */
245 seq_save (mp
); /* synchronize sequences */
246 context_replace (pfolder
, folder
); /* update current folder */
247 context_save (); /* save the context file */
249 if (headersw
&& vec
.size
== 1)
250 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
256 * Decide which "proc" to use
262 /* check if any messages are non-text MIME messages */
264 if (!draftsw
&& !file
) {
265 /* loop through selected messages and check for MIME */
266 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
267 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
272 /* check the file or draft for MIME */
273 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
285 if (folder
&& !draftsw
&& !file
)
286 m_putenv ("mhfolder", folder
);
288 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
289 /* Add "-file" if showing file or draft, */
290 if (draftsw
|| file
) {
291 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
292 vec
.msgs
[vec
.size
- 2] = "-file";
294 /* and add -show for backward compatibility */
295 app_msgarg(&vec
, "-show");
296 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
297 /* If "mhshow", 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 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
303 /* If "mhl", then run it internally */
304 argsplit_insert(&vec
, "mhl", &program
);
305 app_msgarg(&vec
, NULL
);
306 mhl (vec
.size
, vec
.msgs
);
310 argsplit_insert(&vec
, proc
, &program
);
311 app_msgarg(&vec
, NULL
);
312 execvp (program
, vec
.msgs
);
313 adios (proc
, "unable to exec");
314 return 0; /* dead code to satisfy the compiler */
319 * Check if a message or file contains any non-text parts
322 is_nontext (char *msgnam
)
326 char buf
[BUFSIZ
], name
[NAMESZ
];
328 m_getfld_state_t gstate
= 0;
330 if ((fp
= fopen (msgnam
, "r")) == NULL
)
334 int bufsz
= sizeof buf
;
335 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
339 * Check Content-Type field
341 if (!strcasecmp (name
, TYPE_FIELD
)) {
345 cp
= add (buf
, NULL
);
346 while (state
== FLDPLUS
) {
348 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
355 for (; isspace ((unsigned char) *bp
); bp
++)
360 for (bp
++, i
= 0;;) {
390 for (dp
= bp
; istoken (*dp
); dp
++)
397 if ((result
= (strcasecmp (bp
, "plain") != 0)))
400 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
403 if ((result
= !uprf (dp
, "charset")))
405 dp
+= sizeof("charset") - 1;
406 while (isspace ((unsigned char) *dp
))
410 while (isspace ((unsigned char) *dp
))
413 if ((bp
= strchr(++dp
, '"')))
416 for (bp
= dp
; *bp
; bp
++)
417 if (!istoken (*bp
)) {
423 /* Default character set */
426 /* Check the character set */
427 result
= !check_charset (dp
, strlen (dp
));
429 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
440 m_getfld_state_destroy (&gstate
);
447 * Check Content-Transfer-Encoding field
449 if (!strcasecmp (name
, ENCODING_FIELD
)) {
450 cp
= add (buf
, NULL
);
451 while (state
== FLDPLUS
) {
453 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
456 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
458 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
461 result
= (strcasecmp (bp
, "7bit")
462 && strcasecmp (bp
, "8bit")
463 && strcasecmp (bp
, "binary"));
468 m_getfld_state_destroy (&gstate
);
475 * Just skip the rest of this header
476 * field and go to next one.
478 while (state
== FLDPLUS
) {
480 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
485 * We've passed the message header,
486 * so message is just text.
490 m_getfld_state_destroy (&gstate
);