mailbox.c 16.3 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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2006,
   2007 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, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

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

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

#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/folder.h>
#include <mailutils/iterator.h>
#include <mailutils/list.h>
#include <mailutils/locker.h>
#include <mailutils/observer.h>
#include <mailutils/property.h>
#include <mailutils/registrar.h>
#include <mailutils/stream.h>
#include <mailutils/url.h>
#include <mailutils/attribute.h>
#include <mailutils/message.h>
#include <mailutils/mutil.h>

#include <mailbox0.h>
#include <url0.h>

static int
mailbox_folder_create (mu_mailbox_t mbox, const char *name,
		       mu_record_t record)
{
  int rc;
  char *fname;

  if ((rc = mu_url_aget_path (mbox->url, &fname)))
    return rc;

  if (mu_url_is_scheme (mbox->url, "file")
      || mu_url_is_scheme (mbox->url, "mbox")
      || mu_url_is_scheme (mbox->url, "mh")
      || mu_url_is_scheme (mbox->url, "maildir"))
    {
      char *p = strrchr (fname, '/'); /* FIXME: Is this always appropriate? */
      if (p)
	*p = 0;
    }
  rc = mu_folder_create_from_record (&mbox->folder, fname, record);
  free (fname);
  return rc;
}

static char *default_proto;

int
mu_mailbox_set_default_proto (const char *proto)
{
  char *p;
  
  if (mu_registrar_lookup (proto, MU_FOLDER_ATTRIBUTE_FILE, NULL, NULL))
    return MU_ERR_NO_HANDLER;
  p = strdup (proto);
  if (!p)
    return ENOMEM;
  if (default_proto)
    free (default_proto);
  default_proto = p;
  return 0;
}

const char *
mu_mailbox_get_default_proto ()
{
  return default_proto ? default_proto : "/";
}

static int
_create_mailbox0 (mu_mailbox_t *pmbox, mu_url_t url, const char *name)
{
  int status;
  mu_record_t record = NULL;

  if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL)
      == 0)
    {
      mu_log_level_t level;
      int (*m_init) (mu_mailbox_t) = NULL;
      
      mu_record_get_mailbox (record, &m_init);
      if (m_init)
        {
	  int (*u_init) (mu_url_t) = NULL;
	  mu_mailbox_t mbox;

	  /* Allocate memory for mbox.  */
	  mbox = calloc (1, sizeof (*mbox));
	  if (mbox == NULL)
	    return ENOMEM;

	  /* Initialize the internal lock now, so the concrete mailbox
	     could use it. */
	  status = mu_monitor_create (&mbox->monitor, 0, mbox);
	  if (status != 0)
	    {
	      mu_mailbox_destroy (&mbox);
	      return status;
	    }

	  mu_record_get_url (record, &u_init);
	  if (u_init && (status = u_init (url)) != 0)
	    {
	      mu_mailbox_destroy (&mbox);
	      return status;
	    }

	  /* Make sure scheme contains actual mailbox scheme */
	  if (strcmp (url->scheme, record->scheme))
	    {
	      char *p = strdup (record->scheme);
	      if (!p)
		{
		  mu_mailbox_destroy (&mbox);
		  return errno;
		}
	      free (url->scheme);
	      url->scheme = p;
	    }

	  mbox->url = url;
	  
	  /* Create the folder before initializing the concrete mailbox.
	     The mailbox needs it's back pointer. */
	  status = mailbox_folder_create (mbox, name, record);
	  
	  if (status == 0)
	    status = m_init (mbox);   /* Create the concrete mailbox type.  */

	  if (status != 0)
	    mu_mailbox_destroy (&mbox);
	  else
	    {
	      *pmbox = mbox;

	      level = mu_global_debug_level ("mailbox");
	      if (level)
		{
		  int status = mu_debug_create (&mbox->debug, mbox);
		  if (status)
		    return 0; /* FIXME: don't want to bail out just because I
				 failed to create a *debug* object. But I may
				 be wrong... */
		  mu_debug_set_level (mbox->debug, level);
		  if (level & MU_DEBUG_INHERIT)
		    mu_folder_set_debug (mbox->folder, mbox->debug);
		}
	    }
	  
	  return status;
	}
    }
  return MU_ERR_NO_HANDLER;
}

static int
_create_mailbox (mu_mailbox_t *pmbox, const char *name)
{
  int status;
  mu_url_t url;

  status = mu_url_create (&url, name);
  if (status)
    return status;
  status = mu_url_parse (url);
  if (status == 0)
    status = _create_mailbox0 (pmbox, url, name);
  return status;
}

/* The Mailbox Factory.
   Create an iterator for registrar and see if any url scheme match,
   Then we call the mailbox's mu_url_create() to parse the URL. Last
   initialize the concrete mailbox and folder.  */
int
mu_mailbox_create (mu_mailbox_t *pmbox, const char *name)
{
  int rc;
  
  if (pmbox == NULL)
    return MU_ERR_OUT_PTR_NULL;

  if (!mu_is_proto (name) && default_proto)
    {
      char *tmp_name = malloc (strlen (default_proto) + strlen (name) + 1);
      strcpy (tmp_name, default_proto);
      strcat (tmp_name, name);
      rc = _create_mailbox (pmbox, name);
      free (tmp_name);
    }
  else
    rc = _create_mailbox (pmbox, name);
  return rc;
}

void
mu_mailbox_destroy (mu_mailbox_t *pmbox)
{
  if (pmbox && *pmbox)
    {
      mu_mailbox_t mbox = *pmbox;
      mu_monitor_t monitor = mbox->monitor;

      /* Notify the observers.  */
      if (mbox->observable)
	{
	  mu_observable_notify (mbox->observable, MU_EVT_MAILBOX_DESTROY,
				mbox);
	  mu_observable_destroy (&mbox->observable, mbox);
	}

      /* Call the concrete mailbox _destroy method. So it can clean itself.  */
      if (mbox->_destroy)
	mbox->_destroy (mbox);

      mu_monitor_wrlock (monitor);

      /* Close the stream and nuke it */
      if (mbox->stream)
	{
	  /* FIXME: Is this right, should the client be responsible
	     for closing the stream?  */
	  /* mu_stream_close (mbox->stream); */
	  mu_stream_destroy (&mbox->stream, mbox);
	}

      if (mbox->url)
        mu_url_destroy (&mbox->url);

      if (mbox->locker)
	mu_locker_destroy (&mbox->locker);

      if (mbox->debug)
	mu_debug_destroy (&mbox->debug, mbox);

      if (mbox->folder)
	mu_folder_destroy (&mbox->folder);

      if (mbox->property)
	mu_property_destroy (&mbox->property, mbox);

      free (mbox);
      *pmbox = NULL;
      mu_monitor_unlock (monitor);
      mu_monitor_destroy (&monitor, mbox);
    }
}


/* -------------- stub functions ------------------- */

int
mu_mailbox_open (mu_mailbox_t mbox, int flag)
{
  if (mbox == NULL || mbox->_open == NULL)
    return MU_ERR_EMPTY_VFN;
  if (flag & MU_STREAM_QACCESS)
    {
      /* Quick access mailboxes are read-only */
      if (flag & (MU_STREAM_WRITE | MU_STREAM_RDWR
		  | MU_STREAM_APPEND | MU_STREAM_CREAT))
	return EINVAL; /* FIXME: Better error code, please? */
    }
  return mbox->_open (mbox, flag);
}

int
mu_mailbox_close (mu_mailbox_t mbox)
{
  if (mbox == NULL || mbox->_close == NULL)
    return MU_ERR_EMPTY_VFN;

  return mbox->_close (mbox);
}

int
mu_mailbox_flush (mu_mailbox_t mbox, int expunge)
{
  size_t i, total = 0;
  int status = 0;

  if (!mbox)
    return EINVAL;
  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
    return 0;

  mu_mailbox_messages_count (mbox, &total);
  if (mbox->flags & MU_STREAM_APPEND)
    i = total;
  else
    i = 1;
  for ( ; i <= total; i++)
    {
      mu_message_t msg = NULL;
      mu_attribute_t attr = NULL;
      mu_mailbox_get_message (mbox, i, &msg);
      mu_message_get_attribute (msg, &attr);
      mu_attribute_set_seen (attr);
    }

  if (expunge)
    status = mu_mailbox_expunge (mbox);
  else
    status = mu_mailbox_sync (mbox);

  return status;
}

/* messages */
int
mu_mailbox_append_message (mu_mailbox_t mbox, mu_message_t msg)
{
  if (mbox == NULL || mbox->_append_message == NULL)
    return MU_ERR_EMPTY_VFN;
  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
    return EACCES;
  return mbox->_append_message (mbox, msg);
}

int
mu_mailbox_get_message (mu_mailbox_t mbox, size_t msgno,  mu_message_t *pmsg)
{
  if (mbox == NULL || mbox->_get_message == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_get_message (mbox, msgno, pmsg);
}

int
mu_mailbox_quick_get_message (mu_mailbox_t mbox, mu_message_qid_t qid,
			      mu_message_t *pmsg)
{
  if (mbox == NULL || mbox->_quick_get_message == NULL)
    return MU_ERR_EMPTY_VFN;
  if (!(mbox->flags & MU_STREAM_QACCESS))
    return MU_ERR_BADOP;
  return mbox->_quick_get_message (mbox, qid, pmsg);
}

int
mu_mailbox_messages_count (mu_mailbox_t mbox, size_t *num)
{
  if (mbox == NULL || mbox->_messages_count == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_messages_count (mbox, num);
}

int
mu_mailbox_messages_recent (mu_mailbox_t mbox, size_t *num)
{
  if (mbox == NULL || mbox->_messages_recent == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_messages_recent (mbox, num);
}

int
mu_mailbox_message_unseen (mu_mailbox_t mbox, size_t *num)
{
  if (mbox == NULL || mbox->_message_unseen == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_message_unseen (mbox, num);
}

int
mu_mailbox_sync (mu_mailbox_t mbox)
{
  if (mbox == NULL || mbox->_sync == NULL)
    return MU_ERR_EMPTY_VFN;
  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
    return 0;
  return mbox->_sync (mbox);
}

/* Historic alias: */
int
mu_mailbox_save_attributes (mu_mailbox_t mbox)
{
  if (mbox == NULL || mbox->_sync == NULL)
    return MU_ERR_EMPTY_VFN;
  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
    return EACCES;
  return mbox->_sync (mbox);
}

int
mu_mailbox_expunge (mu_mailbox_t mbox)
{
  if (mbox == NULL || mbox->_expunge == NULL)
    return MU_ERR_EMPTY_VFN;
  if (!(mbox->flags & (MU_STREAM_RDWR|MU_STREAM_WRITE|MU_STREAM_APPEND)))
    return EACCES;
  return mbox->_expunge (mbox);
}

int
mu_mailbox_is_updated (mu_mailbox_t mbox)
{
  if (mbox == NULL || mbox->_is_updated == NULL)
    return 1;
  if (mbox->flags & MU_STREAM_QACCESS)
    return 1;
  return mbox->_is_updated (mbox);
}

int
mu_mailbox_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
{
  if (mbox == NULL || mbox->_scan == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_scan (mbox, msgno, pcount);
}

int
mu_mailbox_get_size (mu_mailbox_t mbox, mu_off_t *psize)
{
  int status;
  if (mbox == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  if (mbox->_get_size == NULL
      || (status = mbox->_get_size (mbox, psize)) == ENOSYS)
    {
      /* Fall back to brute-force method */
      size_t i, total;
      mu_off_t size = 0;
      
      status = mu_mailbox_messages_count (mbox, &total);
      if (status)
	return status;
      for (i = 1; i <= total; i++)
	{
	  mu_message_t msg;
	  size_t msgsize;
	  status = mu_mailbox_get_message (mbox, i, &msg);
	  if (status)
	    return status;
	  status = mu_message_size (msg, &msgsize);
	  if (status)
	    return status;
	  size += msgsize;
	}
      *psize = size;
    }
  return status;
}

int
mu_mailbox_uidvalidity (mu_mailbox_t mbox, unsigned long *pvalid)
{
  if (mbox == NULL || mbox->_uidvalidity == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_uidvalidity (mbox, pvalid);
}

int
mu_mailbox_uidnext (mu_mailbox_t mbox, size_t *puidnext)
{
  if (mbox == NULL || mbox->_uidnext == NULL)
    return MU_ERR_EMPTY_VFN;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  return mbox->_uidnext (mbox, puidnext);
}

/* locking */
int
mu_mailbox_set_locker (mu_mailbox_t mbox, mu_locker_t locker)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (mbox->locker)
    mu_locker_destroy (&mbox->locker);
  mbox->locker = locker;
  return 0;
}

int
mu_mailbox_get_locker (mu_mailbox_t mbox, mu_locker_t *plocker)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (plocker == NULL)
    return MU_ERR_OUT_PTR_NULL;
  *plocker = mbox->locker;
  return 0;
}

int
mu_mailbox_get_flags (mu_mailbox_t mbox, int *flags)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (!*flags)
    return MU_ERR_OUT_NULL;
  *flags = mbox->flags;
  return 0;
}

int
mu_mailbox_set_stream (mu_mailbox_t mbox, mu_stream_t stream)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (mbox->flags & MU_STREAM_QACCESS)
    return MU_ERR_BADOP;
  if (mbox->stream)
    mu_stream_destroy (&mbox->stream, mbox);
  mbox->stream = stream;
  return 0;
}

/* FIXME: This is a problem.  We provide a mu_mailbox_get_stream ()
   and this stream is special it should, in theory, represent
   a "view" of a flow of messages.  But providing this perspective
   may make sense for local mailboxes but downright impossible
   for a remote mailbox, short on downloading the entire mailbox
   locally.
   The question is : should this function be removed?
   So far it as been used on local mailboxes to get offsets.  */
int
mu_mailbox_get_stream (mu_mailbox_t mbox, mu_stream_t *pstream)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (pstream == NULL)
    return MU_ERR_OUT_PTR_NULL;

  /* If null two cases:
     - it is no open yet.
     - it a remote stream and the socket stream is on the folder.  */
  if (mbox->stream == NULL)
    {
      if (mbox->folder)
	return mu_folder_get_stream (mbox->folder, pstream);
    }
  *pstream = mbox->stream;
  return 0;
}

int
mu_mailbox_get_observable (mu_mailbox_t mbox, mu_observable_t *pobservable)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (pobservable == NULL)
    return MU_ERR_OUT_PTR_NULL;

  if (mbox->observable == NULL)
    {
      int status = mu_observable_create (&mbox->observable, mbox);
      if (status != 0)
	return status;
    }
  *pobservable = mbox->observable;
  return 0;
}

int
mu_mailbox_get_property (mu_mailbox_t mbox, mu_property_t *pproperty)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (pproperty == NULL)
    return MU_ERR_OUT_PTR_NULL;
  
  if (mbox->property == NULL)
    {
      int status = mu_property_create (&mbox->property, mbox);
      if (status != 0)
	return status;
    }
  *pproperty = mbox->property;
  return 0;
}

int
mu_mailbox_has_debug (mu_mailbox_t mailbox)
{
  if (mailbox == NULL)
    return 0;

  return mailbox->debug ? 1 : 0;
}

int
mu_mailbox_set_debug (mu_mailbox_t mbox, mu_debug_t debug)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (mbox->debug)
    mu_debug_destroy (&mbox->debug, mbox);
  mbox->debug = debug;
  /* FIXME: Honor MU_DEBUG_INHERIT */
  if (!mu_folder_has_debug (mbox->folder))
    mu_folder_set_debug (mbox->folder, debug);
  return 0;
}

int
mu_mailbox_get_debug (mu_mailbox_t mbox, mu_debug_t *pdebug)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (pdebug == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (mbox->debug == NULL)
    {
      int status = mu_debug_create (&mbox->debug, mbox);
      if (status != 0)
	return status;
      /* FIXME: MU_DEBUG_INHERIT?? */
      if (!mu_folder_has_debug (mbox->folder))
	mu_folder_set_debug (mbox->folder, mbox->debug);
    }
  *pdebug = mbox->debug;
  return 0;
}

int
mu_mailbox_get_url (mu_mailbox_t mbox, mu_url_t *purl)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (purl == NULL)
    return MU_ERR_OUT_PTR_NULL;
  *purl = mbox->url;
  return 0;
}

int
mu_mailbox_get_folder (mu_mailbox_t mbox, mu_folder_t *pfolder)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
  if (pfolder == NULL)
    return MU_ERR_OUT_PTR_NULL;
  *pfolder = mbox->folder;
  return 0;
}

int
mu_mailbox_set_folder (mu_mailbox_t mbox, mu_folder_t folder)
{
  if (mbox == NULL)
    return MU_ERR_MBX_NULL;
   mbox->folder = folder;
  return 0;
}

int
mu_mailbox_lock (mu_mailbox_t mbox)
{
  mu_locker_t lock = NULL;
  mu_mailbox_get_locker (mbox, &lock);
  return mu_locker_lock (lock);
}

int
mu_mailbox_unlock (mu_mailbox_t mbox)
{
  mu_locker_t lock = NULL;
  mu_mailbox_get_locker (mbox, &lock);
  return mu_locker_unlock (lock);
}