folder.c 11.8 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
/* 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 <stdlib.h>
#include <string.h>

#include <mailutils/registrar.h>
#include <mailutils/iterator.h>
#include <mailutils/list.h>
#include <mailutils/monitor.h>

#include <misc.h>
#include <folder0.h>

static struct _monitor folder_lock = MU_MONITOR_INITIALIZER;

/* Internal folder list.  */
static list_t known_folder_list;
static int is_known_folder (url_t, folder_t *);
static int is_same_scheme (url_t, url_t);
static int is_same_user (url_t, url_t);
static int is_same_path (url_t, url_t);
static int is_same_host (url_t, url_t);
static int is_same_port (url_t, url_t);

/*  A folder could be a remote one, IMAP, or local, a spool directory
    like $HOME/Mail etc ..  We maintain a known list of folder to
    not generate multiple folder of the same URL.  Meaning when
    folder_create () is call we'll check if we already have a
    folder for that URL and return the same, if not we create a new one.
    The downside, the algo to detect the same URL is very weak, and
    they maybe cases where you want a different folder for the same
    URL, there is not easy way to do this.  */
int
folder_create (folder_t *pfolder, const char *name)
{
  int status = EINVAL;
  record_t record = NULL;
  int (*f_init) __P ((folder_t)) = NULL;
  int (*u_init) __P ((url_t)) = NULL;
  iterator_t iterator;
  list_t list;
  int found = 0;

  if (pfolder == NULL)
    return EINVAL;

  /* Look in the registrar list(iterator), for a match  */
  registrar_get_list (&list);
  status = iterator_create (&iterator, list);
  if (status != 0)
    return status;
  for (iterator_first (iterator); !iterator_is_done (iterator);
       iterator_next (iterator))
    {
      iterator_current (iterator, (void **)&record);
      if (record_is_scheme (record, name))
        {
          record_get_folder (record, &f_init);
          record_get_url (record, &u_init);
          if (f_init && u_init)
	    {
	      found = 1;
	      break;
	    }
        }
    }
  iterator_destroy (&iterator);

  if (found)
    {
      url_t url = NULL;
      folder_t folder = NULL;

      /* Parse the url, it may be a bad one and we should bailout if this
	 failed.  */
      if ((status = url_create (&url, name) != 0)
          || (status = u_init (url)) != 0)
	return status;

      monitor_wrlock (&folder_lock);

      /* Check if we already have the same URL folder.  */
      if (is_known_folder (url, &folder))
	{
	  folder->ref++;
	  *pfolder = folder;
	  url_destroy (&url);
	  monitor_unlock (&folder_lock);
	  return  0;
	}
      else
	monitor_unlock (&folder_lock);

      /* Create a new folder.  */

      /* Allocate memory for folder.  */
      folder = calloc (1, sizeof (*folder));
      if (folder != NULL)
	{
	  folder->url = url;
	  /* Initialize the internal lock, now so the concrete
	     folder could use it.  */
	  status = monitor_create (&(folder->monitor), 0, folder);
	  if (status == 0)
	    {
	      /* Create the concrete folder type.  */
	      status = f_init (folder);
	      if (status == 0)
		{
		  *pfolder = folder;
		  folder->ref++;
		  /* Put on the internal list of known folders.  */
		  if (known_folder_list == NULL)
		    list_create (&known_folder_list);
		  list_append (known_folder_list, folder);
		}
	    }
	  /* Something went wrong, destroy the object. */
	  if (status != 0)
	    {
	      if (folder->monitor)
		monitor_destroy (&(folder->monitor), folder);
	      if (folder->url)
		url_destroy (&(folder->url));
	      free (folder);
	    }
	}
    }
  else
    status = ENOENT;

  return status;
}

void
folder_destroy (folder_t *pfolder)
{
  if (pfolder && *pfolder)
    {
      folder_t folder = *pfolder;
      int destroy_lock = 0;
      monitor_t monitor = folder->monitor;

      monitor_wrlock (monitor);
      monitor_wrlock (&folder_lock);
      folder->ref--;
      /* Remove the folder from the list of known folder.  */
      if (folder->ref <= 0)
	list_remove (known_folder_list, folder);
      if (list_is_empty)
	{
	  list_destroy (&known_folder_list);
	  known_folder_list = NULL;
	}
      monitor_unlock (&folder_lock);
      if (folder->ref <= 0)
	{
	  monitor_unlock (monitor);
	  destroy_lock = 1;
	  /* Notify the observers.  */
	  if (folder->observable)
	    {
	      observable_notify (folder->observable, MU_EVT_FOLDER_DESTROY);
	      observable_destroy (&(folder->observable), folder);
	    }
	  if (folder->_destroy)
	    folder->_destroy (folder);
	  monitor_wrlock (monitor);
	  if (folder->ticket)
	    ticket_destroy (&(folder->ticket), folder);
	  if (folder->authority)
	    authority_destroy (&(folder->authority), folder);
	  if (folder->stream)
	    stream_destroy (&(folder->stream), folder);
	  if (folder->url)
	    url_destroy (&(folder->url));
	  free (folder);
	}
      monitor_unlock (monitor);
      if (destroy_lock)
	monitor_destroy (&monitor, folder);
      *pfolder = NULL;
    }
}

int
folder_open (folder_t folder, int flags)
{
  if (folder == NULL || folder->_open == NULL)
    return ENOSYS;
  return folder->_open (folder, flags);
}

int
folder_close (folder_t folder)
{
  if (folder == NULL || folder->_close == NULL)
    return ENOSYS;
  return folder->_close (folder);
}

int
folder_set_stream (folder_t folder, stream_t stream)
{
  if (folder == NULL)
    return EINVAL;
  if (folder->stream)
    stream_destroy (&(folder->stream), folder);
  folder->stream = stream;
  return 0;
}

int
folder_get_stream (folder_t folder, stream_t *pstream)
{
  if (folder == NULL || pstream == NULL)
    return EINVAL;
  *pstream = folder->stream;
  return 0;
}

int
folder_set_authority (folder_t folder, authority_t authority)
{
  if (folder == NULL)
    return EINVAL;
  if (folder->authority)
    authority_destroy (&(folder->authority), folder);
  folder->authority = authority;
  return 0;
}

int
folder_get_authority (folder_t folder, authority_t *pauthority)
{
  if (folder == NULL || pauthority == NULL)
    return EINVAL;
  *pauthority = folder->authority;
  return 0;
}

int
folder_set_ticket (folder_t folder, ticket_t ticket)
{
  if (folder == NULL)
    return EINVAL;
  if (folder->ticket)
    ticket_destroy (&(folder->ticket), folder);
  folder->ticket = ticket;
  return 0;
}

int
folder_get_ticket (folder_t folder, ticket_t *pticket)
{
  if (folder == NULL || pticket == NULL)
    return EINVAL;
  *pticket = folder->ticket;
  return 0;
}
int
folder_get_observable (folder_t folder, observable_t *pobservable)
{
  if (folder == NULL  || pobservable == NULL)
    return EINVAL;

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

int
folder_set_debug (folder_t folder, debug_t debug)
{
  if (folder == NULL)
    return EINVAL;
  if (folder->debug)
    debug_destroy (&(folder->debug), folder);
  folder->debug = debug;
  return 0;
}

int
folder_get_debug (folder_t folder, debug_t *pdebug)
{
  if (folder == NULL || pdebug == NULL)
    return EINVAL;
  if (folder->debug == NULL)
    {
      int status = debug_create (&(folder->debug), folder);
      if (status != 0)
        return status;
    }
  *pdebug = folder->debug;
  return 0;
}

int
folder_list (folder_t folder, const char *dirname,
	     struct folder_list ***flist, size_t *pnum)
{
  if (folder == NULL || folder->_list == NULL)
    return ENOSYS;
  return folder->_list (folder, dirname, flist, pnum);
}

int
folder_list_destroy (struct folder_list ***pflist, size_t count)
{
  struct folder_list **list;
  size_t i;
  if (pflist == NULL || *pflist == NULL || count == 0)
    return 0;
  list = *pflist;
  for (i = 0 ; i < count; i++)
    {
      if (list[i])
	{
	  if (list[i]->name)
	    free (list[i]->name);
	  free (list[i]);
	}
    }
  free (list);
  *pflist = NULL;
  return 0;
}

int
folder_delete (folder_t folder, const char *name)
{
  if (folder == NULL || folder->_delete == NULL)
    return ENOSYS;
  return folder->_delete (folder, name);
}

int
folder_rename (folder_t folder, const char *oldname, const char *newname)
{
  if (folder == NULL || folder->_rename == NULL)
    return ENOSYS;
  return folder->_rename (folder, oldname, newname);
}

int
folder_get_url (folder_t folder, url_t *purl)
{
  if (folder == NULL || purl == NULL)
    return EINVAL;
  *purl = folder->url;
  return 0;
}

/* ---------------------- Private ---------------------- */

static int is_known_folder (url_t url, folder_t *pfolder)
{
  int ret = 0;
  folder_t folder = NULL;
  iterator_t iterator;

  if (url == NULL || pfolder == NULL)
    return ret;

  if (iterator_create (&iterator, known_folder_list) != 0)
    return ret;

  for (iterator_first (iterator); !iterator_is_done (iterator);
       iterator_next (iterator))
    {
      iterator_current (iterator, (void **)&folder);
      /* Check if the same URL type.  */
      if (folder && folder->url
	  && is_same_scheme (url, folder->url)
	  && is_same_user (url, folder->url)
	  && is_same_path (url, folder->url)
	  && is_same_host (url, folder->url)
	  && is_same_port (url, folder->url))
	{
	  ret = 1;
	  break;
	}
    }
  iterator_destroy (&iterator);
  return ret;
}

static int
is_same_scheme (url_t url1, url_t url2)
{
  size_t i = 0, j = 0;
  char *s1, *s2;
  int ret = 1;

  url_get_scheme (url1, NULL, 0, &i);
  url_get_scheme (url2, NULL, 0, &j);
  s1 = calloc (i + 1, sizeof (char));
  if (s1)
    {
      url_get_scheme (url1, s1, i + 1, NULL);
      s2 = calloc (j + 1, sizeof (char));
      if (s2)
	{
	  url_get_scheme (url2, s2, j + 1, NULL);
	  ret = !strcasecmp (s1, s2);
	  free (s2);
	}
      free (s1);
    }
  return ret;
}

static int
is_same_user (url_t url1, url_t url2)
{
  size_t i = 0, j = 0;
  char *s1, *s2;
  int ret = 0;

  url_get_user (url1, NULL, 0, &i);
  url_get_user (url2, NULL, 0, &j);
  s1 = calloc (i + 1, sizeof (char));
  if (s1)
    {
      url_get_user (url1, s1, i + 1, NULL);
      s2 = calloc (j + 1, sizeof (char));
      if (s2)
	{
	  url_get_user (url2, s2, j + 1, NULL);
	  ret = !strcasecmp (s1, s2);
	  free (s2);
	}
      free (s1);
    }
  return ret;
}

static int
is_same_path (url_t url1, url_t url2)
{
  size_t i = 0, j = 0;
  char *s1, *s2;
  int ret = 0;

  url_get_path (url1, NULL, 0, &i);
  url_get_path (url2, NULL, 0, &j);
  s1 = calloc (i + 1, sizeof (char));
  if (s1)
    {
      url_get_path (url1, s1, i + 1, NULL);
      s2 = calloc (j + 1, sizeof (char));
      if (s2)
	{
	  url_get_path (url2, s2, j + 1, NULL);
	  ret = !strcasecmp (s1, s2);
	  free (s2);
	}
      free (s1);
    }
  return ret;
}

static int
is_same_host (url_t url1, url_t url2)
{
  size_t i = 0, j = 0;
  char *s1, *s2;
  int ret = 0;

  url_get_host (url1, NULL, 0, &i);
  url_get_host (url2, NULL, 0, &j);
  s1 = calloc (i + 1, sizeof (char));
  if (s1)
    {
      url_get_host (url1, s1, i + 1, NULL);
      s2 = calloc (j + 1, sizeof (char));
      if (s2)
	{
	  url_get_host (url2, s2, j + 1, NULL);
	  ret = !strcasecmp (s1, s2);
	  free (s2);
	}
      free (s1);
    }
  return ret;
}

static int
is_same_port (url_t url1, url_t url2)
{
  long p1 = 0, p2 = 0;

  url_get_port (url1, &p1);
  url_get_port (url2, &p2);
  return (p1 == p2);
}