Blame view

mailbox/gocs.c 8.64 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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

   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 <string.h>
#include <mailutils/types.h>
#include <mailutils/gocs.h>
#include <mailutils/mailbox.h>
#include <mailutils/locker.h>
#include <mailutils/mutil.h>
#include <mailutils/mailer.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
Sergey Poznyakoff authored
33
#include <mailutils/debug.h>
34
#include <mailutils/syslog.h>
35
#include <mailutils/registrar.h>
36 37 38 39 40 41 42 43
#include <syslog.h>

int mu_load_user_rcfile = 1;
int mu_load_site_rcfile = 1;
char *mu_load_rcfile = NULL;


int
44
mu_gocs_dummy (enum mu_gocs_op op, void *data)
45 46 47 48 49
{
  return 0;
}

int
50
mu_gocs_mailbox_init (enum mu_gocs_op op, void *data)
51 52 53 54
{
  int rc;
  struct mu_gocs_mailbox *p = data;

55
  if (op == mu_gocs_op_set && p)
56
    {
57 58 59 60
      if (p->mail_spool)
	{
	  rc = mu_set_mail_directory (p->mail_spool);
	  if (rc)
61
	    mu_error (_("cannot set mail directory name to `%s': %s"),
62 63 64 65 66 67 68 69
		      p->mail_spool, mu_strerror (rc));
	  free (p->mail_spool);
	  p->mail_spool = NULL;
	}
      if (p->mailbox_pattern)
	{
	  rc = mu_set_mailbox_pattern (p->mailbox_pattern);
	  if (rc)
70
	    mu_error (_("cannot set mailbox pattern to `%s': %s"),
71 72 73 74 75 76 77
		      p->mailbox_pattern, mu_strerror (rc));
	  free (p->mailbox_pattern);
	  p->mailbox_pattern = NULL;
	}
      if (p->mailbox_type)
	{
	  if (mu_registrar_set_default_scheme (p->mailbox_type))
78
	    mu_error (_("invalid mailbox type: %s"), p->mailbox_type);
79 80 81
	  free (p->mailbox_type);
	  p->mailbox_type = NULL;
	}
82 83 84 85 86
    }
  return 0;
}

int
87
mu_gocs_locking_init (enum mu_gocs_op op, void *data)
88 89 90
{
  struct mu_gocs_locking *p = data;
  
91
  if (!(op == mu_gocs_op_set && p))
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
    return 0;

  if (p->lock_flags)
    {
      int flags = 0;
      char *s;
      
      for (s = p->lock_flags; *s; s++)
	{
	  switch (*s)
	    {
	    case 'E':
	      flags |= MU_LOCKER_EXTERNAL;
	      break;
	      
	    case 'R':
	      flags |= MU_LOCKER_RETRY;
	      break;
	      
	    case 'T':
	      flags |= MU_LOCKER_TIME;
	      break;
	      
	    case 'P':
	      flags |= MU_LOCKER_PID;
	      break;
	      
	    default:
120
	      mu_error (_("invalid lock flag `%c'"), *s);
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
	    }
	}
      mu_locker_set_default_flags (flags, mu_locker_assign);
      free (p->lock_flags);
      p->lock_flags = NULL;
    }

  if (p->lock_retry_count)
    {
      mu_locker_set_default_retry_count (p->lock_retry_count);
      mu_locker_set_default_flags (MU_LOCKER_RETRY, mu_locker_set_bit);
      p->lock_retry_count = 0;
    }

  if (p->lock_retry_timeout)
    {
      mu_locker_set_default_retry_timeout (p->lock_retry_timeout);
      mu_locker_set_default_flags (MU_LOCKER_RETRY, mu_locker_set_bit);
      p->lock_retry_timeout = 0;
    }

  if (p->lock_expire_timeout)
    {
      mu_locker_set_default_expire_timeout (p->lock_expire_timeout);
      mu_locker_set_default_flags (MU_LOCKER_EXTERNAL, mu_locker_set_bit);
      p->lock_expire_timeout = 0;
    }

  if (p->external_locker)
    {
      mu_locker_set_default_external_program (p->external_locker);
      mu_locker_set_default_flags (MU_LOCKER_TIME, mu_locker_set_bit);
      free (p->external_locker);
      p->external_locker = NULL;
    }
  return 0;
}

int
160
mu_gocs_source_email_init (enum mu_gocs_op op, void *data)
161 162 163 164
{
  struct mu_gocs_source_email *p = data;
  int rc;

165
  if (!(op == mu_gocs_op_set && p))
166 167 168 169 170
    return 0;
  
  if (p->address)
    {
      if ((rc = mu_set_user_email (p->address)) != 0)
171
	mu_error (_("invalid email address `%s': %s"),
172 173 174 175 176 177 178 179
		  p->address, mu_strerror (rc));
      free (p->address);
      p->address = NULL;
    }

  if (p->domain)
    {
      if ((rc = mu_set_user_email_domain (p->domain)) != 0)
180
	mu_error (_("invalid email domain `%s': %s"),
181 182 183 184 185 186 187 188 189
		  p->domain, mu_strerror (rc));

      free (p->domain);
      p->domain = NULL;
    }
  return 0;
}

int
190
mu_gocs_mailer_init (enum mu_gocs_op op, void *data)
191 192 193 194
{
  struct mu_gocs_mailer *p = data;
  int rc;

195
  if (!(op == mu_gocs_op_set && p))
196 197 198 199 200
    return 0;
  
  if (p->mailer)
    {
      if ((rc = mu_mailer_set_url_default (p->mailer)) != 0)
201
	mu_error (_("invalid mailer URL `%s': %s"),
202 203 204 205 206 207 208 209
		  p->mailer, mu_strerror (rc));
      free (p->mailer);
      p->mailer = NULL;
    }
  return 0;
}

int
210
mu_gocs_logging_init (enum mu_gocs_op op, void *data)
211 212
{
  struct mu_gocs_logging *p = data;
213 214

  if (op == mu_gocs_op_set)
215
    {
216 217 218 219 220
      if (!p)
	{
	  static struct mu_gocs_logging default_gocs_logging = { LOG_FACILITY };
	  p = &default_gocs_logging;
	}
221
  
222 223 224 225 226 227 228
      if (p->facility)
	{
	  mu_log_facility = p->facility;
	  mu_debug_default_printer = mu_debug_syslog_printer;
	}
      else
	mu_debug_default_printer = mu_debug_stderr_printer;
229

230 231 232
      if (p->tag)
	mu_log_tag = strdup (p->tag);
    }
233 234 235
  return 0;
}

236
int
237
mu_gocs_debug_init (enum mu_gocs_op op, void *data)
238
{
239
  if (op == mu_gocs_op_set && data)
240
    {
241
      struct mu_gocs_debug *p = data;
242 243 244 245 246 247 248 249 250 251 252 253
      if (p->string && p->errpfx)
	{
	  mu_global_debug_from_string (p->string, p->errpfx);
	  free (p->errpfx);
	}
      if (p->line_info >= 0)
	mu_debug_line_info = p->line_info;
    }
  return 0;
}


254 255
struct mu_gocs_entry
{
256
  const char *name;
257
  mu_gocs_init_fp init;
258 259 260 261 262 263 264
};

#define MAX_GOCS 512

static struct mu_gocs_entry _gocs_table[MAX_GOCS];

void
265
mu_gocs_register (const char *capa, mu_gocs_init_fp init)
266 267 268 269 270
{
  int i;
  for (i = 0; _gocs_table[i].name; i++)
    if (i == MAX_GOCS-1)
      {
271
	mu_error (_("gocs table overflow"));
272 273 274 275 276 277 278 279 280 281 282 283 284
	abort ();
      }
  _gocs_table[i].name = capa;
  _gocs_table[i].init = init;
}

int
mu_gocs_enumerate (mu_list_action_t action, void *data)
{
  int i;
  
  for (i = 0; _gocs_table[i].name; i++)
    {
285
      int rc = action ((void*) _gocs_table[i].name, data);
286 287 288 289 290 291
      if (rc)
	return rc;
    }
  return 0;
}

292
static mu_gocs_init_fp
293
find_init_function (struct mu_gocs_entry *tab, const char *capa)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
  for (; tab->name; tab++)
    if (strcmp (tab->name, capa) == 0)
      return tab->init;
  return NULL;
}

static struct mu_gocs_entry std_gocs_table[] = {
  { "common", mu_gocs_dummy },
  { "license", mu_gocs_dummy },
  { "mailbox", mu_gocs_mailbox_init },
  { "locking", mu_gocs_locking_init },
  { "address", mu_gocs_source_email_init },
  { "mailer", mu_gocs_mailer_init },
  { "logging", mu_gocs_logging_init },
309
  { "debug", mu_gocs_debug_init },
310 311 312 313 314
  { "auth", mu_gocs_dummy },
  { NULL }
};

void
315
mu_gocs_register_std (const char *name)
316
{
317
  mu_gocs_init_fp init = find_init_function (std_gocs_table, name);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  if (!init)
    {
      mu_error (_("INTERNAL ERROR at %s:%d: unknown standard capability `%s'"),
		__FILE__, __LINE__, name);
      abort ();
    }
  mu_gocs_register (name, init);
}


struct mu_gocs_data
{
  char *capa;
  void *data;
};

static mu_list_t /* of struct mu_gocs_data */ data_list;

336 337 338 339 340 341 342
static int
_gocs_comp (const void *a, const void *b)
{
  const struct mu_gocs_data *da = a, *db = b;
  return !(strcmp (da->capa, db->capa) == 0 && da->data == db->data);
}

343 344 345 346 347 348 349
void
mu_gocs_store (char *capa, void *data)
{
  struct mu_gocs_data *s;
  if (!data_list)
    {
      mu_list_create (&data_list);
350
      mu_list_set_destroy_item (data_list, mu_list_free_item);
351
      mu_list_set_comparator (data_list, _gocs_comp);
352 353 354 355 356 357 358 359 360
    }
  s = malloc (sizeof *s);
  if (!s)
    {
      mu_error ("%s", mu_strerror (ENOMEM));
      exit (1);
    }
  s->capa = capa;
  s->data = data;
361 362 363 364
  if (mu_list_locate (data_list, s, NULL) == 0)
    free (s);
  else
    mu_list_prepend (data_list, s);
365 366 367 368 369 370
}

int
_gocs_flush (void *item, void *data)
{
  struct mu_gocs_data *s = item;
371
  mu_gocs_init_fp initfun = find_init_function (_gocs_table, s->capa);
372 373 374 375 376 377 378 379

  if (!initfun)
    {
      mu_error (_("INTERNAL ERROR at %s:%d: unknown capability `%s'"),
		__FILE__, __LINE__, s->capa);
      abort ();
    }

380
  if (initfun (mu_gocs_op_set, s->data))
381
    {
382
      mu_error (_("initialization of GOCS `%s' failed"), s->capa);
383 384 385 386 387 388 389 390 391
      return 1;
    }
  
  return 0;
}

void
mu_gocs_flush ()
{
392
  int i;
393
  mu_list_do (data_list, _gocs_flush, NULL);
394 395 396

  for (i = 0; _gocs_table[i].name; i++)
    _gocs_table[i].init (mu_gocs_op_flush, NULL);
397
}