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
|
/*
* Copyright (C) 2000 Helix Code Inc.
*
* Authors: Michael Zucchi <notzed@helixcode.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, 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.
*/
#include "camel-mime-utils.h"
#include "camel-internet-address.h"
static int internet_decode (CamelAddress *, const char *raw);
static char * internet_encode (CamelAddress *);
static void internet_remove (CamelAddress *, int index);
static void camel_internet_address_class_init (CamelInternetAddressClass *klass);
static void camel_internet_address_init (CamelInternetAddress *obj);
static CamelAddressClass *camel_internet_address_parent;
struct _address {
char *name;
char *address;
};
static void
camel_internet_address_class_init(CamelInternetAddressClass *klass)
{
CamelAddressClass *address = (CamelAddressClass *) klass;
camel_internet_address_parent = CAMEL_ADDRESS_CLASS(camel_type_get_global_classfuncs(camel_address_get_type()));
address->decode = internet_decode;
address->encode = internet_encode;
address->remove = internet_remove;
}
static void
camel_internet_address_init(CamelInternetAddress *obj)
{
}
CamelType
camel_internet_address_get_type(void)
{
static CamelType type = CAMEL_INVALID_TYPE;
if (type == CAMEL_INVALID_TYPE) {
type = camel_type_register(camel_address_get_type(), "CamelInternetAddress",
sizeof (CamelInternetAddress),
sizeof (CamelInternetAddressClass),
(CamelObjectClassInitFunc) camel_internet_address_class_init,
NULL,
(CamelObjectInitFunc) camel_internet_address_init,
NULL);
}
return type;
}
static int
internet_decode (CamelAddress *a, const char *raw)
{
struct _header_address *ha, *n;
/* Should probably use its own decoder or something */
ha = header_address_decode(raw);
if (ha) {
n = ha;
while (n) {
if (n->type == HEADER_ADDRESS_NAME) {
camel_internet_address_add((CamelInternetAddress *)a, n->name, n->v.addr);
} else if (n->type == HEADER_ADDRESS_GROUP) {
struct _header_address *g = n->v.members;
while (g) {
if (g->type == HEADER_ADDRESS_NAME)
camel_internet_address_add((CamelInternetAddress *)a, g->name, g->v.addr);
/* otherwise, its an error, infact */
g = g->next;
}
}
n = n->next;
}
header_address_list_clear(&ha);
}
return 0;
}
static char *
internet_encode (CamelAddress *a)
{
int i;
GString *out;
char *ret;
if (a->addresses->len == 0)
return NULL;
out = g_string_new("");
for (i = 0;i < a->addresses->len; i++) {
struct _address *addr = g_ptr_array_index(a->addresses, i);
char *name = header_encode_phrase(addr->name);
if (i != 0)
g_string_append(out, ", ");
if (name) {
if (*name) {
g_string_sprintfa(out, "%s <%s>", name, addr->address);
} else if (addr->address)
g_string_sprintfa(out, "%s", addr->address);
g_free(name);
} else
g_string_sprintfa(out, "%s", addr->address);
}
ret = out->str;
g_string_free(out, FALSE);
return ret;
}
static void
internet_remove (CamelAddress *a, int index)
{
struct _address *addr;
if (index < 0 || index >= a->addresses->len)
return;
addr = g_ptr_array_index(a->addresses, index);
g_free(addr->name);
g_free(addr->address);
g_free(addr);
g_ptr_array_remove_index(a->addresses, index);
}
/**
* camel_internet_address_new:
*
* Create a new CamelInternetAddress object.
*
* Return value: A new CamelInternetAddress object.
**/
CamelInternetAddress *
camel_internet_address_new (void)
{
CamelInternetAddress *new = CAMEL_INTERNET_ADDRESS(camel_object_new(camel_internet_address_get_type()));
return new;
}
/**
* camel_internet_address_add:
* @a:
* @name:
* @address:
*
* Add a new internet address to the address object.
*
* Return value: Index of added entry.
**/
int
camel_internet_address_add (CamelInternetAddress *a, const char *name, const char *address)
{
struct _address *new;
int index;
g_assert(IS_CAMEL_INTERNET_ADDRESS(a));
new = g_malloc(sizeof(*new));
new->name = g_strdup(name);
new->address = g_strdup(address);
index = ((CamelAddress *)a)->addresses->len;
g_ptr_array_add(((CamelAddress *)a)->addresses, new);
return index;
}
/**
* camel_internet_address_get:
* @a:
* @index:
* @namep: Holder for the returned name, or NULL, if not required.
* @addressp: Holder for the returned address, or NULL, if not required.
*
* Get the address at @index.
*
* Return value: TRUE if such an address exists, or FALSE otherwise.
**/
gboolean
camel_internet_address_get (const CamelInternetAddress *a, int index, const char **namep, const char **addressp)
{
struct _address *addr;
g_assert(IS_CAMEL_INTERNET_ADDRESS(a));
g_return_val_if_fail(index >= 0, -1);
if (index >= ((CamelAddress *)a)->addresses->len)
return FALSE;
addr = g_ptr_array_index( ((CamelAddress *)a)->addresses, index);
if (namep)
*namep = addr->name;
if (addressp)
*addressp = addr->address;
return TRUE;
}
/**
* camel_internet_address_find_name:
* @a:
* @name:
* @addressp: Holder for address part, or NULL, if not required.
*
* Find address by real name.
*
* Return value: The index of the address matching the name, or -1
* if no match was found.
**/
int
camel_internet_address_find_name(CamelInternetAddress *a, const char *name, const char **addressp)
{
struct _address *addr;
int i, len;
g_assert(IS_CAMEL_INTERNET_ADDRESS(a));
len = ((CamelAddress *)a)->addresses->len;
for (i=0;i<len;i++) {
addr = g_ptr_array_index( ((CamelAddress *)a)->addresses, i );
if (!strcmp(addr->name, name)) {
if (addressp)
*addressp = addr->address;
return i;
}
}
return -1;
}
/**
* camel_internet_address_find_address:
* @a:
* @address:
* @namep: Return for the matching name, or NULL, if not required.
*
* Find an address by address.
*
* Return value: The index of the address, or -1 if not found.
**/
int
camel_internet_address_find_address(CamelInternetAddress *a, const char *address, const char **namep)
{
struct _address *addr;
int i, len;
g_assert(IS_CAMEL_INTERNET_ADDRESS(a));
len = ((CamelAddress *)a)->addresses->len;
for (i=0;i<len;i++) {
addr = g_ptr_array_index( ((CamelAddress *)a)->addresses, i );
if (!strcmp(addr->address, address)) {
if (namep)
*namep = addr->name;
return i;
}
}
return -1;
}
|