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 procp = 1, 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);
196 if (!context_find ("path"))
197 free (path ("./", TFOLDER));
199 if (draftsw || file) {
201 adios (NULL, "only one file at a time!");
203 app_msgarg(&vec, getcpy (m_draft (folder, NULL, 1, &isdf)));
205 app_msgarg(&vec, file);
210 if (!msgs.size && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
212 app_msgarg(&vec, cp);
220 app_msgarg(&msgs, "next");
223 app_msgarg(&msgs, "prev");
226 app_msgarg(&msgs, "cur");
232 folder = getfolder (1);
233 maildir = m_maildir (folder);
235 if (chdir (maildir) == NOTOK)
236 adios (maildir, "unable to change directory to");
238 /* read folder and create message structure */
239 if (!(mp = folder_read (folder)))
240 adios (NULL, "unable to read folder %s", folder);
242 /* check for empty folder */
244 adios (NULL, "no messages in %s", folder);
246 /* parse all the message ranges/sequences and set SELECTED */
247 for (msgnum = 0; msgnum < msgs.size; msgnum++)
248 if (!m_convert (mp, msgs.msgs[msgnum]))
252 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
253 * since we will use that as a tag to know which messages
254 * to remove from the "unseen" sequence.
256 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
257 if (is_selected(mp, msgnum))
258 set_unseen (mp, msgnum);
260 seq_setprev (mp); /* set the Previous-Sequence */
261 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
263 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
264 if (is_selected(mp, msgnum))
265 app_msgarg(&vec, getcpy (m_name (msgnum)));
267 seq_setcur (mp, mp->hghsel); /* update current message */
268 seq_save (mp); /* synchronize sequences */
269 context_replace (pfolder, folder); /* update current folder */
270 context_save (); /* save the context file */
272 if (headersw && vec.size == 2)
273 printf ("(Message %s:%s)\n", folder, vec.msgs[1]);
279 * Decide which "proc" to use
285 /* check if any messages are non-text MIME messages */
287 if (!draftsw && !file) {
288 /* loop through selected messages and check for MIME */
289 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
290 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
295 /* check the file or draft for MIME */
296 if (is_nontext (vec.msgs[vec.size - 1]))
308 if (folder && !draftsw && !file)
309 m_putenv ("mhfolder", folder);
311 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
312 /* Add "-file" if showing file or draft, */
313 if (draftsw || file) {
314 app_msgarg(&vec, vec.msgs[vec.size - 1]);
315 vec.msgs[vec.size - 2] = "-file";
317 /* and add -show for backward compatibility */
318 app_msgarg(&vec, "-show");
319 } else if (strcmp (r1bindex (proc, '/'), "mhshow") == 0) {
320 /* If "mhshow", add "-file" if showing file or draft. */
321 if (draftsw || file) {
322 app_msgarg(&vec, vec.msgs[vec.size - 1]);
323 vec.msgs[vec.size - 2] = "-file";
325 } else if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
326 /* If "mhl", then run it internally */
328 app_msgarg(&vec, NULL);
329 mhl (vec.size, vec.msgs);
333 vec.msgs[0] = r1bindex (proc, '/');
334 app_msgarg(&vec, NULL);
335 execvp (proc, vec.msgs);
336 adios (proc, "unable to exec");
337 return 0; /* dead code to satisfy the compiler */
342 * Check if a message or file contains any non-text parts
345 is_nontext (char *msgnam)
348 unsigned char *bp, *dp;
350 char buf[BUFSIZ], name[NAMESZ];
353 if ((fp = fopen (msgnam, "r")) == NULL)
356 for (state = FLD;;) {
357 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
362 * Check Content-Type field
364 if (!mh_strcasecmp (name, TYPE_FIELD)) {
368 cp = add (buf, NULL);
369 while (state == FLDPLUS) {
370 state = m_getfld (state, name, buf, sizeof(buf), fp);
377 for (; isspace (*bp); bp++)
382 for (bp++, i = 0;;) {
412 for (dp = bp; istoken (*dp); dp++)
419 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
422 for (dp++; isspace (*dp); dp++)
425 if ((result = !uprf (dp, "charset")))
427 dp += sizeof("charset") - 1;
428 while (isspace (*dp))
432 while (isspace (*dp))
435 if ((bp = strchr(++dp, '"')))
438 for (bp = dp; *bp; bp++)
439 if (!istoken (*bp)) {
445 /* Default character set */
448 /* Check the character set */
449 result = !check_charset (dp, strlen (dp));
451 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
468 * Check Content-Transfer-Encoding field
470 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
471 cp = add (buf, NULL);
472 while (state == FLDPLUS) {
473 state = m_getfld (state, name, buf, sizeof(buf), fp);
476 for (bp = cp; isspace (*bp); bp++)
478 for (dp = bp; istoken (*dp); dp++)
481 result = (mh_strcasecmp (bp, "7bit")
482 && mh_strcasecmp (bp, "8bit")
483 && mh_strcasecmp (bp, "binary"));
494 * Just skip the rest of this header
495 * field and go to next one.
497 while (state == FLDPLUS)
498 state = m_getfld (state, name, buf, sizeof(buf), fp);
502 * We've passed the message header,
503 * so message is just text.