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, 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];
73 struct msgs *mp = NULL;
76 setlocale(LC_ALL, "");
78 invo_name = r1bindex (argv[0], '/');
80 /* read user profile/context */
83 if (!mh_strcasecmp (invo_name, "next")) {
85 } else if (!mh_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);
104 snprintf (buf, sizeof(buf),
105 "%s [+folder] %s[switches] [switches for showproc]",
106 invo_name, mode == SHOW ? "[msgs] ": "");
107 print_help (buf, switches, 1);
110 print_version(invo_name);
115 adios (NULL, "only one file at a time!");
121 "usage: %s [+folder] [switches] [switches for showproc]",
127 adios (NULL, "only one file at a time!");
128 if (!(cp = *argp++) || *cp == '-')
129 adios (NULL, "missing argument to %s", argp[-2]);
130 file = path (cp, TFILE);
142 if (!(cp = *argp++) || *cp == '-')
143 adios (NULL, "missing argument to %s", argp[-2]);
144 vec[vecp++] = getcpy (etcpath(cp));
152 if (!(cp = *argp++) || *cp == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
158 if (!(showproc = *argp++) || *showproc == '-')
159 adios (NULL, "missing argument to %s", argp[-2]);
167 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
168 adios (NULL, "missing argument to %s", argp[-2]);
179 if (*cp == '+' || *cp == '@') {
181 adios (NULL, "only one folder at a time!");
183 folder = pluspath (cp);
193 if (!context_find ("path"))
194 free (path ("./", TFOLDER));
196 if (draftsw || file) {
198 adios (NULL, "only one file at a time!");
199 vec[vecp++] = draftsw
200 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
206 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
216 msgs[msgp++] = "next";
219 msgs[msgp++] = "prev";
222 msgs[msgp++] = "cur";
228 folder = getfolder (1);
229 maildir = m_maildir (folder);
231 if (chdir (maildir) == NOTOK)
232 adios (maildir, "unable to change directory to");
234 /* read folder and create message structure */
235 if (!(mp = folder_read (folder)))
236 adios (NULL, "unable to read folder %s", folder);
238 /* check for empty folder */
240 adios (NULL, "no messages in %s", folder);
242 /* parse all the message ranges/sequences and set SELECTED */
243 for (msgnum = 0; msgnum < msgp; msgnum++)
244 if (!m_convert (mp, msgs[msgnum]))
248 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
249 * since we will use that as a tag to know which messages
250 * to remove from the "unseen" sequence.
252 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
253 if (is_selected(mp, msgnum))
254 set_unseen (mp, msgnum);
256 seq_setprev (mp); /* set the Previous-Sequence */
257 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
259 if (mp->numsel > MAXARGS - 2)
260 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
262 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
263 if (is_selected(mp, msgnum))
264 vec[vecp++] = 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 && vecp == 2)
272 printf ("(Message %s:%s)\n", folder, vec[1]);
280 * Decide which "proc" to use
286 /* check if any messages are non-text MIME messages */
287 if (checkmime && !getenv ("NOMHNPROC")) {
288 if (!draftsw && !file) {
289 /* loop through selected messages and check for MIME */
290 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
291 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
296 /* check the file or draft for MIME */
297 if (is_nontext (vec[vecp - 1]))
309 if (folder && !draftsw && !file)
310 m_putenv ("mhfolder", folder);
313 * For backward compatibility, if the "proc" is mhn,
314 * then add "-show" option. Add "-file" if showing
317 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
318 if (draftsw || file) {
319 vec[vecp] = vec[vecp - 1];
320 vec[vecp - 1] = "-file";
323 vec[vecp++] = "-show";
327 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
329 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
330 vec[vecp] = vec[vecp - 1];
331 vec[vecp - 1] = "-file";
336 * If "proc" is mhl, then run it internally
337 * rather than exec'ing it.
339 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
346 * If you are not using a nmh command as your "proc", then
347 * add the path to the message names. Currently, we are just
348 * checking for mhn here, since we've already taken care of mhl.
350 if (!strcmp (r1bindex (proc, '/'), "mhl")
353 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
354 mp->foldpath = concat (mp->foldpath, "/", NULL);
355 cp = ssequal (maildir, mp->foldpath)
356 ? mp->foldpath + strlen (maildir)
358 for (msgnum = procp; msgnum < vecp; msgnum++)
359 vec[msgnum] = concat (cp, vec[msgnum], NULL);
362 vec[0] = r1bindex (proc, '/');
364 adios (proc, "unable to exec");
365 return 0; /* dead code to satisfy the compiler */
370 * Check if a message or file contains any non-text parts
373 is_nontext (char *msgnam)
376 unsigned char *bp, *dp;
378 char buf[BUFSIZ], name[NAMESZ];
381 if ((fp = fopen (msgnam, "r")) == NULL)
384 for (state = FLD;;) {
385 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
390 * Check Content-Type field
392 if (!mh_strcasecmp (name, TYPE_FIELD)) {
396 cp = add (buf, NULL);
397 while (state == FLDPLUS) {
398 state = m_getfld (state, name, buf, sizeof(buf), fp);
405 for (; isspace (*bp); bp++)
410 for (bp++, i = 0;;) {
440 for (dp = bp; istoken (*dp); dp++)
447 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
450 for (dp++; isspace (*dp); dp++)
453 if ((result = !uprf (dp, "charset")))
455 dp += sizeof("charset") - 1;
456 while (isspace (*dp))
460 while (isspace (*dp))
463 if ((bp = strchr(++dp, '"')))
466 for (bp = dp; *bp; bp++)
467 if (!istoken (*bp)) {
473 /* Default character set */
476 /* Check the character set */
477 result = !check_charset (dp, strlen (dp));
479 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
496 * Check Content-Transfer-Encoding field
498 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
499 cp = add (buf, NULL);
500 while (state == FLDPLUS) {
501 state = m_getfld (state, name, buf, sizeof(buf), fp);
504 for (bp = cp; isspace (*bp); bp++)
506 for (dp = bp; istoken (*dp); dp++)
509 result = (mh_strcasecmp (bp, "7bit")
510 && mh_strcasecmp (bp, "8bit")
511 && mh_strcasecmp (bp, "binary"));
522 * Just skip the rest of this header
523 * field and go to next one.
525 while (state == FLDPLUS)
526 state = m_getfld (state, name, buf, sizeof(buf), fp);
530 * We've passed the message header,
531 * so message is just text.