]> git.datanom.net - vcard-parser.git/blob - src/parser/vcard-parser.c
Begin adding support for carddav server
[vcard-parser.git] / src / parser / vcard-parser.c
1 /*
2 * vcard-parser.c
3 *
4 * Copyright 2019 Michael Rasmussen <mir@datanom.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include <vcard-parser.h>
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 static gchar* Properties[VCARD_PROPERTIES+1] = {
29 "N",
30 "FN",
31 "PHOTO",
32 "BDAY",
33 "ADR",
34 "LABEL",
35 "TEL",
36 "EMAIL",
37 "MAILER",
38 "GEO",
39 "TITLE",
40 "ROLE",
41 "LOGO",
42 "ORG",
43 "NOTE",
44 "REV",
45 "SOUND",
46 "URL",
47 "UID",
48 "VERSION",
49 "KEY",
50 "TZ",
51 "SOURCE",
52 "AGENT",
53 "PROFILE",
54 "CATEGORIES",
55 "SORT-STRING",
56 "PRODID",
57 "NICKNAME",
58 "NAME",
59 "CLASS",
60 "FBURL",
61 "CAPURI",
62 "CALURI",
63 "CALADRURI",
64 "IMPP",
65 "XML",
66 "ANNIVERSARY",
67 "CLIENTPIDMAP",
68 "LANG",
69 "GENDER",
70 "KIND",
71 "MEMBER",
72 "RELATED",
73 "BIRTHPLACE",
74 "DEATHPLACE",
75 "DEATHDATE",
76 "EXPERTISE",
77 "HOBBY",
78 "INTEREST",
79 "ORG-DIRECTORY",
80 NULL
81 };
82
83 static void vcard_property_free(gpointer data) {
84 if (!data) return;
85 VCardProperty* vp = (VCardProperty*) data;
86 g_free(vp->name);
87 g_free(vp->value);
88 g_free(vp);
89 }
90
91 static Property get_property(const gchar* property) {
92 for (Property i = N; i < VCARD_PROPERTIES; i++) {
93 if (g_strcmp0(Properties[i], property) == 0) return i;
94 }
95
96 return VCARD_PROPERTIES;
97 }
98
99 static gboolean check_support(Property property, VCardVersion version) {
100 gboolean support = FALSE;
101
102 if (version == VCARD_VERSION_2_1) {
103 support = property <= TZ ? TRUE : FALSE;
104 } else if (version == VCARD_VERSION_3_0) {
105 support = property <= IMPP ? TRUE : FALSE;
106 } else if (version == VCARD_VERSION_4_0) {
107 support = property <= VCARD_PROPERTIES ? TRUE : FALSE;
108 }
109
110 return support;
111 }
112
113 static void remove_cr(gchar **str) {
114 gchar *src, *dst;
115
116 for (src = dst = *str; *src != '\0'; src++) {
117 *dst = *src;
118 if (*dst != '\r') dst++;
119 }
120 *dst = '\0';
121 }
122
123 static VCardParserResponse has_required_version(const gchar** buf, VCardVersion* version) {
124 const gchar** tmp = buf;
125 gboolean found = FALSE;
126 VCardParserResponse result = VCARD_PARSER_VERSION_MISSING;
127
128 while (*tmp && !found) {
129 if (!g_str_has_prefix(*tmp, "VERSION")) {
130 tmp++;
131 } else {
132 found = TRUE;
133 gchar** b = g_strsplit(*tmp, ":", 0);
134 if (g_strv_length(b) == 2) {
135 if (*version == VCARD_VERSION_DETECT) {
136 *version = str_2_vcard_version(b[1]);
137 result = VCARD_PARSER_OK;
138 } else {
139 if (*version == str_2_vcard_version(b[1])) {
140 result = VCARD_PARSER_OK;
141 } else
142 result = VCARD_PARSER_VERSION_MISMATCH;
143 }
144 }
145 g_strfreev(b);
146 }
147 }
148
149 return result;
150 }
151
152 static gchar** vcard_text_normalize(const gchar** text) {
153 gchar** new = NULL;
154 const gchar** tmp = text;
155 int count = 0;
156 gchar* str = NULL;
157
158 if (text == NULL) return NULL;
159
160 new = g_new0(gchar*, g_strv_length((gchar **) text) + 1);
161 while (*tmp && g_strcmp0(*tmp, "") != 0) {
162 if (*tmp[0] == ' ') {
163 // Multi line value
164 if (str) {
165 gchar* s = g_strdup(str);
166 g_free(str);
167 str = g_strconcat(s, "\n", *tmp+1, NULL);
168 } else
169 str = g_strdup(*tmp+1);
170 } else {
171 if (str) {
172 remove_cr(&str);
173 new[count++] = g_strdup(str);
174 g_free(str);
175 str = NULL;
176 }
177 str = g_strdup(*tmp);
178 }
179 tmp++;
180 }
181 if (str) {
182 remove_cr(&str);
183 new[count++] = g_strdup(str);
184 g_free(str);
185 }
186
187 return new;
188 }
189
190 static VCardParserResponse vcard_parse(gchar** text, VCardVersion version, GHashTable** vcard) {
191 VCardParserResponse r = VCARD_PARSER_ERROR;
192 guint l = g_strv_length(text);
193 gboolean support;
194
195 for (guint i = 0; i < l; i++) {
196 gchar** b = g_strsplit(text[i], ":", 2);
197 if (g_strv_length(b) != 2 || !b[0] || !b[1]) {
198 destroy_hash_table(*vcard);
199 *vcard = NULL;
200 g_strfreev(b);
201 return VCARD_PARSER_ERROR;
202 }
203 gchar** c = g_strsplit(b[0], ";", 0);
204 if (g_strcmp0(c[0], "BEGIN") == 0 ||
205 g_strcmp0(c[0], "VERSION") == 0 ||
206 g_strcmp0(c[0], "END") == 0) {
207 g_strfreev(c);
208 g_strfreev(b);
209 continue;
210 }
211 if (g_str_has_prefix(c[0], "X-")) {
212 support = TRUE;
213 } else {
214 Property property = get_property(c[0]);
215 support = check_support(property, version);
216 }
217 if (support) {
218 GSList* value = g_hash_table_lookup(*vcard, c[0]);
219 VCardProperty* vp = g_new(VCardProperty, 1);
220 vp->name = g_strdup(b[0]);
221 vp->value = g_strdup(b[1]);
222 value = g_slist_append(value, vp);
223 g_hash_table_insert(*vcard, g_strdup(c[0]), value);
224 } else {
225 destroy_hash_table(*vcard);
226 *vcard = NULL;
227 g_strfreev(c);
228 g_strfreev(b);
229 return VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH;
230 }
231 g_strfreev(c);
232 g_strfreev(b);
233 }
234 r = VCARD_PARSER_OK;
235
236 return r;
237 }
238
239 Property vcard_max_property(VCardVersion version) {
240 Property p = VCARD_PROPERTIES;
241
242 if (version == VCARD_VERSION_2_1) {
243 p = TZ;
244 } else if (version == VCARD_VERSION_3_0) {
245 p = IMPP;
246 } else if (version == VCARD_VERSION_4_0) {
247 p = VCARD_PROPERTIES - 1;
248 }
249
250 return p;
251 }
252
253 void destroy_hash_table(GHashTable* ht) {
254 GHashTableIter iter;
255 gpointer key, value;
256
257 g_hash_table_iter_init (&iter, ht);
258 while (g_hash_table_iter_next(&iter, &key, &value)) {
259 if (value)
260 g_slist_free_full((GSList *) value, vcard_property_free);
261 }
262
263 g_hash_table_destroy(ht);
264 }
265
266 void init_g_hash_table(GHashTable** ht, VCardVersion version) {
267 if (!ht) return;
268
269 Property property = vcard_max_property(version);
270 if (property < VCARD_PROPERTIES) {
271 *ht = g_hash_table_new_full(
272 g_str_hash, g_str_equal, (GDestroyNotify) g_free, NULL);
273 for (Property i = N; i <= property; i++) {
274 g_hash_table_insert(*ht, g_strdup(Properties[i]), NULL);
275 }
276 }
277 }
278
279 VCardVersion str_2_vcard_version(const gchar* version) {
280 VCardVersion vv = VCARD_VERSION_DETECT;
281
282 if (version) {
283 if (g_strcmp0("2.1", version) == 0)
284 vv = VCARD_VERSION_2_1;
285 else if (g_strcmp0("3.0", version) == 0)
286 vv = VCARD_VERSION_3_0;
287 else if (g_strcmp0("4.0", version) == 0)
288 vv = VCARD_VERSION_4_0;
289 else
290 vv = VCARD_VERSION_UNSUPPORTED;
291 }
292
293 return vv;
294 }
295
296 gchar* vcard_version_2_str(VCardVersion version) {
297 gchar* vv = NULL;
298
299 if (VCARD_VERSION_2_1 == version)
300 vv = g_strdup("2.1");
301 else if (VCARD_VERSION_3_0 == version)
302 vv = g_strdup("3.0");
303 else if (VCARD_VERSION_4_0 == version)
304 vv = g_strdup("4.0");
305
306 return vv;
307 }
308
309 VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, GHashTable** vcard) {
310 VCardParserResponse r = VCARD_PARSER_ERROR;
311
312 if (text == NULL || strlen(text) < 1) return VCARD_PARSER_OK;
313
314 gchar** b = g_strsplit(text, "\n", 0);
315 gchar** buf = vcard_text_normalize((const gchar**)b);
316 g_strfreev(b);
317 guint l = g_strv_length(buf);
318 //begin, end and version line is required
319 r = VCARD_PARSER_BAD_FORMAT;
320 if (buf && l > 3) {
321 if (g_strcmp0(g_strchomp(buf[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf[l-1]), "END:VCARD") == 0) {
322 r = has_required_version((const gchar **)buf, version);
323 if (r == VCARD_PARSER_OK) {
324 init_g_hash_table(vcard, *version);
325 if (vcard && *vcard) {
326 r = vcard_parse(buf, *version, vcard);
327 } else {
328 r = VCARD_PARSER_ERROR;
329 }
330 }
331 }
332 }
333 g_strfreev(buf);
334
335 return r;
336 }
This page took 0.537844 seconds and 6 git commands to generate.