5223da6b7c95a6b599808ce7d9cfeb7a0ac6d560
[citadel.git] / libcitadel / lib / vcard.c
1 /*
2  * vCard implementation for Citadel
3  *
4  * Copyright (C) 1999-2008 by the citadel.org development team.
5  * This code is freely redistributable under the terms of the GNU General
6  * Public License.  All other rights reserved.
7  */
8
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # if HAVE_SYS_TIME_H
21 #  include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <libcitadel.h>
33
34
35 /* 
36  * Constructor (empty vCard)
37  * Returns an empty vcard
38  */
39 struct vCard *vcard_new() {
40         struct vCard *v;
41
42         v = (struct vCard *) malloc(sizeof(struct vCard));
43         if (v == NULL) return v;
44
45         v->magic = CTDL_VCARD_MAGIC;
46         v->numprops = 0;
47         v->prop = NULL;
48
49         return v;
50 }
51
52 /*
53  * Remove the "charset=" attribute from a vCard property name
54  *
55  */
56 void remove_charset_attribute(char *strbuf)
57 {
58         int i, t;
59         char compare[256];
60
61         t = num_tokens(strbuf, ';');
62         for (i=0; i<t; ++i) {
63                 extract_token(compare, strbuf, i, ';', sizeof compare);
64                 striplt(compare);
65                 if (!strncasecmp(compare, "charset=", 8)) {
66                         remove_token(strbuf, i, ';');
67                 }
68         }
69         if (!IsEmptyStr(strbuf)) {
70                 if (strbuf[strlen(strbuf)-1] == ';') {
71                         strbuf[strlen(strbuf)-1] = 0;
72                 }
73         }
74 }
75
76
77 /*
78  * Add a property to a vCard
79  *
80  * v            vCard structure to which we are adding
81  * propname     name of new property
82  * propvalue    value of new property
83  */
84 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
85         ++v->numprops;
86         v->prop = realloc(v->prop,
87                 (v->numprops * sizeof(struct vCardProp)) );
88         v->prop[v->numprops-1].name = strdup(propname);
89         v->prop[v->numprops-1].value = strdup(propvalue);
90 }
91
92 /*
93  * Constructor - returns a new struct vcard given a serialized vcard
94  */
95 struct vCard *VCardLoad(StrBuf *vbtext) {
96         return vcard_load((char*)ChrPtr(vbtext));
97 }
98
99 /*
100  * Constructor - returns a new struct vcard given a serialized vcard
101  */
102 struct vCard *vcard_load(char *vtext) {
103         struct vCard *v;
104         int valid = 0;
105         char *mycopy, *ptr;
106         char *namebuf, *valuebuf;
107         int i;
108         int colonpos, nlpos;
109
110         if (vtext == NULL) return vcard_new();
111         mycopy = strdup(vtext);
112         if (mycopy == NULL) return NULL;
113
114         /*
115          * First, fix this big pile o' vCard to make it more parseable.
116          * To make it easier to parse, we convert CRLF to LF, and unfold any
117          * multi-line fields into single lines.
118          */
119         for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
120                 if (!strncmp(&mycopy[i], "\r\n", 2)) {
121                         strcpy(&mycopy[i], &mycopy[i+1]);
122                 }
123                 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
124                         strcpy(&mycopy[i], &mycopy[i+1]);
125                 }
126         }
127
128         v = vcard_new();
129         if (v == NULL) return v;
130
131         ptr = mycopy;
132         while (!IsEmptyStr(ptr)) {
133                 colonpos = (-1);
134                 nlpos = (-1);
135                 colonpos = pattern2(ptr, ":");
136                 nlpos = pattern2(ptr, "\n");
137
138                 if ((nlpos > colonpos) && (colonpos > 0)) {
139                         namebuf = malloc(colonpos + 1);
140                         valuebuf = malloc(nlpos - colonpos + 1);
141                         memcpy(namebuf, ptr, colonpos);
142                         namebuf[colonpos] = '\0';
143                         memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
144                         valuebuf[nlpos-colonpos-1] = '\0';
145
146                         if (!strcasecmp(namebuf, "end")) {
147                                 valid = 0;
148                         }
149                         if (    (!strcasecmp(namebuf, "begin"))
150                                 && (!strcasecmp(valuebuf, "vcard"))
151                         ) {
152                                 valid = 1;
153                         }
154
155                         if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
156                                 remove_charset_attribute(namebuf);
157                                 ++v->numprops;
158                                 v->prop = realloc(v->prop,
159                                         (v->numprops * sizeof(struct vCardProp))
160                                 );
161                                 v->prop[v->numprops-1].name = namebuf;
162                                 v->prop[v->numprops-1].value = valuebuf;
163                         } 
164                         else {
165                                 free(namebuf);
166                                 free(valuebuf);
167                         }
168
169                 }
170
171                 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
172                         ++ptr;
173                 }
174                 if (*ptr == '\n') ++ptr;
175         }
176
177         free(mycopy);
178         return v;
179 }
180
181
182 /*
183  * Fetch the value of a particular key.
184  * If is_partial is set to 1, a partial match is ok (for example,
185  * a key of "tel;home" will satisfy a search for "tel").
186  * Set "instance" to a value higher than 0 to return subsequent instances
187  * of the same key.
188  *
189  * Set "get_propname" to nonzero to fetch the property name instead of value.
190  * v            vCard to get keyvalue from
191  * propname     key to retrieve
192  * is_partial
193  * instance     if nonzero return a later token of the value
194  * get_propname if nonzero get the real property name???
195  *
196  * returns the requested value / token / propertyname
197  */
198 char *vcard_get_prop(struct vCard *v, char *propname,
199                         int is_partial, int instance, int get_propname) {
200         int i;
201         int found_instance = 0;
202
203         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
204                 if ( (!strcasecmp(v->prop[i].name, propname))
205                    || (propname[0] == 0)
206                    || (  (!strncasecmp(v->prop[i].name,
207                                         propname, strlen(propname)))
208                          && (v->prop[i].name[strlen(propname)] == ';')
209                          && (is_partial) ) ) {
210                         if (instance == found_instance++) {
211                                 if (get_propname) {
212                                         return(v->prop[i].name);
213                                 }
214                                 else {
215                                         return(v->prop[i].value);
216                                 }
217                         }
218                 }
219         }
220
221         return NULL;
222 }
223
224
225
226
227 /*
228  * Destructor
229  */
230 void vcard_free(struct vCard *v) {
231         int i;
232         
233         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
234         
235         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
236                 free(v->prop[i].name);
237                 free(v->prop[i].value);
238         }
239
240         if (v->prop != NULL) free(v->prop);
241         
242         memset(v, 0, sizeof(struct vCard));
243         free(v);
244 }
245
246
247
248
249 /*
250  * Set a name/value pair in the card
251  * v            vCard to manipulate
252  * name         key to set
253  * value        the value to assign to key
254  * append       if nonzero, append rather than replace if this key already exists.
255  */
256 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
257         int i;
258
259         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
260
261         /* If this key is already present, replace it */
262         if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
263                 if (!strcasecmp(v->prop[i].name, name)) {
264                         free(v->prop[i].name);
265                         free(v->prop[i].value);
266                         v->prop[i].name = strdup(name);
267                         v->prop[i].value = strdup(value);
268                         return;
269                 }
270         }
271
272         /* Otherwise, append it */
273         ++v->numprops;
274         v->prop = realloc(v->prop,
275                 (v->numprops * sizeof(struct vCardProp)) );
276         v->prop[v->numprops-1].name = strdup(name);
277         v->prop[v->numprops-1].value = strdup(value);
278 }
279
280
281
282
283 /*
284  * Serialize a 'struct vcard' into an actual vcard.
285  */
286 char *vcard_serialize(struct vCard *v)
287 {
288         char *ser;
289         int i, j;
290         size_t len;
291         int is_utf8 = 0;
292
293         if (v == NULL) return NULL;                     /* self check */
294         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /* self check */
295
296         /* Set the vCard version number to 2.1 at this time. */
297         vcard_set_prop(v, "VERSION", "2.1", 0);
298
299         /* Figure out how big a buffer we need to allocate */
300         len = 64;       /* for begin, end, and a little padding for safety */
301         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
302                 len = len +
303                         strlen(v->prop[i].name) +
304                         strlen(v->prop[i].value) + 16;
305         }
306
307         ser = malloc(len);
308         if (ser == NULL) return NULL;
309
310         safestrncpy(ser, "begin:vcard\r\n", len);
311         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
312                 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
313                         is_utf8 = 0;
314                         for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
315                                 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
316                                         is_utf8 = 1;
317                                 }
318                         }
319                         strcat(ser, v->prop[i].name);
320                         if (is_utf8) {
321                                 strcat(ser, ";charset=UTF-8");
322                         }
323                         strcat(ser, ":");
324                         strcat(ser, v->prop[i].value);
325                         strcat(ser, "\r\n");
326                 }
327         }
328         strcat(ser, "end:vcard\r\n");
329
330         return ser;
331 }
332
333
334
335 /*
336  * Convert FN (Friendly Name) into N (Name)
337  *
338  * vname        Supplied friendly-name
339  * n            Target buffer to store Name
340  * vname_size   Size of buffer
341  */
342 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
343         char lastname[256];
344         char firstname[256];
345         char middlename[256];
346         char honorific_prefixes[256];
347         char honorific_suffixes[256];
348         char buf[256];
349
350         safestrncpy(buf, n, sizeof buf);
351
352         /* Try to intelligently convert the screen name to a
353          * fully expanded vCard name based on the number of
354          * words in the name
355          */
356         safestrncpy(lastname, "", sizeof lastname);
357         safestrncpy(firstname, "", sizeof firstname);
358         safestrncpy(middlename, "", sizeof middlename);
359         safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
360         safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
361
362         /* Honorific suffixes */
363         if (num_tokens(buf, ',') > 1) {
364                 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
365                         sizeof honorific_suffixes);
366                 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
367         }
368
369         /* Find a last name */
370         extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
371         remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
372
373         /* Find honorific prefixes */
374         if (num_tokens(buf, ' ') > 2) {
375                 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
376                 remove_token(buf, 0, ' ');
377         }
378
379         /* Find a middle name */
380         if (num_tokens(buf, ' ') > 1) {
381                 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
382                 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
383         }
384
385         /* Anything left is probably the first name */
386         safestrncpy(firstname, buf, sizeof firstname);
387         striplt(firstname);
388
389         /* Compose the structured name */
390         snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
391                 honorific_prefixes, honorific_suffixes);
392 }
393
394
395
396