registry.c
4.59 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2002, 2004-2005, 2007-2008, 2010-2012, 2014-2017
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, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sieve-priv.h>
int
mu_sieve_registry_require (mu_sieve_machine_t mach, const char *name,
enum mu_sieve_record type)
{
mu_sieve_registry_t *reg;
reg = mu_sieve_registry_lookup (mach, name, type);
if (!reg)
{
void *handle = mu_sieve_load_ext (mach, name);
if (!handle)
return 1;
reg = mu_sieve_registry_lookup (mach, name, type);
if (!reg)
return 1;
reg->handle = handle;
}
reg->required = 1;
return 0;
}
static void
regunload (void *data)
{
mu_sieve_registry_t *reg = data;
mu_sieve_unload_ext (reg->handle);
}
static int
regcmp (void const *a, void const *b)
{
mu_sieve_registry_t const *rega = a;
mu_sieve_registry_t const *regb = b;
if (rega->type != regb->type)
return rega->type - regb->type;
return strcmp (rega->name, regb->name);
}
mu_sieve_registry_t *
mu_sieve_registry_add (mu_sieve_machine_t mach, const char *name)
{
mu_sieve_registry_t *reg;
int rc;
if (!mach->registry)
{
rc = mu_list_create (&mach->registry);
if (rc)
{
mu_sieve_error (mach, "mu_list_create: %s", mu_strerror (rc));
mu_sieve_abort (mach);
}
mu_list_set_destroy_item (mach->registry, regunload);
mu_list_set_comparator (mach->registry, regcmp);
}
reg = mu_sieve_malloc (mach, sizeof (*reg));
reg->name = name;
reg->handle = NULL;
reg->required = 0;
memset (®->v, 0, sizeof reg->v);
rc = mu_list_append (mach->registry, reg);
if (rc)
{
mu_sieve_error (mach, "mu_list_append: %s", mu_strerror (rc));
mu_sieve_abort (mach);
}
return reg;
}
mu_sieve_registry_t *
mu_sieve_registry_lookup (mu_sieve_machine_t mach, const char *name,
enum mu_sieve_record type)
{
mu_sieve_registry_t key, *reg;
int rc;
key.name = name;
key.type = type;
rc = mu_list_locate (mach->registry, &key, (void**) ®);
if (rc == MU_ERR_NOENT)
return NULL;
else if (rc)
{
mu_sieve_error (mach, _("registry lookup failed: %s"), mu_strerror (rc));
mu_sieve_abort (mach);
}
return reg;
}
void
mu_sieve_register_test_ext (mu_sieve_machine_t mach,
const char *name, mu_sieve_handler_t handler,
mu_sieve_data_type *req_args,
mu_sieve_data_type *opt_args,
mu_sieve_tag_group_t *tags, int required)
{
mu_sieve_registry_t *reg = mu_sieve_registry_add (mach, name);
reg->type = mu_sieve_record_test;
reg->required = required;
reg->v.command.handler = handler;
reg->v.command.req_args = req_args;
reg->v.command.opt_args = opt_args;
reg->v.command.tags = tags;
}
void
mu_sieve_register_test (mu_sieve_machine_t mach,
const char *name, mu_sieve_handler_t handler,
mu_sieve_data_type *arg_types,
mu_sieve_tag_group_t *tags, int required)
{
return mu_sieve_register_test_ext (mach, name, handler,
arg_types, NULL,
tags,
required);
}
void
mu_sieve_register_action_ext (mu_sieve_machine_t mach,
const char *name, mu_sieve_handler_t handler,
mu_sieve_data_type *req_args,
mu_sieve_data_type *opt_args,
mu_sieve_tag_group_t *tags, int required)
{
mu_sieve_registry_t *reg = mu_sieve_registry_add (mach, name);
reg->type = mu_sieve_record_action;
reg->required = required;
reg->v.command.handler = handler;
reg->v.command.req_args = req_args;
reg->v.command.opt_args = opt_args;
reg->v.command.tags = tags;
}
void
mu_sieve_register_action (mu_sieve_machine_t mach,
const char *name, mu_sieve_handler_t handler,
mu_sieve_data_type *arg_types,
mu_sieve_tag_group_t *tags, int required)
{
return mu_sieve_register_action_ext (mach, name, handler,
arg_types, NULL,
tags,
required);
}