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 },
37 { "file file", -4 }, /* interface from showfile */
48 static int is_nontext(char *);
56 main(int argc, char **argv)
58 int headersw = 1, msgp = 0;
59 int checkmime = 1, mime;
60 int vecp = 1, procp = 1, mode = SHOW, msgnum;
61 char *cp, *file = NULL, *folder = NULL, *proc;
62 char buf[BUFSIZ], **argp, **arguments;
63 char *msgs[MAXARGS], *vec[MAXARGS];
64 struct msgs *mp = NULL;
67 setlocale(LC_ALL, "");
69 invo_name = mhbasename(argv[0]);
71 /* read user profile/context */
74 if (!mh_strcasecmp(invo_name, "next")) {
76 } else if (!mh_strcasecmp(invo_name, "prev")) {
79 arguments = getarguments(invo_name, argc, argv, 1);
82 while ((cp = *argp++)) {
84 switch (smatch(++cp, switches)) {
86 ambigsw(cp, switches);
94 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
95 print_help(buf, switches, 1);
98 print_version(invo_name);
104 adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
107 adios(NULL, "only one file at a time!");
108 if (!(cp = *argp++) || *cp == '-')
109 adios(NULL, "missing argument to %s", argp[-2]);
110 file = getcpy(expanddir(cp));
122 if (!(cp = *argp++) || *cp == '-')
123 adios(NULL, "missing argument to %s",
125 vec[vecp++] = getcpy(etcpath(cp));
132 if (!(cp = *argp++) || *cp == '-')
133 adios(NULL, "missing argument to %s",
139 if (!(showproc = *argp++) || *showproc == '-')
140 adios(NULL, "missing argument to %s",
145 if (!(showmimeproc = *argp++) ||
146 *showmimeproc == '-')
147 adios(NULL, "missing argument to %s",
158 if (*cp == '+' || *cp == '@') {
160 adios(NULL, "only one folder at a time!");
162 folder = getcpy(expandfol(cp));
163 } else if (mode != SHOW) {
173 adios(NULL, "only one file at a time!");
179 if (!msgp && !folder && mode == SHOW &&
180 (cp = getenv("mhdraft")) && *cp) {
189 msgs[msgp++] = seq_next;
192 msgs[msgp++] = seq_prev;
195 msgs[msgp++] = seq_cur;
201 folder = getcurfol();
203 /* read folder and create message structure */
204 if (!(mp = folder_read(folder)))
205 adios(NULL, "unable to read folder %s", folder);
207 /* check for empty folder */
209 adios(NULL, "no messages in %s", folder);
211 /* parse all the message ranges/sequences and set SELECTED */
212 for (msgnum = 0; msgnum < msgp; msgnum++)
213 if (!m_convert(mp, msgs[msgnum]))
217 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
218 ** since we will use that as a tag to know which messages
219 ** to remove from the "unseen" sequence.
221 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
222 if (is_selected(mp, msgnum))
223 set_unseen(mp, msgnum);
225 seq_setprev(mp); /* set the Previous-Sequence */
226 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
228 if (mp->numsel > MAXARGS - 2)
229 adios(NULL, "more than %d messages for show exec",
232 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
233 if (is_selected(mp, msgnum))
234 vec[vecp++] = getcpy(m_name(msgnum));
236 seq_setcur(mp, mp->hghsel); /* update current message */
237 seq_save(mp); /* synchronize sequences */
238 context_replace(curfolder, folder); /* update current folder */
239 context_save(); /* save the context file */
241 if (headersw && vecp == 2)
242 printf("(Message %s:%s)\n", folder, vec[1]);
250 ** Decide which "proc" to use
253 /* check if any messages are non-text MIME messages */
257 ** loop through selected messages
258 ** and check for MIME
260 for (msgnum = mp->lowsel;
261 msgnum <= mp->hghsel;
263 if (is_selected(mp, msgnum) && is_nontext(m_name(msgnum))) {
268 /* check the file for MIME */
269 if (is_nontext(vec[vecp - 1]))
281 m_putenv("mhfolder", folder);
283 /* If the "proc" is "mhshow", add "-file" if showing file. */
284 if (strcmp(mhbasename(proc), "mhshow") == 0 && file ) {
285 vec[vecp] = vec[vecp - 1];
286 vec[vecp - 1] = "-file";
290 /* Add the path to the message names. */
292 for (msgnum = procp; msgnum < vecp; msgnum++) {
293 vec[msgnum] = concat(mp->foldpath, "/",
298 vec[0] = mhbasename(proc);
300 adios(proc, "unable to exec");
301 return 0; /* dead code to satisfy the compiler */
305 ** Check if a message or file contains any non-text parts
308 is_nontext(char *msgnam)
311 unsigned char *bp, *dp;
313 char buf[BUFSIZ], name[NAMESZ];
316 if ((fp = fopen(msgnam, "r")) == NULL)
319 for (state = FLD;;) {
320 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
325 ** Check Content-Type field
327 if (!mh_strcasecmp(name, TYPE_FIELD)) {
332 while (state == FLDPLUS) {
333 state = m_getfld(state, name, buf,
341 for (; isspace(*bp); bp++)
346 for (bp++, i = 0;;) {
376 for (dp = bp; istoken(*dp); dp++)
383 if ((result = (mh_strcasecmp(bp,
387 for (dp++; isspace(*dp); dp++)
390 if ((result = !uprf(dp,
393 dp += sizeof("charset") - 1;
401 if ((bp = strchr(++dp, '"')))
404 for (bp = dp; *bp; bp++)
411 /* Default character set */
414 /* Check the character set */
415 result = !check_charset(dp, strlen(dp));
417 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
434 ** Check Content-Transfer-Encoding field
436 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
438 while (state == FLDPLUS) {
439 state = m_getfld(state, name, buf,
443 for (bp = cp; isspace(*bp); bp++)
445 for (dp = bp; istoken(*dp); dp++)
448 result = (mh_strcasecmp(bp, "7bit")
449 && mh_strcasecmp(bp, "8bit")
450 && mh_strcasecmp(bp, "binary"));
461 ** Just skip the rest of this header
462 ** field and go to next one.
464 while (state == FLDPLUS)
465 state = m_getfld(state, name, buf, sizeof(buf),
470 ** We've passed the message header,
471 ** so message is just text.