handler.c 28.4 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */ 

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/stat.h>

#include "mutt.h"
#include "mutt_curses.h"
#include "rfc1524.h"
#include "keymap.h"
#include "mime.h"
#include "copy.h"



#ifdef _PGPPATH
#include "pgp.h"
#endif



typedef void handler_f (BODY *, STATE *);
typedef handler_f *handler_t;

int Index_hex[128] = {
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
     0, 1, 2, 3,  4, 5, 6, 7,  8, 9,-1,-1, -1,-1,-1,-1,
    -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
};

int Index_64[128] = {
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
};

void mutt_decode_xbit (STATE *s, long len, int istext)
{
Thomas Roessler's avatar
Thomas Roessler committed
68 69 70
  int c;
  int lbreak = 1;
  
Thomas Roessler's avatar
Thomas Roessler committed
71 72
  if (istext)
  {
Thomas Roessler's avatar
Thomas Roessler committed
73
    while ((c = fgetc(s->fpin)) != EOF && len--)
Thomas Roessler's avatar
Thomas Roessler committed
74
    {
Thomas Roessler's avatar
Thomas Roessler committed
75
      if(lbreak && s->prefix)
Thomas Roessler's avatar
Thomas Roessler committed
76
      {
Thomas Roessler's avatar
Thomas Roessler committed
77 78
	state_puts(s->prefix, s);
	lbreak = 0;
Thomas Roessler's avatar
Thomas Roessler committed
79
      }
Thomas Roessler's avatar
Thomas Roessler committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	  
      if (c == '\r' && len)
      {
	int ch;
	
	if((ch = fgetc(s->fpin)) != '\n')
	  ungetc(ch, s->fpin);
	else
	{
	  c = ch;
	  len--;
	}
	
      }
      fputc(c, s->fpout);
      if(c == '\n')
	lbreak = 1;
Thomas Roessler's avatar
Thomas Roessler committed
97 98 99 100 101 102
    }
  }
  else
    mutt_copy_bytes (s->fpin, s->fpout, len);
}

Thomas Roessler's avatar
Thomas Roessler committed
103 104 105 106 107 108 109 110 111 112 113 114
static int handler_state_fgetc(STATE *s)
{
  int ch;
  
  if((ch = fgetc(s->fpin)) == EOF)
  {
    dprint(1, (debugfile, "handler_state_fgetc: unexpected EOF.\n"));
    state_puts ("[-- Error: unexpected end of file! --]\n", s);
  }
  return ch;
}

Thomas Roessler's avatar
Thomas Roessler committed
115 116
void mutt_decode_quoted (STATE *s, long len, int istext)
{
Thomas Roessler's avatar
Thomas Roessler committed
117
  int ch, lbreak = 1;
Thomas Roessler's avatar
Thomas Roessler committed
118 119 120

  while (len > 0)
  {
Thomas Roessler's avatar
Thomas Roessler committed
121
    if ((ch = handler_state_fgetc(s)) == EOF)
Thomas Roessler's avatar
Thomas Roessler committed
122
      break;
Thomas Roessler's avatar
Thomas Roessler committed
123 124 125 126 127 128 129 130

    len--;
    
    if (s->prefix && lbreak)
      state_puts (s->prefix, s);
    
    lbreak = 0;
    if (ch == '=')
Thomas Roessler's avatar
Thomas Roessler committed
131
    {
Thomas Roessler's avatar
Thomas Roessler committed
132 133 134 135 136 137 138 139
      int ch1, ch2;
      
      if(!len || (ch1 = handler_state_fgetc(s)) == EOF)
	break;

      len--;
      
      if (ch1 == '\n' || ch1 == '\r' || ch1 == ' ' || ch1 == '\t')
Thomas Roessler's avatar
Thomas Roessler committed
140
      {
Thomas Roessler's avatar
Thomas Roessler committed
141 142 143 144
	/* Skip whitespace at the end of the line since MIME does not
	 * allow for it
	 */
	if(ch1 != '\n')
Thomas Roessler's avatar
Thomas Roessler committed
145
	{
Thomas Roessler's avatar
Thomas Roessler committed
146 147 148 149 150 151 152 153 154 155 156 157 158
	  while(len && (ch1 = handler_state_fgetc(s)) != EOF)
	  {
	    len--;
	    if(ch1 == '\n')
	      break;
	  }
	}

	if(ch1 == EOF)
	  break;

	ch = EOF;

Thomas Roessler's avatar
Thomas Roessler committed
159
      }
Thomas Roessler's avatar
Thomas Roessler committed
160
      else
Thomas Roessler's avatar
Thomas Roessler committed
161
      {
Thomas Roessler's avatar
Thomas Roessler committed
162 163 164 165 166 167 168
	if(!len || (ch2 = handler_state_fgetc(s)) == EOF)
	  break;

	len--;
	
        ch = hexval (ch1) << 4;
        ch |= hexval (ch2);
Thomas Roessler's avatar
Thomas Roessler committed
169
      }
Thomas Roessler's avatar
Thomas Roessler committed
170 171 172 173 174 175
    } /* ch == '=' */
    else if (istext && ch == '\r')
    {
      int ch1;

      if((ch1 =fgetc(s->fpin)) == '\n')
Thomas Roessler's avatar
Thomas Roessler committed
176
      {
Thomas Roessler's avatar
Thomas Roessler committed
177 178
	ch = ch1;
	len--;
Thomas Roessler's avatar
Thomas Roessler committed
179
      }
Thomas Roessler's avatar
Thomas Roessler committed
180 181
      else
	ungetc(ch1, s->fpin);
Thomas Roessler's avatar
Thomas Roessler committed
182
    }
Thomas Roessler's avatar
Thomas Roessler committed
183 184 185 186 187 188

    if(ch != EOF)
      state_putc (ch, s);

    if(ch == '\n')
      lbreak = 1;
Thomas Roessler's avatar
Thomas Roessler committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  }
}

void mutt_decode_base64 (STATE *s, long len, int istext)
{
  char buf[5];
  int c1, c2, c3, c4, ch, cr = 0, i;

  buf[4] = 0;

  if (s->prefix) state_puts (s->prefix, s);

  while (len > 0)
  {
    for (i = 0 ; i < 4 && len > 0 ; len--)
    {
      if ((ch = fgetc (s->fpin)) == EOF)
	return;
      if (!ISSPACE (ch))
	buf[i++] = ch;
    }
    if (i != 4)
      return; /* didn't get a multiple of four chars! */

Thomas Roessler's avatar
Thomas Roessler committed
213 214
    c1 = base64val (buf[0]);
    c2 = base64val (buf[1]);
Thomas Roessler's avatar
Thomas Roessler committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    ch = (c1 << 2) | (c2 >> 4);

    if (cr && ch != '\n') state_putc ('\r', s);
    cr = 0;
      
    if (istext && ch == '\r')
      cr = 1;
    else
    {
      state_putc (ch, s);
      if (ch == '\n' && s->prefix) state_puts (s->prefix, s);
    }

    if (buf[2] == '=')
      break;
Thomas Roessler's avatar
Thomas Roessler committed
230
    c3 = base64val (buf[2]);
Thomas Roessler's avatar
Thomas Roessler committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    ch = ((c2 & 0xf) << 4) | (c3 >> 2);

    if (cr && ch != '\n')
      state_putc ('\r', s);
    cr = 0;

    if (istext && ch == '\r')
      cr = 1;
    else
    {
      state_putc (ch, s);
      if (ch == '\n' && s->prefix)
	state_puts (s->prefix, s);
    }

    if (buf[3] == '=') break;
Thomas Roessler's avatar
Thomas Roessler committed
247
    c4 = base64val (buf[3]);
Thomas Roessler's avatar
Thomas Roessler committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    ch = ((c3 & 0x3) << 6) | c4;

    if (cr && ch != '\n')
      state_putc ('\r', s);
    cr = 0;

    if (istext && ch == '\r')
      cr = 1;
    else
    {
      state_putc (ch, s);
      if (ch == '\n' && s->prefix)
	state_puts (s->prefix, s);
    }
  }
}

/* ----------------------------------------------------------------------------
 * A (not so) minimal implementation of RFC1563.
 */

#define IndentSize (4)
    
enum { RICH_PARAM=0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL, 
  RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
  RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG };

static struct {
  const char *tag_name;
  int index;
} EnrichedTags[] = {
  { "param",		RICH_PARAM },
  { "bold",		RICH_BOLD },
  { "italic",		RICH_ITALIC },
  { "underline",	RICH_UNDERLINE },
  { "nofill",		RICH_NOFILL },
  { "excerpt",		RICH_EXCERPT },
  { "indent",		RICH_INDENT },
  { "indentright",	RICH_INDENT_RIGHT },
  { "center",		RICH_CENTER },
  { "flushleft",	RICH_FLUSHLEFT },
  { "flushright",	RICH_FLUSHRIGHT },
  { "flushboth",	RICH_FLUSHLEFT },
  { "color",		RICH_COLOR },
  { "x-color",		RICH_COLOR },
  { NULL,		-1 }
};

struct enriched_state
{
  char *buffer;
  char *line;
  char *param;
  size_t buff_len;
  size_t line_len;
  size_t line_used;
  size_t line_max;
  size_t indent_len;
  size_t word_len;
  size_t buff_used;
  size_t param_len;
  int tag_level[RICH_LAST_TAG];
  int WrapMargin;
  STATE *s;
};

static void enriched_wrap (struct enriched_state *stte)
{
  int x;
  int extra;

  if (stte->line_len)
  {
    if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
    {
      /* Strip trailing white space */
      size_t y = stte->line_used - 1;

      while (y && ISSPACE (stte->line[y]))
      {
	stte->line[y] = '\0';
	y--;
	stte->line_used--;
	stte->line_len--;
      }
      if (stte->tag_level[RICH_CENTER])
      {
	/* Strip leading whitespace */
	y = 0;

	while (stte->line[y] && ISSPACE (stte->line[y]))
	  y++;
	if (y)
	{
	  size_t z;

	  for (z = y ; z <= stte->line_used; z++)
	  {
	    stte->line[z - y] = stte->line[z];
	  }

	  stte->line_len -= y;
	  stte->line_used -= y;
	}
      }
    }

    extra = stte->WrapMargin - stte->line_len - stte->indent_len -
      (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
    if (extra > 0) 
    {
      if (stte->tag_level[RICH_CENTER]) 
      {
	x = extra / 2;
	while (x)
	{
	  state_putc (' ', stte->s);
	  x--;
	}
      } 
      else if (stte->tag_level[RICH_FLUSHRIGHT])
      {
	x = extra-1;
	while (x)
	{
	  state_putc (' ', stte->s);
	  x--;
	}
      }
    }
    state_puts (stte->line, stte->s);
  }

  state_putc ('\n', stte->s);
  stte->line[0] = '\0';
  stte->line_len = 0;
  stte->line_used = 0;
  stte->indent_len = 0;
  if (stte->s->prefix)
  {
    state_puts (stte->s->prefix, stte->s);
    stte->indent_len += strlen (stte->s->prefix);
  }

  if (stte->tag_level[RICH_EXCERPT])
  {
    x = stte->tag_level[RICH_EXCERPT];
    while (x) 
    {
      if (stte->s->prefix)
      {
	state_puts (stte->s->prefix, stte->s);
	    stte->indent_len += strlen (stte->s->prefix);
      }
      else
      {
	state_puts ("> ", stte->s);
	stte->indent_len += strlen ("> ");
      }
      x--;
    }
  }
  else
    stte->indent_len = 0;
  if (stte->tag_level[RICH_INDENT])
  {
    x = stte->tag_level[RICH_INDENT] * IndentSize;
    stte->indent_len += x;
    while (x) 
    {
      state_putc (' ', stte->s);
      x--;
    }
  }
}

static void enriched_flush (struct enriched_state *stte, int wrap)
{
  if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len > 
      (stte->WrapMargin - (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize) - 
       stte->indent_len)))
    enriched_wrap (stte);

  if (stte->buff_used)
  {
    stte->buffer[stte->buff_used] = '\0';
    stte->line_used += stte->buff_used;
    if (stte->line_used > stte->line_max)
    {
      stte->line_max = stte->line_used;
      safe_realloc ((void **) &stte->line, stte->line_max + 1);
    }
    strcat (stte->line, stte->buffer);
    stte->line_len += stte->word_len;
    stte->word_len = 0;
    stte->buff_used = 0;
  }
  if (wrap) 
    enriched_wrap(stte);
}


static void enriched_putc (int c, struct enriched_state *stte)
{
  if (stte->tag_level[RICH_PARAM]) 
  {
    if (stte->tag_level[RICH_COLOR]) 
    {
      stte->param[stte->param_len++] = c;
    }
    return; /* nothing to do */
  }

  /* see if more space is needed (plus extra for possible rich characters) */
  if (stte->buff_len < stte->buff_used + 3)
  {
    stte->buff_len += LONG_STRING;
    safe_realloc ((void **) &stte->buffer, stte->buff_len + 1);
  }

  if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0' )
  {
    if (c == '\t')
      stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
    else
      stte->word_len++;
    
    stte->buffer[stte->buff_used++] = c;
    enriched_flush (stte, 0);
  }
  else
  {
    if (stte->s->flags & M_DISPLAY)
    {
      if (stte->tag_level[RICH_BOLD])
      {
	stte->buffer[stte->buff_used++] = c;
	stte->buffer[stte->buff_used++] = '\010';
	stte->buffer[stte->buff_used++] = c;
      }
      else if (stte->tag_level[RICH_UNDERLINE])
      {

	stte->buffer[stte->buff_used++] = '_';
	stte->buffer[stte->buff_used++] = '\010';
	stte->buffer[stte->buff_used++] = c;
      }
      else if (stte->tag_level[RICH_ITALIC])
      {
	stte->buffer[stte->buff_used++] = c;
	stte->buffer[stte->buff_used++] = '\010';
	stte->buffer[stte->buff_used++] = '_';
      }
      else
      {
	stte->buffer[stte->buff_used++] = c;
      }
    }
    else
    {
      stte->buffer[stte->buff_used++] = c;
    }
    stte->word_len++;
  }
}

static void enriched_puts (char *s, struct enriched_state *stte)
{
  char *c;

  if (stte->buff_len < stte->buff_used + strlen(s))
  {
    stte->buff_len += LONG_STRING;
    safe_realloc ((void **) &stte->buffer, stte->buff_len + 1);
  }
  c = s;
  while (*c)
  {
    stte->buffer[stte->buff_used++] = *c;
    c++;
  }
}

static void enriched_set_flags (const char *tag, struct enriched_state *stte)
{
  const char *tagptr = tag;
  int i, j;

  if (*tagptr == '/')
    tagptr++;
  
  for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
    if (strcasecmp (EnrichedTags[i].tag_name,tagptr) == 0)
    {
      j = EnrichedTags[i].index;
      break;
    }

  if (j != -1)
  {
    if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
      enriched_flush (stte, 1);

    if (*tag == '/')
    {
      if (stte->tag_level[j]) /* make sure not to go negative */
	stte->tag_level[j]--;
      if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM && stte->tag_level[RICH_COLOR])
      {
	stte->param[stte->param_len] = '\0';
	if (!strcasecmp(stte->param, "black"))
	{
	  enriched_puts("\033[30m", stte);
	}
	else if (!strcasecmp(stte->param, "red"))
	{
	  enriched_puts("\033[31m", stte);
	}
	else if (!strcasecmp(stte->param, "green"))
	{
	  enriched_puts("\033[32m", stte);
	}
	else if (!strcasecmp(stte->param, "yellow"))
	{
	  enriched_puts("\033[33m", stte);
	}
	else if (!strcasecmp(stte->param, "blue"))
	{
	  enriched_puts("\033[34m", stte);
	}
	else if (!strcasecmp(stte->param, "magenta"))
	{
	  enriched_puts("\033[35m", stte);
	}
	else if (!strcasecmp(stte->param, "cyan"))
	{
	  enriched_puts("\033[36m", stte);
	}
	else if (!strcasecmp(stte->param, "white"))
	{
	  enriched_puts("\033[37m", stte);
	}
	stte->param_len = 0;
	stte->param[0] = '\0';
      }
      if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR)
      {
	enriched_puts("\033[0m", stte);
      }
    }
    else
      stte->tag_level[j]++;

    if (j == RICH_EXCERPT)
      enriched_flush(stte, 1);
  }
}

void text_enriched_handler (BODY *a, STATE *s)
{
  enum {
    TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
  } state = TEXT;

  long bytes = a->length;
  struct enriched_state stte;
  int c = 0;
  int tag_len = 0;
  char tag[LONG_STRING + 1];

  memset (&stte, 0, sizeof (stte));
  stte.s = s;
  stte.WrapMargin = ((s->flags & M_DISPLAY) ? (COLS-4) : ((COLS-4)<72)?(COLS-4):72);
  stte.line_max = stte.WrapMargin * 4;
  stte.line = (char *) safe_calloc (1, stte.line_max + 1);
  stte.param = (char *) safe_calloc (1, STRING);

  if (s->prefix)
  {
    state_puts (s->prefix, s);
    stte.indent_len += strlen (s->prefix);
  }

  while (state != DONE)
  {
    if (state != ST_EOF)
    {
      if (!bytes || (c = fgetc (s->fpin)) == EOF)
	state = ST_EOF;
      else
	bytes--;
    }

    switch (state)
    {
      case TEXT :
	switch (c)
	{
	  case '<' :
	    state = LANGLE;
	    break;

	  case '\n' :
	    if (stte.tag_level[RICH_NOFILL])
	    {
	      enriched_flush (&stte, 1);
	    }
	    else 
	    {
	      enriched_putc (' ', &stte);
	      state = NEWLINE;
	    }
	    break;

	  default:
	    enriched_putc (c, &stte);
	}
	break;

      case LANGLE :
	if (c == '<')
	{
	  enriched_putc (c, &stte);
	  state = TEXT;
	  break;
	}
	else
	{
	  tag_len = 0;
	  state = TAG;
	}
	/* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
      case TAG :
	if (c == '>')
	{
	  tag[tag_len] = '\0';
	  enriched_set_flags (tag, &stte);
	  state = TEXT;
	}
	else if (tag_len < LONG_STRING)  /* ignore overly long tags */
	  tag[tag_len++] = c;
	else
	  state = BOGUS_TAG;
	break;

      case BOGUS_TAG :
	if (c == '>')
	  state = TEXT;
	break;

      case NEWLINE :
	if (c == '\n')
	  enriched_flush (&stte, 1);
	else
	{
	  ungetc (c, s->fpin);
	  bytes++;
	  state = TEXT;
	}
	break;

      case ST_EOF :
	enriched_putc ('\0', &stte);
Thomas Roessler's avatar
Thomas Roessler committed
711
        enriched_flush (&stte, 1);
Thomas Roessler's avatar
Thomas Roessler committed
712 713 714 715 716 717 718 719 720 721
	state = DONE;
	break;

      case DONE: /* not reached, but gcc complains if this is absent */
	break;
    }
  }

  state_putc ('\n', s); /* add a final newline */

Thomas Roessler's avatar
Thomas Roessler committed
722 723 724
  FREE (&(stte.buffer));
  FREE (&(stte.line));
  FREE (&(stte.param));
Thomas Roessler's avatar
Thomas Roessler committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
}                                                                              

#define TXTPLAIN    1
#define TXTENRICHED 2
#define TXTHTML     3

void alternative_handler (BODY *a, STATE *s)
{
  BODY *choice = NULL;
  BODY *b;
  LIST *t;
  char buf[STRING];
  int type = 0;

  /* First, search list of prefered types */
  t = AlternativeOrderList;
  while (t && !choice)
  {
    if (a && a->parts) 
      b = a->parts;
    else
      b = a;
    while (b && !choice)
    {
      int i;

      /* catch base only matches */
      i = strlen (t->data) - 1;
      if (!strchr(t->data, '/') || 
	  (i > 0 && t->data[i-1] == '/' && t->data[i] == '*'))
      {
Thomas Roessler's avatar
Thomas Roessler committed
756
	if (!strcasecmp(t->data, TYPE(b)))
Thomas Roessler's avatar
Thomas Roessler committed
757 758 759 760 761 762
	{
	  choice = b;
	}
      }
      else
      {
Thomas Roessler's avatar
Thomas Roessler committed
763
	snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
Thomas Roessler's avatar
Thomas Roessler committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
	if (!strcasecmp(t->data, buf))
	{
	  choice = b;
	}
      }
      b = b->next;
    }
    t = t->next;
  }
  /* Next, look for an autoviewable type */
  if (a && a->parts) 
    b = a->parts;
  else
    b = a;
  while (b && !choice)
  {
Thomas Roessler's avatar
Thomas Roessler committed
780
    snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
Thomas Roessler's avatar
Thomas Roessler committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    if (mutt_is_autoview (buf))
    {
      rfc1524_entry *entry = rfc1524_new_entry ();

      if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW))
      {
	choice = b;
      }
      rfc1524_free_entry (&entry);
    }
    b = b->next;
  }

  /* Then, look for a text entry */
  if (!choice)
  {
    if (a && a->parts) 
      b = a->parts;
    else
      b = a;
    while (b)
    {
      if (b->type == TYPETEXT)
      {
	if (strcasecmp ("plain", b->subtype) == 0)
	{
	  choice = b;
	  type = TXTPLAIN;
	}
	else if (strcasecmp ("enriched", b->subtype) == 0)
	{
	  if (type == 0 || type > TXTENRICHED)
	  {
	    choice = b;
	    type = TXTENRICHED;
	  }
	}
	else if (strcasecmp ("html", b->subtype) == 0)
	{
	  if (type == 0)
	  {
	    choice = b;
	    type = TXTHTML;
	  }
	}
      }
      b = b->next;
    }
  }
  /* Finally, look for other possibilities */
  if (!choice)
  {
    if (a && a->parts) 
      b = a->parts;
    else
      b = a;
    while (b && !choice)
    {
      if (mutt_can_decode (b))
	choice = b;
      b = b->next;
    }
  }
  if (choice)
  {
    if (s->flags & M_DISPLAY && !option (OPTWEED))
    {
      fseek (s->fpin, choice->hdr_offset, 0);
      mutt_copy_bytes(s->fpin, s->fpout, choice->offset-choice->hdr_offset);
    }
    mutt_body_handler (choice, s);
  }
  else if (s->flags & M_DISPLAY)
  {
    /* didn't find anything that we could display! */
    state_puts("[-- Error:  Could not display any parts of Multipart/Alternative! --]\n", s);
  }
}

/* handles message/rfc822 body parts */
void message_handler (BODY *a, STATE *s)
{
  struct stat st;
  BODY *b;
  long off_start;

  off_start = ftell (s->fpin);
  if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE)
  {
    fstat (fileno (s->fpin), &st);
    b = mutt_new_body ();
    b->length = (long) st.st_size;
    b->parts = mutt_parse_messageRFC822 (s->fpin, b);
  }
  else
    b = a;

  if (b->parts)
  {
    mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
	(((s->flags & M_DISPLAY) && option (OPTWEED)) ? (CH_WEED | CH_REORDER) : 0) |
	(s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM, s->prefix);

    if (s->prefix)
      state_puts (s->prefix, s);
    state_putc ('\n', s);

    mutt_body_handler (b->parts, s);
  }

  if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE)
    mutt_free_body (&b);
}

/* returns 1 if decoding the attachment will produce output */
int mutt_can_decode (BODY *a)
{
  char type[STRING];

Thomas Roessler's avatar
Thomas Roessler committed
900
  snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
Thomas Roessler's avatar
Thomas Roessler committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
  if (mutt_is_autoview (type))
    return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
  else if (a->type == TYPETEXT)
    return (1);
  else if (a->type == TYPEMESSAGE)
    return (1);
  else if (a->type == TYPEMULTIPART)
  {



#ifdef _PGPPATH
    if (strcasecmp (a->subtype, "signed") == 0 ||
	strcasecmp (a->subtype, "encrypted") == 0)
      return (1);
    else
#endif



    {
      BODY *p;

      for (p = a->parts; p; p = p->next)
      {
	if (mutt_can_decode (p))
	  return (1);
      }
    }
  }



#ifdef _PGPPATH
  else if (a->type == TYPEAPPLICATION)
  {
    if (strcasecmp (a->subtype, "pgp") == 0 ||
	strcasecmp (a->subtype, "x-pgp-message") == 0 ||
	strcasecmp (a->subtype, "pgp-signed") == 0 ||
	strcasecmp (a->subtype, "pgp-keys") == 0)
      return (1);
  }
#endif



  return (0);
}

void multipart_handler (BODY *a, STATE *s)
{
  BODY *b, *p;
  char buffer[STRING];
  char length[5];
  struct stat st;
  int count;

  if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE)
  {
    fstat (fileno (s->fpin), &st);
    b = mutt_new_body ();
    b->length = (long) st.st_size;
    b->parts = mutt_parse_multipart (s->fpin,
		  mutt_get_parameter ("boundary", a->parameter),
		  (long) st.st_size, strcasecmp ("digest", a->subtype) == 0);
  }
  else
    b = a;

  for (p = b->parts, count = 1; p; p = p->next, count++)
  {
    if (s->flags & M_DISPLAY)
    {
      snprintf (buffer, sizeof (buffer), "[-- Attachment #%d", count);
      state_puts (buffer, s);
      if (p->description || p->filename || p->form_name)
      {
	state_puts (": ", s);
	state_puts (p->description ? p->description :
		    p->filename ? p->filename : p->form_name, s);
      }
      state_puts (" --]\n", s);

      mutt_pretty_size (length, sizeof (length), p->length);

      snprintf (buffer, sizeof (buffer),
		"[-- Type: %s/%s, Encoding: %s, Size: %s --]\n",
Thomas Roessler's avatar
Thomas Roessler committed
988
	       TYPE (p), p->subtype, ENCODING (p->encoding), length);
Thomas Roessler's avatar
Thomas Roessler committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
      state_puts (buffer, s);
      if (!option (OPTWEED))
      {
	fseek (s->fpin, p->hdr_offset, 0);
	mutt_copy_bytes(s->fpin, s->fpout, p->offset-p->hdr_offset);
      }
      else
	state_putc ('\n', s);
    }
    else
    {
      if (p->description && mutt_can_decode (p))
      {
	state_puts ("Content-Description: ", s);
	state_puts (p->description, s);
	state_putc ('\n', s);
      }
      if (p->form_name)
      {
	state_puts (p->form_name, s);
	state_puts (": \n", s);
      }
    }
    mutt_body_handler (p, s);
    state_putc ('\n', s);
  }

  if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE)
    mutt_free_body (&b);
}

void autoview_handler (BODY *a, STATE *s)
{
  rfc1524_entry *entry = rfc1524_new_entry ();
  char buffer[LONG_STRING];
  char type[STRING];
  char command[LONG_STRING];
  char tempfile[_POSIX_PATH_MAX] = "";
Thomas Roessler's avatar
Thomas Roessler committed
1027
  char *fname;
Thomas Roessler's avatar
Thomas Roessler committed
1028 1029 1030 1031 1032 1033
  FILE *fpin = NULL;
  FILE *fpout = NULL;
  FILE *fperr = NULL;
  int piped = FALSE;
  pid_t thepid;

Thomas Roessler's avatar
Thomas Roessler committed
1034
  snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
Thomas Roessler's avatar
Thomas Roessler committed
1035 1036
  rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);

Thomas Roessler's avatar
Thomas Roessler committed
1037 1038 1039 1040
  fname = safe_strdup (a->filename);
  mutt_sanitize_filename (fname);
  rfc1524_expand_filename (entry->nametemplate, fname, tempfile, sizeof (tempfile));
  FREE (&fname);
Thomas Roessler's avatar
Thomas Roessler committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

  if (entry->command)
  {
    strfcpy (command, entry->command, sizeof (command));

    /* rfc1524_expand_command returns 0 if the file is required */
    piped = rfc1524_expand_command (a, tempfile, type, command, sizeof (command));

    if (s->flags & M_DISPLAY)
    {
      char mesg[STRING];

      snprintf (mesg, sizeof (buffer), "[-- Autoview using %s --]\n", command);
      state_puts (mesg, s);
      mutt_message("Invoking autoview command: %s",command);
    }

    if (piped)
    {
      thepid = mutt_create_filter (command, &fpin, &fpout, &fperr);
      mutt_copy_bytes (s->fpin, fpin, a->length);
      fclose (fpin);
    }
    else
    {
      if ((fpin = safe_fopen (tempfile, "w")) == NULL)
      {
        mutt_perror ("fopen");
	rfc1524_free_entry (&entry);
        return;
      }

      mutt_copy_bytes (s->fpin, fpin, a->length);
      fclose (fpin);
      thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
    }

    if (s->prefix)
    {
      while (fgets (buffer, sizeof(buffer), fpout) != NULL)
      {
        state_puts (s->prefix, s);
        state_puts (buffer, s);
      }
      /* check for data on stderr */
      if (fgets (buffer, sizeof(buffer), fperr)) 
      {
	if (s->flags & M_DISPLAY) 
	{
	  char mesg[STRING];

	  snprintf (mesg, sizeof (buffer), "[-- Autoview stderr of %s --]\n", 
	      command);
	  state_puts (mesg, s);
	}
	state_puts (s->prefix, s);
	state_puts (buffer, s);
	while (fgets (buffer, sizeof(buffer), fperr) != NULL)
	{
	  state_puts (s->prefix, s);
	  state_puts (buffer, s);
	}
      }
    }
    else
    {
      mutt_copy_stream (fpout, s->fpout);
      /* Check for stderr messages */
      if (fgets (buffer, sizeof(buffer), fperr))
      {
	if (s->flags & M_DISPLAY)
	{
	  char mesg[STRING];

	  snprintf (mesg, sizeof (buffer), "[-- Autoview stderr of %s --]\n", 
	      command);
	  state_puts (mesg, s);
	}
	state_puts (buffer, s);
	mutt_copy_stream (fperr, s->fpout);
      }
    }

    fclose (fpout);
    fclose (fperr);
    mutt_wait_filter (thepid);
    mutt_unlink (tempfile);
    if (s->flags & M_DISPLAY) 
      mutt_clear_error ();
  }
  rfc1524_free_entry (&entry);
}

void mutt_decode_attachment (BODY *b, STATE *s)
{
  fseek (s->fpin, b->offset, 0);
  switch (b->encoding)
  {
    case ENCQUOTEDPRINTABLE:
      mutt_decode_quoted (s, b->length, mutt_is_text_type (b->type, b->subtype));
      break;
    case ENCBASE64:
      mutt_decode_base64 (s, b->length, mutt_is_text_type (b->type, b->subtype));
      break;
    default:
      mutt_decode_xbit (s, b->length, mutt_is_text_type (b->type, b->subtype));
      break;
  }
}

void mutt_body_handler (BODY *b, STATE *s)
{
  int decode = 0;
  int plaintext = 0;
  FILE *fp = NULL;
  char tempfile[_POSIX_PATH_MAX];
  handler_t handler = NULL;
  long tmpoffset = 0;
  size_t tmplength = 0;
  char type[STRING];

  /* first determine which handler to use to process this part */

Thomas Roessler's avatar
Thomas Roessler committed
1164
  snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
Thomas Roessler's avatar
Thomas Roessler committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
  if (mutt_is_autoview (type))
  {
    rfc1524_entry *entry = rfc1524_new_entry ();

    if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW))
      handler = autoview_handler;
    rfc1524_free_entry (&entry);
  }
  else if (b->type == TYPETEXT)
  {
    if (strcasecmp ("plain", b->subtype) == 0)
    {
      /* avoid copying this part twice since removing the transfer-encoding is
       * the only operation needed.
       */
      plaintext = 1;
    }
    else if (strcasecmp ("enriched", b->subtype) == 0)
      handler = text_enriched_handler;
    else if (strcasecmp ("rfc822-headers", b->subtype) == 0)
      plaintext = 1;
  }
  else if (b->type == TYPEMESSAGE)
  {
Thomas Roessler's avatar
Thomas Roessler committed
1189
    if(mutt_is_message_type(b->type, b->subtype))
Thomas Roessler's avatar
Thomas Roessler committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
      handler = message_handler;
    else if (!strcasecmp ("delivery-status", b->subtype))
      plaintext = 1;
  }
  else if (b->type == TYPEMULTIPART)
  {



#ifdef _PGPPATH
    char *p;
#endif /* _PGPPATH */



    if (strcasecmp ("alternative", b->subtype) == 0)
      handler = alternative_handler;



#ifdef _PGPPATH
    else if (strcasecmp ("signed", b->subtype) == 0)
    {
      p = mutt_get_parameter ("protocol", b->parameter);

      if (!p)
        mutt_error ("Error: Multipart/Signed has no protocol.");
      else if (strcasecmp ("application/pgp-signature", p) == 0)
      {
	if (s->flags & M_VERIFY)
	  handler = pgp_signed_handler;
      }
    }
    else if (strcasecmp ("encrypted", b->subtype) == 0)
    {
      p = mutt_get_parameter ("protocol", b->parameter);

      if (!p)
        mutt_error ("Error: Multipart/Encrypted has no protocol parameter!");
      else if (strcasecmp ("application/pgp-encrypted", p) == 0)
        handler = pgp_encrypted_handler;
    }
#endif /* _PGPPATH */



    if (!handler)
      handler = multipart_handler;
  }



#ifdef _PGPPATH
  else if (b->type == TYPEAPPLICATION)
  {
    if (strcasecmp ("pgp", b->subtype) == 0 ||
	strcasecmp ("x-pgp-message", b->subtype) == 0 ||
	strcasecmp ("pgp-signed", b->subtype) == 0 ||
	strcasecmp ("pgp-keys", b->subtype) == 0)
      
      handler = application_pgp_handler;
  }
#endif /* _PGPPATH */



  if (plaintext || handler)
  {
    fseek (s->fpin, b->offset, 0);

    /* see if we need to decode this part before processing it */
    if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE ||
	(s->prefix && plaintext))
    {
      int origType = b->type;
      char *savePrefix = NULL;

      if (!plaintext)
      {
	/* decode to a tempfile, saving the original destination */
	fp = s->fpout;
	mutt_mktemp (tempfile);
	if ((s->fpout = safe_fopen (tempfile, "w")) == NULL)
	{
	  mutt_error ("Unable to open temporary file!");
	  return;
	}
	/* decoding the attachment changes the size and offset, so save a copy
	 * of the "real" values now, and restore them after processing
	 */
	tmplength = b->length;
	tmpoffset = b->offset;

	/* if we are decoding binary bodies, we don't want to prefix each
	 * line with the prefix or else the data will get corrupted.
	 */
	savePrefix = s->prefix;
	s->prefix = NULL;

	decode = 1;
      }
      else
	b->type = TYPETEXT;

      mutt_decode_attachment (b, s);

      if (decode)
      {
	b->length = ftell (s->fpout);
	b->offset = 0;
	fclose (s->fpout);

	/* restore final destination and substitute the tempfile for input */
	s->fpout = fp;
	fp = s->fpin;
	s->fpin = fopen (tempfile, "r");
	unlink (tempfile);

	/* restore the prefix */
	s->prefix = savePrefix;
      }

      b->type = origType;
    }
    else if (plaintext)
      mutt_copy_bytes (s->fpin, s->fpout, b->length);

    /* process the (decoded) body part */
    if (handler)
    {
      handler (b, s);

      if (decode)
      {
	b->length = tmplength;
	b->offset = tmpoffset;

	/* restore the original source stream */
	fclose (s->fpin);
	s->fpin = fp;
      }
    }
  }
  else if (s->flags & M_DISPLAY)
  {
Thomas Roessler's avatar
Thomas Roessler committed
1335
    fprintf (s->fpout, "[-- %s/%s is unsupported ", TYPE (b), b->subtype);
Thomas Roessler's avatar
Thomas Roessler committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    if (!option (OPTVIEWATTACH))
    {
      if (km_expand_key (type, sizeof(type),
			km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
	fprintf (s->fpout, "(use '%s' to view this part)", type);
      else
	fputs ("(need 'view-attachments' bound to key!)", s->fpout);
    }
    fputs (" --]\n", s->fpout);
  }
}