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 = mhbasename(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), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
100 print_help(buf, switches, 1);
103 print_version(invo_name);
109 adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
112 adios(NULL, "only one file at a time!");
113 if (!(cp = *argp++) || *cp == '-')
114 adios(NULL, "missing argument to %s", argp[-2]);
115 file = getcpy(expanddir(cp));
127 if (!(cp = *argp++) || *cp == '-')
128 adios(NULL, "missing argument to %s", argp[-2]);
129 vec[vecp++] = getcpy(etcpath(cp));
136 if (!(cp = *argp++) || *cp == '-')
137 adios(NULL, "missing argument to %s", argp[-2]);
142 if (!(showproc = *argp++) || *showproc == '-')
143 adios(NULL, "missing argument to %s", argp[-2]);
151 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
152 adios(NULL, "missing argument to %s", argp[-2]);
163 if (*cp == '+' || *cp == '@') {
165 adios(NULL, "only one folder at a time!");
167 folder = getcpy(expandfol(cp));
168 } else if (mode != SHOW) {
178 adios(NULL, "only one file at a time!");
184 if (!msgp && !folder && mode == SHOW &&
185 (cp = getenv("mhdraft")) && *cp) {
194 msgs[msgp++] = "next";
197 msgs[msgp++] = "prev";
200 msgs[msgp++] = "cur";
206 folder = getcurfol();
207 maildir = toabsdir(folder);
209 if (chdir(maildir) == NOTOK)
210 adios(maildir, "unable to change directory to");
212 /* read folder and create message structure */
213 if (!(mp = folder_read(folder)))
214 adios(NULL, "unable to read folder %s", folder);
216 /* check for empty folder */
218 adios(NULL, "no messages in %s", folder);
220 /* parse all the message ranges/sequences and set SELECTED */
221 for (msgnum = 0; msgnum < msgp; msgnum++)
222 if (!m_convert(mp, msgs[msgnum]))
226 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
227 ** since we will use that as a tag to know which messages
228 ** to remove from the "unseen" sequence.
230 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
231 if (is_selected(mp, msgnum))
232 set_unseen(mp, msgnum);
234 seq_setprev(mp); /* set the Previous-Sequence */
235 seq_setunseen(mp, 1); /* unset the Unseen-Sequence */
237 if (mp->numsel > MAXARGS - 2)
238 adios(NULL, "more than %d messages for show exec",
241 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
242 if (is_selected(mp, msgnum))
243 vec[vecp++] = getcpy(m_name(msgnum));
245 seq_setcur(mp, mp->hghsel); /* update current message */
246 seq_save(mp); /* synchronize sequences */
247 context_replace(curfolder, folder); /* update current folder */
248 context_save(); /* save the context file */
250 if (headersw && vecp == 2)
251 printf("(Message %s:%s)\n", folder, vec[1]);
259 ** Decide which "proc" to use
265 /* check if any messages are non-text MIME messages */
266 if (checkmime && !getenv("NOMHNPROC")) {
269 ** loop through selected messages
270 ** and check for MIME
272 for (msgnum = mp->lowsel;
273 msgnum <= mp->hghsel;
275 if (is_selected(mp, msgnum) && is_nontext(m_name(msgnum))) {
280 /* check the file for MIME */
281 if (is_nontext(vec[vecp - 1]))
294 m_putenv("mhfolder", folder);
297 ** For backward compatibility, if the "proc" is mhn,
298 ** then add "-show" option. Add "-file" if showing
301 if (strcmp(mhbasename(proc), "mhn") == 0) {
303 vec[vecp] = vec[vecp - 1];
304 vec[vecp - 1] = "-file";
307 vec[vecp++] = "-show";
311 /* If the "proc" is "mhshow", add "-file" if showing file. */
312 if (strcmp(mhbasename(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(mhbasename(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(mhbasename(proc), "mhl")
336 concat(toabsdir("+"), "/", NULL)) != NOTOK) {
337 mp->foldpath = concat(mp->foldpath, "/", NULL);
338 cp = isprefix(maildir, mp->foldpath)
339 ? mp->foldpath + strlen(maildir)
341 for (msgnum = procp; msgnum < vecp; msgnum++)
342 vec[msgnum] = concat(cp, vec[msgnum], NULL);
345 vec[0] = mhbasename(proc);
347 adios(proc, "unable to exec");
348 return 0; /* dead code to satisfy the compiler */
352 ** Check if a message or file contains any non-text parts
355 is_nontext(char *msgnam)
358 unsigned char *bp, *dp;
360 char buf[BUFSIZ], name[NAMESZ];
363 if ((fp = fopen(msgnam, "r")) == NULL)
366 for (state = FLD;;) {
367 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
372 ** Check Content-Type field
374 if (!mh_strcasecmp(name, TYPE_FIELD)) {
379 while (state == FLDPLUS) {
380 state = m_getfld(state, name, buf,
388 for (; isspace(*bp); bp++)
393 for (bp++, i = 0;;) {
423 for (dp = bp; istoken(*dp); dp++)
430 if ((result = (mh_strcasecmp(bp,
434 for (dp++; isspace(*dp); dp++)
437 if ((result = !uprf(dp,
440 dp += sizeof("charset") - 1;
448 if ((bp = strchr(++dp, '"')))
451 for (bp = dp; *bp; bp++)
458 /* Default character set */
461 /* Check the character set */
462 result = !check_charset(dp, strlen(dp));
464 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
481 ** Check Content-Transfer-Encoding field
483 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
485 while (state == FLDPLUS) {
486 state = m_getfld(state, name, buf,
490 for (bp = cp; isspace(*bp); bp++)
492 for (dp = bp; istoken(*dp); dp++)
495 result = (mh_strcasecmp(bp, "7bit")
496 && mh_strcasecmp(bp, "8bit")
497 && mh_strcasecmp(bp, "binary"));
508 ** Just skip the rest of this header
509 ** field and go to next one.
511 while (state == FLDPLUS)
512 state = m_getfld(state, name, buf, sizeof(buf),
517 ** We've passed the message header,
518 ** so message is just text.