2 * show.c -- show/list messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 static struct swit switches[] = {
16 #define NOCHECKMIMESW 1
23 { "form formfile", 0 },
25 { "moreproc program", 0 },
29 { "length lines", 0 },
31 { "width columns", 0 },
33 { "showproc program", 0 },
35 { "showmimeproc program", 0 },
41 { "file file", -4 }, /* interface from showfile */
52 static int is_nontext(char *);
54 /* prototype from mhlsbr.c */
55 int mhl (int, char **);
63 main (int argc, char **argv)
65 int draftsw = 0, headersw = 1, msgp = 0;
66 int nshow = 0, checkmime = 1, mime;
67 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
68 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
69 char buf[BUFSIZ], **argp, **arguments;
70 char *msgs[MAXARGS], *vec[MAXARGS];
71 struct msgs *mp = NULL;
74 setlocale(LC_ALL, "");
76 invo_name = r1bindex (argv[0], '/');
78 /* read user profile/context */
81 if (!mh_strcasecmp (invo_name, "next")) {
83 } else if (!mh_strcasecmp (invo_name, "prev")) {
86 arguments = getarguments (invo_name, argc, argv, 1);
89 while ((cp = *argp++)) {
91 switch (smatch (++cp, switches)) {
93 ambigsw (cp, switches);
101 snprintf (buf, sizeof(buf),
102 "%s [+folder] %s[switches] [switches for showproc]",
103 invo_name, mode == SHOW ? "[msgs] ": "");
104 print_help (buf, switches, 1);
107 print_version(invo_name);
112 adios (NULL, "only one file at a time!");
118 "usage: %s [+folder] [switches] [switches for showproc]",
124 adios (NULL, "only one file at a time!");
125 if (!(cp = *argp++) || *cp == '-')
126 adios (NULL, "missing argument to %s", argp[-2]);
127 file = path (cp, TFILE);
139 if (!(cp = *argp++) || *cp == '-')
140 adios (NULL, "missing argument to %s", argp[-2]);
141 vec[vecp++] = getcpy (etcpath(cp));
148 if (!(cp = *argp++) || *cp == '-')
149 adios (NULL, "missing argument to %s", argp[-2]);
154 if (!(showproc = *argp++) || *showproc == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
163 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
164 adios (NULL, "missing argument to %s", argp[-2]);
175 if (*cp == '+' || *cp == '@') {
177 adios (NULL, "only one folder at a time!");
179 folder = pluspath (cp);
189 if (!context_find ("path"))
190 free (path ("./", TFOLDER));
192 if (draftsw || file) {
194 adios (NULL, "only one file at a time!");
195 vec[vecp++] = draftsw
196 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
202 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
212 msgs[msgp++] = "next";
215 msgs[msgp++] = "prev";
218 msgs[msgp++] = "cur";
224 folder = getfolder (1);
225 maildir = m_maildir (folder);
227 if (chdir (maildir) == NOTOK)
228 adios (maildir, "unable to change directory to");
230 /* read folder and create message structure */
231 if (!(mp = folder_read (folder)))
232 adios (NULL, "unable to read folder %s", folder);
234 /* check for empty folder */
236 adios (NULL, "no messages in %s", folder);
238 /* parse all the message ranges/sequences and set SELECTED */
239 for (msgnum = 0; msgnum < msgp; msgnum++)
240 if (!m_convert (mp, msgs[msgnum]))
244 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
245 * since we will use that as a tag to know which messages
246 * to remove from the "unseen" sequence.
248 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
249 if (is_selected(mp, msgnum))
250 set_unseen (mp, msgnum);
252 seq_setprev (mp); /* set the Previous-Sequence */
253 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
255 if (mp->numsel > MAXARGS - 2)
256 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
258 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
259 if (is_selected(mp, msgnum))
260 vec[vecp++] = getcpy (m_name (msgnum));
262 seq_setcur (mp, mp->hghsel); /* update current message */
263 seq_save (mp); /* synchronize sequences */
264 context_replace (pfolder, folder); /* update current folder */
265 context_save (); /* save the context file */
267 if (headersw && vecp == 2)
268 printf ("(Message %s:%s)\n", folder, vec[1]);
276 * Decide which "proc" to use
282 /* check if any messages are non-text MIME messages */
283 if (checkmime && !getenv ("NOMHNPROC")) {
284 if (!draftsw && !file) {
285 /* loop through selected messages and check for MIME */
286 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
287 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
292 /* check the file or draft for MIME */
293 if (is_nontext (vec[vecp - 1]))
305 if (folder && !draftsw && !file)
306 m_putenv ("mhfolder", folder);
309 * For backward compatibility, if the "proc" is mhn,
310 * then add "-show" option. Add "-file" if showing
313 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
314 if (draftsw || file) {
315 vec[vecp] = vec[vecp - 1];
316 vec[vecp - 1] = "-file";
319 vec[vecp++] = "-show";
323 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
325 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
326 vec[vecp] = vec[vecp - 1];
327 vec[vecp - 1] = "-file";
332 * If "proc" is mhl, then run it internally
333 * rather than exec'ing it.
335 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
342 * If you are not using a nmh command as your "proc", then
343 * add the path to the message names. Currently, we are just
344 * checking for mhn here, since we've already taken care of mhl.
346 if (!strcmp (r1bindex (proc, '/'), "mhl")
349 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
350 mp->foldpath = concat (mp->foldpath, "/", NULL);
351 cp = ssequal (maildir, mp->foldpath)
352 ? mp->foldpath + strlen (maildir)
354 for (msgnum = procp; msgnum < vecp; msgnum++)
355 vec[msgnum] = concat (cp, vec[msgnum], NULL);
358 vec[0] = r1bindex (proc, '/');
360 adios (proc, "unable to exec");
361 return 0; /* dead code to satisfy the compiler */
365 * Check if a message or file contains any non-text parts
368 is_nontext (char *msgnam)
371 unsigned char *bp, *dp;
373 char buf[BUFSIZ], name[NAMESZ];
376 if ((fp = fopen (msgnam, "r")) == NULL)
379 for (state = FLD;;) {
380 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
385 * Check Content-Type field
387 if (!mh_strcasecmp (name, TYPE_FIELD)) {
391 cp = add (buf, NULL);
392 while (state == FLDPLUS) {
393 state = m_getfld (state, name, buf, sizeof(buf), fp);
400 for (; isspace (*bp); bp++)
405 for (bp++, i = 0;;) {
435 for (dp = bp; istoken (*dp); dp++)
442 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
445 for (dp++; isspace (*dp); dp++)
448 if ((result = !uprf (dp, "charset")))
450 dp += sizeof("charset") - 1;
451 while (isspace (*dp))
455 while (isspace (*dp))
458 if ((bp = strchr(++dp, '"')))
461 for (bp = dp; *bp; bp++)
462 if (!istoken (*bp)) {
468 /* Default character set */
471 /* Check the character set */
472 result = !check_charset (dp, strlen (dp));
474 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
491 * Check Content-Transfer-Encoding field
493 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
494 cp = add (buf, NULL);
495 while (state == FLDPLUS) {
496 state = m_getfld (state, name, buf, sizeof(buf), fp);
499 for (bp = cp; isspace (*bp); bp++)
501 for (dp = bp; istoken (*dp); dp++)
504 result = (mh_strcasecmp (bp, "7bit")
505 && mh_strcasecmp (bp, "8bit")
506 && mh_strcasecmp (bp, "binary"));
517 * Just skip the rest of this header
518 * field and go to next one.
520 while (state == FLDPLUS)
521 state = m_getfld (state, name, buf, sizeof(buf), fp);
525 * We've passed the message header,
526 * so message is just text.