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