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 },
39 { "file file", -4 }, /* interface from showfile */
50 static int is_nontext(char *);
52 /* prototype from mhlsbr.c */
53 int mhl (int, char **);
61 main (int argc, char **argv)
63 int headersw = 1, msgp = 0;
64 int nshow = 0, checkmime = 1, mime;
65 int vecp = 1, procp = 1, mode = SHOW, msgnum;
66 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
67 char buf[BUFSIZ], **argp, **arguments;
68 char *msgs[MAXARGS], *vec[MAXARGS];
69 struct msgs *mp = NULL;
72 setlocale(LC_ALL, "");
74 invo_name = r1bindex (argv[0], '/');
76 /* read user profile/context */
79 if (!mh_strcasecmp (invo_name, "next")) {
81 } else if (!mh_strcasecmp (invo_name, "prev")) {
84 arguments = getarguments (invo_name, argc, argv, 1);
87 while ((cp = *argp++)) {
89 switch (smatch (++cp, switches)) {
91 ambigsw (cp, switches);
99 snprintf (buf, sizeof(buf),
100 "%s [+folder] %s[switches] [switches for showproc]",
101 invo_name, mode == SHOW ? "[msgs] ": "");
102 print_help (buf, switches, 1);
105 print_version(invo_name);
111 adios (NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
114 adios (NULL, "only one file at a time!");
115 if (!(cp = *argp++) || *cp == '-')
116 adios (NULL, "missing argument to %s", argp[-2]);
117 file = path (cp, TFILE);
129 if (!(cp = *argp++) || *cp == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
131 vec[vecp++] = getcpy (etcpath(cp));
138 if (!(cp = *argp++) || *cp == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
144 if (!(showproc = *argp++) || *showproc == '-')
145 adios (NULL, "missing argument to %s", argp[-2]);
153 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
154 adios (NULL, "missing argument to %s", argp[-2]);
165 if (*cp == '+' || *cp == '@') {
167 adios (NULL, "only one folder at a time!");
169 folder = pluspath (cp);
179 if (!context_find ("path"))
180 free (path ("./", TFOLDER));
184 adios (NULL, "only one file at a time!");
190 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
199 msgs[msgp++] = "next";
202 msgs[msgp++] = "prev";
205 msgs[msgp++] = "cur";
211 folder = getfolder (1);
212 maildir = m_maildir (folder);
214 if (chdir (maildir) == NOTOK)
215 adios (maildir, "unable to change directory to");
217 /* read folder and create message structure */
218 if (!(mp = folder_read (folder)))
219 adios (NULL, "unable to read folder %s", folder);
221 /* check for empty folder */
223 adios (NULL, "no messages in %s", folder);
225 /* parse all the message ranges/sequences and set SELECTED */
226 for (msgnum = 0; msgnum < msgp; msgnum++)
227 if (!m_convert (mp, msgs[msgnum]))
231 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
232 * since we will use that as a tag to know which messages
233 * to remove from the "unseen" sequence.
235 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
236 if (is_selected(mp, msgnum))
237 set_unseen (mp, msgnum);
239 seq_setprev (mp); /* set the Previous-Sequence */
240 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
242 if (mp->numsel > MAXARGS - 2)
243 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
245 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
246 if (is_selected(mp, msgnum))
247 vec[vecp++] = getcpy (m_name (msgnum));
249 seq_setcur (mp, mp->hghsel); /* update current message */
250 seq_save (mp); /* synchronize sequences */
251 context_replace (pfolder, folder); /* update current folder */
252 context_save (); /* save the context file */
254 if (headersw && vecp == 2)
255 printf ("(Message %s:%s)\n", folder, vec[1]);
263 * Decide which "proc" to use
269 /* check if any messages are non-text MIME messages */
270 if (checkmime && !getenv ("NOMHNPROC")) {
272 /* loop through selected messages and check for MIME */
273 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
274 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
279 /* check the file for MIME */
280 if (is_nontext (vec[vecp - 1]))
293 m_putenv ("mhfolder", folder);
296 * For backward compatibility, if the "proc" is mhn,
297 * then add "-show" option. Add "-file" if showing
300 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
302 vec[vecp] = vec[vecp - 1];
303 vec[vecp - 1] = "-file";
306 vec[vecp++] = "-show";
310 /* If the "proc" is "mhshow", add "-file" if showing file.
312 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && file ) {
313 vec[vecp] = vec[vecp - 1];
314 vec[vecp - 1] = "-file";
319 * If "proc" is mhl, then run it internally
320 * rather than exec'ing it.
322 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
329 * If you are not using a nmh command as your "proc", then
330 * add the path to the message names. Currently, we are just
331 * checking for mhn here, since we've already taken care of mhl.
333 if (!strcmp (r1bindex (proc, '/'), "mhl")
335 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
336 mp->foldpath = concat (mp->foldpath, "/", NULL);
337 cp = ssequal (maildir, mp->foldpath)
338 ? mp->foldpath + strlen (maildir)
340 for (msgnum = procp; msgnum < vecp; msgnum++)
341 vec[msgnum] = concat (cp, vec[msgnum], NULL);
344 vec[0] = r1bindex (proc, '/');
346 adios (proc, "unable to exec");
347 return 0; /* dead code to satisfy the compiler */
351 * Check if a message or file contains any non-text parts
354 is_nontext (char *msgnam)
357 unsigned char *bp, *dp;
359 char buf[BUFSIZ], name[NAMESZ];
362 if ((fp = fopen (msgnam, "r")) == NULL)
365 for (state = FLD;;) {
366 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
371 * Check Content-Type field
373 if (!mh_strcasecmp (name, TYPE_FIELD)) {
377 cp = add (buf, NULL);
378 while (state == FLDPLUS) {
379 state = m_getfld (state, name, buf, sizeof(buf), fp);
386 for (; isspace (*bp); bp++)
391 for (bp++, i = 0;;) {
421 for (dp = bp; istoken (*dp); dp++)
428 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
431 for (dp++; isspace (*dp); dp++)
434 if ((result = !uprf (dp, "charset")))
436 dp += sizeof("charset") - 1;
437 while (isspace (*dp))
441 while (isspace (*dp))
444 if ((bp = strchr(++dp, '"')))
447 for (bp = dp; *bp; bp++)
448 if (!istoken (*bp)) {
454 /* Default character set */
457 /* Check the character set */
458 result = !check_charset (dp, strlen (dp));
460 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
477 * Check Content-Transfer-Encoding field
479 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
480 cp = add (buf, NULL);
481 while (state == FLDPLUS) {
482 state = m_getfld (state, name, buf, sizeof(buf), fp);
485 for (bp = cp; isspace (*bp); bp++)
487 for (dp = bp; istoken (*dp); dp++)
490 result = (mh_strcasecmp (bp, "7bit")
491 && mh_strcasecmp (bp, "8bit")
492 && mh_strcasecmp (bp, "binary"));
503 * Just skip the rest of this header
504 * field and go to next one.
506 while (state == FLDPLUS)
507 state = m_getfld (state, name, buf, sizeof(buf), fp);
511 * We've passed the message header,
512 * so message is just text.