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