3 * show.c -- show/list messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 static struct swit switches[] = {
17 #define NOCHECKMIMESW 1
24 { "form formfile", 0 },
26 { "moreproc program", 0 },
30 { "length lines", 0 },
32 { "width columns", 0 },
34 { "showproc program", 0 },
36 { "showmimeproc program", 0 },
42 { "file file", -4 }, /* interface from showfile */
44 { "fmtproc program", 0 },
57 static int is_nontext(char *);
65 main (int argc, char **argv)
67 int draftsw = 0, headersw = 1;
68 int nshow = 0, checkmime = 1, mime;
69 int isdf = 0, mode = SHOW, msgnum;
70 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
71 char buf[BUFSIZ], **argp, **arguments;
72 struct msgs *mp = NULL;
73 struct msgs_array msgs = { 0, 0, NULL };
74 struct msgs_array vec = { 0, 0, NULL };
77 setlocale(LC_ALL, "");
79 invo_name = r1bindex (argv[0], '/');
81 app_msgarg(&vec, NULL); /* placeholder, filled later with proc name */
83 /* read user profile/context */
86 if (!mh_strcasecmp (invo_name, "next")) {
88 } else if (!mh_strcasecmp (invo_name, "prev")) {
91 arguments = getarguments (invo_name, argc, argv, 1);
94 while ((cp = *argp++)) {
96 switch (smatch (++cp, switches)) {
98 ambigsw (cp, switches);
103 app_msgarg(&vec, --cp);
107 snprintf (buf, sizeof(buf),
108 "%s [+folder] %s[switches] [switches for showproc]",
109 invo_name, mode == SHOW ? "[msgs] ": "");
110 print_help (buf, switches, 1);
113 print_version(invo_name);
118 adios (NULL, "only one file at a time!");
124 "usage: %s [+folder] [switches] [switches for showproc]",
130 adios (NULL, "only one file at a time!");
131 if (!(cp = *argp++) || *cp == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 file = path (cp, TFILE);
144 app_msgarg(&vec, --cp);
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
147 app_msgarg(&vec, getcpy (etcpath(cp)));
154 app_msgarg(&vec, --cp);
155 if (!(cp = *argp++) || *cp == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
157 app_msgarg(&vec, cp);
161 if (!(showproc = *argp++) || *showproc == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
170 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
182 if (*cp == '+' || *cp == '@') {
184 adios (NULL, "only one folder at a time!");
186 folder = pluspath (cp);
191 app_msgarg(&msgs, cp);
195 if (!context_find ("path"))
196 free (path ("./", TFOLDER));
198 if (draftsw || file) {
200 adios (NULL, "only one file at a time!");
202 app_msgarg(&vec, getcpy (m_draft (folder, NULL, 1, &isdf)));
204 app_msgarg(&vec, file);
209 if (!msgs.size && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
211 app_msgarg(&vec, cp);
219 app_msgarg(&msgs, "next");
222 app_msgarg(&msgs, "prev");
225 app_msgarg(&msgs, "cur");
231 folder = getfolder (1);
232 maildir = m_maildir (folder);
234 if (chdir (maildir) == NOTOK)
235 adios (maildir, "unable to change directory to");
237 /* read folder and create message structure */
238 if (!(mp = folder_read (folder)))
239 adios (NULL, "unable to read folder %s", folder);
241 /* check for empty folder */
243 adios (NULL, "no messages in %s", folder);
245 /* parse all the message ranges/sequences and set SELECTED */
246 for (msgnum = 0; msgnum < msgs.size; msgnum++)
247 if (!m_convert (mp, msgs.msgs[msgnum]))
251 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
252 * since we will use that as a tag to know which messages
253 * to remove from the "unseen" sequence.
255 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
256 if (is_selected(mp, msgnum))
257 set_unseen (mp, msgnum);
259 seq_setprev (mp); /* set the Previous-Sequence */
260 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
262 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
263 if (is_selected(mp, msgnum))
264 app_msgarg(&vec, getcpy (m_name (msgnum)));
266 seq_setcur (mp, mp->hghsel); /* update current message */
267 seq_save (mp); /* synchronize sequences */
268 context_replace (pfolder, folder); /* update current folder */
269 context_save (); /* save the context file */
271 if (headersw && vec.size == 2)
272 printf ("(Message %s:%s)\n", folder, vec.msgs[1]);
278 * Decide which "proc" to use
284 /* check if any messages are non-text MIME messages */
286 if (!draftsw && !file) {
287 /* loop through selected messages and check for MIME */
288 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
289 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
294 /* check the file or draft for MIME */
295 if (is_nontext (vec.msgs[vec.size - 1]))
307 if (folder && !draftsw && !file)
308 m_putenv ("mhfolder", folder);
310 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
311 /* Add "-file" if showing file or draft, */
312 if (draftsw || file) {
313 app_msgarg(&vec, vec.msgs[vec.size - 1]);
314 vec.msgs[vec.size - 2] = "-file";
316 /* and add -show for backward compatibility */
317 app_msgarg(&vec, "-show");
318 } else if (strcmp (r1bindex (proc, '/'), "mhshow") == 0) {
319 /* If "mhshow", add "-file" if showing file or draft. */
320 if (draftsw || file) {
321 app_msgarg(&vec, vec.msgs[vec.size - 1]);
322 vec.msgs[vec.size - 2] = "-file";
324 } else if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
325 /* If "mhl", then run it internally */
327 app_msgarg(&vec, NULL);
328 mhl (vec.size, vec.msgs);
332 vec.msgs[0] = r1bindex (proc, '/');
333 app_msgarg(&vec, NULL);
334 execvp (proc, vec.msgs);
335 adios (proc, "unable to exec");
336 return 0; /* dead code to satisfy the compiler */
341 * Check if a message or file contains any non-text parts
344 is_nontext (char *msgnam)
347 unsigned char *bp, *dp;
349 char buf[BUFSIZ], name[NAMESZ];
352 if ((fp = fopen (msgnam, "r")) == NULL)
355 for (state = FLD;;) {
356 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
361 * Check Content-Type field
363 if (!mh_strcasecmp (name, TYPE_FIELD)) {
367 cp = add (buf, NULL);
368 while (state == FLDPLUS) {
369 state = m_getfld (state, name, buf, sizeof(buf), fp);
376 for (; isspace (*bp); bp++)
381 for (bp++, i = 0;;) {
411 for (dp = bp; istoken (*dp); dp++)
418 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
421 for (dp++; isspace (*dp); dp++)
424 if ((result = !uprf (dp, "charset")))
426 dp += sizeof("charset") - 1;
427 while (isspace (*dp))
431 while (isspace (*dp))
434 if ((bp = strchr(++dp, '"')))
437 for (bp = dp; *bp; bp++)
438 if (!istoken (*bp)) {
444 /* Default character set */
447 /* Check the character set */
448 result = !check_charset (dp, strlen (dp));
450 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
467 * Check Content-Transfer-Encoding field
469 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
470 cp = add (buf, NULL);
471 while (state == FLDPLUS) {
472 state = m_getfld (state, name, buf, sizeof(buf), fp);
475 for (bp = cp; isspace (*bp); bp++)
477 for (dp = bp; istoken (*dp); dp++)
480 result = (mh_strcasecmp (bp, "7bit")
481 && mh_strcasecmp (bp, "8bit")
482 && mh_strcasecmp (bp, "binary"));
493 * Just skip the rest of this header
494 * field and go to next one.
496 while (state == FLDPLUS)
497 state = m_getfld (state, name, buf, sizeof(buf), fp);
501 * We've passed the message header,
502 * so message is just text.