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);
334 * If you are not using a nmh command as your "proc", then
335 * add the path to the message names. Currently, we are just
336 * checking for mhn here, since we've already taken care of mhl.
338 if (!strcmp (r1bindex (proc, '/'), "mhl")
341 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
342 mp->foldpath = concat (mp->foldpath, "/", NULL);
343 cp = ssequal (maildir, mp->foldpath)
344 ? mp->foldpath + strlen (maildir)
346 for (msgnum = procp; msgnum < vec.size; msgnum++)
347 vec.msgs[msgnum] = concat (cp, vec.msgs[msgnum], NULL);
350 vec.msgs[0] = r1bindex (proc, '/');
351 app_msgarg(&vec, NULL);
352 execvp (proc, vec.msgs);
353 adios (proc, "unable to exec");
354 return 0; /* dead code to satisfy the compiler */
359 * Check if a message or file contains any non-text parts
362 is_nontext (char *msgnam)
365 unsigned char *bp, *dp;
367 char buf[BUFSIZ], name[NAMESZ];
370 if ((fp = fopen (msgnam, "r")) == NULL)
373 for (state = FLD;;) {
374 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
379 * Check Content-Type field
381 if (!mh_strcasecmp (name, TYPE_FIELD)) {
385 cp = add (buf, NULL);
386 while (state == FLDPLUS) {
387 state = m_getfld (state, name, buf, sizeof(buf), fp);
394 for (; isspace (*bp); bp++)
399 for (bp++, i = 0;;) {
429 for (dp = bp; istoken (*dp); dp++)
436 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
439 for (dp++; isspace (*dp); dp++)
442 if ((result = !uprf (dp, "charset")))
444 dp += sizeof("charset") - 1;
445 while (isspace (*dp))
449 while (isspace (*dp))
452 if ((bp = strchr(++dp, '"')))
455 for (bp = dp; *bp; bp++)
456 if (!istoken (*bp)) {
462 /* Default character set */
465 /* Check the character set */
466 result = !check_charset (dp, strlen (dp));
468 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
485 * Check Content-Transfer-Encoding field
487 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
488 cp = add (buf, NULL);
489 while (state == FLDPLUS) {
490 state = m_getfld (state, name, buf, sizeof(buf), fp);
493 for (bp = cp; isspace (*bp); bp++)
495 for (dp = bp; istoken (*dp); dp++)
498 result = (mh_strcasecmp (bp, "7bit")
499 && mh_strcasecmp (bp, "8bit")
500 && mh_strcasecmp (bp, "binary"));
511 * Just skip the rest of this header
512 * field and go to next one.
514 while (state == FLDPLUS)
515 state = m_getfld (state, name, buf, sizeof(buf), fp);
519 * We've passed the message header,
520 * so message is just text.