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 = path(cp, TFILE);
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 = pluspath(cp);
168 } else if (mode != SHOW) {
176 if (!context_find("path"))
177 free(path("./", TFOLDER));
181 adios(NULL, "only one file at a time!");
187 if (!msgp && !folder && mode == SHOW &&
188 (cp = getenv("mhdraft")) && *cp) {
197 msgs[msgp++] = "next";
200 msgs[msgp++] = "prev";
203 msgs[msgp++] = "cur";
209 folder = getfolder(FCUR);
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)))
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 < msgp; msgnum++)
225 if (!m_convert(mp, 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 if (mp->numsel > MAXARGS - 2)
241 adios(NULL, "more than %d messages for show exec",
244 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
245 if (is_selected(mp, msgnum))
246 vec[vecp++] = getcpy(m_name(msgnum));
248 seq_setcur(mp, mp->hghsel); /* update current message */
249 seq_save(mp); /* synchronize sequences */
250 context_replace(pfolder, folder); /* update current folder */
251 context_save(); /* save the context file */
253 if (headersw && vecp == 2)
254 printf("(Message %s:%s)\n", folder, vec[1]);
262 ** Decide which "proc" to use
268 /* check if any messages are non-text MIME messages */
269 if (checkmime && !getenv("NOMHNPROC")) {
272 ** loop through selected messages
273 ** and check for MIME
275 for (msgnum = mp->lowsel;
276 msgnum <= mp->hghsel;
278 if (is_selected(mp, msgnum) && is_nontext(m_name(msgnum))) {
283 /* check the file for MIME */
284 if (is_nontext(vec[vecp - 1]))
297 m_putenv("mhfolder", folder);
300 ** For backward compatibility, if the "proc" is mhn,
301 ** then add "-show" option. Add "-file" if showing
304 if (strcmp(mhbasename(proc), "mhn") == 0) {
306 vec[vecp] = vec[vecp - 1];
307 vec[vecp - 1] = "-file";
310 vec[vecp++] = "-show";
314 /* If the "proc" is "mhshow", add "-file" if showing file. */
315 if (strcmp(mhbasename(proc), "mhshow") == 0 && file ) {
316 vec[vecp] = vec[vecp - 1];
317 vec[vecp - 1] = "-file";
322 ** If "proc" is mhl, then run it internally
323 ** rather than exec'ing it.
325 if (strcmp(mhbasename(proc), "mhl") == 0) {
332 ** If you are not using a nmh command as your "proc", then
333 ** add the path to the message names. Currently, we are just
334 ** checking for mhn here, since we've already taken care of mhl.
336 if (!strcmp(mhbasename(proc), "mhl")
339 concat(m_maildir(""), "/", NULL)) != NOTOK) {
340 mp->foldpath = concat(mp->foldpath, "/", NULL);
341 cp = isprefix(maildir, mp->foldpath)
342 ? mp->foldpath + strlen(maildir)
344 for (msgnum = procp; msgnum < vecp; msgnum++)
345 vec[msgnum] = concat(cp, vec[msgnum], NULL);
348 vec[0] = mhbasename(proc);
350 adios(proc, "unable to exec");
351 return 0; /* dead code to satisfy the compiler */
355 ** Check if a message or file contains any non-text parts
358 is_nontext(char *msgnam)
361 unsigned char *bp, *dp;
363 char buf[BUFSIZ], name[NAMESZ];
366 if ((fp = fopen(msgnam, "r")) == NULL)
369 for (state = FLD;;) {
370 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
375 ** Check Content-Type field
377 if (!mh_strcasecmp(name, TYPE_FIELD)) {
382 while (state == FLDPLUS) {
383 state = m_getfld(state, name, buf,
391 for (; isspace(*bp); bp++)
396 for (bp++, i = 0;;) {
426 for (dp = bp; istoken(*dp); dp++)
433 if ((result = (mh_strcasecmp(bp,
437 for (dp++; isspace(*dp); dp++)
440 if ((result = !uprf(dp,
443 dp += sizeof("charset") - 1;
451 if ((bp = strchr(++dp, '"')))
454 for (bp = dp; *bp; bp++)
461 /* Default character set */
464 /* Check the character set */
465 result = !check_charset(dp, strlen(dp));
467 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
484 ** Check Content-Transfer-Encoding field
486 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
488 while (state == FLDPLUS) {
489 state = m_getfld(state, name, buf,
493 for (bp = cp; isspace(*bp); bp++)
495 for (dp = bp; istoken(*dp); dp++)
498 result = (mh_strcasecmp(bp, "7bit")
499 && mh_strcasecmp(bp, "8bit")
500 && mh_strcasecmp(bp, "binary"));
511 ** Just skip the rest of this header
512 ** field and go to next one.
514 while (state == FLDPLUS)
515 state = m_getfld(state, name, buf, sizeof(buf),
520 ** We've passed the message header,
521 ** so message is just text.