mbox.c 26.7 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 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 900 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 988 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 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2011 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */

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

#include <stdlib.h>
#include <string.h>

#include <mailutils/errno.h>
#include <mailutils/stream.h>
#include <mailutils/message.h>
#include <mailutils/folder.h>
#include <mailutils/assoc.h>
#include <mailutils/url.h>
#include <mailutils/io.h>
#include <mailutils/nls.h>
#include <mailutils/diag.h>
#include <mailutils/filter.h>
#include <mailutils/observer.h>
#include <mailutils/envelope.h>
#include <mailutils/address.h>
#include <mailutils/attribute.h>
#include <mailutils/header.h>
#include <mailutils/body.h>
#include <mailutils/sys/imap.h>

#define _imap_mbx_clrerr(imbx) ((imbx)->last_error = 0)
#define _imap_mbx_errno(imbx) ((imbx)->last_error)
#define _imap_mbx_uptodate(imbx) ((imbx)->flags & _MU_IMAP_MBX_UPTODATE)

static int _imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount);
static int _imap_mbx_is_updated (mu_mailbox_t mbox);

/* ------------------------------- */
/* Auxiliary message functions     */
/* ------------------------------- */

static size_t
_imap_msg_no (struct _mu_imap_message *imsg)
{
  return imsg - imsg->imbx->msgs + 1;
}

static int
_imap_fetch_with_callback (mu_imap_t imap, char *msgset, char *items,
			   mu_imap_callback_t cb, void *data)
{
  int rc;
  
  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH, cb, data);
  rc = mu_imap_fetch (imap, 0, msgset, items);
  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH, NULL, NULL);
  return rc;
}

static void
_imap_msg_free (struct _mu_imap_message *msg)
{
  mu_message_imapenvelope_free (msg->env);
  mu_message_destroy (&msg->message, msg);
}

struct save_closure
{
  mu_stream_t save_stream;
  size_t size;
  struct _mu_imap_message *imsg;
};

static int
_save_message_parser (void *item, void *data)
{
  union mu_imap_fetch_response *resp = item;
  struct save_closure *clos = data;

  if (resp->type == MU_IMAP_FETCH_BODY)
    {
      struct _mu_imap_message *imsg = clos->imsg;
      struct _mu_imap_mailbox *imbx = imsg->imbx;
      int rc;
      mu_stream_t istr, flt;
      mu_off_t size;
  
      rc = mu_static_memory_stream_create (&istr, resp->body.text,
					   strlen (resp->body.text));
      
      if (rc)
	{
	  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		    (_("mu_static_memory_stream_create: %s"),
		     mu_strerror (rc)));
	  imbx->last_error = rc;
	  return 0;
	}

      rc = mu_filter_create (&flt, istr, "CRLF", MU_FILTER_DECODE,
			     MU_STREAM_READ);
      mu_stream_unref (istr);
      if (rc)
	{
	  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		    (_("mu_filter_create: %s"), mu_strerror (rc)));
	  imbx->last_error = rc;
	  return 0;
	}
      
      rc = mu_stream_copy (clos->save_stream, flt, 0, &size);
      mu_stream_destroy (&flt);
      if (rc)
	{
	  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		    (_("copying to cache failed: %s"), mu_strerror (rc)));
	  imbx->last_error = rc;
	  return 0;
	}
      clos->size = size;
    }
  else
    mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE0,
	      (_("fetch returned a not requested item %d"),
	       resp->type));
  return 0;
}

static void
_save_message (void *data, int code, size_t sdat, void *pdat)
{
  mu_list_t list = pdat;
  mu_list_foreach (list, _save_message_parser, data);
}

static int
__imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
		       mu_stream_t *pstr)
{
  int rc;
  struct _mu_imap_mailbox *imbx = imsg->imbx;
  mu_folder_t folder = imbx->mbox->folder;
  mu_imap_t imap = folder->data;
  
  if (!(imsg->flags & _MU_IMAP_MSG_CACHED))
    {
      char *msgset;
	  
      if (!imbx->cache)
	{
	  rc = mu_temp_file_stream_create (&imbx->cache, NULL, 0);
	  if (rc)
	    /* FIXME: Try to recover first */
	    return rc;

	  mu_stream_set_buffer (imbx->cache, mu_buffer_full, 8192);
	}

      rc = mu_stream_size (imbx->cache, &imsg->offset);
      if (rc)
	return rc;

      rc = mu_asprintf (&msgset, "%lu", msgno);
      if (rc == 0)
	{
	  struct save_closure clos;
	  clos.imsg = imsg;
	  clos.save_stream = imbx->cache;
	  _imap_mbx_clrerr (imbx);
	  rc = _imap_fetch_with_callback (imap, msgset, "BODY[]",
					  _save_message, &clos);
	  free (msgset);
	  if (rc == 0 && !_imap_mbx_errno (imbx))
	    imsg->message_size = clos.size;
	}

      if (rc)
	return rc;

      imsg->flags |= _MU_IMAP_MSG_CACHED;
    }
  return mu_streamref_create_abridged (pstr, imbx->cache,
				       imsg->offset,
				       imsg->offset + imsg->message_size - 1);
}

static int
_imap_msg_scan (struct _mu_imap_message *imsg)
{
  int rc;
  mu_stream_t stream;
  struct mu_message_scan scan;
  size_t msgno = _imap_msg_no (imsg);
  
  if (imsg->flags & _MU_IMAP_MSG_SCANNED)
    return 0;
  
  rc = __imap_msg_get_stream (imsg, msgno, &stream);
  if (rc)
    return rc;
  
  scan.flags = MU_SCAN_SEEK | MU_SCAN_SIZE;
  scan.message_start = 0;
  scan.message_size = imsg->message_size;
  rc = mu_stream_scan_message (stream, &scan);
  mu_stream_unref (stream);

  if (rc == 0)
    {
      imsg->body_start = scan.body_start;
      imsg->body_end = scan.body_end;
      imsg->header_lines = scan.header_lines;
      imsg->body_lines = scan.body_lines;
      imsg->message_lines = imsg->header_lines + 1 + imsg->body_lines;
      imsg->flags |= _MU_IMAP_MSG_SCANNED;
    }
  
  return rc;
}

/* ------------------------------- */
/* Message envelope                */
/* ------------------------------- */

static int
_imap_env_date (mu_envelope_t env, char *buf, size_t len,
		size_t *pnwrite)
{
  struct _mu_imap_message *imsg = mu_envelope_get_owner (env);
  mu_stream_t str;
  int rc;
  
  if (!buf)
    rc = mu_nullstream_create (&str, MU_STREAM_WRITE);
  else
    rc = mu_fixed_memory_stream_create (&str, buf, len, MU_STREAM_WRITE);
  if (rc == 0)
    {
      mu_stream_stat_buffer statbuf;
      mu_stream_set_stat (str, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT),
			  statbuf);
      rc = mu_c_streamftime (str, MU_DATETIME_FROM,
			     &imsg->env->date, &imsg->env->tz);
      mu_stream_destroy (&str);
      if (pnwrite)
	*pnwrite = statbuf[MU_STREAM_STAT_OUT];
    }
  return rc;
}

static int
_imap_env_sender (mu_envelope_t env, char *buf, size_t len,
		  size_t *pnwrite)
{
  struct _mu_imap_message *imsg = mu_envelope_get_owner (env);
  mu_address_t addr = imsg->env->sender ? imsg->env->sender : imsg->env->from;

  if (!addr)
    return MU_ERR_NOENT;
  return mu_address_get_email (addr, 1, buf, len, pnwrite);
}
  
static int
_imap_msg_env_setup (struct _mu_imap_message *imsg, mu_message_t message)
{
  mu_envelope_t env;
  int rc = mu_envelope_create (&env, imsg);
  if (rc == 0)
    {
      mu_envelope_set_sender (env, _imap_env_sender, imsg);
      mu_envelope_set_date (env, _imap_env_date, imsg);
      rc = mu_message_set_envelope (message, env, imsg);
    }
  return rc;
}

/* ------------------------------- */
/* Message attributes              */
/* ------------------------------- */
static int
_imap_attr_get_flags (mu_attribute_t attr, int *pflags)
{
  struct _mu_imap_message *imsg = mu_attribute_get_owner (attr);

  if (!imsg)
    return EINVAL;
  if (pflags)
    *pflags = imsg->attr_flags;
  return 0;
}

static int
_imap_attr_set_flags (mu_attribute_t attr, int flags)
{
  struct _mu_imap_message *imsg = mu_attribute_get_owner (attr);

  if (!imsg)
    return EINVAL;
  imsg->attr_flags |= flags;
  imsg->flags |= _MU_IMAP_MSG_ATTRCHG;
  return 0;
}

static int
_imap_attr_clr_flags (mu_attribute_t attr, int flags)
{
  struct _mu_imap_message *imsg = mu_attribute_get_owner (attr);

  if (!imsg)
    return EINVAL;
  imsg->attr_flags |= flags;
  imsg->flags |= _MU_IMAP_MSG_ATTRCHG;
  return 0;
}

int
_imap_msg_attr_setup (struct _mu_imap_message *imsg, mu_message_t message)
{
  mu_attribute_t attribute;
  int rc = mu_attribute_create (&attribute, imsg);
  if (rc == 0)
    {
      mu_attribute_set_get_flags (attribute, _imap_attr_get_flags, imsg);
      mu_attribute_set_set_flags (attribute, _imap_attr_set_flags, imsg);
      mu_attribute_set_unset_flags (attribute, _imap_attr_clr_flags, imsg);
      rc = mu_message_set_attribute (message, attribute, imsg);
    }
  return rc;
}

/* ------------------------------- */
/* Header functions                */
/* ------------------------------- */
static int
_imap_hdr_fill (void *data, char **pbuf, size_t *plen)
{
  struct _mu_imap_message *imsg = data;
  struct _mu_imap_mailbox *imbx = imsg->imbx;
  mu_imap_t imap = imbx->mbox->folder->data;
  struct save_closure clos;
  char *msgset;
  unsigned long msgno = _imap_msg_no (imsg);
  int rc;
  
  clos.imsg = imsg;
  rc = mu_memory_stream_create (&clos.save_stream, MU_STREAM_RDWR);
  _imap_mbx_clrerr (imbx);
  rc = mu_asprintf (&msgset, "%lu", msgno);
  if (rc == 0)
    {
      rc = _imap_fetch_with_callback (imap, msgset, "BODY.PEEK[HEADER]",
				      _save_message, &clos);
      free (msgset);
      if (rc == 0)
	{
	  char *buf;
	  mu_off_t size;
	  
	  mu_stream_size (clos.save_stream, &size);
	  buf = malloc (size + 1);
	  if (!buf)
	    rc = ENOMEM;
	  else
	    {
	      mu_stream_seek (clos.save_stream, 0, MU_SEEK_SET, NULL);
	      rc = mu_stream_read (clos.save_stream, buf, size, NULL);
	      if (rc == 0)
		{
		  *pbuf = buf;
		  *plen = size;
		}
	      else
		free (buf);
	    }
	}
    }
  mu_stream_destroy (&clos.save_stream);
  return rc;
}

static int
_imap_msg_header_setup (struct _mu_imap_message *imsg, mu_message_t message)
{
  int rc;
  mu_header_t header = NULL;

  rc = mu_header_create (&header, NULL, 0);
  if (rc)
    return rc;
  mu_header_set_fill (header, _imap_hdr_fill, imsg);
  return mu_message_set_header (message, header, imsg);
}

/* ------------------------------- */
/* Body functions                  */
/* ------------------------------- */
static int
_imap_body_get_stream (mu_body_t body, mu_stream_t *pstr)
{
  mu_message_t msg = mu_body_get_owner (body);
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  struct _mu_imap_mailbox *imbx = imsg->imbx;
  int rc;

  rc = _imap_msg_scan (imsg);
  if (rc)
    return rc;
  return mu_streamref_create_abridged (pstr, imbx->cache,
				       imsg->offset + imsg->body_start,
				       imsg->offset + imsg->body_end - 1);
}

static int
_imap_body_size (mu_body_t body, size_t *psize)
{
  mu_message_t msg = mu_body_get_owner (body);
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  int rc;

  rc = _imap_msg_scan (imsg);
  if (rc)
    return rc;
  *psize = imsg->body_end - imsg->body_start;
  return 0;
}

static int
_imap_body_lines (mu_body_t body, size_t *psize)
{
  mu_message_t msg = mu_body_get_owner (body);
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  int rc;

  rc = _imap_msg_scan (imsg);
  if (rc)
    return rc;
  *psize = imsg->body_lines;
  return 0;
}

static int
_imap_mbx_body_setup (struct _mu_imap_message *imsg, mu_message_t message)
{
  int rc;
  mu_body_t body;

  /* FIXME: The owner of the body *must* be the message it belongs to. */
  rc = mu_body_create (&body, message);
  if (rc)
    return rc;

  mu_body_set_get_stream (body, _imap_body_get_stream, message);
  mu_body_set_size (body, _imap_body_size, message);
  mu_body_set_lines (body, _imap_body_lines, message);

  return mu_message_set_body (message, body, imsg);
}


/* ------------------------------- */
/* Message functions               */
/* ------------------------------- */

static int
_imap_msg_get_stream (mu_message_t msg, mu_stream_t *pstr)
{
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  return __imap_msg_get_stream (imsg, _imap_msg_no (imsg), pstr);
}

static int
_imap_msg_size (mu_message_t msg, size_t *psize)
{
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  *psize = imsg->message_size;
  return 0;
}

static int
_imap_msg_lines (mu_message_t msg, size_t *plines, int quick)
{
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  struct _mu_imap_mailbox *imbx = imsg->imbx;
  mu_mailbox_t mbox = imbx->mbox;
  
  if (!(imsg->flags & _MU_IMAP_MSG_LINES))
    {
      int rc;
      
      if (quick && !(imsg->flags & _MU_IMAP_MSG_CACHED))
	return MU_ERR_INFO_UNAVAILABLE;
      if (!_imap_mbx_uptodate (imbx))
	_imap_mbx_scan (mbox, 1, NULL);
      rc = _imap_msg_scan (imsg);
      if (rc)
	return rc;
    }
  *plines = imsg->message_lines;
  return 0;
}

static int
_copy_imapenvelope (struct mu_imapenvelope *env,
		    struct mu_imapenvelope const *src)
{  
  env->date = src->date;
  env->tz   = src->tz;
  
  if (src->subject && (env->subject = strdup (src->subject)) == NULL)
    return ENOMEM;
  if (src->from && (env->from = mu_address_dup (src->from)) == NULL)
    return ENOMEM;
  if (src->sender && (env->sender = mu_address_dup (src->sender)) == NULL)
    return ENOMEM;
  if (src->reply_to && (env->reply_to = mu_address_dup (src->reply_to))
      == NULL)
    return ENOMEM;
  if (src->to && (env->to = mu_address_dup (src->to)) == NULL)
    return ENOMEM;
  if (src->cc && (env->cc = mu_address_dup (src->cc)) == NULL)
    return ENOMEM;
  if (src->bcc && (env->bcc = mu_address_dup (src->bcc)) == NULL)
    return ENOMEM;
  if (src->in_reply_to && (env->in_reply_to = strdup (src->in_reply_to))
      == NULL)
    return ENOMEM;
  if (src->message_id && (env->message_id = strdup (src->message_id)) == NULL)
    return ENOMEM;
  return 0;
}
		   
static int
_imap_msg_imapenvelope (mu_message_t msg, struct mu_imapenvelope **penv)
{
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  int rc = 0;
  struct mu_imapenvelope *env = calloc (1, sizeof (*env));

  if (!env)
    return ENOMEM;
  rc = _copy_imapenvelope (env, imsg->env);
  if (rc)
    mu_message_imapenvelope_free (env);
  else
    *penv = env;
  return rc;
}

static int
fetch_bodystructure_parser (void *item, void *data)
{
  union mu_imap_fetch_response *resp = item;
  struct mu_bodystructure **pbs = data;

  if (resp->type == MU_IMAP_FETCH_BODYSTRUCTURE)
    {
      *pbs = resp->bodystructure.bs;
      resp->bodystructure.bs = NULL;
    }
  else
    mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE0,
	      (_("fetch returned a not requested item %d"),
	       resp->type));
  return 0;
}

static void
_imap_bodystructure_callback (void *data, int code, size_t sdat, void *pdat)
{
  mu_list_t list = pdat;
  mu_list_foreach (list, fetch_bodystructure_parser, data);
}
  
static int
_imap_msg_bodystructure (mu_message_t msg, struct mu_bodystructure **pbs)
{
  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
  struct _mu_imap_mailbox *imbx = imsg->imbx;
  mu_imap_t imap = imbx->mbox->folder->data;
  int rc;
  char *msgset;
  
  rc = mu_asprintf (&msgset, "%lu", _imap_msg_no (imsg));
  if (rc)
    return rc;

  rc = _imap_fetch_with_callback (imap, msgset, "BODYSTRUCTURE",
				  _imap_bodystructure_callback, pbs);
  free (msgset);
  return rc;
}

static int
_imap_mbx_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
{
  struct _mu_imap_mailbox *imbx = mailbox->data;
  struct _mu_imap_message *imsg;
  int rc;
  
  /* If we did not start a scanning yet do it now.  */
  if (!_imap_mbx_uptodate (imbx))
    _imap_mbx_scan (mailbox, 1, NULL);

  if (msgno > imbx->msgs_cnt)
    return MU_ERR_NOENT;

  imsg = imbx->msgs + msgno - 1;
  if (!imsg->message)
    {
      mu_message_t msg;
  
      rc = mu_message_create (&msg, imsg);
      if (rc)
	return rc;
      mu_message_set_get_stream (msg, _imap_msg_get_stream, imsg);
      mu_message_set_size (msg, _imap_msg_size, imsg);
      mu_message_set_lines (msg, _imap_msg_lines, imsg);
      mu_message_set_imapenvelope (msg, _imap_msg_imapenvelope, imsg);
      mu_message_set_bodystructure (msg, _imap_msg_bodystructure, imsg);
      
      do
	{
	  rc = _imap_msg_env_setup (imsg, msg);
	  if (rc)
	    break;
	  rc = _imap_msg_attr_setup (imsg, msg);
	  if (rc)
	    break;
	  rc = _imap_msg_header_setup (imsg, msg);
	  if (rc)
	    break;
	  rc = _imap_mbx_body_setup (imsg, msg);
	}
      while (0);
      
      if (rc)
	{
	  mu_message_destroy (&msg, imsg);
	  return rc;
	}
      imsg->message = msg;

    }
  *pmsg = imsg->message;
  return 0;
}

/* ------------------------------- */
/* Mailbox functions               */
/* ------------------------------- */
static int
_imap_realloc_messages (struct _mu_imap_mailbox *imbx, size_t count)
{
  if (count > imbx->msgs_max)
    {
      struct _mu_imap_message *newmsgs = realloc (imbx->msgs,
						  count * sizeof (*newmsgs));
      if (!newmsgs)
	return ENOMEM;
      memset (newmsgs + imbx->msgs_max, 0,
	      sizeof (*newmsgs) * (count - imbx->msgs_max));
      imbx->msgs = newmsgs;
      imbx->msgs_max = count;
    }
  return 0;
}

static void
_imap_mbx_destroy (mu_mailbox_t mailbox)
{
  size_t i;
  struct _mu_imap_mailbox *imbx = mailbox->data;
  
  if (!imbx)
    return;

  if (imbx->msgs)
    {
      for (i = 0; i < imbx->msgs_cnt; i++)
	_imap_msg_free (imbx->msgs + i);
      free (imbx->msgs);
    }
  mu_stream_unref (imbx->cache);
  free (imbx);
  mailbox->data = NULL;
}

static void
_imap_update_callback (void *data, int code, size_t sdat, void *pdat)
{
  struct _mu_imap_mailbox *imbx = data;
  memcpy (&imbx->stats, pdat, sizeof (imbx->stats));
  imbx->flags &= ~_MU_IMAP_MBX_UPTODATE;
}

static int
_imap_mbx_open (mu_mailbox_t mbox, int flags)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  mu_folder_t folder = mbox->folder;
  int rc;
  const char *mbox_name;
  mu_url_t url;
  mu_imap_t imap;
  
  mbox->flags = flags;
  
  rc = mu_mailbox_get_url (mbox, &url);
  if (rc)
    return rc;
  rc = mu_url_sget_path (url, &mbox_name);
  if (rc == MU_ERR_NOENT)
    mbox_name = "INBOX";
  else if (rc)
    return rc;
      
  rc = mu_folder_open (folder, flags);
  if (rc)
    return rc;

  imap = folder->data;

  mu_imap_register_callback_function (imap, MU_IMAP_CB_RECENT_COUNT,
				      _imap_update_callback,
				      imbx);
  mu_imap_register_callback_function (imap, MU_IMAP_STAT_MESSAGE_COUNT,
				      _imap_update_callback,
				      imbx);
  
  rc = mu_imap_select (imap, mbox_name,
		       flags & (MU_STREAM_WRITE|MU_STREAM_APPEND),
		       &imbx->stats);
  if (rc)
    return rc;

  if (imbx->stats.flags & MU_IMAP_STAT_MESSAGE_COUNT)
    rc = _imap_realloc_messages (imbx, imbx->stats.message_count);
  
  return rc;
}

static int
_imap_mbx_close (mu_mailbox_t mbox)
{
  int rc;
  mu_folder_t folder = mbox->folder;
  mu_imap_t imap = folder->data;

  if (mu_imap_capability_test (imap, "UNSELECT", NULL) == 0)
    rc = mu_imap_unselect (imap);
  else
    rc = mu_imap_close (imap);
  return rc;
}

static int
_imap_messages_count (mu_mailbox_t mbox, size_t *pcount)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  if (imbx->stats.flags & MU_IMAP_STAT_MESSAGE_COUNT)
    *pcount = imbx->stats.message_count;
  else
    return MU_ERR_INFO_UNAVAILABLE;
  return 0;
}
  
static int
_imap_messages_recent (mu_mailbox_t mbox, size_t *pcount)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  if (imbx->stats.flags & MU_IMAP_STAT_RECENT_COUNT)
    *pcount = imbx->stats.recent_count;
  else
    return MU_ERR_INFO_UNAVAILABLE;
  return 0;
}

static int
_imap_uidnext (mu_mailbox_t mbox, size_t *pn)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  if (imbx->stats.flags & MU_IMAP_STAT_UIDNEXT)
    *pn = imbx->stats.uidnext;
  else
    return MU_ERR_INFO_UNAVAILABLE;
  return 0;
}

static int
_imap_message_unseen (mu_mailbox_t mbox, size_t *pn)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  if (imbx->stats.flags & MU_IMAP_STAT_FIRST_UNSEEN)
    *pn = imbx->stats.uidnext;
  else
    return MU_ERR_INFO_UNAVAILABLE;
  return 0;
}

static int
_imap_uidvalidity (mu_mailbox_t mbox, unsigned long *pn)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  if (imbx->stats.flags & MU_IMAP_STAT_UIDVALIDITY)
    *pn = imbx->stats.uidvalidity;
  else
    return MU_ERR_INFO_UNAVAILABLE;
  return 0;
}

static int
_imap_mbx_expunge (mu_mailbox_t mbox)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  mu_folder_t folder = mbox->folder;
  mu_imap_t imap = folder->data;
  size_t i;
  char *msgset;
  int rc;
  int delflg = 0;
  
  for (i = 0; i < imbx->msgs_cnt; i++)
    {
      if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
	{
	  rc = mu_asprintf (&msgset, "%lu", i + 1);
	  if (rc)
	    break;
	  rc = mu_imap_store_flags (imap, 0, msgset,
				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
				    imbx->msgs[i].attr_flags);
	  delflg |= imbx->msgs[i].attr_flags & MU_ATTRIBUTE_DELETED;
	  free (msgset);
	  if (rc)
	    break;
	}
    }

  if (rc)
    return rc;

  if (delflg)
    rc = mu_imap_expunge (imap);
  
  return rc;
}

static int
_imap_mbx_sync (mu_mailbox_t mbox)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  mu_folder_t folder = mbox->folder;
  mu_imap_t imap = folder->data;
  size_t i;
  char *msgset;
  int rc;
  
  if (!imbx->msgs)
    return 0; 

  for (i = 0; i < imbx->msgs_cnt; i++)
    {
      if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
	{
	  rc = mu_asprintf (&msgset, "%lu", i + 1);
	  if (rc)
	    break;
	  rc = mu_imap_store_flags (imap, 0, msgset,
				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
				    imbx->msgs[i].attr_flags);
	  free (msgset);
	  if (rc)
	    break;
	}
    }
  
  return rc;
}

static int
_imap_mbx_append_message (mu_mailbox_t mbox, mu_message_t msg)
{
  int rc;
  mu_folder_t folder = mbox->folder;
  mu_imap_t imap = folder->data;
  mu_url_t url;
  const char *mbox_name;
  
  rc = mu_mailbox_get_url (mbox, &url);
  if (rc)
    return rc;
  rc = mu_url_sget_path (url, &mbox_name);
  return mu_imap_append_message (imap, mbox_name, 0, NULL, NULL, msg);
}

static int _compute_lines (struct mu_bodystructure *bs, size_t *pcount);

static int
sum_lines (void *item, void *data)
{
  struct mu_bodystructure *bs = item;
  size_t *pn = data;
  size_t n;

  if (_compute_lines (bs, &n))
    return 1;
  *pn += n;
  return 0;
}

static int
_compute_lines (struct mu_bodystructure *bs, size_t *pcount)
{
  switch (bs->body_message_type)
    {
    case mu_message_other:
      break;

    case mu_message_text:
      *pcount = bs->v.text.body_lines;
      return 0;

    case mu_message_rfc822:
      *pcount = bs->v.rfc822.body_lines;
      return 0;
      
    case mu_message_multipart:
      *pcount = 0;
      return mu_list_foreach (bs->v.multipart.body_parts, sum_lines, pcount);
    }
  return 1;
}

static int
fetch_response_parser (void *item, void *data)
{
  union mu_imap_fetch_response *resp = item;
  struct _mu_imap_message *imsg = data;

  switch (resp->type)
    {
    case MU_IMAP_FETCH_UID:
      imsg->uid = resp->uid.uid;
      break;
      
    case MU_IMAP_FETCH_FLAGS:
      imsg->attr_flags = resp->flags.flags;
      break;
      
    case MU_IMAP_FETCH_ENVELOPE:
      imsg->env = resp->envelope.imapenvelope;
      resp->envelope.imapenvelope = NULL; /* Steal the envelope */
      break;
      
    case MU_IMAP_FETCH_RFC822_SIZE:
      imsg->message_size = resp->rfc822_size.size;
      break;
      
    case MU_IMAP_FETCH_BODYSTRUCTURE:
      {
	size_t n;
	if (_compute_lines (resp->bodystructure.bs, &n) == 0)
	  {
	    imsg->message_lines = n;
	    imsg->flags |= _MU_IMAP_MSG_LINES;
	  }
      }
      break;
      
    default:
      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE0,
		(_("fetch returned a not requested item %d"),
		 resp->type));
      break;
    }
  return 0;
}
  
static void
_imap_fetch_callback (void *data, int code, size_t sdat, void *pdat)
{
  struct _mu_imap_mailbox *imbx = data;
  mu_mailbox_t mbox = imbx->mbox;
  mu_list_t list = pdat;
  int rc;
  struct _mu_imap_message *imsg;
  
  rc = _imap_realloc_messages (imbx, sdat - 1);
  if (rc)
    {
      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		(_("cannot reallocate array of messages: %s"),
		 mu_strerror (rc)));
      imbx->last_error = rc;
      return;
    }
  if (imbx->msgs_cnt < sdat)
    imbx->msgs_cnt = sdat;
  imsg = imbx->msgs + sdat - 1;
  imsg->imbx = imbx;
  mu_list_foreach (list, fetch_response_parser, imsg);

  if (mbox->observable)
    {
      if (((sdat + 1) % 10) == 0)
	mu_observable_notify (imbx->mbox->observable,
			      MU_EVT_MAILBOX_PROGRESS, NULL);
    }
}

static int
_imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  mu_folder_t folder = mbox->folder;
  mu_imap_t imap = folder->data;
  char *msgset;
  int rc;
  static char _imap_scan_items[] = "(UID FLAGS ENVELOPE RFC822.SIZE BODY)";
  
  rc = mu_asprintf (&msgset, "%lu:*", (unsigned long) msgno);
  if (rc)
    return rc;

  _imap_mbx_clrerr (imbx);
  rc = _imap_fetch_with_callback (imap, msgset, _imap_scan_items,
				  _imap_fetch_callback, imbx);
  free (msgset);
  if (rc == 0)
    rc = _imap_mbx_errno (imbx);
  if (rc == 0)
    {
      size_t i;
      mu_off_t total = 0;

      imbx->flags |= _MU_IMAP_MBX_UPTODATE;

      for (i = 1; i <= imbx->msgs_cnt; i++)
	{
	  total += imbx->msgs[i-1].message_size;
	  /* MU_EVT_MESSAGE_ADD must be delivered only when it is already
	     possible to retrieve the message in question.  It could not be
	     done in the fetch handler for obvious reasons.  Hence the extra
	     loop. */
	  if (mbox->observable)
	    mu_observable_notify (mbox->observable, MU_EVT_MESSAGE_ADD, &i);
	}
      imbx->total_size = total;
      
      if (pcount)
	*pcount = imbx->msgs_cnt;
    }
  return rc;
}

static int
_imap_mbx_is_updated (mu_mailbox_t mbox)
{
  struct _mu_imap_mailbox *imbx = mbox->data;
  mu_folder_t folder = mbox->folder;
  mu_imap_t imap = folder->data;
  int rc;
  
  rc = mu_imap_noop (imap);
  if (rc)
    {
      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		(_("mu_imap_noop: %s"), mu_strerror (rc)));
      imbx->last_error = rc;
    }
  return imbx->flags & _MU_IMAP_MBX_UPTODATE;
}

int
_mu_imap_mailbox_init (mu_mailbox_t mailbox)
{
  struct _mu_imap_mailbox *mbx = calloc (1, sizeof (*mbx));

  if (!mbx)
    return ENOMEM;
  mbx->mbox = mailbox;
  mailbox->data = mbx;

  mailbox->_destroy = _imap_mbx_destroy;
  mailbox->_open = _imap_mbx_open;
  mailbox->_close = _imap_mbx_close;
  mailbox->_expunge = _imap_mbx_expunge;

  mailbox->_messages_count = _imap_messages_count;
  mailbox->_messages_recent = _imap_messages_recent;
  mailbox->_message_unseen = _imap_message_unseen;
  mailbox->_uidvalidity = _imap_uidvalidity;
  mailbox->_uidnext = _imap_uidnext;
  
  mailbox->_scan = _imap_mbx_scan;
  mailbox->_is_updated = _imap_mbx_is_updated;
  mailbox->_get_message = _imap_mbx_get_message;
  mailbox->_sync = _imap_mbx_sync;
  
  mailbox->_append_message = _imap_mbx_append_message;
  
  return 0;
}