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 { "showproc program", 0 },
27 { "showmimeproc program", 0 },
29 { "file file", -4 }, /* interface from whatnow (listproc) */
40 static int is_nontext(char *);
48 main(int argc, char **argv)
50 int headersw = 1, msgp = 0;
51 int checkmime = 1, mime;
52 int vecp = 1, procp = 1, mode = SHOW, msgnum;
53 char *cp, *file = NULL, *folder = NULL, *proc;
54 char buf[BUFSIZ], **argp, **arguments;
55 char *msgs[MAXARGS], *vec[MAXARGS];
56 struct msgs *mp = NULL;
59 setlocale(LC_ALL, "");
61 invo_name = mhbasename(argv[0]);
63 /* read user profile/context */
66 if (!mh_strcasecmp(invo_name, "next")) {
68 } else if (!mh_strcasecmp(invo_name, "prev")) {
71 arguments = getarguments(invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch(++cp, switches)) {
78 ambigsw(cp, switches);
85 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
86 print_help(buf, switches, 1);
89 print_version(invo_name);
95 adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
98 adios(NULL, "only one file at a time!");
99 if (!(cp = *argp++) || *cp == '-')
100 adios(NULL, "missing argument to %s", argp[-2]);
101 file = getcpy(expanddir(cp));
113 if (!(cp = *argp++) || *cp == '-')
114 adios(NULL, "missing argument to %s",
116 vec[vecp++] = getcpy(etcpath(cp));
120 if (!(showproc = *argp++) || *showproc == '-')
121 adios(NULL, "missing argument to %s",
126 if (!(showmimeproc = *argp++) ||
127 *showmimeproc == '-')
128 adios(NULL, "missing argument to %s",
139 if (*cp == '+' || *cp == '@') {
141 adios(NULL, "only one folder at a time!");
143 folder = getcpy(expandfol(cp));
144 } else if (mode != SHOW) {
154 adios(NULL, "only one file at a time!");
160 if (!msgp && !folder && mode == SHOW &&
161 (cp = getenv("mhdraft")) && *cp) {
170 msgs[msgp++] = seq_next;
173 msgs[msgp++] = seq_prev;
176 msgs[msgp++] = seq_cur;
182 folder = getcurfol();
184 /* read folder and create message structure */
185 if (!(mp = folder_read(folder)))
186 adios(NULL, "unable to read folder %s", folder);
188 /* check for empty folder */
190 adios(NULL, "no messages in %s", folder);
192 /* parse all the message ranges/sequences and set SELECTED */
193 for (msgnum = 0; msgnum < msgp; msgnum++)
194 if (!m_convert(mp, msgs[msgnum]))
198 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
199 ** since we will use that as a tag to know which messages
200 ** to remove from the "unseen" sequence.
202 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
203 if (is_selected(mp, msgnum))
204 set_unseen(mp, msgnum);
206 seq_setprev(mp); /* set the Previous-Sequence */
207 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
209 if (mp->numsel > MAXARGS - 2)
210 adios(NULL, "more than %d messages for show exec",
213 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
214 if (is_selected(mp, msgnum))
215 vec[vecp++] = getcpy(m_name(msgnum));
217 seq_setcur(mp, mp->hghsel); /* update current message */
218 seq_save(mp); /* synchronize sequences */
219 context_replace(curfolder, folder); /* update current folder */
220 context_save(); /* save the context file */
222 if (headersw && vecp == 2)
223 printf("(Message %s:%s)\n", folder, vec[1]);
231 ** Decide which "proc" to use
234 /* check if any messages are non-text MIME messages */
237 /* check the file for MIME */
238 if (is_nontext(vec[vecp - 1])) {
243 ** loop through selected messages
244 ** and check for MIME
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel;
248 if (is_selected(mp, msgnum) &&
249 is_nontext(m_name(msgnum))) {
257 proc = (mime) ? showmimeproc : showproc;
260 m_putenv("mhfolder", folder);
262 /* Special-cased because mhshow takes msg not files args. */
263 if (strcmp(mhbasename(proc), "mhshow")==0) {
265 vec[vecp] = vec[vecp - 1];
266 vec[vecp - 1] = "-file";
269 vec[vecp++] = concat("+", folder, NULL);
271 vec[0] = mhbasename(proc);
273 adios(proc, "unable to exec");
276 /* Add the path to the message names. */
278 for (msgnum = procp; msgnum < vecp; msgnum++) {
279 vec[msgnum] = concat(mp->foldpath, "/",
284 vec[0] = mhbasename(proc);
286 adios(proc, "unable to exec");
287 return 0; /* dead code to satisfy the compiler */
291 ** Check if a message or file contains any non-text parts
294 is_nontext(char *msgnam)
297 unsigned char *bp, *dp;
299 char buf[BUFSIZ], name[NAMESZ];
302 if ((fp = fopen(msgnam, "r")) == NULL)
305 for (state = FLD;;) {
306 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
311 ** Check Content-Type field
313 if (!mh_strcasecmp(name, TYPE_FIELD)) {
318 while (state == FLDPLUS) {
319 state = m_getfld(state, name, buf,
326 for (; isspace(*bp); bp++)
331 for (bp++, i = 0;;) {
361 for (dp = bp; istoken(*dp); dp++)
368 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
375 if ((result = (mh_strcasecmp(bp,
379 for (dp++; isspace(*dp); dp++)
382 if ((result = !uprf(dp,
385 dp += sizeof("charset") - 1;
393 if ((bp = strchr(++dp, '"')))
396 for (bp = dp; *bp; bp++)
403 /* Default character set */
406 /* Check the character set */
407 result = !check_charset(dp, strlen(dp));
419 ** Check Content-Transfer-Encoding field
421 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
423 while (state == FLDPLUS) {
424 state = m_getfld(state, name, buf,
428 for (bp = cp; isspace(*bp); bp++)
430 for (dp = bp; istoken(*dp); dp++)
433 result = (mh_strcasecmp(bp, "7bit")
434 && mh_strcasecmp(bp, "8bit")
435 && mh_strcasecmp(bp, "binary"));
446 ** Just skip the rest of this header
447 ** field and go to next one.
449 while (state == FLDPLUS)
450 state = m_getfld(state, name, buf, sizeof(buf),
455 ** We've passed the message header,
456 ** so message is just text.