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]))
275 proc = (mime) ? showmimeproc : showproc;
278 m_putenv("mhfolder", folder);
280 /* Special-cased because mhshow takes msg not files args. */
281 if (strcmp(mhbasename(proc), "mhshow")==0) {
283 vec[vecp] = vec[vecp - 1];
284 vec[vecp - 1] = "-file";
287 vec[vecp++] = concat("+", folder, NULL);
289 vec[0] = mhbasename(proc);
291 adios(proc, "unable to exec");
294 /* Add the path to the message names. */
296 for (msgnum = procp; msgnum < vecp; msgnum++) {
297 vec[msgnum] = concat(mp->foldpath, "/",
302 vec[0] = mhbasename(proc);
304 adios(proc, "unable to exec");
305 return 0; /* dead code to satisfy the compiler */
309 ** Check if a message or file contains any non-text parts
312 is_nontext(char *msgnam)
315 unsigned char *bp, *dp;
317 char buf[BUFSIZ], name[NAMESZ];
320 if ((fp = fopen(msgnam, "r")) == NULL)
323 for (state = FLD;;) {
324 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
329 ** Check Content-Type field
331 if (!mh_strcasecmp(name, TYPE_FIELD)) {
336 while (state == FLDPLUS) {
337 state = m_getfld(state, name, buf,
345 for (; isspace(*bp); bp++)
350 for (bp++, i = 0;;) {
380 for (dp = bp; istoken(*dp); dp++)
387 if ((result = (mh_strcasecmp(bp,
391 for (dp++; isspace(*dp); dp++)
394 if ((result = !uprf(dp,
397 dp += sizeof("charset") - 1;
405 if ((bp = strchr(++dp, '"')))
408 for (bp = dp; *bp; bp++)
415 /* Default character set */
418 /* Check the character set */
419 result = !check_charset(dp, strlen(dp));
421 if (!(result = (mh_strcasecmp(bp, "text") != 0))) {
438 ** Check Content-Transfer-Encoding field
440 if (!mh_strcasecmp(name, ENCODING_FIELD)) {
442 while (state == FLDPLUS) {
443 state = m_getfld(state, name, buf,
447 for (bp = cp; isspace(*bp); bp++)
449 for (dp = bp; istoken(*dp); dp++)
452 result = (mh_strcasecmp(bp, "7bit")
453 && mh_strcasecmp(bp, "8bit")
454 && mh_strcasecmp(bp, "binary"));
465 ** Just skip the rest of this header
466 ** field and go to next one.
468 while (state == FLDPLUS)
469 state = m_getfld(state, name, buf, sizeof(buf),
474 ** We've passed the message header,
475 ** so message is just text.