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",
130 vec[vecp++] = getcpy(etcpath(cp));
137 if (!(cp = *argp++) || *cp == '-')
138 adios(NULL, "missing argument to %s",
144 if (!(showproc = *argp++) || *showproc == '-')
145 adios(NULL, "missing argument to %s",
154 if (!(showmimeproc = *argp++) ||
155 *showmimeproc == '-')
156 adios(NULL, "missing argument to %s",
168 if (*cp == '+' || *cp == '@') {
170 adios(NULL, "only one folder at a time!");
172 folder = getcpy(expandfol(cp));
173 } else if (mode != SHOW) {
183 adios(NULL, "only one file at a time!");
189 if (!msgp && !folder && mode == SHOW &&
190 (cp = getenv("mhdraft")) && *cp) {
199 msgs[msgp++] = seq_next;
202 msgs[msgp++] = seq_prev;
205 msgs[msgp++] = seq_cur;
211 folder = getcurfol();
212 maildir = toabsdir(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, 0); /* unset unseen seqs for shown msgs */
242 if (mp->numsel > MAXARGS - 2)
243 adios(NULL, "more than %d messages for show exec",
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
247 if (is_selected(mp, msgnum))
248 vec[vecp++] = getcpy(m_name(msgnum));
250 seq_setcur(mp, mp->hghsel); /* update current message */
251 seq_save(mp); /* synchronize sequences */
252 context_replace(curfolder, folder); /* update current folder */
253 context_save(); /* save the context file */
255 if (headersw && vecp == 2)
256 printf("(Message %s:%s)\n", folder, vec[1]);
264 ** Decide which "proc" to use
270 /* check if any messages are non-text MIME messages */
271 if (checkmime && !getenv("NOMHNPROC")) {
274 ** loop through selected messages
275 ** and check for MIME
277 for (msgnum = mp->lowsel;
278 msgnum <= mp->hghsel;
280 if (is_selected(mp, msgnum) && is_nontext(m_name(msgnum))) {
285 /* check the file for MIME */
286 if (is_nontext(vec[vecp - 1]))
299 m_putenv("mhfolder", folder);
302 ** For backward compatibility, if the "proc" is mhn,
303 ** then add "-show" option. Add "-file" if showing
306 if (strcmp(mhbasename(proc), "mhn") == 0) {
308 vec[vecp] = vec[vecp - 1];
309 vec[vecp - 1] = "-file";
312 vec[vecp++] = "-show";
316 /* If the "proc" is "mhshow", add "-file" if showing file. */
317 if (strcmp(mhbasename(proc), "mhshow") == 0 && file ) {
318 vec[vecp] = vec[vecp - 1];
319 vec[vecp - 1] = "-file";
324 ** If "proc" is mhl, then run it internally
325 ** rather than exec'ing it.
327 if (strcmp(mhbasename(proc), "mhl") == 0) {
334 ** If you are not using a nmh command as your "proc", then
335 ** add the path to the message names. Currently, we are just
336 ** checking for mhn here, since we've already taken care of mhl.
338 if (!strcmp(mhbasename(proc), "mhl")
341 concat(toabsdir("+"), "/", NULL)) != NOTOK) {
342 mp->foldpath = concat(mp->foldpath, "/", NULL);
343 cp = isprefix(maildir, mp->foldpath)
344 ? mp->foldpath + strlen(maildir)
346 for (msgnum = procp; msgnum < vecp; msgnum++)
347 vec[msgnum] = concat(cp, vec[msgnum], NULL);
350 vec[0] = mhbasename(proc);
352 adios(proc, "unable to exec");
353 return 0; /* dead code to satisfy the compiler */
357 ** Check if a message or file contains any non-text parts
360 is_nontext(char *msgnam)
363 unsigned char *bp, *dp;
365 char buf[BUFSIZ], name[NAMESZ];
368 if ((fp = fopen(msgnam, "r")) == NULL)
371 for (state = FLD;;) {
372 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
377 ** Check Content-Type field
379 if (!mh_strcasecmp(name, TYPE_FIELD)) {
384 while (state == FLDPLUS) {
385 state = m_getfld(state, name, buf,
393 for (; isspace(*bp); bp++)
398 for (bp++, i = 0;;) {
428 for (dp = bp; istoken(*dp); dp++)
435 if ((result = (mh_strcasecmp(bp,
439 for (dp++; isspace(*dp); dp++)
442 if ((result = !uprf(dp,
445 dp += sizeof("charset") - 1;
453 if ((bp = strchr(++dp, '"')))
456 for (bp = dp; *bp; bp++)
463 /* Default character set */
466 /* Check the character set */
467 result = !check_charset(dp, strlen(dp));
469 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
486 ** Check Content-Transfer-Encoding field
488 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
490 while (state == FLDPLUS) {
491 state = m_getfld(state, name, buf,
495 for (bp = cp; isspace(*bp); bp++)
497 for (dp = bp; istoken(*dp); dp++)
500 result = (mh_strcasecmp(bp, "7bit")
501 && mh_strcasecmp(bp, "8bit")
502 && mh_strcasecmp(bp, "binary"));
513 ** Just skip the rest of this header
514 ** field and go to next one.
516 while (state == FLDPLUS)
517 state = m_getfld(state, name, buf, sizeof(buf),
522 ** We've passed the message header,
523 ** so message is just text.