mbx_mh.c 30.6 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 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 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 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 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 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.  */

/* First draft by Sergey Poznyakoff */

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

#ifdef WITH_PTHREAD
# ifdef HAVE_PTHREAD_H
#  define _XOPEN_SOURCE  500
#  include <pthread.h>
# endif
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <dirent.h>

#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/message.h>
#include <mailutils/stream.h>
#include <mailutils/body.h>
#include <mailutils/header.h>
#include <mailutils/attribute.h>
#include <mailutils/error.h>
#include <registrar0.h>
#include <mailbox0.h>

#define MAX_OPEN_STREAMS 64

struct _mh_data;
struct _mh_message
{
  struct _mh_message *next;
  struct _mh_message *prev;

  stream_t stream;          /* Associated file stream */
  off_t body_start;         /* Offset of body start in the message file */
  off_t body_end;           /* Offset of body end (size of file, effectively */

  size_t seq_number;           /* message sequence number */
  size_t uid;               /* IMAP uid. */

  int attr_flags;           /* Attribute flags */
  int deleted;              /* Was the message originally deleted */

  size_t header_lines;
  size_t body_lines;

  message_t message; /* Corresponding message_t */
  struct _mh_data *mhd; /* Back pointer.  */
};

struct _mh_data
{
  /* List of messages: */
  size_t msg_count; /* number of messages in the list */
  struct _mh_message *msg_head;  /* First */
  struct _mh_message *msg_tail;  /* Last */

  unsigned long uidvalidity;
  size_t uidnext;                /* Next available UID */

  char *name;                    /* Directory name */

  /* Pool of open message streams */
  struct _mh_message *msg_pool[MAX_OPEN_STREAMS];
  int pool_first;    /* Index to the first used entry in msg_pool */
  int pool_last;     /* Index to the first free entry in msg_pool */

  time_t mtime;      /* Time of last modification */

  mailbox_t mailbox; /* Back pointer. */
};

static void mh_destroy __P((mailbox_t mailbox));
static int mh_open __P ((mailbox_t, int));
static int mh_close __P ((mailbox_t));
static int mh_get_message __P ((mailbox_t, size_t, message_t *));
static int mh_append_message __P ((mailbox_t, message_t));
static int mh_messages_count __P ((mailbox_t, size_t *));
static int mh_messages_recent __P ((mailbox_t, size_t *));
static int mh_message_unseen __P ((mailbox_t, size_t *));
static int mh_expunge __P ((mailbox_t));
static int mh_save_attributes __P ((mailbox_t));
static int mh_uidvalidity __P ((mailbox_t, unsigned long *));
static int mh_uidnext __P ((mailbox_t, size_t *));
static int mh_scan __P ((mailbox_t, size_t, size_t *));
static int mh_scan0 __P ((mailbox_t mailbox, size_t msgno, size_t *pcount));
static int mh_is_updated __P ((mailbox_t));
static int mh_get_size __P ((mailbox_t, off_t *));

static int mh_body_read __P ((stream_t, char *, size_t, off_t, size_t *));
static int mh_body_readline __P ((stream_t, char *, size_t, off_t, size_t *));
static int mh_stream_size __P ((stream_t stream, off_t *psize));

static int mh_body_size __P ((body_t body, size_t *psize));
static int mh_body_lines __P ((body_t body, size_t *plines));

static int mh_message_uid __P ((message_t msg, size_t *puid));

static int mh_message_stream_open __P((struct _mh_message *mhm));
static void mh_message_stream_close __P((struct _mh_message *mhm));

static int mh_header_fill __P((header_t header, char *buffer, size_t len,
			       off_t off, size_t *pnread));
static int mh_header_size __P((header_t header, size_t *psize));
static int mh_header_lines __P((header_t header, size_t *plines));

static int mh_get_attr_flags __P((attribute_t attr, int *pflags));
static int mh_set_attr_flags __P((attribute_t attr, int flags));
static int mh_unset_attr_flags __P((attribute_t attr, int flags));

static void _mh_message_insert __P((struct _mh_data *mhd,
				    struct _mh_message *msg));
static void _mh_message_delete __P((struct _mh_data *mhd,
				    struct _mh_message *msg));
static int mh_pool_open __P((struct _mh_message *mhm));

static int mh_envelope_date __P((envelope_t envelope, char *buf, size_t len,
				 size_t *psize));
static int mh_envelope_sender __P((envelope_t envelope, char *buf, size_t len,
				   size_t *psize));

/* Return filename for the message.
   NOTE: Allocates memory. */
static char *
_mh_message_name (struct _mh_message *mhm, int deleted)
{
  char *filename;
  size_t len = strlen (mhm->mhd->name) + 32;
  filename = malloc (len);
  if (deleted)
    snprintf (filename, len, "%s/,%d", mhm->mhd->name, mhm->seq_number);
  else
    snprintf (filename, len, "%s/%d", mhm->mhd->name, mhm->seq_number);
  return filename;
}

int
_mailbox_mh_init (mailbox_t mailbox)
{
  struct _mh_data *mhd;
  size_t name_len;

  if (mailbox == NULL)
    return EINVAL;

  mhd = mailbox->data = calloc (1, sizeof (*mhd));
  if (mailbox->data == NULL)
    return ENOMEM;

  /* Back pointer.  */
  mhd->mailbox = mailbox;

  url_get_path (mailbox->url, NULL, 0, &name_len);
  mhd->name = calloc (name_len + 1, sizeof (char));
  if (mhd->name == NULL)
    {
      free (mhd);
      mailbox->data = NULL;
      return ENOMEM;
    }
  url_get_path (mailbox->url, mhd->name, name_len + 1, NULL);

  /* Overloading the defaults.  */
  mailbox->_destroy = mh_destroy;

  mailbox->_open = mh_open;
  mailbox->_close = mh_close;

  /* Overloading of the entire mailbox object methods.  */
  mailbox->_get_message = mh_get_message;
  mailbox->_append_message = mh_append_message;
  mailbox->_messages_count = mh_messages_count;
  mailbox->_messages_recent = mh_messages_recent;
  mailbox->_message_unseen = mh_message_unseen;
  mailbox->_expunge = mh_expunge;
  mailbox->_save_attributes = mh_save_attributes;
  mailbox->_uidvalidity = mh_uidvalidity;
  mailbox->_uidnext = mh_uidnext;

  mailbox->_scan = mh_scan;
  mailbox->_is_updated = mh_is_updated;

  mailbox->_get_size = mh_get_size;

  /* Set our properties.  */
  mailbox->properties = calloc (1, sizeof (*(mailbox->properties)));
  if (mailbox->properties == NULL)
    return ENOMEM;
  mailbox->properties_count = 1;
  mailbox->properties[0].key = strdup ("TYPE");
  mailbox->properties[0].value = strdup ("MH");

  MAILBOX_DEBUG1 (mailbox, MU_DEBUG_TRACE, "mh_init(%s)\n", mhd->name);
  return 0;
}

static void
mh_destroy (mailbox_t mailbox)
{
  struct _mh_data *mhd = mailbox->data;
  struct _mh_message *msg, *next;

  if (!mhd)
    return;

  monitor_wrlock (mailbox->monitor);
  msg = mhd->msg_head;
  while (msg)
    {
      next = msg->next;
      message_destroy (&msg->message, msg);
      free (msg);
      msg = next;
    }

  if (mhd->name)
    free (mhd->name);

  free (mhd);
  mailbox->data = NULL;
  monitor_unlock (mailbox->monitor);
}

static int
mh_open (mailbox_t mailbox, int flags)
{
  /* unsigned long seq_num = 0; */
  /* struct dirent *entry; */
  struct _mh_data *mhd = mailbox->data;
  int status = 0;
  struct stat st;

  mailbox->flags = flags;
  if (stat (mhd->name, &st) < 0)
    return errno;

  if (!S_ISDIR (st.st_mode))
    return EINVAL;

  mhd->mtime = st.st_mtime;

  /* FIXME: something else? */
  if (mailbox->locker == NULL)
    status = locker_create (&mailbox->locker, mhd->name, strlen (mhd->name),
			    MU_LOCKER_PID | MU_LOCKER_FCNTL);
  return 0;
}

static int
mh_close (mailbox_t mailbox)
{
  if (!mailbox)
    return EINVAL;
  return locker_unlock (mailbox->locker);
}

static struct _mh_message *
_mh_get_message (struct _mh_data *mhd, size_t msgno)
{
  size_t n;
  struct _mh_message *msg;

  for (n = 1, msg = mhd->msg_head; msg && n < msgno; n++, msg = msg->next)
    ;

  return msg;
}

/* Find the message with the given sequence number */
static struct _mh_message *
_mh_get_message_seq (struct _mh_data *mhd, size_t seq)
{
  struct _mh_message *msg;

  for (msg = mhd->msg_head; msg && msg->seq_number < seq; msg = msg->next)
    ;

  if (msg)
    return msg->seq_number == seq ? msg : NULL;
  return NULL;
}

static int
mh_get_message (mailbox_t mailbox, size_t msgno, message_t *pmsg)
{
  int status;
  struct _mh_data *mhd = mailbox->data;
  struct _mh_message *mhm;
  message_t msg = NULL;

  /* Sanity checks.  */
  if (pmsg == NULL || mhd == NULL)
    return EINVAL;

  /* If we did not start a scanning yet do it now.  */
  if (mhd->msg_count == 0)
    {
      status = mh_scan0 (mailbox, 1, NULL);
      if (status != 0)
	return status;
    }

  if ((mhm = _mh_get_message (mhd, msgno)) == NULL)
    return EINVAL;

  /* Check if we already have it.  */
  if (mhm->message)
    {
      if (pmsg)
	*pmsg = mhm->message;
      return 0;
    }

  MAILBOX_DEBUG2 (mailbox, MU_DEBUG_TRACE, "mh_get_message(%s, %d)\n",
		  mhd->name, msgno);

  /* Get an empty message struct.  */
  status = message_create (&msg, mhm);
  if (status != 0)
    return status;

  /* Set the header.  */
  {
    header_t header = NULL;
    status = header_create (&header, NULL, 0, msg);
    if (status != 0)
      {
	message_destroy (&msg, mhm);
	return status;
      }
    header_set_fill (header, mh_header_fill, msg);
    header_set_size (header, mh_header_size, msg);
    header_set_lines (header, mh_header_lines, msg);
    /*FIXME:
    header_set_get_fvalue (header, mh_header_get_fvalue, msg);
    */
    message_set_header (msg, header, mhm);
  }

  /* Set the attribute.  */
  {
    attribute_t attribute;
    status = attribute_create (&attribute, msg);
    if (status != 0)
      {
	message_destroy (&msg, mhm);
	return status;
      }
    attribute_set_get_flags (attribute, mh_get_attr_flags, msg);
    attribute_set_set_flags (attribute, mh_set_attr_flags, msg);
    attribute_set_unset_flags (attribute, mh_unset_attr_flags, msg);
    message_set_attribute (msg, attribute, mhm);
  }

  /* Prepare the body.  */
  {
    body_t body = NULL;
    stream_t stream = NULL;
    if ((status = body_create (&body, msg)) != 0
	|| (status = stream_create (&stream,
				    mailbox->flags | MU_STREAM_SEEKABLE,
				    body)) != 0)
      {
	body_destroy (&body, msg);
	stream_destroy (&stream, body);
	message_destroy (&msg, mhm);
	return status;
      }
    stream_set_read (stream, mh_body_read, body);
    stream_set_readline (stream, mh_body_readline, body);
    stream_set_size (stream, mh_stream_size, body);
    body_set_stream (body, stream, msg);
    body_set_size (body, mh_body_size, msg);
    body_set_lines (body, mh_body_lines, msg);
    message_set_body (msg, body, mhm);
  }

  /* Set the envelope.  */
  {
    envelope_t envelope = NULL;
    status = envelope_create (&envelope, msg);
    if (status != 0)
      {
	message_destroy (&msg, mhm);
	return status;
      }
    envelope_set_sender (envelope, mh_envelope_sender, msg);
    envelope_set_date (envelope, mh_envelope_date, msg);
    message_set_envelope (msg, envelope, mhm);
  }

  /* Set the UID.  */
  message_set_uid (msg, mh_message_uid, mhm);

  /* Attach the message to the mailbox mbox data.  */
  mhm->message = msg;
  message_set_mailbox (msg, mailbox, mhm);

  *pmsg = msg;

  return 0;
}

static int
mh_append_message (mailbox_t mailbox, message_t msg)
{
  /*FIXME*/
  (void)mailbox;
  (void)msg;
  return ENOSYS;
}

static int
mh_messages_count (mailbox_t mailbox, size_t *pcount)
{
  struct _mh_data *mhd = mailbox->data;

  if (mhd == NULL)
    return EINVAL;

  if (!mh_is_updated (mailbox))
    return mh_scan0 (mailbox,  mhd->msg_count, pcount);

  if (pcount)
    *pcount = mhd->msg_count;

  return 0;
}

static int
mh_messages_recent (mailbox_t mailbox, size_t *pcount)
{
  struct _mh_data *mhd = mailbox->data;
  struct _mh_message *mhm;
  size_t count;

  /* If we did not start a scanning yet do it now.  */
  if (mhd->msg_count == 0)
    {
      int status = mh_scan0 (mailbox, 1, NULL);
      if (status != 0)
	return status;
    }
  count = 0;
  for (mhm = mhd->msg_head; mhm; mhm = mhm->next)
    {
      if ((mhm->attr_flags == 0) ||
	  ! ((mhm->attr_flags & MU_ATTRIBUTE_SEEN)
	     && (mhm->attr_flags & MU_ATTRIBUTE_READ)))
	count++;
    }
  *pcount = count;
  return 0;
}

static int
mh_message_unseen (mailbox_t mailbox, size_t *pmsgno)
{
  struct _mh_data *mhd = mailbox->data;
  struct _mh_message *mhm;
  size_t i, unseen;

  /* If we did not start a scanning yet do it now.  */
  if (mhd->msg_count == 0)
    {
      int status = mh_scan0 (mailbox, 1, NULL);
      if (status != 0)
	return status;
    }
  for (unseen = i = 1, mhm = mhd->msg_head; mhm; i++, mhm = mhm->next)
    {
      if ((mhm->attr_flags == 0) ||
	  !((mhm->attr_flags & MU_ATTRIBUTE_SEEN)
	    && (mhm->attr_flags & MU_ATTRIBUTE_READ)))
	{
	  unseen = i;
	  break;
	}
    }
  *pmsgno = unseen;
  return 0;
}

static FILE *
_mh_tempfile(struct _mh_data *mhd, char **namep)
{
  char *filename;
  int fd;

  filename = malloc (strlen (mhd->name) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
  if (!filename)
    return NULL;
  sprintf (filename, "%s/muXXXXXX", mhd->name);

#ifdef HAVE_MKSTEMP
  {
    int save_mask = umask (077);
    fd = mkstemp (filename);
    umask (save_mask);
  }
#else
  if (mktemp (filename))
    fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
  else
    fd = -1;
#endif

  if (fd == -1)
    {
      free (filename);
      return NULL;
    }

  *namep = filename;
  return fdopen (fd, "w");
}

/* Save message. FIXME: This is very raw. */
static int
_mh_message_save (struct _mh_message *mhm, char *msg_name)
{
  stream_t stream = NULL;
  FILE *fp;
  char buffer[512];
  off_t off = 0;
  size_t n;
  char *filename;

  fp = _mh_tempfile (mhm->mhd, &filename);
  if (!fp)
    return errno;

  if (!mhm->message)
    {
      mh_pool_open (mhm);
      stream = mhm->stream;
    }
  else
    message_get_stream (mhm->message, &stream);

  if (!stream)
    {
      fclose (fp);
      return errno;
    }

  while (stream_readline(stream, buffer, sizeof(buffer) - 1, off, &n) == 0
	 && n != 0)
    {
      fwrite (buffer, 1, n, fp);
      off += n;
    }

  fclose (fp);
  rename (filename, msg_name);
  free (filename);
  return 0;
}

static int
mh_expunge (mailbox_t mailbox)
{
  struct _mh_data *mhd = mailbox->data;
  struct _mh_message *mhm;

  if (mhd == NULL)
    return EINVAL;

  if (mhd->msg_count == 0)
    return 0;

  /* Find the first dirty(modified) message.  */
  for (mhm = mhd->msg_head; mhm; mhm = mhm->next)
    {
      if ((mhm->attr_flags & MU_ATTRIBUTE_MODIFIED) ||
	  (mhm->attr_flags & MU_ATTRIBUTE_DELETED) ||
	  (mhm->message && message_is_modified (mhm->message)))
	break;
    }

  if (!mhm)
    return 0; /* Nothing changed, just return.  */

  while (mhm)
    {
      struct _mh_message *next = mhm->next;
      char *msg_name;

      msg_name = _mh_message_name (mhm,
				   mhm->attr_flags & MU_ATTRIBUTE_DELETED);
      _mh_message_save (mhm, msg_name);
      free (msg_name);

      if (mhm->attr_flags & MU_ATTRIBUTE_DELETED)
	{
	  if (!mhm->deleted)
	    {
	      /* Delete original message */
	      msg_name = _mh_message_name (mhm, 0);
	      unlink (msg_name);
	      free (msg_name);
	    }
	  _mh_message_delete (mhd, mhm);
	}
      else if (mhm->deleted)
	{
	  /* Delete original file */
	  msg_name = _mh_message_name (mhm, 1);
	  unlink (msg_name);
	  free (msg_name);
	}
      mhm = next;
    }

  return 0;
}

static int
mh_save_attributes (mailbox_t mailbox)
{
  /*FIXME*/
  (void)mailbox;
  return ENOSYS;
}

static int
mh_uidvalidity (mailbox_t mailbox, unsigned long *puidvalidity)
{
  struct _mh_data *mhd = mailbox->data;
  int status = mh_messages_count (mailbox, NULL);
  if (status != 0)
    return status;
  /* If we did not start a scanning yet do it now.  */
  if (mhd->msg_count == 0)
    {
      status = mh_scan0 (mailbox, 1, NULL);
      if (status != 0)
	return status;
    }
  if (puidvalidity)
    *puidvalidity = mhd->uidvalidity;
  return 0;
}

static int
mh_uidnext (mailbox_t mailbox, size_t *puidnext)
{
  struct _mh_data *mhd = mailbox->data;
  int status = mh_messages_count (mailbox, NULL);
  if (status != 0)
    return status;
  /* If we did not start a scanning yet do it now.  */
  if (mhd->msg_count == 0)
    {
      status = mh_scan0 (mailbox, 1, NULL);
      if (status != 0)
	return status;
    }
   if (puidnext)
     *puidnext = mhd->uidnext;
  return 0;
}

/* FIXME: effectively the same as mbox_cleanup */
static void
mh_cleanup (void *arg)
{
  mailbox_t mailbox = arg;
  monitor_unlock (mailbox->monitor);
  locker_unlock (mailbox->locker);
}

/* Reset the IMAP uids, if necessary. UID according to IMAP RFC is a 32 bit
   ascending number for each messages  */
static void
mh_reset_imap_uids (struct _mh_data *mhd)
{
  size_t uid;
  size_t ouid;
  struct _mh_message *msg;

  for (uid = ouid = 0, msg = mhd->msg_head; msg; msg = msg->next)
    {
      uid = msg->uid;
      if (uid <= ouid)
	{
	  uid = ouid + 1;
	  msg->uid = ouid = uid;
	  msg->attr_flags |= MU_ATTRIBUTE_MODIFIED;
	}
      else
	ouid = uid;
    }
  if (mhd->msg_count > 0 && uid >= mhd->uidnext)
    {
      mhd->uidnext = uid + 1;
      mhd->msg_head->attr_flags |= MU_ATTRIBUTE_MODIFIED;
    }
}

/* Insert message msg into the message list on the appropriate position */
static void
_mh_message_insert (struct _mh_data *mhd, struct _mh_message *msg)
{
  struct _mh_message *p;
  struct _mh_message *prev;
  size_t n = msg->seq_number;

  for (p = mhd->msg_head; p && p->seq_number < n; p = p->next)
    ;

  if (!p)
    {
      msg->next = NULL;
      msg->prev = mhd->msg_tail;
      mhd->msg_tail = msg;
      if (!mhd->msg_head)
	mhd->msg_head = msg;
    }
  else
    {
      msg->next = p;
      msg->prev = p->prev;
      p->prev = msg;
    }
  if ((prev = msg->prev) != NULL)
    prev->next = msg;
  else
    mhd->msg_head = msg;
  msg->mhd = mhd;
  mhd->msg_count++;
}

static void
_mh_message_delete (struct _mh_data *mhd, struct _mh_message *msg)
{
  struct _mh_message *p;

  if ((p = msg->next) != NULL)
    p->prev = msg->prev;
  else
    mhd->msg_tail = msg->prev;

  if ((p = msg->prev) != NULL)
    p->next = msg->next;
  else
    mhd->msg_head = msg->next;

  message_destroy (&msg->message, msg);
  free (msg);
  mhd->msg_count--;
}

/* Scan given message and fill mh_message_t fields.
   NOTE: the function assumes mhm->stream != NULL.
   FIXME: Cache all the information and do not rescan the message when
   re-opening it. */
static int
mh_scan_message (struct _mh_message *mhm)
{
  stream_t stream = mhm->stream;
  char buf[1024];
  off_t off = 0;
  size_t n;
  int status;
  int in_header = 1;
  size_t hlines = 0;
  size_t blines = 0;
  size_t body_start = 0;

  while ((status = stream_readline (stream, buf, sizeof (buf), off, &n) == 0)
	 && n != 0)
    {
      if (in_header)
	{
	  if (buf[0] == '\n')
	    {
	      in_header = 0;
	      body_start = off+1;
	    }
	  if (buf[n - 1] == '\n')
	    hlines++;

	  /* Process particular attributes */
	  if (strncasecmp (buf, "status:", 7) == 0)
	    {
	      int deleted = mhm->attr_flags & MU_ATTRIBUTE_DELETED;
	      string_to_flags (buf, &mhm->attr_flags);
	      mhm->attr_flags |= deleted;
	    }
	  else if (strncasecmp (buf, "x-imapbase:", 11) == 0)
	    {
	      char *p;
	      mhm->mhd->uidvalidity = strtoul (buf + 11, &p, 10);
	      mhm->mhd->uidnext = strtoul (p, NULL, 10);
	    }
	  else if (strncasecmp (buf, "x-uid:", 6) == 0)
	    {
	      mhm->uid = strtoul (buf + 6, NULL, 10);
	    }
	}
      else
	{
	  if (buf[n - 1] == '\n')
	    blines++;
	}
      off += n;
    }

  mhm->header_lines = hlines;
  mhm->body_lines = blines;
  mhm->body_start = body_start;
  mhm->body_end = off;
  return 0;
}

/* Scan the mailbox */
static int
mh_scan0 (mailbox_t mailbox, size_t msgno, size_t *pcount)
{
  struct _mh_data *mhd = mailbox->data;
  DIR *dir;
  struct dirent *entry;
  int status = 0;
  struct stat st;
  (void)msgno;

  if (mhd == NULL)
    return EINVAL;

  dir = opendir (mhd->name);
  if (!dir)
    return errno;

  monitor_wrlock (mailbox->monitor);

#ifdef WITH_PTHREAD
  pthread_cleanup_push (mh_cleanup, (void *)mailbox);
#endif

  locker_lock (mailbox->locker, MU_LOCKER_RDLOCK);

  /* Do actual work */
  while ((entry = readdir (dir)))
    {
      char *namep;
      int attr_flags;
      size_t num;
      struct _mh_message *msg;

      attr_flags = 0;
      switch (entry->d_name[0])
	{
	case '.':
	  /* FIXME: .mh_sequences */
	  continue;
	case ',':
	  attr_flags |= MU_ATTRIBUTE_DELETED;
	  namep = entry->d_name+1;
	  break;
	case '0':case '1':case '2':case '3':case '4':
	case '5':case '6':case '7':case '8':case '9':
	  namep = entry->d_name;
	  break;
	default:
	  /*FIXME: Invalid entry. Report? */
	  continue;
	}

      num = strtoul (namep, &namep, 10);
      if (namep[0])
	continue;

      msg = _mh_get_message_seq (mhd, num);
      if (!msg)
	{
	  msg = calloc (1, sizeof(*msg));

	  msg->seq_number = num;
	  msg->attr_flags = attr_flags;
	  msg->deleted = attr_flags & MU_ATTRIBUTE_DELETED;

	  _mh_message_insert (mhd, msg);

	  /* This scans the message */
	  mh_message_stream_open (msg);
	  mh_message_stream_close (msg);
	}
      else
	{
	  msg->attr_flags = attr_flags;
	}
    }

  closedir (dir);

  if (stat (mhd->name, &st) == 0)
    mhd->mtime = st.st_mtime;

  if (pcount)
    *pcount = mhd->msg_count;

  /* Reset the uidvalidity.  */
  if (mhd->msg_count > 0)
    {
      if (mhd->uidvalidity == 0)
	{
	  mhd->uidvalidity = (unsigned long)time (NULL);
	  mhd->uidnext = mhd->msg_count + 1;
	  /* Tell that we have been modified for expunging.  */
	  if (mhd->msg_head)
	    mhd->msg_head->attr_flags |= MU_ATTRIBUTE_MODIFIED;
	}
    }

  mh_reset_imap_uids (mhd);

  /* Clean up the things */

  mh_cleanup (mailbox);
#ifdef WITH_PTHREAD
  pthread_cleanup_pop (0);
#endif
  return status;
}


static int
mh_scan (mailbox_t mailbox, size_t msgno, size_t *pcount)
{
  struct _mh_data *mhd = mailbox->data;

  if (! mh_is_updated (mailbox))
    return mh_scan0 (mailbox, msgno, pcount);

  if (pcount)
    *pcount = mhd->msg_count;

  return 0;
}

/* Is the internal representation of the mailbox up to date.
   Return 1 if so, 0 otherwise. */
static int
mh_is_updated (mailbox_t mailbox)
{
  struct stat st;
  struct _mh_data *mhd = mailbox->data;

  if (!mhd->msg_head)
    return 0;

  if (stat (mhd->name, &st) < 0)
    return 1;

  return mhd->mtime == st.st_mtime;
}

static int
mh_get_size (mailbox_t mailbox, off_t *psize)
{
  /*FIXME*/
  (void)mailbox;
  (void)psize;
  return ENOSYS;
}

/* Return number of open streams residing in a message pool */
static int
mh_pool_open_count(struct _mh_data *mhd)
{
  int cnt = mhd->pool_last - mhd->pool_first;
  if (cnt < 0)
    cnt += MAX_OPEN_STREAMS;
  return cnt;
}

/* Look up a _mh_message in the pool of open messages.
   Returns 1 if the message is found in the pool, and 0 otherwise. */
static int
mh_pool_lookup (struct _mh_message *mhm)
{
  struct _mh_data *mhd = mhm->mhd;
  int i;

  for (i = mhd->pool_first; i != mhd->pool_last; )
    {
      if (mhd->msg_pool[i] == mhm)
	return 1;
      if (++i == MAX_OPEN_STREAMS)
	i = 0;
    }
  return 0;
}

/* Open a stream associated with the message mhm. If the stream is
   already open, do nothing */
static int
mh_pool_open (struct _mh_message *mhm)
{
  struct _mh_data *mhd = mhm->mhd;
  if (mh_pool_lookup (mhm))
    return 0;
  if (mh_pool_open_count(mhd) == MAX_OPEN_STREAMS)
    {
      mh_message_stream_close (mhd->msg_pool[mhd->pool_first++]);
      mhd->pool_first %= MAX_OPEN_STREAMS;
    }
  mh_message_stream_open (mhm);
  mhd->msg_pool[mhd->pool_last++] = mhm;
  mhd->pool_last %= MAX_OPEN_STREAMS;
  return 0;
}

/* Attach a stream to a given message structure. The latter is supposed
   to be already added to the open message pool. */
int
mh_message_stream_open (struct _mh_message *mhm)
{
  struct _mh_data *mhd = mhm->mhd;
  char *filename = NULL;
  int status;

  if (file_stream_create (&mhm->stream))
    return errno;

  filename = _mh_message_name (mhm, mhm->deleted);

  if (!filename)
    return ENOMEM;

  status = stream_open (mhm->stream, filename, 0, mhd->mailbox->flags);

  free (filename);

  if (status == 0)
    status = mh_scan_message (mhm);

  return status;
}

/* Close the stream associated with the given message. */
void
mh_message_stream_close (struct _mh_message *mhm)
{
  if (mhm)
    {
      stream_close (mhm->stream);
      mhm->stream = NULL;
    }
}

/* Reading functions */

static int
mh_readstream (struct _mh_message *mhm, char *buffer, size_t buflen,
	       off_t off, size_t *pnread, int isreadline,
	       off_t start, off_t end)
{
  size_t nread = 0;
  int status = 0;
  off_t ln;

  if (buffer == NULL || buflen == 0)
    {
      if (pnread)
	*pnread = nread;
      return 0;
    }

  monitor_rdlock (mhm->mhd->mailbox->monitor);
#ifdef WITH_PTHREAD
  /* read() is cancellation point since we're doing a potentially
     long operation.  Lets make sure we clean the state.  */
  pthread_cleanup_push (mh_cleanup, (void *)mhm->mhd->mailbox);
#endif

  ln = end - (start + off);
  if (ln > 0)
    {
      /* Position the file pointer and the buffer.  */
      nread = ((size_t)ln < buflen) ? ln : buflen;
      if (isreadline)
	status = stream_readline (mhm->stream, buffer, buflen,
				  start + off, &nread);
      else
	status = stream_read (mhm->stream, buffer, nread,
			      start + off, &nread);
    }

  monitor_unlock (mhm->mhd->mailbox->monitor);
#ifdef WITH_PTHREAD
  pthread_cleanup_pop (0);
#endif

  if (pnread)
    *pnread = nread;
  return status;
}

static int
mh_body_read (stream_t is, char *buffer, size_t buflen, off_t off,
	      size_t *pnread)
{
  body_t body = stream_get_owner (is);
  message_t msg = body_get_owner (body);
  struct _mh_message *mhm = message_get_owner (msg);
  mh_pool_open (mhm);
  return mh_readstream (mhm, buffer, buflen, off, pnread, 0,
			mhm->body_start, mhm->body_end);
}

static int
mh_body_readline (stream_t is, char *buffer, size_t buflen,
		  off_t off, size_t *pnread)
{
  body_t body = stream_get_owner (is);
  message_t msg = body_get_owner (body);
  struct _mh_message *mhm = message_get_owner (msg);
  mh_pool_open (mhm);
  return mh_readstream (mhm, buffer, buflen, off, pnread, 1,
			mhm->body_start, mhm->body_end);
}

/* Return corresponding sizes */

static int
mh_stream_size (stream_t stream, off_t *psize)
{
  body_t body = stream_get_owner (stream);
  return mh_body_size (body, (size_t*) psize);
}

static int
mh_body_size (body_t body, size_t *psize)
{
  message_t msg = body_get_owner (body);
  struct _mh_message *mhm = message_get_owner (msg);
  if (mhm == NULL)
    return EINVAL;
  if (psize)
    *psize = mhm->body_end - mhm->body_start;
  return 0;
}


static int
mh_body_lines (body_t body, size_t *plines)
{
  message_t msg = body_get_owner (body);
  struct _mh_message *mhm = message_get_owner (msg);
  if (mhm == NULL)
    return EINVAL;
  if (plines)
    *plines = mhm->body_lines;
  return 0;
}

static int
mh_message_uid (message_t msg, size_t *puid)
{
  struct _mh_message *mhm = message_get_owner (msg);
  if (puid)
    *puid = mhm->uid;
  return 0;
}

/* Headers */
static int
mh_header_fill (header_t header, char *buffer, size_t len,
		off_t off, size_t *pnread)
{
  message_t msg = header_get_owner (header);
  struct _mh_message *mhm = message_get_owner (msg);

  mh_pool_open (mhm);
  return mh_readstream (mhm, buffer, len, off, pnread, 0,
			0, mhm->body_start);
}

static int
mh_header_size (header_t header, size_t *psize)
{
  message_t msg = header_get_owner (header);
  struct _mh_message *mhm = message_get_owner (msg);
  if (mhm == NULL)
    return EINVAL;
  if (psize)
    *psize = mhm->body_start;
  return 0;
}

static int
mh_header_lines (header_t header, size_t *plines)
{
  message_t msg = header_get_owner (header);
  struct _mh_message *mhm = message_get_owner (msg);
  if (mhm == NULL)
    return EINVAL;
  if (plines)
    *plines = mhm->header_lines;
  return 0;
}

/* Attributes */
static int
mh_get_attr_flags (attribute_t attr, int *pflags)
{
  message_t msg = attribute_get_owner (attr);
  struct _mh_message *mhm = message_get_owner (msg);

  if (mhm == NULL)
    return EINVAL;
  if (pflags)
    *pflags = mhm->attr_flags;
  return 0;
}

static int
mh_set_attr_flags (attribute_t attr, int flags)
{
  message_t msg = attribute_get_owner (attr);
  struct _mh_message *mhm = message_get_owner (msg);

  if (mhm == NULL)
    return EINVAL;
  mhm->attr_flags |= flags;
  return 0;
}

static int
mh_unset_attr_flags (attribute_t attr, int flags)
{
  message_t msg = attribute_get_owner (attr);
  struct _mh_message *mhm = message_get_owner (msg);

  if (mhm == NULL)
    return EINVAL;
  mhm->attr_flags &= ~flags;
  return 0;
}

/* Envelope */
static int
mh_envelope_date (envelope_t envelope, char *buf, size_t len,
		  size_t *psize)
{
  message_t msg = envelope_get_owner (envelope);
  struct _mh_message *mhm = message_get_owner (msg);
  size_t n = 0;
  int status;
  char buffer[512];
  char *s;

  if (mhm == NULL)
    return EINVAL;

  mh_pool_open (mhm);

  status = stream_readline (mhm->stream, buffer, sizeof(buffer),
			    0, &n);
  if (status != 0)
    {
      if (psize)
	*psize = 0;
      return status;
    }

  /* Format:  "From [sender] [date]" */
  /* strlen ("From ") == 5 */
  if (n > 5 && (s = strchr (buffer + 5, ' ')) != NULL)
    {
      if (buf && len > 0)
	{
	  len--; /* Leave space for the null.  */
	  strncpy (buf, s + 1, len)[len] = '\0';
	  len = strlen (buf);
	}
      else
	len = strlen (s + 1);
    }
  else
    len = 0;

  if (psize)
    *psize = len;
  return 0;
}

static int
mh_envelope_sender (envelope_t envelope, char *buf, size_t len,
		    size_t *psize)
{
  message_t msg = envelope_get_owner (envelope);
  struct _mh_message *mhm = message_get_owner (msg);
  size_t n = 0;
  int status;
  char buffer[512];
  char *s;

  if (mhm == NULL)
    return EINVAL;

  mh_pool_open (mhm);

  status = stream_readline (mhm->stream, buffer, sizeof(buffer),
			    0, &n);
  if (status != 0)
    {
      if (psize)
	*psize = 0;
      return status;
    }

  /* Format:  "From [sender] [date]" */
  /* strlen ("From ") == 5 */
  if (n > 5 && (s = strchr (buffer + 5, ' ')) != NULL)
    {
      /* Put a NULL to isolate the sender string, make a C string.  */
      *s = '\0';
      if (buf && len > 0)
	{
	  len--; /* leave space for the null */
	  strncpy (buf, buffer + 5, len)[len] = '\0';
	  len = strlen (buf);
	}
      else
	len = strlen (buffer + 5);
    }
  else
    len = 0;

  if (psize)
    *psize = len;
  return 0;
}