]> code.citadel.org Git - citadel.git/blob - citadel/vcard.c
Brought over some vcard.c changes from WebCit
[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  * \brief Constructor (empty vCard)
44  * \return an empty vcard
45  */
46 struct vCard *vcard_new() {
47         struct vCard *v;
48
49         v = (struct vCard *) malloc(sizeof(struct vCard));
50         if (v == NULL) return v;
51
52         v->magic = CTDL_VCARD_MAGIC;
53         v->numprops = 0;
54         v->prop = NULL;
55
56         return v;
57 }
58
59 /**
60  * \brief       Remove the "charset=" attribute from a vCard property name
61  *
62  * \param       strbuf          The property name string to be stripped
63  */
64 void remove_charset_attribute(char *strbuf)
65 {
66         int i, t;
67         char compare[256];
68
69         t = num_tokens(strbuf, ';');
70         for (i=0; i<t; ++i) {
71                 extract_token(compare, strbuf, i, ';', sizeof compare);
72                 striplt(compare);
73                 if (!strncasecmp(compare, "charset=", 8)) {
74                         remove_token(strbuf, i, ';');
75                 }
76         }
77         if (strlen(strbuf) > 0) {
78                 if (strbuf[strlen(strbuf)-1] == ';') {
79                         strbuf[strlen(strbuf)-1] = 0;
80                 }
81         }
82 }
83
84
85 /*
86  * \brief       Add a property to a vCard
87  *
88  * \param       v               vCard structure to which we are adding
89  * \param       propname        name of new property
90  * \param       propvalue       value of new property
91  */
92 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
93         ++v->numprops;
94         v->prop = realloc(v->prop,
95                 (v->numprops * sizeof(struct vCardProp)) );
96         v->prop[v->numprops-1].name = strdup(propname);
97         v->prop[v->numprops-1].value = strdup(propvalue);
98 }
99
100
101
102 /**
103  * \brief Constructor (supply serialized vCard)
104  * \param vtext the text to parse into the new vcard
105  * \return the parsed VCard
106  */
107 struct vCard *vcard_load(char *vtext) {
108         struct vCard *v;
109         int valid = 0;
110         char *mycopy, *ptr;
111         char *namebuf, *valuebuf;
112         int i;
113         int colonpos, nlpos;
114
115         if (vtext == NULL) return vcard_new();
116         mycopy = strdup(vtext);
117         if (mycopy == NULL) return NULL;
118
119         /**
120          * First, fix this big pile o' vCard to make it more parseable.
121          * To make it easier to parse, we convert CRLF to LF, and unfold any
122          * multi-line fields into single lines.
123          */
124         for (i=0; i<strlen(mycopy); ++i) {
125                 if (!strncmp(&mycopy[i], "\r\n", 2)) {
126                         strcpy(&mycopy[i], &mycopy[i+1]);
127                 }
128                 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
129                         strcpy(&mycopy[i], &mycopy[i+1]);
130                 }
131         }
132
133         v = vcard_new();
134         if (v == NULL) return v;
135
136         ptr = mycopy;
137         while (strlen(ptr)>0) {
138                 colonpos = (-1);
139                 nlpos = (-1);
140                 colonpos = pattern2(ptr, ":");
141                 nlpos = pattern2(ptr, "\n");
142
143                 if ((nlpos > colonpos) && (colonpos > 0)) {
144                         namebuf = malloc(colonpos + 1);
145                         valuebuf = malloc(nlpos - colonpos + 1);
146                         strncpy(namebuf, ptr, colonpos);
147                         namebuf[colonpos] = 0;
148                         strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
149                         valuebuf[nlpos-colonpos-1] = 0;
150
151                         if (!strcasecmp(namebuf, "end")) {
152                                 valid = 0;
153                         }
154                         if (    (!strcasecmp(namebuf, "begin"))
155                                 && (!strcasecmp(valuebuf, "vcard"))
156                         ) {
157                                 valid = 1;
158                         }
159
160                         if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
161                                 remove_charset_attribute(namebuf);
162                                 ++v->numprops;
163                                 v->prop = realloc(v->prop,
164                                         (v->numprops * sizeof(struct vCardProp))
165                                 );
166                                 v->prop[v->numprops-1].name = namebuf;
167                                 v->prop[v->numprops-1].value = valuebuf;
168                         } 
169                         else {
170                                 free(namebuf);
171                                 free(valuebuf);
172                         }
173
174                 }
175
176                 while ( (*ptr != '\n') && (strlen(ptr)>0) ) {
177                         ++ptr;
178                 }
179                 if (*ptr == '\n') ++ptr;
180         }
181
182         free(mycopy);
183         return v;
184 }
185
186
187 /**
188  * \brief Fetch the value of a particular key.
189  * If is_partial is set to 1, a partial match is ok (for example,
190  * a key of "tel;home" will satisfy a search for "tel").
191  * Set "instance" to a value higher than 0 to return subsequent instances
192  * of the same key.
193  * Set "get_propname" to nonzero to fetch the property name instead of value.
194  * \param v vCard to get keyvalue from
195  * \param propname key to retrieve
196  * \param is_partial dunno???
197  * \param instance if >0 return a later token of the value
198  * \param get_propname if nonzero get the real property name???
199  * \return 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  * \brief Destructor
232  * kill a vCard
233  * \param v the vCard to purge from memory
234  */
235 void vcard_free(struct vCard *v) {
236         int i;
237         
238         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
239         
240         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
241                 free(v->prop[i].name);
242                 free(v->prop[i].value);
243         }
244
245         if (v->prop != NULL) free(v->prop);
246         
247         memset(v, 0, sizeof(struct vCard));
248         free(v);
249 }
250
251
252
253
254 /**
255  * \brief Set a name/value pair in the card
256  * \param v vCard to inspect
257  * \param name key to set
258  * \param value the value to assign to key
259  * \param append should we append the value to an existing one?
260  */
261 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
262         int i;
263
264         if (v->magic != CTDL_VCARD_MAGIC) return;       /** Self-check */
265
266         /** If this key is already present, replace it */
267         if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
268                 if (!strcasecmp(v->prop[i].name, name)) {
269                         free(v->prop[i].name);
270                         free(v->prop[i].value);
271                         v->prop[i].name = strdup(name);
272                         v->prop[i].value = strdup(value);
273                         return;
274                 }
275         }
276
277         /** Otherwise, append it */
278         ++v->numprops;
279         v->prop = realloc(v->prop,
280                 (v->numprops * sizeof(struct vCardProp)) );
281         v->prop[v->numprops-1].name = strdup(name);
282         v->prop[v->numprops-1].value = strdup(value);
283 }
284
285
286
287
288 /**
289  * \brief Serialize a struct vcard into a standard text/x-vcard MIME type.
290  * \param v vCard to serialize
291  * \return the serialized vCard
292  */
293 char *vcard_serialize(struct vCard *v)
294 {
295         char *ser;
296         int i, j;
297         size_t len;
298         int is_utf8 = 0;
299
300         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /** self check */
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")) {
316                         is_utf8 = 0;
317                         for (j=0; i<strlen(v->prop[i].value); ++i) {
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  * \brief       Convert FN (Friendly Name) into N (Name)
340  *
341  * \param       vname           Supplied friendly-name
342  * \param       n               Target buffer to store Name
343  * \param       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