summaryrefslogtreecommitdiff
path: root/libmailutils/gocs.c
blob: 40bde9d0dcfac687c2ec7658b20e37d749bec29a (plain)
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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.

   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>
#include <mailutils/debug.h>
#include <mailutils/syslog.h>
#include <mailutils/registrar.h>
#include <syslog.h>

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


int
mu_gocs_dummy (enum mu_gocs_op op, void *data)
{
  return 0;
}

int
mu_gocs_mailbox_init (enum mu_gocs_op op, void *data)
{
  int rc;
  struct mu_gocs_mailbox *p = data;

  if (op == mu_gocs_op_set && p)
    {
      if (p->mail_spool)
	{
	  rc = mu_set_mail_directory (p->mail_spool);
	  if (rc)
	    mu_error (_("cannot set mail directory name to `%s': %s"),
		      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)
	    mu_error (_("cannot set mailbox pattern to `%s': %s"),
		      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))
	    mu_error (_("invalid mailbox type: %s"), p->mailbox_type);
	  free (p->mailbox_type);
	  p->mailbox_type = NULL;
	}
    }
  return 0;
}

int
mu_gocs_locking_init (enum mu_gocs_op op, void *data)
{
  struct mu_gocs_locking *p = data;
  
  if (!(op == mu_gocs_op_set && p))
    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:
	      mu_error (_("invalid lock flag `%c'"), *s);
	    }
	}
      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
mu_gocs_source_email_init (enum mu_gocs_op op, void *data)
{
  struct mu_gocs_source_email *p = data;
  int rc;

  if (!(op == mu_gocs_op_set && p))
    return 0;
  
  if (p->address)
    {
      if ((rc = mu_set_user_email (p->address)) != 0)
	mu_error (_("invalid email address `%s': %s"),
		  p->address, mu_strerror (rc));
      free (p->address);
      p->address = NULL;
    }

  if (p->domain)
    {
      if ((rc = mu_set_user_email_domain (p->domain)) != 0)
	mu_error (_("invalid email domain `%s': %s"),
		  p->domain, mu_strerror (rc));

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

int
mu_gocs_mailer_init (enum mu_gocs_op op, void *data)
{
  struct mu_gocs_mailer *p = data;
  int rc;

  if (!(op == mu_gocs_op_set && p))
    return 0;
  
  if (p->mailer)
    {
      if ((rc = mu_mailer_set_url_default (p->mailer)) != 0)
	mu_error (_("invalid mailer URL `%s': %s"),
		  p->mailer, mu_strerror (rc));
      free (p->mailer);
      p->mailer = NULL;
    }
  return 0;
}

int
mu_gocs_logging_init (enum mu_gocs_op op, void *data)
{
  struct mu_gocs_logging *p = data;

  if (op == mu_gocs_op_set)
    {
      if (!p)
	{
	  static struct mu_gocs_logging default_gocs_logging = { LOG_FACILITY };
	  p = &default_gocs_logging;
	}
  
      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;

      if (p->tag)
	mu_log_tag = strdup (p->tag);
    }
  return 0;
}

int
mu_gocs_debug_init (enum mu_gocs_op op, void *data)
{
  if (op == mu_gocs_op_set && data)
    {
      struct mu_gocs_debug *p = data;
      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;
}


struct mu_gocs_entry
{
  const char *name;
  mu_gocs_init_fp init;
};

#define MAX_GOCS 512

static struct mu_gocs_entry _gocs_table[MAX_GOCS];

void
mu_gocs_register (const char *capa, mu_gocs_init_fp init)
{
  int i;
  for (i = 0; _gocs_table[i].name; i++)
    if (i == MAX_GOCS-1)
      {
	mu_error (_("gocs table overflow"));
	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++)
    {
      int rc = action ((void*) _gocs_table[i].name, data);
      if (rc)
	return rc;
    }
  return 0;
}

static mu_gocs_init_fp
find_init_function (struct mu_gocs_entry *tab, const char *capa)
{
  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 },
  { "debug", mu_gocs_debug_init },
  { "auth", mu_gocs_dummy },
  { NULL }
};

void
mu_gocs_register_std (const char *name)
{
  mu_gocs_init_fp init = find_init_function (std_gocs_table, name);
  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;

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);
}

void
mu_gocs_store (char *capa, void *data)
{
  struct mu_gocs_data *s;
  if (!data_list)
    {
      mu_list_create (&data_list);
      mu_list_set_destroy_item (data_list, mu_list_free_item);
      mu_list_set_comparator (data_list, _gocs_comp);
    }
  s = malloc (sizeof *s);
  if (!s)
    {
      mu_error ("%s", mu_strerror (ENOMEM));
      exit (1);
    }
  s->capa = capa;
  s->data = data;
  if (mu_list_locate (data_list, s, NULL) == 0)
    free (s);
  else
    mu_list_prepend (data_list, s);
}

int
_gocs_flush (void *item, void *data)
{
  struct mu_gocs_data *s = item;
  mu_gocs_init_fp initfun = find_init_function (_gocs_table, s->capa);

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

  if (initfun (mu_gocs_op_set, s->data))
    {
      mu_error (_("initialization of GOCS `%s' failed"), s->capa);
      return 1;
    }
  
  return 0;
}

void
mu_gocs_flush ()
{
  int i;
  mu_list_do (data_list, _gocs_flush, NULL);

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

Return to:

Send suggestions and report system problems to the System administrator.