3 * show.c -- show/list messages
11 static struct swit switches[] = {
14 #define NOCHECKMIMESW 1
21 { "form formfile", 0 },
23 { "moreproc program", 0 },
27 { "length lines", 0 },
29 { "width columns", 0 },
31 { "showproc program", 0 },
33 { "showmimeproc program", 0 },
39 { "file file", -4 }, /* interface from showfile */
50 static int is_nontext(char *);
58 main (int argc, char **argv)
60 int draftsw = 0, headersw = 1, msgp = 0;
61 int nshow = 0, checkmime = 1, mime;
62 int vecp = 1, procp = 1, isdf = 0, mode = SHOW, msgnum;
63 char *cp, *maildir, *file = NULL, *folder = NULL, *proc;
64 char buf[BUFSIZ], **argp, **arguments;
65 char *msgs[MAXARGS], *vec[MAXARGS];
69 setlocale(LC_ALL, "");
71 invo_name = r1bindex (argv[0], '/');
73 /* read user profile/context */
76 if (!strcasecmp (invo_name, "next")) {
78 } else if (!strcasecmp (invo_name, "prev")) {
81 arguments = getarguments (invo_name, argc, argv, 1);
84 while ((cp = *argp++)) {
86 switch (smatch (++cp, switches)) {
88 ambigsw (cp, switches);
96 snprintf (buf, sizeof(buf),
97 "%s [+folder] %s[switches] [switches for showproc]",
98 invo_name, mode == SHOW ? "[msgs] ": "");
99 print_help (buf, switches, 1);
102 print_version(invo_name);
107 adios (NULL, "only one file at a time!");
113 "usage: %s [+folder] [switches] [switches for showproc]",
119 adios (NULL, "only one file at a time!");
120 if (!(cp = *argp++) || *cp == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
122 file = path (cp, TFILE);
134 if (!(cp = *argp++) || *cp == '-')
135 adios (NULL, "missing argument to %s", argp[-2]);
136 vec[vecp++] = getcpy (etcpath(cp));
143 if (!(cp = *argp++) || *cp == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
149 if (!(showproc = *argp++) || *showproc == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
158 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
159 adios (NULL, "missing argument to %s", argp[-2]);
170 if (*cp == '+' || *cp == '@') {
172 adios (NULL, "only one folder at a time!");
174 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
184 if (!context_find ("path"))
185 free (path ("./", TFOLDER));
187 if (draftsw || file) {
189 adios (NULL, "only one file at a time!");
190 vec[vecp++] = draftsw
191 ? getcpy (m_draft (folder, msgp ? msgs[0] : NULL, 1, &isdf))
197 if (!msgp && !folder && mode == SHOW && (cp = getenv ("mhdraft")) && *cp) {
207 msgs[msgp++] = "next";
210 msgs[msgp++] = "prev";
213 msgs[msgp++] = "cur";
219 folder = getfolder (1);
220 maildir = m_maildir (folder);
222 if (chdir (maildir) == NOTOK)
223 adios (maildir, "unable to change directory to");
225 /* read folder and create message structure */
226 if (!(mp = folder_read (folder)))
227 adios (NULL, "unable to read folder %s", folder);
229 /* check for empty folder */
231 adios (NULL, "no messages in %s", folder);
233 /* parse all the message ranges/sequences and set SELECTED */
234 for (msgnum = 0; msgnum < msgp; msgnum++)
235 if (!m_convert (mp, msgs[msgnum]))
239 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
240 * since we will use that as a tag to know which messages
241 * to remove from the "unseen" sequence.
243 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
244 if (is_selected(mp, msgnum))
245 set_unseen (mp, msgnum);
247 seq_setprev (mp); /* set the Previous-Sequence */
248 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
250 if (mp->numsel > MAXARGS - 2)
251 adios (NULL, "more than %d messages for show exec", MAXARGS - 2);
253 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
254 if (is_selected(mp, msgnum))
255 vec[vecp++] = getcpy (m_name (msgnum));
257 seq_setcur (mp, mp->hghsel); /* update current message */
258 seq_save (mp); /* synchronize sequences */
259 context_replace (pfolder, folder); /* update current folder */
260 context_save (); /* save the context file */
262 if (headersw && vecp == 2)
263 printf ("(Message %s:%s)\n", folder, vec[1]);
271 * Decide which "proc" to use
277 /* check if any messages are non-text MIME messages */
278 if (checkmime && !getenv ("NOMHNPROC")) {
279 if (!draftsw && !file) {
280 /* loop through selected messages and check for MIME */
281 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
282 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
287 /* check the file or draft for MIME */
288 if (is_nontext (vec[vecp - 1]))
300 if (folder && !draftsw && !file)
301 m_putenv ("mhfolder", folder);
304 * For backward compatibility, if the "proc" is mhn,
305 * then add "-show" option. Add "-file" if showing
308 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
309 if (draftsw || file) {
310 vec[vecp] = vec[vecp - 1];
311 vec[vecp - 1] = "-file";
314 vec[vecp++] = "-show";
318 /* If the "proc" is "mhshow", add "-file" if showing file or draft.
320 if (strcmp (r1bindex (proc, '/'), "mhshow") == 0 && (draftsw || file) ) {
321 vec[vecp] = vec[vecp - 1];
322 vec[vecp - 1] = "-file";
327 * If "proc" is mhl, then run it internally
328 * rather than exec'ing it.
330 if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
337 * If you are not using a nmh command as your "proc", then
338 * add the path to the message names. Currently, we are just
339 * checking for mhn here, since we've already taken care of mhl.
341 if (!strcmp (r1bindex (proc, '/'), "mhl")
344 && chdir (maildir = concat (m_maildir (""), "/", NULL)) != NOTOK) {
345 mp->foldpath = concat (mp->foldpath, "/", NULL);
346 cp = ssequal (maildir, mp->foldpath)
347 ? mp->foldpath + strlen (maildir)
349 for (msgnum = procp; msgnum < vecp; msgnum++)
350 vec[msgnum] = concat (cp, vec[msgnum], NULL);
353 vec[0] = r1bindex (proc, '/');
355 adios (proc, "unable to exec");
359 * Cheat: we are loaded with adrparse, which wants a routine called
360 * OfficialName(). We call adrparse:getm() with the correct arguments
361 * to prevent OfficialName() from being called. Hence, the following
362 * is to keep the loader happy.
366 OfficialName (char *name)
373 * Check if a message or file contains any non-text parts
376 is_nontext (char *msgnam)
380 char buf[BUFSIZ], name[NAMESZ];
383 if ((fp = fopen (msgnam, "r")) == NULL)
386 for (state = FLD;;) {
387 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
392 * Check Content-Type field
394 if (!strcasecmp (name, TYPE_FIELD)) {
398 cp = add (buf, NULL);
399 while (state == FLDPLUS) {
400 state = m_getfld (state, name, buf, sizeof(buf), fp);
407 for (; isspace (*bp); bp++)
412 for (bp++, i = 0;;) {
442 for (dp = bp; istoken (*dp); dp++)
449 if ((result = (strcasecmp (bp, "plain") != 0)))
452 for (dp++; isspace (*dp); dp++)
455 if ((result = !uprf (dp, "charset")))
457 dp += sizeof("charset") - 1;
458 while (isspace (*dp))
462 while (isspace (*dp))
465 if ((bp = strchr(++dp, '"')))
468 for (bp = dp; *bp; bp++)
475 /* Default character set */
478 /* Check the character set */
479 result = !check_charset (dp, strlen (dp));
481 if (!(result = (strcasecmp (bp, "text") != 0))) {
498 * Check Content-Transfer-Encoding field
500 if (!strcasecmp (name, ENCODING_FIELD)) {
501 cp = add (buf, NULL);
502 while (state == FLDPLUS) {
503 state = m_getfld (state, name, buf, sizeof(buf), fp);
506 for (bp = cp; isspace (*bp); bp++)
508 for (dp = bp; istoken (*dp); dp++)
511 result = (strcasecmp (bp, "7bit")
512 && strcasecmp (bp, "8bit")
513 && strcasecmp (bp, "binary"));
524 * Just skip the rest of this header
525 * field and go to next one.
527 while (state == FLDPLUS)
528 state = m_getfld (state, name, buf, sizeof(buf), fp);
532 * We've passed the message header,
533 * so message is just text.