Move vcard.c into libcitadel.
[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-2007 by Art Cancro
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
96 /**
97  * Constructor - returns a new struct vcard given a serialized vcard
98  */
99 struct vCard *vcard_load(char *vtext) {
100         struct vCard *v;
101         int valid = 0;
102         char *mycopy, *ptr;
103         char *namebuf, *valuebuf;
104         int i;
105         int colonpos, nlpos;
106
107         if (vtext == NULL) return vcard_new();
108         mycopy = strdup(vtext);
109         if (mycopy == NULL) return NULL;
110
111         /**
112          * First, fix this big pile o' vCard to make it more parseable.
113          * To make it easier to parse, we convert CRLF to LF, and unfold any
114          * multi-line fields into single lines.
115          */
116         for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
117                 if (!strncmp(&mycopy[i], "\r\n", 2)) {
118                         strcpy(&mycopy[i], &mycopy[i+1]);
119                 }
120                 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
121                         strcpy(&mycopy[i], &mycopy[i+1]);
122                 }
123         }
124
125         v = vcard_new();
126         if (v == NULL) return v;
127
128         ptr = mycopy;
129         while (!IsEmptyStr(ptr)) {
130                 colonpos = (-1);
131                 nlpos = (-1);
132                 colonpos = pattern2(ptr, ":");
133                 nlpos = pattern2(ptr, "\n");
134
135                 if ((nlpos > colonpos) && (colonpos > 0)) {
136                         namebuf = malloc(colonpos + 1);
137                         valuebuf = malloc(nlpos - colonpos + 1);
138                         strncpy(namebuf, ptr, colonpos);
139                         namebuf[colonpos] = 0;
140                         strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
141                         valuebuf[nlpos-colonpos-1] = 0;
142
143                         if (!strcasecmp(namebuf, "end")) {
144                                 valid = 0;
145                         }
146                         if (    (!strcasecmp(namebuf, "begin"))
147                                 && (!strcasecmp(valuebuf, "vcard"))
148                         ) {
149                                 valid = 1;
150                         }
151
152                         if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
153                                 remove_charset_attribute(namebuf);
154                                 ++v->numprops;
155                                 v->prop = realloc(v->prop,
156                                         (v->numprops * sizeof(struct vCardProp))
157                                 );
158                                 v->prop[v->numprops-1].name = namebuf;
159                                 v->prop[v->numprops-1].value = valuebuf;
160                         } 
161                         else {
162                                 free(namebuf);
163                                 free(valuebuf);
164                         }
165
166                 }
167
168                 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
169                         ++ptr;
170                 }
171                 if (*ptr == '\n') ++ptr;
172         }
173
174         free(mycopy);
175         return v;
176 }
177
178
179 /**
180  * Fetch the value of a particular key.
181  * If is_partial is set to 1, a partial match is ok (for example,
182  * a key of "tel;home" will satisfy a search for "tel").
183  * Set "instance" to a value higher than 0 to return subsequent instances
184  * of the same key.
185  *
186  * Set "get_propname" to nonzero to fetch the property name instead of value.
187  * v            vCard to get keyvalue from
188  * propname     key to retrieve
189  * is_partial
190  * instance     if nonzero return a later token of the value
191  * get_propname if nonzero get the real property name???
192  *
193  * returns the requested value / token / propertyname
194  */
195 char *vcard_get_prop(struct vCard *v, char *propname,
196                         int is_partial, int instance, int get_propname) {
197         int i;
198         int found_instance = 0;
199
200         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
201                 if ( (!strcasecmp(v->prop[i].name, propname))
202                    || (propname[0] == 0)
203                    || (  (!strncasecmp(v->prop[i].name,
204                                         propname, strlen(propname)))
205                          && (v->prop[i].name[strlen(propname)] == ';')
206                          && (is_partial) ) ) {
207                         if (instance == found_instance++) {
208                                 if (get_propname) {
209                                         return(v->prop[i].name);
210                                 }
211                                 else {
212                                         return(v->prop[i].value);
213                                 }
214                         }
215                 }
216         }
217
218         return NULL;
219 }
220
221
222
223
224 /*
225  * Destructor
226  */
227 void vcard_free(struct vCard *v) {
228         int i;
229         
230         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
231         
232         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
233                 free(v->prop[i].name);
234                 free(v->prop[i].value);
235         }
236
237         if (v->prop != NULL) free(v->prop);
238         
239         memset(v, 0, sizeof(struct vCard));
240         free(v);
241 }
242
243
244
245
246 /*
247  * Set a name/value pair in the card
248  * v            vCard to manipulate
249  * name         key to set
250  * value        the value to assign to key
251  * append       if nonzero, append rather than replace if this key already exists.
252  */
253 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
254         int i;
255
256         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
257
258         /** If this key is already present, replace it */
259         if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
260                 if (!strcasecmp(v->prop[i].name, name)) {
261                         free(v->prop[i].name);
262                         free(v->prop[i].value);
263                         v->prop[i].name = strdup(name);
264                         v->prop[i].value = strdup(value);
265                         return;
266                 }
267         }
268
269         /* Otherwise, append it */
270         ++v->numprops;
271         v->prop = realloc(v->prop,
272                 (v->numprops * sizeof(struct vCardProp)) );
273         v->prop[v->numprops-1].name = strdup(name);
274         v->prop[v->numprops-1].value = strdup(value);
275 }
276
277
278
279
280 /*
281  * Serialize a 'struct vcard' into an actual vcard.
282  */
283 char *vcard_serialize(struct vCard *v)
284 {
285         char *ser;
286         int i, j;
287         size_t len;
288         int is_utf8 = 0;
289
290         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /* self check */
291
292         /** Figure out how big a buffer we need to allocate */
293         len = 64;       /* for begin, end, and a little padding for safety */
294         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
295                 len = len +
296                         strlen(v->prop[i].name) +
297                         strlen(v->prop[i].value) + 16;
298         }
299
300         ser = malloc(len);
301         if (ser == NULL) return NULL;
302
303         safestrncpy(ser, "begin:vcard\r\n", len);
304         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
305                 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
306                         is_utf8 = 0;
307                         for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
308                                 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
309                                         is_utf8 = 1;
310                                 }
311                         }
312                         strcat(ser, v->prop[i].name);
313                         if (is_utf8) {
314                                 strcat(ser, ";charset=UTF-8");
315                         }
316                         strcat(ser, ":");
317                         strcat(ser, v->prop[i].value);
318                         strcat(ser, "\r\n");
319                 }
320         }
321         strcat(ser, "end:vcard\r\n");
322
323         return ser;
324 }
325
326
327
328 /*
329  * Convert FN (Friendly Name) into N (Name)
330  *
331  * vname        Supplied friendly-name
332  * n            Target buffer to store Name
333  * vname_size   Size of buffer
334  */
335 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
336         char lastname[256];
337         char firstname[256];
338         char middlename[256];
339         char honorific_prefixes[256];
340         char honorific_suffixes[256];
341         char buf[256];
342
343         safestrncpy(buf, n, sizeof buf);
344
345         /* Try to intelligently convert the screen name to a
346          * fully expanded vCard name based on the number of
347          * words in the name
348          */
349         safestrncpy(lastname, "", sizeof lastname);
350         safestrncpy(firstname, "", sizeof firstname);
351         safestrncpy(middlename, "", sizeof middlename);
352         safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
353         safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
354
355         /* Honorific suffixes */
356         if (num_tokens(buf, ',') > 1) {
357                 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
358                         sizeof honorific_suffixes);
359                 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
360         }
361
362         /* Find a last name */
363         extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
364         remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
365
366         /* Find honorific prefixes */
367         if (num_tokens(buf, ' ') > 2) {
368                 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
369                 remove_token(buf, 0, ' ');
370         }
371
372         /* Find a middle name */
373         if (num_tokens(buf, ' ') > 1) {
374                 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
375                 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
376         }
377
378         /* Anything left is probably the first name */
379         safestrncpy(firstname, buf, sizeof firstname);
380         striplt(firstname);
381
382         /* Compose the structured name */
383         snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
384                 honorific_prefixes, honorific_suffixes);
385 }
386
387
388
389