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 */
53 static int is_nontext(char *);
55 /* prototype from mhlsbr.c */
56 int mhl (int, char **);
64 main (int argc, char **argv)
66 int draftsw = 0, headersw = 1, msgp = 0;
67 int nshow = 0, checkmime = 1, mime;
68 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
69 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
70 char buf[BUFSIZ], **argp, **arguments;
71 char *msgs[MAXARGS], *vec[MAXARGS];
72 struct msgs *mp = NULL;
75 setlocale(LC_ALL, "");
77 invo_name = r1bindex (argv[0], '/');
79 /* read user profile/context */
82 if (!mh_strcasecmp (invo_name, "next")) {
84 } else if (!mh_strcasecmp (invo_name, "prev")) {
87 arguments = getarguments (invo_name, argc, argv, 1);
90 while ((cp = *argp++)) {
92 switch (smatch (++cp, switches)) {
94 ambigsw (cp, switches);
102 snprintf (buf, sizeof(buf),
103 "%s [+folder] %s[switches] [switches for showproc]",
104 invo_name, mode == SHOW ? "[msgs] ": "");
105 print_help (buf, switches, 1);
108 print_version(invo_name);
113 adios (NULL, "only one file at a time!");
119 "usage: %s [+folder] [switches] [switches for showproc]",
125 adios (NULL, "only one file at a time!");
126 if (!(cp = *argp++) || *cp == '-')
127 adios (NULL, "missing argument to %s", argp[-2]);
128 file = path (cp, TFILE);
140 if (!(cp = *argp++) || *cp == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
142 vec[vecp++] = getcpy (etcpath(cp));
149 if (!(cp = *argp++) || *cp == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
155 if (!(showproc = *argp++) || *showproc == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
164 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
165 adios (NULL, "missing argument to %s", argp[-2]);
176 if (*cp == '+' || *cp == '@') {
178 adios (NULL, "only one folder at a time!");
180 folder = pluspath (cp);
190 if (!context_find ("path"))
191 free (path ("./", TFOLDER));
193 if (draftsw || file) {
195 adios (NULL, "only one file at a time!");
196 vec[vecp++] = draftsw
197 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
203 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
213 msgs[msgp++] = "next";
216 msgs[msgp++] = "prev";
219 msgs[msgp++] = "cur";
225 folder = getfolder (1);
226 maildir = m_maildir (folder);
228 if (chdir (maildir) == NOTOK)
229 adios (maildir, "unable to change directory to");
231 /* read folder and create message structure */
232 if (!(mp = folder_read (folder)))
233 adios (NULL, "unable to read folder %s", folder);
235 /* check for empty folder */
237 adios (NULL, "no messages in %s", folder);
239 /* parse all the message ranges/sequences and set SELECTED */
240 for (msgnum = 0; msgnum < msgp; msgnum++)
241 if (!m_convert (mp, msgs[msgnum]))
245 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
246 * since we will use that as a tag to know which messages
247 * to remove from the "unseen" sequence.
249 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
250 if (is_selected(mp, msgnum))
251 set_unseen (mp, msgnum);
253 seq_setprev (mp); /* set the Previous-Sequence */
254 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
256 if (mp->numsel > MAXARGS - 2)
257 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
259 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
260 if (is_selected(mp, msgnum))
261 vec[vecp++] = getcpy (m_name (msgnum));
263 seq_setcur (mp, mp->hghsel); /* update current message */
264 seq_save (mp); /* synchronize sequences */
265 context_replace (pfolder, folder); /* update current folder */
266 context_save (); /* save the context file */
268 if (headersw && vecp == 2)
269 printf ("(Message %s:%s)\n", folder, vec[1]);
277 * Decide which "proc" to use
283 /* check if any messages are non-text MIME messages */
284 if (checkmime && !getenv ("NOMHNPROC")) {
285 if (!draftsw && !file) {
286 /* loop through selected messages and check for MIME */
287 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
288 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
293 /* check the file or draft for MIME */
294 if (is_nontext (vec[vecp - 1]))
306 if (folder && !draftsw && !file)
307 m_putenv ("mhfolder", folder);
310 * For backward compatibility, if the "proc" is mhn,
311 * then add "-show" option. Add "-file" if showing
314 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
315 if (draftsw || file) {
316 vec[vecp] = vec[vecp - 1];
317 vec[vecp - 1] = "-file";
320 vec[vecp++] = "-show";
324 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
326 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
327 vec[vecp] = vec[vecp - 1];
328 vec[vecp - 1] = "-file";
333 * If "proc" is mhl, then run it internally
334 * rather than exec'ing it.
336 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
343 * If you are not using a nmh command as your "proc", then
344 * add the path to the message names. Currently, we are just
345 * checking for mhn here, since we've already taken care of mhl.
347 if (!strcmp (r1bindex (proc, '/'), "mhl")
350 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
351 mp->foldpath = concat (mp->foldpath, "/", NULL);
352 cp = ssequal (maildir, mp->foldpath)
353 ? mp->foldpath + strlen (maildir)
355 for (msgnum = procp; msgnum < vecp; msgnum++)
356 vec[msgnum] = concat (cp, vec[msgnum], NULL);
359 vec[0] = r1bindex (proc, '/');
361 adios (proc, "unable to exec");
362 return 0; /* dead code to satisfy the compiler */
366 * Cheat: we are loaded with adrparse, which wants a routine called
367 * OfficialName(). We call adrparse:getm() with the correct arguments
368 * to prevent OfficialName() from being called. Hence, the following
369 * is to keep the loader happy.
373 OfficialName (char *name)
380 * Check if a message or file contains any non-text parts
383 is_nontext (char *msgnam)
386 unsigned char *bp, *dp;
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 (!mh_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 = (mh_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++)
477 if (!istoken (*bp)) {
483 /* Default character set */
486 /* Check the character set */
487 result = !check_charset (dp, strlen (dp));
489 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
506 * Check Content-Transfer-Encoding field
508 if (!mh_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 = (mh_strcasecmp (bp, "7bit")
520 && mh_strcasecmp (bp, "8bit")
521 && mh_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.