Blame view

imap4d/namespace.c 9.67 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
Sergey Poznyakoff authored
2
   Copyright (C) 1999, 2001, 2005, 2007-2012, 2014-2017 Free Software
3
   Foundation, Inc.
4

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

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18

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

21
struct namespace namespace[NS_MAX] = {
22 23 24
  [NS_PERSONAL] = { "personal" },
  [NS_OTHER]    = { "other" },
  [NS_SHARED]   = { "shared" }
25 26 27
};
  
static mu_assoc_t prefixes;
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
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);
}
58

59 60
void
translate_delim (char *dst, char const *src, int dst_delim, int src_delim)
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
  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);
94
      	
95 96 97 98 99 100 101 102 103 104 105 106 107
      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, '/');
	  
108 109 110
	  if (!pfx->scheme)
	    mu_registrar_get_default_record (&pfx->record);
	  
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	  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)
    {
129
      if (pfx->ns != NS_PERSONAL)
130 131 132 133 134 135 136
	{
	  mu_error (_("empty prefix not allowed in the namespace %s"),
		    namespace[pfx->ns].name);
	  exit (EX_CONFIG);
	}
    }
  else
137
    {
138 139 140 141 142 143
      struct namespace *priv;
      
      pfx = mu_zalloc (sizeof (*pfx));
      pfx->prefix = mu_strdup ("");
      pfx->dir = mu_strdup ("$home");
      pfx->delim = '/';
Sergey Poznyakoff authored
144
      mu_registrar_get_default_record (&pfx->record);
145
      priv = namespace_lookup ("personal");
146 147 148 149 150 151 152 153
      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);
	}
154
    }
155 156
  
  mu_assoc_sort_r (prefixes, cmplen, NULL);
157
}
158 159 160
      
static char *
prefix_translate_name (struct namespace_prefix const *pfx, char const *name,
161
		       size_t namelen)
162
{
163
  size_t pfxlen = strlen (pfx->prefix);
Sergey Poznyakoff authored
164 165 166 167 168 169 170 171 172 173
  int delim = 0;
  
  if (pfxlen && pfx->prefix[pfxlen-1] == pfx->delim)
    {
      pfxlen--;
      delim = pfx->delim;
    }
  
  if ((pfxlen <= namelen && memcmp (pfx->prefix, name, pfxlen) == 0)
      && (delim == 0 || (name[pfxlen] == delim || name[pfxlen] == 0)))
174
    {
175 176 177 178
      char *tmpl, *p;

      name += pfxlen;

179
      if (pfx->ns == NS_PERSONAL && strcmp (name, "INBOX") == 0)
180 181 182 183 184
	{
	  tmpl = mu_strdup (auth_data->mailbox);
	  return tmpl;//FIXME
	}
      
185 186
      tmpl = mu_alloc (namelen - pfxlen + strlen (pfx->dir) + 2);
      p = tmpl;
187 188 189 190
      
      p = mu_stpcpy (p, pfx->dir);
      if (*name)
	{
191
	  if (pfx->ns == NS_OTHER)
192
	    {
193 194 195
	      if (pfx->prefix[strlen (pfx->prefix) - 1] == pfx->delim)
		++name;

196 197 198 199 200
	      while (*name && *name != pfx->delim)
		name++;
	    }
	  else if (*name != pfx->delim)
	    *p++ = '/';
201 202
	  translate_delim (p, name, '/', pfx->delim);
	}
203
      
204
      return tmpl;
205
    }
206
  return NULL;
207 208
}

209
static char *
210
translate_name (char const *name, struct namespace_prefix const **return_pfx)
211
{
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  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;
	}
234

235
      res = prefix_translate_name (pfx, name, namelen);
236 237 238 239 240 241 242 243 244 245
      if (res)
	{
	  if (return_pfx)
	    *return_pfx = pfx;
	  break;
	}
    }
  mu_iterator_destroy (&itr);

  return res;
246
}
247 248 249 250

static char *
extract_username (char const *name, struct namespace_prefix const *pfx)
{
251 252
  char const *p;
  char *end;
253 254
  char *user;
  size_t len;
255 256 257 258 259

  if (strlen (name) < strlen (pfx->prefix))
    return NULL;
  p = name + strlen (pfx->prefix);
  end = strchr (p, pfx->delim);
260
  
261 262 263 264
  if (end)
    len = end - p;
  else
    len = strlen (p);
265 266 267 268

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

269 270 271 272 273 274 275
  user = mu_alloc (len + 1);
  memcpy (user, p, len);
  user[len] = 0;
  return user;
}

char *
276
namespace_translate_name (char const *name,
277
			  struct namespace_prefix const **return_pfx)
278
{
279 280 281
  char *res = NULL;
  struct namespace_prefix const *pfx;
  
282
  res = translate_name (name, &pfx);
283

284
  if (res)
285
    {
286 287
      mu_assoc_t assoc;
      int rc;
288 289
      char *dir;
      
290 291 292 293 294 295 296 297
      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);
	}
      
298
      switch (pfx->ns)
299
	{
300
	case NS_PERSONAL:
301 302
	  mu_assoc_install (assoc, "user", auth_data->name);
	  mu_assoc_install (assoc, "home", real_homedir);
303 304
	  break;

305
	case NS_OTHER:
306 307
	  {
	    struct mu_auth_data *adata;
308 309 310
	    char *user = extract_username (name, pfx);
	    mu_assoc_install (assoc, "user", user);
	    adata = mu_get_auth_by_name (user);
311 312
	    if (adata)
	      {
313
		mu_assoc_install (assoc, "home", mu_strdup (adata->dir));
314 315
		mu_auth_data_free (adata);
	      }
316
	    mu_assoc_set_destroy_item (assoc, mu_list_free_item);
317 318 319
	  }
	  break;

320
	case NS_SHARED:
321
	  break;
322
	}
323 324

      rc = mu_str_expand (&dir, res, assoc);
325
      free (res);
326 327
      mu_assoc_destroy (&assoc);
      if (rc)
328
	{
329 330 331 332 333 334 335 336 337 338 339
	  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);
340 341
	}
      
342 343
      res = dir;
      trim_delim (res, '/');
344
    }
345 346 347 348 349 350 351 352 353
  else if (mu_c_strcasecmp (name, "INBOX") == 0 && auth_data->change_uid)
    {
      res = mu_strdup (auth_data->mailbox);
      pfx = mu_assoc_get (prefixes, "");
    }
      
  if (res && return_pfx)
    *return_pfx = pfx;

354
  return res;
355 356
}

357
char *
358
namespace_get_name (char const *name, mu_record_t *rec, int *mode)
359 360
{
  struct namespace_prefix const *pfx;
361
  char *path = namespace_translate_name (name, &pfx);
362 363 364 365 366
  if (strcmp (name, pfx->prefix) == 0)
    {
      free (path);
      return NULL;
    }
367 368
  if (rec)
    *rec = pfx->record;
369 370 371 372 373
  if (mode)
    *mode = namespace[pfx->ns].mode;
  return path;
}

374
static int
375
prefix_printer(void *item, void *data)
376
{
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  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 (")");
    }
400
}
401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/*
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-".
*/

418
int
419 420
imap4d_namespace (struct imap4d_session *session,
                  struct imap4d_command *command, imap4d_tokbuf_t tok)
421
{
422
  if (imap4d_tokbuf_argc (tok) != 2)
423
    return io_completion_response (command, RESP_BAD, "Invalid arguments");
424

425
  io_sendf ("* NAMESPACE ");
426

427
  print_namespace (NS_PERSONAL);
428
  io_sendf (" ");
429
  print_namespace (NS_OTHER);
430
  io_sendf (" ");
431
  print_namespace (NS_SHARED);
432
  io_sendf ("\n");
433

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