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 *);
61 main (int argc, char **argv)
63 int draftsw = 0, headersw = 1, msgp = 0;
64 int nshow = 0, checkmime = 1, mime;
65 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
66 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
67 char buf[BUFSIZ], **argp, **arguments;
68 char *msgs[MAXARGS], *vec[MAXARGS];
69 struct msgs *mp = NULL;
72 setlocale(LC_ALL, "");
74 invo_name = r1bindex (argv[0], '/');
76 /* read user profile/context */
79 if (!mh_strcasecmp (invo_name, "next")) {
81 } else if (!mh_strcasecmp (invo_name, "prev")) {
84 arguments = getarguments (invo_name, argc, argv, 1);
87 while ((cp = *argp++)) {
89 switch (smatch (++cp, switches)) {
91 ambigsw (cp, switches);
99 snprintf (buf, sizeof(buf),
100 "%s [+folder] %s[switches] [switches for showproc]",
101 invo_name, mode == SHOW ? "[msgs] ": "");
102 print_help (buf, switches, 1);
105 print_version(invo_name);
110 adios (NULL, "only one file at a time!");
116 "usage: %s [+folder] [switches] [switches for showproc]",
122 adios (NULL, "only one file at a time!");
123 if (!(cp = *argp++) || *cp == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
125 file = path (cp, TFILE);
137 if (!(cp = *argp++) || *cp == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
139 vec[vecp++] = getcpy (etcpath(cp));
146 if (!(cp = *argp++) || *cp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
152 if (!(showproc = *argp++) || *showproc == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
161 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
173 if (*cp == '+' || *cp == '@') {
175 adios (NULL, "only one folder at a time!");
177 folder = pluspath (cp);
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER));
190 if (draftsw || file) {
192 adios (NULL, "only one file at a time!");
193 vec[vecp++] = draftsw
194 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
200 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
210 msgs[msgp++] = "next";
213 msgs[msgp++] = "prev";
216 msgs[msgp++] = "cur";
222 folder = getfolder (1);
223 maildir = m_maildir (folder);
225 if (chdir (maildir) == NOTOK)
226 adios (maildir, "unable to change directory to");
228 /* read folder and create message structure */
229 if (!(mp = folder_read (folder)))
230 adios (NULL, "unable to read folder %s", folder);
232 /* check for empty folder */
234 adios (NULL, "no messages in %s", folder);
236 /* parse all the message ranges/sequences and set SELECTED */
237 for (msgnum = 0; msgnum < msgp; msgnum++)
238 if (!m_convert (mp, msgs[msgnum]))
242 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
243 * since we will use that as a tag to know which messages
244 * to remove from the "unseen" sequence.
246 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
247 if (is_selected(mp, msgnum))
248 set_unseen (mp, msgnum);
250 seq_setprev (mp); /* set the Previous-Sequence */
251 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
253 if (mp->numsel > MAXARGS - 2)
254 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
256 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
257 if (is_selected(mp, msgnum))
258 vec[vecp++] = getcpy (m_name (msgnum));
260 seq_setcur (mp, mp->hghsel); /* update current message */
261 seq_save (mp); /* synchronize sequences */
262 context_replace (pfolder, folder); /* update current folder */
263 context_save (); /* save the context file */
265 if (headersw && vecp == 2)
266 printf ("(Message %s:%s)\n", folder, vec[1]);
274 * Decide which "proc" to use
280 /* check if any messages are non-text MIME messages */
281 if (checkmime && !getenv ("NOMHNPROC")) {
282 if (!draftsw && !file) {
283 /* loop through selected messages and check for MIME */
284 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
285 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
290 /* check the file or draft for MIME */
291 if (is_nontext (vec[vecp - 1]))
303 if (folder && !draftsw && !file)
304 m_putenv ("mhfolder", folder);
307 * For backward compatibility, if the "proc" is mhn,
308 * then add "-show" option. Add "-file" if showing
311 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
312 if (draftsw || file) {
313 vec[vecp] = vec[vecp - 1];
314 vec[vecp - 1] = "-file";
317 vec[vecp++] = "-show";
321 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
323 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
324 vec[vecp] = vec[vecp - 1];
325 vec[vecp - 1] = "-file";
330 * If "proc" is mhl, then run it internally
331 * rather than exec'ing it.
333 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
340 * If you are not using a nmh command as your "proc", then
341 * add the path to the message names. Currently, we are just
342 * checking for mhn here, since we've already taken care of mhl.
344 if (!strcmp (r1bindex (proc, '/'), "mhl")
347 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
348 mp->foldpath = concat (mp->foldpath, "/", NULL);
349 cp = ssequal (maildir, mp->foldpath)
350 ? mp->foldpath + strlen (maildir)
352 for (msgnum = procp; msgnum < vecp; msgnum++)
353 vec[msgnum] = concat (cp, vec[msgnum], NULL);
356 vec[0] = r1bindex (proc, '/');
358 adios (proc, "unable to exec");
359 return 0; /* dead code to satisfy the compiler */
363 * Cheat: we are loaded with adrparse, which wants a routine called
364 * OfficialName(). We call adrparse:getm() with the correct arguments
365 * to prevent OfficialName() from being called. Hence, the following
366 * is to keep the loader happy.
370 OfficialName (char *name)
377 * Check if a message or file contains any non-text parts
380 is_nontext (char *msgnam)
383 unsigned char *bp, *dp;
385 char buf[BUFSIZ], name[NAMESZ];
388 if ((fp = fopen (msgnam, "r")) == NULL)
391 for (state = FLD;;) {
392 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
397 * Check Content-Type field
399 if (!mh_strcasecmp (name, TYPE_FIELD)) {
403 cp = add (buf, NULL);
404 while (state == FLDPLUS) {
405 state = m_getfld (state, name, buf, sizeof(buf), fp);
412 for (; isspace (*bp); bp++)
417 for (bp++, i = 0;;) {
447 for (dp = bp; istoken (*dp); dp++)
454 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
457 for (dp++; isspace (*dp); dp++)
460 if ((result = !uprf (dp, "charset")))
462 dp += sizeof("charset") - 1;
463 while (isspace (*dp))
467 while (isspace (*dp))
470 if ((bp = strchr(++dp, '"')))
473 for (bp = dp; *bp; bp++)
474 if (!istoken (*bp)) {
480 /* Default character set */
483 /* Check the character set */
484 result = !check_charset (dp, strlen (dp));
486 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
503 * Check Content-Transfer-Encoding field
505 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
506 cp = add (buf, NULL);
507 while (state == FLDPLUS) {
508 state = m_getfld (state, name, buf, sizeof(buf), fp);
511 for (bp = cp; isspace (*bp); bp++)
513 for (dp = bp; istoken (*dp); dp++)
516 result = (mh_strcasecmp (bp, "7bit")
517 && mh_strcasecmp (bp, "8bit")
518 && mh_strcasecmp (bp, "binary"));
529 * Just skip the rest of this header
530 * field and go to next one.
532 while (state == FLDPLUS)
533 state = m_getfld (state, name, buf, sizeof(buf), fp);
537 * We've passed the message header,
538 * so message is just text.