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 { "width columns", 0 },
29 { "showproc program", 0 },
31 { "showmimeproc program", 0 },
33 { "file file", -4 }, /* interface from lproc (whatnow) */
44 static int is_nontext(char *);
52 main(int argc, char **argv)
54 int headersw = 1, msgp = 0;
55 int checkmime = 1, mime;
56 int vecp = 1, procp = 1, mode = SHOW, msgnum;
57 char *cp, *file = NULL, *folder = NULL, *proc;
58 char buf[BUFSIZ], **argp, **arguments;
59 char *msgs[MAXARGS], *vec[MAXARGS];
60 struct msgs *mp = NULL;
63 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
70 if (!mh_strcasecmp(invo_name, "next")) {
72 } else if (!mh_strcasecmp(invo_name, "prev")) {
75 arguments = getarguments(invo_name, argc, argv, 1);
78 while ((cp = *argp++)) {
80 switch (smatch(++cp, switches)) {
82 ambigsw(cp, switches);
89 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
90 print_help(buf, switches, 1);
93 print_version(invo_name);
99 adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
102 adios(NULL, "only one file at a time!");
103 if (!(cp = *argp++) || *cp == '-')
104 adios(NULL, "missing argument to %s", argp[-2]);
105 file = getcpy(expanddir(cp));
117 if (!(cp = *argp++) || *cp == '-')
118 adios(NULL, "missing argument to %s",
120 vec[vecp++] = getcpy(etcpath(cp));
126 if (!(cp = *argp++) || *cp == '-')
127 adios(NULL, "missing argument to %s",
133 if (!(showproc = *argp++) || *showproc == '-')
134 adios(NULL, "missing argument to %s",
139 if (!(showmimeproc = *argp++) ||
140 *showmimeproc == '-')
141 adios(NULL, "missing argument to %s",
152 if (*cp == '+' || *cp == '@') {
154 adios(NULL, "only one folder at a time!");
156 folder = getcpy(expandfol(cp));
157 } else if (mode != SHOW) {
167 adios(NULL, "only one file at a time!");
173 if (!msgp && !folder && mode == SHOW &&
174 (cp = getenv("mhdraft")) && *cp) {
183 msgs[msgp++] = seq_next;
186 msgs[msgp++] = seq_prev;
189 msgs[msgp++] = seq_cur;
195 folder = getcurfol();
197 /* read folder and create message structure */
198 if (!(mp = folder_read(folder)))
199 adios(NULL, "unable to read folder %s", folder);
201 /* check for empty folder */
203 adios(NULL, "no messages in %s", folder);
205 /* parse all the message ranges/sequences and set SELECTED */
206 for (msgnum = 0; msgnum < msgp; msgnum++)
207 if (!m_convert(mp, msgs[msgnum]))
211 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
212 ** since we will use that as a tag to know which messages
213 ** to remove from the "unseen" sequence.
215 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
216 if (is_selected(mp, msgnum))
217 set_unseen(mp, msgnum);
219 seq_setprev(mp); /* set the Previous-Sequence */
220 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
222 if (mp->numsel > MAXARGS - 2)
223 adios(NULL, "more than %d messages for show exec",
226 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
227 if (is_selected(mp, msgnum))
228 vec[vecp++] = getcpy(m_name(msgnum));
230 seq_setcur(mp, mp->hghsel); /* update current message */
231 seq_save(mp); /* synchronize sequences */
232 context_replace(curfolder, folder); /* update current folder */
233 context_save(); /* save the context file */
235 if (headersw && vecp == 2)
236 printf("(Message %s:%s)\n", folder, vec[1]);
244 ** Decide which "proc" to use
247 /* check if any messages are non-text MIME messages */
251 ** loop through selected messages
252 ** and check for MIME
254 for (msgnum = mp->lowsel;
255 msgnum <= mp->hghsel;
257 if (is_selected(mp, msgnum) && is_nontext(m_name(msgnum))) {
262 /* check the file for MIME */
263 if (is_nontext(vec[vecp - 1]))
269 proc = (mime) ? showmimeproc : showproc;
272 m_putenv("mhfolder", folder);
274 /* Special-cased because mhshow takes msg not files args. */
275 if (strcmp(mhbasename(proc), "mhshow")==0) {
277 vec[vecp] = vec[vecp - 1];
278 vec[vecp - 1] = "-file";
281 vec[vecp++] = concat("+", folder, NULL);
283 vec[0] = mhbasename(proc);
285 adios(proc, "unable to exec");
288 /* Add the path to the message names. */
290 for (msgnum = procp; msgnum < vecp; msgnum++) {
291 vec[msgnum] = concat(mp->foldpath, "/",
296 vec[0] = mhbasename(proc);
298 adios(proc, "unable to exec");
299 return 0; /* dead code to satisfy the compiler */
303 ** Check if a message or file contains any non-text parts
306 is_nontext(char *msgnam)
309 unsigned char *bp, *dp;
311 char buf[BUFSIZ], name[NAMESZ];
314 if ((fp = fopen(msgnam, "r")) == NULL)
317 for (state = FLD;;) {
318 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
323 ** Check Content-Type field
325 if (!mh_strcasecmp(name, TYPE_FIELD)) {
330 while (state == FLDPLUS) {
331 state = m_getfld(state, name, buf,
339 for (; isspace(*bp); bp++)
344 for (bp++, i = 0;;) {
374 for (dp = bp; istoken(*dp); dp++)
381 if ((result = (mh_strcasecmp(bp,
385 for (dp++; isspace(*dp); dp++)
388 if ((result = !uprf(dp,
391 dp += sizeof("charset") - 1;
399 if ((bp = strchr(++dp, '"')))
402 for (bp = dp; *bp; bp++)
409 /* Default character set */
412 /* Check the character set */
413 result = !check_charset(dp, strlen(dp));
415 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
432 ** Check Content-Transfer-Encoding field
434 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
436 while (state == FLDPLUS) {
437 state = m_getfld(state, name, buf,
441 for (bp = cp; isspace(*bp); bp++)
443 for (dp = bp; istoken(*dp); dp++)
446 result = (mh_strcasecmp(bp, "7bit")
447 && mh_strcasecmp(bp, "8bit")
448 && mh_strcasecmp(bp, "binary"));
459 ** Just skip the rest of this header
460 ** field and go to next one.
462 while (state == FLDPLUS)
463 state = m_getfld(state, name, buf, sizeof(buf),
468 ** We've passed the message header,
469 ** so message is just text.