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