message.c 19.4 KB
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 711 712 713 714 715 716 717 718 719 720 721 722 723 724 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 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, 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 Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <ctype.h>

#include <mailutils/stream.h>
#include <misc.h>
#include <message0.h>

static int message_read (stream_t is, char *buf, size_t buflen,
			 off_t off, size_t *pnread );
static int message_write (stream_t os, const char *buf, size_t buflen,
			  off_t off, size_t *pnwrite);
static int message_get_fd (stream_t stream, int *pfd);
static int message_from (envelope_t envelope, char *buf, size_t len,
			 size_t *pnwrite);
static int message_date (envelope_t envelope, char *buf, size_t len,
			 size_t *pnwrite);

/*  Allocate ressources for the message_t.  */
int
message_create (message_t *pmsg, void *owner)
{
  message_t msg;
  int status;

  if (pmsg == NULL)
    return EINVAL;
  msg = calloc (1, sizeof (*msg));
  if (msg == NULL)
    return ENOMEM;
  status = monitor_create (&(msg->monitor), msg);
  if (status != 0)
    {
      free (msg);
      return status;
    }
  msg->owner = owner;
  msg->ref = 1;
  *pmsg = msg;
  return 0;
}

void
message_destroy (message_t *pmsg, void *owner)
{
  if (pmsg && *pmsg)
    {
      message_t msg = *pmsg;
      monitor_t monitor = msg->monitor;
      int destroy_lock = 0;

      monitor_wrlock (monitor);
      msg->ref--;
      if ((msg->owner && msg->owner == owner)
	  || (msg->owner == NULL && msg->ref <= 0))
	{
	  destroy_lock =  1;
	  /* Notify the listeners.  */
	  /* FIXME: to be removed since we do not supoort this event.  */
	  if (msg->observable)
	    {
	      observable_notify (msg->observable, MU_EVT_MESSAGE_DESTROY);
	      observable_destroy (&(msg->observable), msg);
	    }

	  /* Envelope.  */
	  if (msg->envelope)
	    envelope_destroy (&(msg->envelope), msg);

	  /* Header.  */
	  if (msg->header)
	    header_destroy (&(msg->header), msg);

	  /* Body.  */
	  if (msg->body)
	    body_destroy (&(msg->body), msg);

	  /* Attribute.  */
	  if (msg->attribute)
	    attribute_destroy (&(msg->attribute), msg);

	  /* Stream.  */
	  if (msg->stream)
	    stream_destroy (&(msg->stream), msg);

	  /*  Mime.  */
	  if (msg->mime)
	    mime_destroy (&(msg->mime));

	  /* Loose the owner.  */
	  msg->owner = NULL;

	  if (msg->ref <= 0)
	    free (msg);
	}
      monitor_unlock (monitor);
      if (destroy_lock)
	monitor_destroy (&monitor, msg);
      /* Loose the link */
      *pmsg = NULL;
    }
}

int
message_ref (message_t msg)
{
  if (msg)
    {
      monitor_wrlock (msg->monitor);
      msg->ref++;
      monitor_unlock (msg->monitor);
    }
  return 0;
}

void *
message_get_owner (message_t msg)
{
  return (msg == NULL) ? NULL : msg->owner;
}

int
message_get_header (message_t msg, header_t *phdr)
{
  if (msg == NULL || phdr == NULL)
    return EINVAL;

  /* Is it a floating mesg */
  monitor_wrlock (msg->monitor);
  if (msg->header == NULL)
    {
      header_t header;
      int status = header_create (&header, NULL, 0, msg);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
      msg->header = header;
    }
  monitor_unlock (msg->monitor);
  *phdr = msg->header;
  return 0;
}

int
message_set_header (message_t msg, header_t hdr, void *owner)
{
  if (msg == NULL )
    return EINVAL;
  if (msg->owner != owner)
     return EACCES;
  /* Make sure we destoy the old if it was own by the mesg */
  /* FIXME:  I do not know if somebody has already a ref on this ? */
  monitor_wrlock (msg->monitor);
  if (msg->header)
    header_destroy (&(msg->header), msg);
  msg->header = hdr;
  monitor_unlock (msg->monitor);
  return 0;
}

int
message_get_body (message_t msg, body_t *pbody)
{
  if (msg == NULL || pbody == NULL)
    return EINVAL;

  monitor_wrlock (msg->monitor);
  /* Is it a floating mesg.  */
  if (msg->body == NULL)
    {
      body_t body;
      int status = body_create (&body, msg);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
      msg->body = body;
    }
  monitor_unlock (msg->monitor);
  *pbody = msg->body;
  return 0;
}

int
message_set_body (message_t msg, body_t body, void *owner)
{
  if (msg == NULL )
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  /* Make sure we destoy the old if it was own by the mesg.  */
  /* FIXME:  I do not know if somebody has already a ref on this ? */
  monitor_wrlock (msg->monitor);
  if (msg->body)
    body_destroy (&(msg->body), msg);
  msg->body = body;
  monitor_unlock (msg->monitor);
  return 0;
}

int
message_set_stream (message_t msg, stream_t stream, void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  /* Make sure we destoy the old if it was own by the mesg.  */
  /* FIXME:  I do not know if somebody has already a ref on this ? */
  monitor_wrlock (msg->monitor);
  if (msg->stream)
    stream_destroy (&(msg->stream), msg);
  msg->stream = stream;
  monitor_unlock (msg->monitor);
  return 0;
}

int
message_get_stream (message_t msg, stream_t *pstream)
{
  if (msg == NULL || pstream == NULL)
    return EINVAL;

  monitor_wrlock (msg->monitor);
  if (msg->stream == NULL)
    {
      stream_t stream;
      int status;
      status = stream_create (&stream, MU_STREAM_RDWR, msg);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
      stream_set_read (stream, message_read, msg);
      stream_set_write (stream, message_write, msg);
      stream_set_fd (stream, message_get_fd, msg);
      stream_set_flags (stream, MU_STREAM_RDWR);
      msg->stream = stream;
    }
  monitor_unlock (msg->monitor);

  *pstream = msg->stream;
  return 0;
}

int
message_set_lines (message_t msg, int (*_lines)
		   (message_t, size_t *), void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_lines = _lines;
  return 0;
}

int
message_lines (message_t msg, size_t *plines)
{
  size_t hlines, blines;
  int ret = 0;

  if (msg == NULL)
    return EINVAL;
  /* Overload.  */
  if (msg->_lines)
    return msg->_lines (msg, plines);
  if (plines)
    {
      hlines = blines = 0;
      if ( ( ret = header_lines (msg->header, &hlines) ) == 0 )
	      ret = body_lines (msg->body, &blines);
      *plines = hlines + blines;
    }
  return ret;
}

int
message_set_size (message_t msg, int (*_size)
		  (message_t, size_t *), void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_size = _size;
  return 0;
}

int
message_size (message_t msg, size_t *psize)
{
  size_t hsize, bsize;
	int ret = 0;

  if (msg == NULL)
    return EINVAL;
  /* Overload ? */
  if (msg->_size)
    return msg->_size (msg, psize);
  if (psize)
    {
      hsize = bsize = 0;
      if ( ( ret = header_size (msg->header, &hsize) ) == 0 )
		ret = body_size (msg->body, &bsize);
      *psize = hsize + bsize;
    }
  return ret;
}

int
message_get_envelope (message_t msg, envelope_t *penvelope)
{
  if (msg == NULL || penvelope == NULL)
    return EINVAL;

  monitor_wrlock (msg->monitor);
  if (msg->envelope == NULL)
    {
      envelope_t envelope;
      int status = envelope_create (&envelope, msg);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
      envelope_set_from (envelope, message_from, msg);
      envelope_set_date (envelope, message_date, msg);
      msg->envelope = envelope;
    }
  monitor_unlock (msg->monitor);
  *penvelope = msg->envelope;
  return 0;
}

int
message_set_envelope (message_t msg, envelope_t envelope, void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  monitor_wrlock (msg->monitor);
  if (msg->envelope)
    envelope_destroy (&(msg->envelope), msg);
  msg->envelope = envelope;
  monitor_unlock (msg->monitor);
  return 0;
}

int
message_get_attribute (message_t msg, attribute_t *pattribute)
{
  if (msg == NULL || pattribute == NULL)
   return EINVAL;
  monitor_wrlock (msg->monitor);
  if (msg->attribute == NULL)
    {
      attribute_t attribute;
      int status = attribute_create (&attribute, msg);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
      msg->attribute = attribute;
    }
  monitor_unlock (msg->monitor);
  *pattribute = msg->attribute;
  return 0;
}

int
message_set_attribute (message_t msg, attribute_t attribute, void *owner)
{
  if (msg == NULL)
   return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  monitor_wrlock (msg->monitor);
  if (msg->attribute)
    attribute_destroy (&(msg->attribute), owner);
  msg->attribute = attribute;
  monitor_unlock (msg->monitor);
  return 0;
}

int
message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwriten)
{
  header_t header = NULL;
  size_t n = 0;
  int status;

  if (msg == NULL || buffer == NULL || buflen == 0)
    return EINVAL;

  buffer[0] = '\0';
  if (msg->_get_uidl)
    return msg->_get_uidl (msg, buffer, buflen, pwriten);

  /* Be compatible with Qpopper ? qppoper saves the UIDL in "X-UIDL".
     We use "Message-ID" as a fallback.  Is this bad ? should we generate
     a chksum or do the same as Qpopper save it in the header.  */
  message_get_header (msg, &header);
  if ((status = header_get_value (header, "X-UIDL", buffer, buflen, &n)) == 0
      || (status = header_get_value (header, "Message-ID", buffer,
				     buflen, &n)) == 0)
    {
      /* FIXME: Is header_get_value suppose to do this ?  */
      /* We need to collapse the header if it was mutiline.  e points to the
	 last char meaning in a C string that's '\0', s to the start. We also
	 remove the spesky '<' '>' if they are around.  */
      char *s, *e;
      for (s = buffer, e = buffer + n; s <= e; s++)
	{
	  if (isspace (*s) || *s == '<' || *s == '>')
	    {
	      memmove (s, s + 1, e - (s + 1));
	      e -= 1;
	      *e = '\0';
	    }
	}
    }
  return status;
}

int
message_set_uidl (message_t msg, int (* _get_uidl)
		  __P ((message_t, char *, size_t, size_t *)), void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_get_uidl = _get_uidl;
  return 0;
}

int
message_set_is_multipart (message_t msg, int (*_is_multipart)
			  __P ((message_t, int *)), void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_is_multipart = _is_multipart;
  return 0;
}

int
message_is_multipart (message_t msg, int *p_is_mp)
{
  header_t header;
  int status;
  size_t len = 0;

  if (msg == NULL || p_is_mp)
    return EINVAL;

  message_get_header(msg, &header);
  status = header_get_value (header, "Content-Type", NULL, 0, &len);
  if (status == 0)
    {
      char *content_type = calloc (len + 1, sizeof (char));
      if (content_type == NULL)
	return ENOMEM;
      status = header_get_value (header, "Content-Type", content_type,
				 len, NULL);
      *p_is_mp = strncasecmp ("multipart", content_type,
			      strlen ("multipart")) ? 0: 1;
      free (content_type);
    }
  else
    *p_is_mp = 0;
  return status;
}

int
message_get_num_parts (message_t msg, size_t *pparts)
{
  if (msg == NULL || pparts == NULL)
    return EINVAL;

  if (msg->_get_num_parts)
    return msg->_get_num_parts (msg, pparts);

  monitor_wrlock (msg->monitor);
  if (msg->mime == NULL)
    {
      int status = mime_create (&(msg->mime), msg, 0);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
    }
  monitor_unlock (msg->monitor);
  return mime_get_num_parts (msg->mime, pparts);
}

int
message_set_get_num_parts (message_t msg, int (*_get_num_parts)
			   __P ((message_t, size_t *)), void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_get_num_parts = _get_num_parts;
  return 0;
}

int
message_get_part (message_t msg, size_t part, message_t *pmsg)
{
  if (msg == NULL || pmsg == NULL)
    return EINVAL;

  /* Overload.  */
  if (msg->_get_part)
    return msg->_get_part (msg, part, pmsg);

  monitor_wrlock (msg->monitor);
  if (msg->mime == NULL)
    {
      int status = mime_create (&(msg->mime), msg, 0);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
    }
  monitor_unlock (msg->monitor);
  return mime_get_part (msg->mime, part, pmsg);
}

int
message_set_get_part (message_t msg, int (*_get_part)
		      __P ((message_t, size_t, message_t *)),
		      void *owner)
{
  if (msg == NULL)
    return EINVAL;
  if (msg->owner != owner)
    return EACCES;
  msg->_get_part = _get_part;
  return 0;
}

int
message_get_observable (message_t msg, observable_t *pobservable)
{
  if (msg == NULL || pobservable == NULL)
    return EINVAL;

  monitor_wrlock (msg->monitor);
  if (msg->observable == NULL)
    {
      int status = observable_create (&(msg->observable), msg);
      if (status != 0)
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
    }
  monitor_unlock (msg->monitor);
  *pobservable = msg->observable;
  return 0;
}

static int
message_read (stream_t is, char *buf, size_t buflen,
	      off_t off, size_t *pnread )
{
  message_t msg =  stream_get_owner (is);
  stream_t his, bis;
  size_t hread, hsize, bread, bsize;

  if (msg == NULL)
    return EINVAL;

  bsize = hsize = bread = hread = 0;
  his = bis = NULL;

  header_size (msg->header, &hsize);
  body_size (msg->body, &bsize);

  /* On some remote sever (POP) the size of the header and body is not known
     until you start reading them.  So by checking hsize == bsize == 0,
     this kludge is a way of detecting the anomalie and start by the
     header.  */
  if ((size_t)off < hsize || (hsize == 0 && bsize == 0))
    {
      header_get_stream (msg->header, &his);
      stream_read (his, buf, buflen, off, &hread);
    }
  else
    {
      body_get_stream (msg->body, &bis);
      stream_read (bis, buf, buflen, off - hsize, &bread);
    }

  if (pnread)
    *pnread = hread + bread;
  return 0;
}

static int
message_write (stream_t os, const char *buf, size_t buflen,
	       off_t off, size_t *pnwrite)
{
  message_t msg = stream_get_owner (os);
  int status = 0;
  size_t bufsize = buflen;

  if (msg == NULL)
    return EINVAL;

  /* Skip the obvious.  */
  if (buf == NULL || *buf == '\0' || buflen == 0)
    {
      if (pnwrite)
	*pnwrite = 0;
      return 0;
    }

  monitor_wrlock (msg->monitor);
  if (!msg->hdr_done)
    {
      size_t len;
      char *nl;
      char *thdr;
      while (!msg->hdr_done && (nl = memchr (buf, '\n', buflen)) != NULL)
	{
	  len = nl - buf + 1;
	  /* Allocate more buffer to hold the header.  */
	  thdr = realloc (msg->hdr_buf, msg->hdr_buflen + len);
	  if (thdr == NULL)
	    {
	      free (msg->hdr_buf);
	      msg->hdr_buf = NULL;
	      msg->hdr_buflen = 0;
	      monitor_unlock (msg->monitor);
	      return ENOMEM;
	    }
	  else
	    msg->hdr_buf = thdr;
	  memcpy (msg->hdr_buf + msg->hdr_buflen, buf, len);
	  msg->hdr_buflen += len;
	  /* We detect an empty line .i.e "^\n$" this signal the end of the
	     header.  */
	  if (buf == nl)
	    {
	      header_destroy (&(msg->header), msg);
	      status = header_create (&(msg->header), msg->hdr_buf,
				      msg->hdr_buflen, msg);
	      free (msg->hdr_buf);
	      msg->hdr_buf = NULL;
	      if (status != 0)
		{
		  msg->hdr_buflen = 0;
		  monitor_unlock (msg->monitor);
		  return status;
		}
	      msg->hdr_done = 1;
	    }
	  buf = nl + 1;
	  buflen -= len;
	}
    }
  monitor_unlock (msg->monitor);

  /* Message header is not complete but was not a full line.  */
  if (!msg->hdr_done && buflen > 0)
    {
      char *thdr = realloc (msg->hdr_buf, msg->hdr_buflen + buflen);
      monitor_wrlock (msg->monitor);
      if (thdr == NULL)
	{
	  free (msg->hdr_buf);
	  msg->hdr_buf = NULL;
	  msg->hdr_buflen = 0;
	  monitor_unlock (msg->monitor);
	  return ENOMEM;
	}
      else
	msg->hdr_buf = thdr;
      memcpy (msg->hdr_buf + msg->hdr_buflen, buf, buflen);
      msg->hdr_buflen += buflen;
      buflen = 0;
      monitor_unlock (msg->monitor);
    }
  else if (buflen > 0) /* In the body.  */
    {
      stream_t bs;
      body_t body;
      size_t written = 0;
      if ((status = message_get_body (msg, &body)) != 0 ||
	  (status = body_get_stream (msg->body, &bs)) != 0)
	{
	  free (msg->hdr_buf);
	  msg->hdr_buf = NULL;
	  msg->hdr_buflen = msg->hdr_done = 0;
	  return status;
	}
      if (off < (off_t)msg->hdr_buflen)
	off = 0;
      else
	off -= msg->hdr_buflen;
      status = stream_write (bs, buf, buflen, off, &written);
      buflen -= written;
    }
  if (pnwrite)
    *pnwrite = bufsize - buflen;
  return status;
}

static int
message_get_fd (stream_t stream, int *pfd)
{
  message_t msg = stream_get_owner (stream);
  body_t body;
  stream_t is;

  if (msg == NULL)
    return EINVAL;

  monitor_wrlock (msg->monitor);
  /* Probably being lazy, then create a body for the stream.  */
  if (msg->body == NULL)
    {
      int status = body_create (&body, msg);
      if (status != 0 )
	{
	  monitor_unlock (msg->monitor);
	  return status;
	}
      msg->body = body;
    }
  else
      body = msg->body;
  monitor_unlock (msg->monitor);

  body_get_stream (body, &is);
  return stream_get_fd (is, pfd);
}

static int
message_date (envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
{
  message_t msg = envelope_get_owner (envelope);
  time_t t;
  size_t n;

  if (msg == NULL)
    return EINVAL;

  /* FIXME: extract the time from "Date:".  */

  /* Catch all.  */
  /* FIXME: ctime() is not thread safe use strftime().  */
  t = time (NULL);
  n = strlen (ctime (&t));

  if (buf == NULL || len == 0)
    {
      if (pnwrite)
	*pnwrite = n;
      return 0;
    }
  n = (n > len) ? len : n;
  strncpy (buf, ctime (&t), n);
  buf [n] = '\0';
  if (pnwrite)
    *pnwrite = n;
  return 0;
}

static int
message_from (envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
{
  message_t msg = envelope_get_owner (envelope);
  header_t header = NULL;
  size_t n = 0;
  int status;

  if (msg == NULL)
    return EINVAL;

  /* Can it be extracted from the From:  */
  message_get_header (msg, &header);
  status = header_get_value (header, MU_HEADER_FROM, NULL, 0, &n);
  if (status == 0 && n != 0)
    {
      char *from;
      char *addr;
      from = calloc (1, n + 1);
      if (from == NULL)
	return ENOMEM;
      addr = calloc (1, n + 1);
      if (addr == NULL)
	{
	  free (from);
	  return ENOMEM;
	}
      header_get_value (header, MU_HEADER_FROM, from, n + 1, NULL);
      if (parseaddr (from, addr, n + 1) == 0)
	{
	  size_t i = strlen (addr);
	  n = (i > len) ? len : i;
	  if (buf && len > 0)
	    {
	      memcpy (buf, addr, n);
	      buf[n] = '\0';
	    }
	  free (addr);
	  free (from);
	  if (pnwrite)
	    *pnwrite = n;
	  return 0;
	}
      free (addr);
      free (from);
    }
  else if (status == EAGAIN)
    return status;

  /* oops */
  n = (7 > len) ? len: 7;
  if (buf && len > 0)
    {
      memcpy (buf, "unknown", n);
      buf [n] = '\0';
    }

  if (pnwrite)
    *pnwrite = n;
  return 0;
}