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 },
27 { "length lines", 0 },
29 { "width columns", 0 },
31 { "showproc program", 0 },
33 { "showmimeproc program", 0 },
35 { "file file", -4 }, /* interface from showfile */
46 static int is_nontext(char *);
54 main(int argc, char **argv)
56 int headersw = 1, msgp = 0;
57 int checkmime = 1, mime;
58 int vecp = 1, procp = 1, mode = SHOW, msgnum;
59 char *cp, *file = NULL, *folder = NULL, *proc;
60 char buf[BUFSIZ], **argp, **arguments;
61 char *msgs[MAXARGS], *vec[MAXARGS];
62 struct msgs *mp = NULL;
65 setlocale(LC_ALL, "");
67 invo_name = mhbasename(argv[0]);
69 /* read user profile/context */
72 if (!mh_strcasecmp(invo_name, "next")) {
74 } else if (!mh_strcasecmp(invo_name, "prev")) {
77 arguments = getarguments(invo_name, argc, argv, 1);
80 while ((cp = *argp++)) {
82 switch (smatch(++cp, switches)) {
84 ambigsw(cp, switches);
91 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
92 print_help(buf, switches, 1);
95 print_version(invo_name);
101 adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
104 adios(NULL, "only one file at a time!");
105 if (!(cp = *argp++) || *cp == '-')
106 adios(NULL, "missing argument to %s", argp[-2]);
107 file = getcpy(expanddir(cp));
119 if (!(cp = *argp++) || *cp == '-')
120 adios(NULL, "missing argument to %s",
122 vec[vecp++] = getcpy(etcpath(cp));
129 if (!(cp = *argp++) || *cp == '-')
130 adios(NULL, "missing argument to %s",
136 if (!(showproc = *argp++) || *showproc == '-')
137 adios(NULL, "missing argument to %s",
142 if (!(showmimeproc = *argp++) ||
143 *showmimeproc == '-')
144 adios(NULL, "missing argument to %s",
155 if (*cp == '+' || *cp == '@') {
157 adios(NULL, "only one folder at a time!");
159 folder = getcpy(expandfol(cp));
160 } else if (mode != SHOW) {
170 adios(NULL, "only one file at a time!");
176 if (!msgp && !folder && mode == SHOW &&
177 (cp = getenv("mhdraft")) && *cp) {
186 msgs[msgp++] = seq_next;
189 msgs[msgp++] = seq_prev;
192 msgs[msgp++] = seq_cur;
198 folder = getcurfol();
200 /* read folder and create message structure */
201 if (!(mp = folder_read(folder)))
202 adios(NULL, "unable to read folder %s", folder);
204 /* check for empty folder */
206 adios(NULL, "no messages in %s", folder);
208 /* parse all the message ranges/sequences and set SELECTED */
209 for (msgnum = 0; msgnum < msgp; msgnum++)
210 if (!m_convert(mp, msgs[msgnum]))
214 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
215 ** since we will use that as a tag to know which messages
216 ** to remove from the "unseen" sequence.
218 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
219 if (is_selected(mp, msgnum))
220 set_unseen(mp, msgnum);
222 seq_setprev(mp); /* set the Previous-Sequence */
223 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
225 if (mp->numsel > MAXARGS - 2)
226 adios(NULL, "more than %d messages for show exec",
229 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
230 if (is_selected(mp, msgnum))
231 vec[vecp++] = getcpy(m_name(msgnum));
233 seq_setcur(mp, mp->hghsel); /* update current message */
234 seq_save(mp); /* synchronize sequences */
235 context_replace(curfolder, folder); /* update current folder */
236 context_save(); /* save the context file */
238 if (headersw && vecp == 2)
239 printf("(Message %s:%s)\n", folder, vec[1]);
247 ** Decide which "proc" to use
250 /* check if any messages are non-text MIME messages */
254 ** loop through selected messages
255 ** and check for MIME
257 for (msgnum = mp->lowsel;
258 msgnum <= mp->hghsel;
260 if (is_selected(mp, msgnum) && is_nontext(m_name(msgnum))) {
265 /* check the file for MIME */
266 if (is_nontext(vec[vecp - 1]))
272 proc = (mime) ? showmimeproc : showproc;
275 m_putenv("mhfolder", folder);
277 /* Special-cased because mhshow takes msg not files args. */
278 if (strcmp(mhbasename(proc), "mhshow")==0) {
280 vec[vecp] = vec[vecp - 1];
281 vec[vecp - 1] = "-file";
284 vec[vecp++] = concat("+", folder, NULL);
286 vec[0] = mhbasename(proc);
288 adios(proc, "unable to exec");
291 /* Add the path to the message names. */
293 for (msgnum = procp; msgnum < vecp; msgnum++) {
294 vec[msgnum] = concat(mp->foldpath, "/",
299 vec[0] = mhbasename(proc);
301 adios(proc, "unable to exec");
302 return 0; /* dead code to satisfy the compiler */
306 ** Check if a message or file contains any non-text parts
309 is_nontext(char *msgnam)
312 unsigned char *bp, *dp;
314 char buf[BUFSIZ], name[NAMESZ];
317 if ((fp = fopen(msgnam, "r")) == NULL)
320 for (state = FLD;;) {
321 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
326 ** Check Content-Type field
328 if (!mh_strcasecmp(name, TYPE_FIELD)) {
333 while (state == FLDPLUS) {
334 state = m_getfld(state, name, buf,
342 for (; isspace(*bp); bp++)
347 for (bp++, i = 0;;) {
377 for (dp = bp; istoken(*dp); dp++)
384 if ((result = (mh_strcasecmp(bp,
388 for (dp++; isspace(*dp); dp++)
391 if ((result = !uprf(dp,
394 dp += sizeof("charset") - 1;
402 if ((bp = strchr(++dp, '"')))
405 for (bp = dp; *bp; bp++)
412 /* Default character set */
415 /* Check the character set */
416 result = !check_charset(dp, strlen(dp));
418 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
435 ** Check Content-Transfer-Encoding field
437 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
439 while (state == FLDPLUS) {
440 state = m_getfld(state, name, buf,
444 for (bp = cp; isspace(*bp); bp++)
446 for (dp = bp; istoken(*dp); dp++)
449 result = (mh_strcasecmp(bp, "7bit")
450 && mh_strcasecmp(bp, "8bit")
451 && mh_strcasecmp(bp, "binary"));
462 ** Just skip the rest of this header
463 ** field and go to next one.
465 while (state == FLDPLUS)
466 state = m_getfld(state, name, buf, sizeof(buf),
471 ** We've passed the message header,
472 ** so message is just text.