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