namespace.c 9.64 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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2001, 2005, 2007-2012, 2014-2017 Free Software
   Foundation, Inc.

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

   GNU Mailutils 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 General Public License for more details.

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

#include "imap4d.h"
#include <mailutils/assoc.h>

struct namespace namespace[NS_MAX] = {
  [NS_PERSONAL] = { "personal" },
  [NS_OTHER]    = { "other" },
  [NS_SHARED]   = { "shared" }
};
  
static mu_assoc_t prefixes;

struct namespace *
namespace_lookup (char const *name)
{
  size_t i;
  
  for (i = 0; i < NS_MAX; i++)
    if (strcmp (namespace[i].name, name) == 0)
      {
	if (!namespace[i].prefixes)
	  {
	    int rc = mu_list_create (&namespace[i].prefixes);
	    if (rc)
	      {
		mu_diag_funcall (MU_DIAG_ERROR, "mu_list_create", NULL, rc);
		abort ();
	      }
	  }
	return &namespace[i];
      }
  return NULL;
}

static int
cmplen (const char *aname, const void *adata,
	const char *bname, const void *bdata,
	void *closure)
{
  return strlen (bname) - strlen (aname);
}

void
translate_delim (char *dst, char const *src, int dst_delim, int src_delim)
{
  do
    *dst++ = *src == src_delim ? dst_delim : *src;
  while (*src++);
}  

static void
trim_delim (char *str, int delim)
{
  size_t len = strlen (str);
  while (len && str[len-1] == '/')
    len--;
  str[len] = 0;
}

void
namespace_init (void)
{
  int i;
  int rc;
  struct namespace_prefix *pfx;
  
  if (mu_assoc_create (&prefixes, 0))
    imap4d_bye (ERR_NO_MEM);
  for (i = 0; i < NS_MAX; i++)
    {
      mu_iterator_t itr;
      
      if (mu_list_is_empty (namespace[i].prefixes))
	continue;
      
      if (mu_list_get_iterator (namespace[i].prefixes, &itr))
	imap4d_bye (ERR_NO_MEM);
      
	
      for (mu_iterator_first (itr);
	   !mu_iterator_is_done (itr); mu_iterator_next (itr))
	{
	  rc = mu_iterator_current (itr, (void **)&pfx);
	  if (rc)
	    {
	      mu_diag_funcall (MU_DIAG_ERROR, "mu_iterator_current", NULL, rc);
	      continue;
	    }
	  pfx->ns = i;

	  trim_delim (pfx->dir, '/');
	  
	  rc = mu_assoc_install (prefixes, pfx->prefix, pfx);
	  if (rc == MU_ERR_EXISTS)
	    {
	      mu_error (_("%s: namespace prefix appears twice"), pfx->prefix);
	      exit (EX_CONFIG);
	    }
	  else if (rc)
	    {
	      mu_error (_("%s: can't install prefix: %s"),
			pfx->prefix, mu_strerror (rc));
	      exit (EX_CONFIG);
	    }
	}
    }

  pfx = mu_assoc_get (prefixes, "");
  if (pfx)
    {
      if (pfx->ns != NS_PERSONAL)
	{
	  mu_error (_("empty prefix not allowed in the namespace %s"),
		    namespace[pfx->ns].name);
	  exit (EX_CONFIG);
	}
    }
  else
    {
      struct namespace *priv;
      
      pfx = mu_zalloc (sizeof (*pfx));
      pfx->prefix = mu_strdup ("");
      pfx->dir = mu_strdup ("$home");
      pfx->delim = '/';
      priv = namespace_lookup ("personal");
      mu_list_prepend (priv->prefixes, pfx);
      rc = mu_assoc_install (prefixes, pfx->prefix, pfx);
      if (rc)
	{
	  mu_error (_("%s: can't install prefix: %s"),
		    pfx->prefix, mu_strerror (rc));
	  exit (EX_CONFIG);
	}
    }
  
  mu_assoc_sort_r (prefixes, cmplen, NULL);
}
      
static char *
prefix_translate_name (struct namespace_prefix const *pfx, char const *name,
		       size_t namelen, int url)
{
  size_t pfxlen = strlen (pfx->prefix);

  if (pfxlen <= namelen && memcmp (pfx->prefix, name, pfxlen) == 0)
    {
      char *tmpl, *p;

      if (!pfx->scheme)
	url = 0;
      name += pfxlen;

      if (pfx->ns == NS_PERSONAL && strcmp (name, "INBOX") == 0)
	{
	  tmpl = mu_strdup (auth_data->mailbox);
	  return tmpl;//FIXME
	}
      
      tmpl = mu_alloc (namelen - pfxlen + strlen (pfx->dir)
		       + (url ? strlen (pfx->scheme) + 3 : 0)
		       + 2);
      if (url)
	{
	  p = mu_stpcpy (tmpl, pfx->scheme);
	  p = mu_stpcpy (p, "://");
	}
      else
	p = tmpl;
      
      p = mu_stpcpy (p, pfx->dir);
      if (*name)
	{
	  if (pfx->ns == NS_OTHER
	      && pfx->prefix[strlen(pfx->prefix) - 1] != pfx->delim)
	    {
	      while (*name && *name != pfx->delim)
		name++;
	    }
	  else if (*name != pfx->delim)
	    *p++ = '/';
	  translate_delim (p, name, '/', pfx->delim);
	}
      
      return tmpl;
    }
  return NULL;
}

static char *
i_translate_name (char const *name, int url, int ns,
		  struct namespace_prefix const **return_pfx)
{
  mu_iterator_t itr;
  int rc;
  char *res = NULL;
  size_t namelen = strlen (name);

  rc = mu_assoc_get_iterator (prefixes, &itr);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_assoc_get_iterator", NULL, rc);
      return NULL;
    }
  for (mu_iterator_first (itr);
       !mu_iterator_is_done (itr); mu_iterator_next (itr))
    {
      struct namespace_prefix *pfx;
      
      rc = mu_iterator_current (itr, (void **)&pfx);
      if (rc)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_iterator_current", NULL, rc);
	  continue;
	}

      if (ns != NS_MAX && ns != pfx->ns)
	continue;
      
      res = prefix_translate_name (pfx, name, namelen, url);
      if (res)
	{
	  if (return_pfx)
	    *return_pfx = pfx;
	  break;
	}
    }
  mu_iterator_destroy (&itr);

  return res;
}

static char *
extract_username (char const *name, struct namespace_prefix const *pfx)
{
  char const *p = name + strlen (pfx->prefix);
  char *end = strchr (p, pfx->delim);
  char *user;
  size_t len;
  
  if (end)
    len = end - p;
  else
    len = strlen (p);

  if (len == 0)
    return mu_strdup (auth_data->name);

  user = mu_alloc (len + 1);
  memcpy (user, p, len);
  user[len] = 0;
  return user;
}

char *
namespace_translate_name (char const *name, int url,
			  struct namespace_prefix const **return_pfx)
{
  char *res = NULL;
  struct namespace_prefix const *pfx;
  
  if (mu_c_strcasecmp (name, "INBOX") == 0 && auth_data->change_uid)
    {
      res = mu_strdup (auth_data->mailbox);
      pfx = mu_assoc_get (prefixes, "");
    }
  else
    res = i_translate_name (name, url, NS_MAX, &pfx);

  if (res)
    {
      mu_assoc_t assoc;
      int rc;
      char *dir;
      
      rc = mu_assoc_create (&assoc, 0);
      if (rc)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_assoc_create", NULL, rc);
	  free (res);
	  imap4d_bye (ERR_NO_MEM);
	}
      
      switch (pfx->ns)
	{
	case NS_PERSONAL:
	  mu_assoc_install (assoc, "user", auth_data->name);
	  mu_assoc_install (assoc, "home", real_homedir);
	  break;

	case NS_OTHER:
	  {
	    struct mu_auth_data *adata;
	    char *user = extract_username (name, pfx);
	    mu_assoc_install (assoc, "user", user);
	    adata = mu_get_auth_by_name (user);
	    if (adata)
	      {
		mu_assoc_install (assoc, "home", mu_strdup (adata->dir));
		mu_auth_data_free (adata);
	      }
	    mu_assoc_set_destroy_item (assoc, mu_list_free_item);
	  }
	  break;

	case NS_SHARED:
	  break;
	}

      rc = mu_str_expand (&dir, res, assoc);
      free (res);
      mu_assoc_destroy (&assoc);
      if (rc)
	{
	  if (rc == MU_ERR_FAILURE)
	    {
	      mu_error (_("cannot expand line `%s': %s"), res, dir);
	      free (dir);
	    }
	  else
	    {
	      mu_error (_("cannot expand line `%s': %s"), res,
			mu_strerror (rc));
	    }
	  imap4d_bye (ERR_NO_MEM);
	}
      
      res = dir;
      trim_delim (res, '/');
      
      if (return_pfx)
	*return_pfx = pfx;
    }
  return res;
}

char *
namespace_get_url (char const *name, int *mode)
{
  struct namespace_prefix const *pfx;
  char *path = namespace_translate_name (name, 1, &pfx);

  if (path && pfx->scheme)
    {
      char *p = mu_alloc (strlen (pfx->scheme) + 3 + strlen (path) + 1);
      strcpy (p, pfx->scheme);
      strcat (p, "://");
      strcat (p, path);
      free (path);
      path = p;
    }
  if (mode)
    *mode = namespace[pfx->ns].mode;
  return path;
}

static int
prefix_printer(void *item, void *data)
{
  struct namespace_prefix *pfx = item;
  int *first = data;

  if (*first)
    *first = 0;
  else
    io_sendf (" ");
  io_sendf ("(\"%s\" \"%c\")", pfx->prefix, pfx->delim);
  return 0;
}

static void
print_namespace (int ns)
{
  if (mu_list_is_empty (namespace[ns].prefixes))
    io_sendf ("NIL");
  else
    {
      int first = 1;
      io_sendf ("(");
      mu_list_foreach (namespace[ns].prefixes, prefix_printer, &first);
      io_sendf (")");
    }
}

/*
5. NAMESPACE Command

   Arguments: none

   Response:  an untagged NAMESPACE response that contains the prefix
                 and hierarchy delimiter to the server's Personal
                 Namespace(s), Other Users' Namespace(s), and Shared
                 Namespace(s) that the server wishes to expose. The
                 response will contain a NIL for any namespace class
                 that is not available. Namespace_Response_Extensions
                 MAY be included in the response.
                 Namespace_Response_Extensions which are not on the IETF
                 standards track, MUST be prefixed with an "X-".
*/

int
imap4d_namespace (struct imap4d_session *session,
                  struct imap4d_command *command, imap4d_tokbuf_t tok)
{
  if (imap4d_tokbuf_argc (tok) != 2)
    return io_completion_response (command, RESP_BAD, "Invalid arguments");

  io_sendf ("* NAMESPACE ");

  print_namespace (NS_PERSONAL);
  io_sendf (" ");
  print_namespace (NS_OTHER);
  io_sendf (" ");
  print_namespace (NS_SHARED);
  io_sendf ("\n");

  return io_completion_response (command, RESP_OK, "Completed");
}