registrar.c
5.32 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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <registrar0.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/*
Builtin mailbox types.
A circular list is use for the builtin.
Proper locking is not done when accessing the list.
FIXME: not thread-safe. */
static struct _registrar registrar [] = {
{ NULL, NULL, 0, ®istrar[1] }, /* sentinel, head list */
{ &_url_file_registrar, &_mailbox_mbox_registrar, 0, ®istrar[2] },
{ &_url_mbox_registrar, &_mailbox_mbox_registrar, 0, ®istrar[3] },
{ &_url_unix_registrar, &_mailbox_unix_registrar, 0, ®istrar[4] },
{ &_url_maildir_registrar, &_mailbox_maildir_registrar, 0, ®istrar[5] },
{ &_url_mmdf_registrar, &_mailbox_mmdf_registrar, 0, ®istrar[6] },
{ &_url_pop_registrar, &_mailbox_pop_registrar, 0, ®istrar[7] },
{ &_url_imap_registrar, &_mailbox_imap_registrar, 0, ®istrar[0] },
};
static void
free_ureg (struct url_registrar *ureg)
{
if (ureg)
{
free (ureg->scheme);
free (ureg);
}
}
static void
free_mreg (struct mailbox_registrar *mreg)
{
if (mreg)
{
free (mreg->name);
free (mreg);
}
}
int
registrar_add (struct url_registrar *new_ureg,
struct mailbox_registrar *new_mreg, int *id)
{
struct _registrar *entry;
struct url_registrar *ureg = NULL;
struct mailbox_registrar *mreg;
/* Must registrar a mailbox */
if (new_mreg == NULL)
return EINVAL;
/* Mailbox */
mreg = calloc (1, sizeof (*mreg));
if (mreg == NULL)
return ENOMEM;
if (new_mreg->name)
{
mreg->name = strdup (new_mreg->name);
if (mreg->name == NULL)
{
free (mreg);
return ENOMEM;
}
}
mreg->_init = new_mreg->_init;
mreg->_destroy = new_mreg->_destroy;
/* URL */
if (new_ureg)
{
ureg = calloc (1, sizeof (*ureg));
if (ureg == NULL)
{
free_mreg (mreg);
return ENOMEM;
}
if (new_ureg->scheme)
{
ureg->scheme = strdup (new_ureg->scheme);
if (ureg->scheme == NULL)
{
free_mreg (mreg);
free_ureg (ureg);
return ENOMEM;
}
}
ureg->_init = new_ureg->_init;
ureg->_destroy = new_ureg->_destroy;
}
/* Register them to the list */
entry = calloc (1, sizeof (*entry));
if (entry == NULL)
{
free_mreg (mreg);
free_ureg (ureg);
return ENOMEM;
}
entry->ureg = ureg;
entry->mreg = mreg;
entry->is_allocated = 1;
entry->next = registrar->next;
registrar->next = entry;
if (id)
*id = (int)entry;
return 0;
}
int
registrar_remove (int id)
{
struct _registrar *current, *previous;
for (previous = registrar, current = registrar->next;
current != registrar;
previous = current, current = current->next)
{
if ((int)current == id)
{
previous->next = current->next;
if (current->is_allocated)
{
free_ureg (current->ureg);
free_mreg (current->mreg);
}
free (current);
return 0;;
}
}
return EINVAL;
}
int
registrar_get (int id,
struct url_registrar **ureg, struct mailbox_registrar **mreg)
{
struct _registrar *current;
for (current = registrar->next; current != registrar;
current = current->next)
{
if ((int)current == id)
{
if (mreg)
*mreg = current->mreg;
if (ureg)
*ureg = current->ureg;
return 0;
}
}
return EINVAL;
}
int
registrar_entry_count (size_t *num)
{
struct _registrar *current;
size_t count;
for (count = 0, current = registrar->next; current != registrar;
current = current->next, count++)
;
if (num)
*num = count;
return 0;
}
int
registrar_entry (size_t num, struct url_registrar **ureg,
struct mailbox_registrar **mreg, int *id)
{
struct _registrar *current;
size_t count, status;
for (status = ENOENT, count = 0, current = registrar->next;
current != registrar; current = current->next, count++)
{
if (num == count)
{
if (ureg)
*ureg = current->ureg;
if (mreg)
*mreg = current->mreg;
if (id)
*id = (int)current;
status = 0;
break;
}
}
return status;
}
int
registrar_list (struct url_registrar **ureg, struct mailbox_registrar **mreg,
int *id, registrar_t *reg)
{
struct _registrar *current;
if (reg == NULL)
return EINVAL;
current = *reg;
if (current == NULL)
current = registrar;
if (current->next == registrar)
return -1;
if (ureg)
*ureg = current->ureg;
if (mreg)
*mreg = current->mreg;
if (id)
*id = (int)current;
*reg = current->next;
return 0;
}