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.
16 static struct swit switches[] = {
19 #define NOCHECKMIMESW 1
26 { "form formfile", 0 },
28 { "moreproc program", 0 },
32 { "length lines", 0 },
34 { "width columns", 0 },
36 { "showproc program", 0 },
38 { "showmimeproc program", 0 },
44 { "file file", -4 }, /* interface from showfile */
55 static int is_nontext(char *);
57 /* prototype from mhlsbr.c */
58 int mhl (int, char **);
66 main (int argc, char **argv)
68 int draftsw = 0, headersw = 1, msgp = 0;
69 int nshow = 0, checkmime = 1, mime;
70 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
71 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
72 char buf[BUFSIZ], **argp, **arguments;
73 char *msgs[MAXARGS], *vec[MAXARGS];
77 setlocale(LC_ALL, "");
79 invo_name = r1bindex (argv[0], '/');
81 /* read user profile/context */
84 if (!mh_strcasecmp (invo_name, "next")) {
86 } else if (!mh_strcasecmp (invo_name, "prev")) {
89 arguments = getarguments (invo_name, argc, argv, 1);
92 while ((cp = *argp++)) {
94 switch (smatch (++cp, switches)) {
96 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));
151 if (!(cp = *argp++) || *cp == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
157 if (!(showproc = *argp++) || *showproc == '-')
158 adios (NULL, "missing argument to %s", argp[-2]);
166 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
178 if (*cp == '+' || *cp == '@') {
180 adios (NULL, "only one folder at a time!");
182 folder = pluspath (cp);
192 if (!context_find ("path"))
193 free (path ("./", TFOLDER));
195 if (draftsw || file) {
197 adios (NULL, "only one file at a time!");
198 vec[vecp++] = draftsw
199 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
205 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
215 msgs[msgp++] = "next";
218 msgs[msgp++] = "prev";
221 msgs[msgp++] = "cur";
227 folder = getfolder (1);
228 maildir = m_maildir (folder);
230 if (chdir (maildir) == NOTOK)
231 adios (maildir, "unable to change directory to");
233 /* read folder and create message structure */
234 if (!(mp = folder_read (folder)))
235 adios (NULL, "unable to read folder %s", folder);
237 /* check for empty folder */
239 adios (NULL, "no messages in %s", folder);
241 /* parse all the message ranges/sequences and set SELECTED */
242 for (msgnum = 0; msgnum < msgp; msgnum++)
243 if (!m_convert (mp, msgs[msgnum]))
247 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
248 * since we will use that as a tag to know which messages
249 * to remove from the "unseen" sequence.
251 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
252 if (is_selected(mp, msgnum))
253 set_unseen (mp, msgnum);
255 seq_setprev (mp); /* set the Previous-Sequence */
256 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
258 if (mp->numsel > MAXARGS - 2)
259 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
261 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
262 if (is_selected(mp, msgnum))
263 vec[vecp++] = getcpy (m_name (msgnum));
265 seq_setcur (mp, mp->hghsel); /* update current message */
266 seq_save (mp); /* synchronize sequences */
267 context_replace (pfolder, folder); /* update current folder */
268 context_save (); /* save the context file */
270 if (headersw && vecp == 2)
271 printf ("(Message %s:%s)\n", folder, vec[1]);
279 * Decide which "proc" to use
285 /* check if any messages are non-text MIME messages */
286 if (checkmime && !getenv ("NOMHNPROC")) {
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[vecp - 1]))
308 if (folder && !draftsw && !file)
309 m_putenv ("mhfolder", folder);
312 * For backward compatibility, if the "proc" is mhn,
313 * then add "-show" option. Add "-file" if showing
316 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
317 if (draftsw || file) {
318 vec[vecp] = vec[vecp - 1];
319 vec[vecp - 1] = "-file";
322 vec[vecp++] = "-show";
326 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
328 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
329 vec[vecp] = vec[vecp - 1];
330 vec[vecp - 1] = "-file";
335 * If "proc" is mhl, then run it internally
336 * rather than exec'ing it.
338 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
345 * If you are not using a nmh command as your "proc", then
346 * add the path to the message names. Currently, we are just
347 * checking for mhn here, since we've already taken care of mhl.
349 if (!strcmp (r1bindex (proc, '/'), "mhl")
352 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
353 mp->foldpath = concat (mp->foldpath, "/", NULL);
354 cp = ssequal (maildir, mp->foldpath)
355 ? mp->foldpath + strlen (maildir)
357 for (msgnum = procp; msgnum < vecp; msgnum++)
358 vec[msgnum] = concat (cp, vec[msgnum], NULL);
361 vec[0] = r1bindex (proc, '/');
363 adios (proc, "unable to exec");
364 return 0; /* dead code to satisfy the compiler */
368 * Cheat: we are loaded with adrparse, which wants a routine called
369 * OfficialName(). We call adrparse:getm() with the correct arguments
370 * to prevent OfficialName() from being called. Hence, the following
371 * is to keep the loader happy.
375 OfficialName (char *name)
382 * Check if a message or file contains any non-text parts
385 is_nontext (char *msgnam)
389 char buf[BUFSIZ], name[NAMESZ];
392 if ((fp = fopen (msgnam, "r")) == NULL)
395 for (state = FLD;;) {
396 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
401 * Check Content-Type field
403 if (!mh_strcasecmp (name, TYPE_FIELD)) {
407 cp = add (buf, NULL);
408 while (state == FLDPLUS) {
409 state = m_getfld (state, name, buf, sizeof(buf), fp);
416 for (; isspace (*bp); bp++)
421 for (bp++, i = 0;;) {
451 for (dp = bp; istoken (*dp); dp++)
458 if ((result = (mh_strcasecmp (bp, "plain") != 0)))
461 for (dp++; isspace (*dp); dp++)
464 if ((result = !uprf (dp, "charset")))
466 dp += sizeof("charset") - 1;
467 while (isspace (*dp))
471 while (isspace (*dp))
474 if ((bp = strchr(++dp, '"')))
477 for (bp = dp; *bp; bp++)
478 if (!istoken (*bp)) {
484 /* Default character set */
487 /* Check the character set */
488 result = !check_charset (dp, strlen (dp));
490 if (!(result = (mh_strcasecmp (bp, "text") != 0))) {
507 * Check Content-Transfer-Encoding field
509 if (!mh_strcasecmp (name, ENCODING_FIELD)) {
510 cp = add (buf, NULL);
511 while (state == FLDPLUS) {
512 state = m_getfld (state, name, buf, sizeof(buf), fp);
515 for (bp = cp; isspace (*bp); bp++)
517 for (dp = bp; istoken (*dp); dp++)
520 result = (mh_strcasecmp (bp, "7bit")
521 && mh_strcasecmp (bp, "8bit")
522 && mh_strcasecmp (bp, "binary"));
533 * Just skip the rest of this header
534 * field and go to next one.
536 while (state == FLDPLUS)
537 state = m_getfld (state, name, buf, sizeof(buf), fp);
541 * We've passed the message header,
542 * so message is just text.