]> git.datanom.net - vcard-parser.git/blob - src/parser/vcard-parser.c
All features added
[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 Property get_property(const gchar* property) {
84 for (Property i = N; i < VCARD_PROPERTIES; i++) {
85 if (g_strcmp0(Properties[i], property) == 0) return i;
86 }
87
88 return VCARD_PROPERTIES;
89 }
90
91 static gboolean check_support(Property property, VCardVersion version) {
92 gboolean support = FALSE;
93
94 if (version == VCARD_VERSION_2_1) {
95 support = property <= TZ ? TRUE : FALSE;
96 } else if (version == VCARD_VERSION_3_0) {
97 support = property <= IMPP ? TRUE : FALSE;
98 } else if (version == VCARD_VERSION_4_0) {
99 support = property <= VCARD_PROPERTIES ? TRUE : FALSE;
100 }
101
102 return support;
103 }
104
105 static void remove_cr(gchar **str) {
106 gchar *src, *dst;
107
108 for (src = dst = *str; *src != '\0'; src++) {
109 *dst = *src;
110 if (*dst != '\r') dst++;
111 }
112 *dst = '\0';
113 }
114
115 static VCardParserResponse has_required_version(const gchar** buf, VCardVersion* version) {
116 const gchar** tmp = buf;
117 gboolean found = FALSE;
118 VCardParserResponse result = VCARD_PARSER_VERSION_MISSING;
119
120 while (*tmp && !found) {
121 if (!g_str_has_prefix(*tmp, "VERSION")) {
122 tmp++;
123 } else {
124 found = TRUE;
125 gchar** b = g_strsplit(*tmp, ":", 0);
126 if (g_strv_length(b) == 2) {
127 if (*version == VCARD_VERSION_DETECT) {
128 *version = str_2_vcard_version(b[1]);
129 result = VCARD_PARSER_OK;
130 } else {
131 if (*version == str_2_vcard_version(b[1])) {
132 result = VCARD_PARSER_OK;
133 } else
134 result = VCARD_PARSER_VERSION_MISMATCH;
135 }
136 }
137 g_strfreev(b);
138 }
139 }
140
141 return result;
142 }
143
144 static gchar** vcard_text_normalize(const gchar** text) {
145 gchar** new = NULL;
146 const gchar** tmp = text;
147 int count = 0;
148 gchar* str = NULL;
149
150 if (text == NULL) return NULL;
151
152 new = g_new0(gchar*, g_strv_length((gchar **) text) + 1);
153 while (*tmp && g_strcmp0(*tmp, "") != 0) {
154 if (*tmp[0] == ' ') {
155 // Multi line value
156 if (str) {
157 gchar* s = g_strdup(str);
158 g_free(str);
159 str = g_strconcat(s, "\n", *tmp+1, NULL);
160 } else
161 str = g_strdup(*tmp+1);
162 } else {
163 if (str) {
164 remove_cr(&str);
165 new[count++] = g_strdup(str);
166 g_free(str);
167 str = NULL;
168 }
169 str = g_strdup(*tmp);
170 }
171 tmp++;
172 }
173 if (str) {
174 remove_cr(&str);
175 new[count++] = g_strdup(str);
176 g_free(str);
177 }
178
179 return new;
180 }
181
182 static VCardParserResponse vcard_parse(gchar** text, VCardVersion version, GHashTable** vcard) {
183 VCardParserResponse r = VCARD_PARSER_ERROR;
184 guint l = g_strv_length(text);
185 gboolean support;
186
187 for (guint i = 0; i < l; i++) {
188 gchar** b = g_strsplit(text[i], ":", 2);
189 if (g_strv_length(b) != 2 || !b[0] || !b[1]) {
190 destroy_hash_table(*vcard);
191 *vcard = NULL;
192 g_strfreev(b);
193 return VCARD_PARSER_ERROR;
194 }
195 gchar** c = g_strsplit(b[0], ";", 0);
196 if (g_strcmp0(c[0], "BEGIN") == 0 ||
197 g_strcmp0(c[0], "VERSION") == 0 ||
198 g_strcmp0(c[0], "END") == 0) {
199 g_strfreev(c);
200 g_strfreev(b);
201 continue;
202 }
203 if (g_str_has_prefix(c[0], "X-")) {
204 support = TRUE;
205 } else {
206 Property property = get_property(c[0]);
207 support = check_support(property, version);
208 }
209 if (support) {
210 GString* val = g_string_new(NULL);
211 if (c[1]) {
212 val = g_string_append(val, c[1]);
213 guint l = g_strv_length(c);
214 if (l > 2) {
215 for (guint j = 2; j < l; j++) {
216 val = g_string_append(val, ";");
217 val = g_string_append(val, c[j]);
218 }
219 }
220 if (b[1]) {
221 val = g_string_append(val, ":");
222 val = g_string_append(val, b[1]);
223 }
224 } else {
225 val = g_string_append(val, b[1]);
226 }
227 GSList* value = g_hash_table_lookup(*vcard, c[0]);
228 VCardProperty* vp = g_new0(VCardProperty, 1);
229 vp->name = g_strdup(c[0]); //b[0]
230 vp->value = g_string_free_and_steal(val); //b[1]
231 value = g_slist_append(value, vp);
232 g_hash_table_insert(*vcard, g_strdup(c[0]), value);
233 } else {
234 destroy_hash_table(*vcard);
235 *vcard = NULL;
236 g_strfreev(c);
237 g_strfreev(b);
238 return VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH;
239 }
240 g_strfreev(c);
241 g_strfreev(b);
242 }
243 r = VCARD_PARSER_OK;
244
245 return r;
246 }
247
248 Property vcard_max_property(VCardVersion version) {
249 Property p = VCARD_PROPERTIES;
250
251 if (version == VCARD_VERSION_2_1) {
252 p = TZ;
253 } else if (version == VCARD_VERSION_3_0) {
254 p = IMPP;
255 } else if (version == VCARD_VERSION_4_0) {
256 p = VCARD_PROPERTIES - 1;
257 }
258
259 return p;
260 }
261
262 void destroy_hash_table(GHashTable* ht) {
263 g_hash_table_destroy(ht);
264 }
265
266 GHashTable* init_g_hash_table(VCardVersion version) {
267 GHashTable* ht = NULL;
268
269 Property property = vcard_max_property(version);
270 if (property < VCARD_PROPERTIES) {
271
272 ht = g_hash_table_new_full(
273 g_str_hash, g_str_equal, (GDestroyNotify) g_free,
274 (GDestroyNotify) vcard_slist_property_free);
275
276 for (Property i = N; i <= property; i++) {
277 g_hash_table_insert(ht, g_strdup(Properties[i]), NULL);
278 }
279 }
280
281 return ht;
282 }
283
284 VCardVersion str_2_vcard_version(const gchar* version) {
285 VCardVersion vv = VCARD_VERSION_DETECT;
286
287 if (version) {
288 if (g_strcmp0("2.1", version) == 0)
289 vv = VCARD_VERSION_2_1;
290 else if (g_strcmp0("3.0", version) == 0)
291 vv = VCARD_VERSION_3_0;
292 else if (g_strcmp0("4.0", version) == 0)
293 vv = VCARD_VERSION_4_0;
294 else
295 vv = VCARD_VERSION_UNSUPPORTED;
296 }
297
298 return vv;
299 }
300
301 gchar* vcard_version_2_str(VCardVersion version) {
302 gchar* vv = NULL;
303
304 if (VCARD_VERSION_2_1 == version)
305 vv = g_strdup("2.1");
306 else if (VCARD_VERSION_3_0 == version)
307 vv = g_strdup("3.0");
308 else if (VCARD_VERSION_4_0 == version)
309 vv = g_strdup("4.0");
310
311 return vv;
312 }
313
314 VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, GHashTable** vcard) {
315 VCardParserResponse r = VCARD_PARSER_ERROR;
316
317 if (text == NULL || strlen(text) < 1) return VCARD_PARSER_OK;
318
319 gchar** b = g_strsplit(text, "\n", 0);
320 gchar** buf = vcard_text_normalize((const gchar**)b);
321 g_strfreev(b);
322 guint l = g_strv_length(buf);
323 //begin, end and version line is required
324 r = VCARD_PARSER_BAD_FORMAT;
325 if (buf && l > 3) {
326 if (g_strcmp0(g_strchomp(buf[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf[l-1]), "END:VCARD") == 0) {
327 r = has_required_version((const gchar **)buf, version);
328 if (r == VCARD_PARSER_OK) {
329 *vcard =init_g_hash_table(*version);
330 if (vcard && *vcard) {
331 r = vcard_parse(buf, *version, vcard);
332 } else {
333 r = VCARD_PARSER_ERROR;
334 }
335 }
336 }
337 }
338 g_strfreev(buf);
339
340 return r;
341 }
This page took 0.761876 seconds and 6 git commands to generate.