url.c 8.88 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
/* 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.  */

#include <url_mbox.h>
#include <url_unix.h>
#include <url_mdir.h>
#include <url_mh.h>
#include <url_mmdf.h>
#include <url_pop.h>
#include <url_imap.h>
#include <url_mail.h>

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

/* Forward prototypes */
static int get_scheme  (const url_t, char *, size_t, size_t *);
static int get_mscheme (const url_t, char **, size_t *);
static int get_user    (const url_t, char *, size_t, size_t *);
static int get_muser   (const url_t, char **, size_t *);
static int get_passwd  (const url_t, char *, size_t, size_t *);
static int get_mpasswd (const url_t, char **, size_t *);
static int get_host    (const url_t, char *, size_t, size_t *);
static int get_mhost   (const url_t, char **, size_t *);
static int get_port    (const url_t, long *);
static int get_path    (const url_t, char *, size_t, size_t *);
static int get_mpath   (const url_t, char **, size_t *);
static int get_query   (const url_t, char *, size_t, size_t *);
static int get_mquery  (const url_t, char **, size_t *);
static int get_id      (const url_t, int *id);

/*
  Builtin url types.
  We are using a simple circular list to hold the builtin type. */
static struct url_builtin
{
  const struct url_type *utype;
  int is_malloc;
  struct url_builtin * next;
} url_builtin [] = {
  { NULL, 0,               &url_builtin[1] }, /* Sentinel, head list */
  { &_url_mbox_type, 0,    &url_builtin[2] },
  { &_url_unix_type, 0,    &url_builtin[3] },
  { &_url_maildir_type, 0, &url_builtin[4] },
  { &_url_mh_type, 0,      &url_builtin[5] },
  { &_url_mmdf_type, 0,    &url_builtin[6] },
  { &_url_pop_type, 0,     &url_builtin[7] },
  { &_url_imap_type, 0,    &url_builtin[8] },
  { &_url_mailto_type, 0,  &url_builtin[0] },
};

/*
  FIXME: Proper locking is not done when accessing the list
  this code is not thread-safe .. TODO */
int
url_add_type (struct url_type *utype)
{
  struct url_builtin *current = malloc (sizeof (*current));
  if (current == NULL)
    return -1;
  utype->id = (int)utype; /* It just has to be uniq */
  current->utype = utype;
  current->is_malloc = 1;
  current->next = url_builtin->next;
  url_builtin->next = current;
  return 0;
}

int
url_remove_type (const struct url_type *utype)
{
  struct url_builtin *current, *previous;
  for (previous = url_builtin, current = url_builtin->next;
       current != url_builtin;
       previous = current, current = current->next)
    {
      if (current->utype == utype)
	{
	  previous->next = current->next;
	  if (current->is_malloc)
	    free (current);
	  return 0;;
	}
    }
  return -1;
}

int
url_list_type (struct url_type *list, size_t len, size_t *n)
{
  struct url_builtin *current;
  size_t i = 0;
  for (i = 0, current = url_builtin->next; current != url_builtin;
       current = current->next, i++)
    {
      if (list)
	{
	  if (i < len)
	    list[i] = *(current->utype);
	  else
	    break;
	}
    }
  if (n)
    *n = i;
  return 0;
}

int
url_list_mtype (struct url_type **mlist, size_t *n)
{
  struct url_type *utype;
  size_t i;

  url_list_type (NULL, 0, &i);
  utype = calloc (i, sizeof (*utype));
  if (utype == NULL)
    {
      return ENOMEM;
    }

  *mlist = utype;
  url_list_type (utype, i, n);
  return 0;
}

int
url_init (url_t * purl, const char *name)
{
  int status = -1;
  const struct url_type *utype;
  struct url_builtin *ub;

  /* Sanity checks */
  if (name == NULL || *name == '\0')
    {
      return status;
    }

  /* Search for a known scheme */
  for (ub = url_builtin->next; ub != url_builtin; ub = ub->next)
    {
      utype = ub->utype;
      if (strncasecmp (name, utype->scheme, utype->len) == 0)
	{
	  status = 0;
	  break;
	}
    }

  /* Found one initialize it */
  if (status == 0)
    {
      status = utype->_init (purl, name);
      if (status == 0)
	{
	  url_t url = *purl;
	  if (url->utype == NULL)
	    url->utype = utype;
	  if (url->_get_scheme == NULL)
	    url->_get_scheme = get_scheme;
	  if (url->_get_mscheme == NULL)
	    url->_get_mscheme = get_mscheme;
	  if (url->_get_user == NULL)
	    url->_get_user = get_user;
	  if (url->_get_muser == NULL)
	    url->_get_muser = get_muser;
	  if (url->_get_passwd == NULL)
	    url->_get_passwd = get_passwd;
	  if (url->_get_mpasswd == NULL)
	    url->_get_mpasswd = get_mpasswd;
	  if (url->_get_host == NULL)
	    url->_get_host = get_host;
	  if (url->_get_mhost == NULL)
	    url->_get_mhost = get_mhost;
	  if (url->_get_port == NULL)
	    url->_get_port = get_port;
	  if (url->_get_path == NULL)
	    url->_get_path = get_path;
	  if (url->_get_mpath == NULL)
	    url->_get_mpath = get_mpath;
	  if (url->_get_query == NULL)
	    url->_get_query = get_query;
	  if (url->_get_mquery == NULL)
	    url->_get_mquery = get_mquery;
	  if (url->_get_id == NULL)
	    url->_get_id = get_id;
	}
    }
  return status;
}

void
url_destroy (url_t *purl)
{
  if (purl && *purl)
    {
      const struct url_type *utype = (*purl)->utype;
      utype->_destroy(purl);
      (*purl) = NULL;
    }
}


/* Simple stub functions they all call _cpystr */

static int
get_scheme (const url_t u, char *s, size_t len, size_t *n)
{
  size_t i;
  if (u == NULL)
    return EINVAL;
  i = _cpystr (s, u->scheme, len);
  if (n)
    *n = i;
  return 0;
}
static int
get_mscheme (const url_t u, char **s, size_t *n)
{
  size_t i;
  if (u == NULL || s == NULL || u->_get_scheme (u, NULL, 0, &i) != 0)
    {
      return EINVAL;
    }
  *s = malloc (++i);
  if (*s == NULL)
    {
      return ENOMEM;
    }
  u->_get_scheme (u, *s, 0, n);
  return 0;
}

static int
get_user (const url_t u, char *s, size_t len, size_t *n)
{
  size_t i;
  if (u == NULL)
    return EINVAL;
  i = _cpystr (s, u->user, len);
  if (n)
    *n = i;
  return 0;
}
static int
get_muser (const url_t u, char **s, size_t *n)
{
  size_t i;
  if (u == NULL || s == NULL || u->_get_user (u, NULL, 0, &i) != 0)
    {
      return EINVAL;
    }
  *s = malloc (++i);
  if (*s == NULL)
    {
      return ENOMEM;
    }
  u->_get_user (u, *s, i, n);
  return 0;
}

/* FIXME: We should not store passwd in clear, but rather
   have a simple encoding, and decoding mechanism */
static int
get_passwd (const url_t u, char *s, size_t len, size_t *n)
{
  size_t i;
  if (u == NULL)
    return EINVAL;
  i = _cpystr (s, u->passwd, len);
  if (n)
    *n = i;
  return 0;
}
static int
get_mpasswd (const url_t u, char **s, size_t *n)
{
  size_t i;
  if (u == NULL || s == NULL || u->_get_passwd (u, NULL, 0, &i) != 0)
    {
      return EINVAL;
    }
  *s = malloc (++i);
  if (*s == NULL)
    {
      return ENOMEM;
    }
  u->_get_passwd (u, *s, i, n);
  return 0;
}

static int
get_host (const url_t u, char *s, size_t len, size_t *n)
{
  size_t i;
  if (u == NULL)
    return EINVAL;
  i = _cpystr (s, u->host, len);
  if (n)
    *n = i;
  return 0;
}
static int
get_mhost (const url_t u, char **s, size_t *n)
{
  size_t i;
  if (u == NULL || s == NULL || u->_get_host (u, NULL, 0, &i) != 0)
    {
      return EINVAL;
    }
  *s = malloc (++i);
  if (*s == NULL)
    {
      return ENOMEM;
    }
  u->_get_host (u, *s, i, n);
  return 0;
}

static int
get_port (const url_t u, long * p)
{
  *p = u->port;
  return 0;
}

static int
get_path (const url_t u, char *s, size_t len, size_t *n)
{
  size_t i;
  if (u == NULL)
    return EINVAL;
  i = _cpystr(s, u->path, len);
  if (n)
    *n = i;
  return 0;
}
static int
get_mpath (const url_t u, char **s, size_t *n)
{
  size_t i;
  if (u == NULL || s == NULL || u->_get_path (u, NULL, 0, &i) != 0)
    {
      return EINVAL;
    }
  *s = malloc (++i);
  if (*s == NULL)
    {
      return ENOMEM;
    }
  u->_get_path (u, *s, i, n);
  return 0;
}

static int
get_query (const url_t u, char *s, size_t len, size_t *n)
{
  size_t i;
  if (u == NULL)
    return EINVAL;
  i = _cpystr(s, u->query, len);
  if (n)
    *n = i;
  return 0;
}
static int
get_mquery (const url_t u, char **s, size_t *n)
{
  size_t i;
  if (u == NULL || s == NULL || u->_get_query (u, NULL, 0, &i) != 0)
    {
      return EINVAL;
    }
  *s = malloc (++i);
  if (*s == NULL)
    {
      return ENOMEM;
    }
  u->_get_query (u, *s, i, n);
  return 0;
}

static int
get_id (const url_t u, int *id)
{
  const struct url_type *utype = u->utype;
  *id = utype->id;
  return 0;
}