3 * show.c -- show/list messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 static struct swit switches[] = {
18 #define NOCHECKMIMESW 1
25 { "form formfile", 0 },
27 { "moreproc program", 0 },
31 { "length lines", 0 },
33 { "width columns", 0 },
35 { "showproc program", 0 },
37 { "showmimeproc program", 0 },
43 { "file file", -4 }, /* interface from showfile */
54 static int is_nontext(char *);
56 /* prototype from mhlsbr.c */
57 int mhl (int, char **);
65 main (int argc, char **argv)
67 int draftsw = 0, headersw = 1, msgp = 0;
68 int nshow = 0, checkmime = 1, mime;
69 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
70 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
71 char buf[BUFSIZ], **argp, **arguments;
72 char *msgs[MAXARGS], *vec[MAXARGS];
76 setlocale(LC_ALL, "");
78 invo_name = r1bindex (argv[0], '/');
80 /* read user profile/context */
83 if (!strcasecmp (invo_name, "next")) {
85 } else if (!strcasecmp (invo_name, "prev")) {
88 arguments = getarguments (invo_name, argc, argv, 1);
91 while ((cp = *argp++)) {
93 switch (smatch (++cp, switches)) {
95 ambigsw (cp, switches);
103 snprintf (buf, sizeof(buf),
104 "%s [+folder] %s[switches] [switches for showproc]",
105 invo_name, mode == SHOW ? "[msgs] ": "");
106 print_help (buf, switches, 1);
109 print_version(invo_name);
114 adios (NULL, "only one file at a time!");
120 "usage: %s [+folder] [switches] [switches for showproc]",
126 adios (NULL, "only one file at a time!");
127 if (!(cp = *argp++) || *cp == '-')
128 adios (NULL, "missing argument to %s", argp[-2]);
129 file = path (cp, TFILE);
141 if (!(cp = *argp++) || *cp == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
143 vec[vecp++] = getcpy (etcpath(cp));
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
156 if (!(showproc = *argp++) || *showproc == '-')
157 adios (NULL, "missing argument to %s", argp[-2]);
165 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
166 adios (NULL, "missing argument to %s", argp[-2]);
177 if (*cp == '+' || *cp == '@') {
179 adios (NULL, "only one folder at a time!");
181 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
191 if (!context_find ("path"))
192 free (path ("./", TFOLDER));
194 if (draftsw || file) {
196 adios (NULL, "only one file at a time!");
197 vec[vecp++] = draftsw
198 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
204 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
214 msgs[msgp++] = "next";
217 msgs[msgp++] = "prev";
220 msgs[msgp++] = "cur";
226 folder = getfolder (1);
227 maildir = m_maildir (folder);
229 if (chdir (maildir) == NOTOK)
230 adios (maildir, "unable to change directory to");
232 /* read folder and create message structure */
233 if (!(mp = folder_read (folder)))
234 adios (NULL, "unable to read folder %s", folder);
236 /* check for empty folder */
238 adios (NULL, "no messages in %s", folder);
240 /* parse all the message ranges/sequences and set SELECTED */
241 for (msgnum = 0; msgnum < msgp; msgnum++)
242 if (!m_convert (mp, msgs[msgnum]))
246 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
247 * since we will use that as a tag to know which messages
248 * to remove from the "unseen" sequence.
250 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
251 if (is_selected(mp, msgnum))
252 set_unseen (mp, msgnum);
254 seq_setprev (mp); /* set the Previous-Sequence */
255 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
257 if (mp->numsel > MAXARGS - 2)
258 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
260 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
261 if (is_selected(mp, msgnum))
262 vec[vecp++] = getcpy (m_name (msgnum));
264 seq_setcur (mp, mp->hghsel); /* update current message */
265 seq_save (mp); /* synchronize sequences */
266 context_replace (pfolder, folder); /* update current folder */
267 context_save (); /* save the context file */
269 if (headersw && vecp == 2)
270 printf ("(Message %s:%s)\n", folder, vec[1]);
278 * Decide which "proc" to use
284 /* check if any messages are non-text MIME messages */
285 if (checkmime && !getenv ("NOMHNPROC")) {
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[vecp - 1]))
307 if (folder && !draftsw && !file)
308 m_putenv ("mhfolder", folder);
311 * For backward compatibility, if the "proc" is mhn,
312 * then add "-show" option. Add "-file" if showing
315 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
316 if (draftsw || file) {
317 vec[vecp] = vec[vecp - 1];
318 vec[vecp - 1] = "-file";
321 vec[vecp++] = "-show";
325 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
327 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
328 vec[vecp] = vec[vecp - 1];
329 vec[vecp - 1] = "-file";
334 * If "proc" is mhl, then run it internally
335 * rather than exec'ing it.
337 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
344 * If you are not using a nmh command as your "proc", then
345 * add the path to the message names. Currently, we are just
346 * checking for mhn here, since we've already taken care of mhl.
348 if (!strcmp (r1bindex (proc, '/'), "mhl")
351 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
352 mp->foldpath = concat (mp->foldpath, "/", NULL);
353 cp = ssequal (maildir, mp->foldpath)
354 ? mp->foldpath + strlen (maildir)
356 for (msgnum = procp; msgnum < vecp; msgnum++)
357 vec[msgnum] = concat (cp, vec[msgnum], NULL);
360 vec[0] = r1bindex (proc, '/');
362 adios (proc, "unable to exec");
363 return 0; /* dead code to satisfy the compiler */
367 * Cheat: we are loaded with adrparse, which wants a routine called
368 * OfficialName(). We call adrparse:getm() with the correct arguments
369 * to prevent OfficialName() from being called. Hence, the following
370 * is to keep the loader happy.
374 OfficialName (char *name)
381 * Check if a message or file contains any non-text parts
384 is_nontext (char *msgnam)
388 char buf[BUFSIZ], name[NAMESZ];
391 if ((fp = fopen (msgnam, "r")) == NULL)
394 for (state = FLD;;) {
395 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
400 * Check Content-Type field
402 if (!strcasecmp (name, TYPE_FIELD)) {
406 cp = add (buf, NULL);
407 while (state == FLDPLUS) {
408 state = m_getfld (state, name, buf, sizeof(buf), fp);
415 for (; isspace (*bp); bp++)
420 for (bp++, i = 0;;) {
450 for (dp = bp; istoken (*dp); dp++)
457 if ((result = (strcasecmp (bp, "plain") != 0)))
460 for (dp++; isspace (*dp); dp++)
463 if ((result = !uprf (dp, "charset")))
465 dp += sizeof("charset") - 1;
466 while (isspace (*dp))
470 while (isspace (*dp))
473 if ((bp = strchr(++dp, '"')))
476 for (bp = dp; *bp; bp++)
483 /* Default character set */
486 /* Check the character set */
487 result = !check_charset (dp, strlen (dp));
489 if (!(result = (strcasecmp (bp, "text") != 0))) {
506 * Check Content-Transfer-Encoding field
508 if (!strcasecmp (name, ENCODING_FIELD)) {
509 cp = add (buf, NULL);
510 while (state == FLDPLUS) {
511 state = m_getfld (state, name, buf, sizeof(buf), fp);
514 for (bp = cp; isspace (*bp); bp++)
516 for (dp = bp; istoken (*dp); dp++)
519 result = (strcasecmp (bp, "7bit")
520 && strcasecmp (bp, "8bit")
521 && strcasecmp (bp, "binary"));
532 * Just skip the rest of this header
533 * field and go to next one.
535 while (state == FLDPLUS)
536 state = m_getfld (state, name, buf, sizeof(buf), fp);
540 * We've passed the message header,
541 * so message is just text.