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