2 * $Id: vcard.c 5754 2007-11-16 05:52:26Z ajc $
4 * vCard implementation for Citadel
6 * Copyright (C) 1999-2008 by the citadel.org development team.
7 * This code is freely redistributable under the terms of the GNU General
8 * Public License. All other rights reserved.
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
34 #include <libcitadel.h>
38 * Constructor (empty vCard)
39 * Returns an empty vcard
41 struct vCard *vcard_new() {
44 v = (struct vCard *) malloc(sizeof(struct vCard));
45 if (v == NULL) return v;
47 v->magic = CTDL_VCARD_MAGIC;
55 * Remove the "charset=" attribute from a vCard property name
58 void remove_charset_attribute(char *strbuf)
63 t = num_tokens(strbuf, ';');
65 extract_token(compare, strbuf, i, ';', sizeof compare);
67 if (!strncasecmp(compare, "charset=", 8)) {
68 remove_token(strbuf, i, ';');
71 if (!IsEmptyStr(strbuf)) {
72 if (strbuf[strlen(strbuf)-1] == ';') {
73 strbuf[strlen(strbuf)-1] = 0;
80 * Add a property to a vCard
82 * v vCard structure to which we are adding
83 * propname name of new property
84 * propvalue value of new property
86 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
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);
95 * Constructor - returns a new struct vcard given a serialized vcard
97 struct vCard *VCardLoad(StrBuf *vbtext) {
98 return vcard_load((char*)ChrPtr(vbtext));
102 * Constructor - returns a new struct vcard given a serialized vcard
104 struct vCard *vcard_load(char *vtext) {
108 char *namebuf, *valuebuf;
112 if (vtext == NULL) return vcard_new();
113 mycopy = strdup(vtext);
114 if (mycopy == NULL) return NULL;
117 * First, fix this big pile o' vCard to make it more parseable.
118 * To make it easier to parse, we convert CRLF to LF, and unfold any
119 * multi-line fields into single lines.
121 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
122 if (!strncmp(&mycopy[i], "\r\n", 2)) {
123 strcpy(&mycopy[i], &mycopy[i+1]);
125 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
126 strcpy(&mycopy[i], &mycopy[i+1]);
131 if (v == NULL) return v;
134 while (!IsEmptyStr(ptr)) {
137 colonpos = pattern2(ptr, ":");
138 nlpos = pattern2(ptr, "\n");
140 if ((nlpos > colonpos) && (colonpos > 0)) {
141 namebuf = malloc(colonpos + 1);
142 valuebuf = malloc(nlpos - colonpos + 1);
143 memcpy(namebuf, ptr, colonpos);
144 namebuf[colonpos] = '\0';
145 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
146 valuebuf[nlpos-colonpos-1] = '\0';
148 if (!strcasecmp(namebuf, "end")) {
151 if ( (!strcasecmp(namebuf, "begin"))
152 && (!strcasecmp(valuebuf, "vcard"))
157 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
158 remove_charset_attribute(namebuf);
160 v->prop = realloc(v->prop,
161 (v->numprops * sizeof(struct vCardProp))
163 v->prop[v->numprops-1].name = namebuf;
164 v->prop[v->numprops-1].value = valuebuf;
173 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
176 if (*ptr == '\n') ++ptr;
185 * Fetch the value of a particular key.
186 * If is_partial is set to 1, a partial match is ok (for example,
187 * a key of "tel;home" will satisfy a search for "tel").
188 * Set "instance" to a value higher than 0 to return subsequent instances
191 * Set "get_propname" to nonzero to fetch the property name instead of value.
192 * v vCard to get keyvalue from
193 * propname key to retrieve
195 * instance if nonzero return a later token of the value
196 * get_propname if nonzero get the real property name???
198 * returns the requested value / token / propertyname
200 char *vcard_get_prop(struct vCard *v, char *propname,
201 int is_partial, int instance, int get_propname) {
203 int found_instance = 0;
205 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
206 if ( (!strcasecmp(v->prop[i].name, propname))
207 || (propname[0] == 0)
208 || ( (!strncasecmp(v->prop[i].name,
209 propname, strlen(propname)))
210 && (v->prop[i].name[strlen(propname)] == ';')
211 && (is_partial) ) ) {
212 if (instance == found_instance++) {
214 return(v->prop[i].name);
217 return(v->prop[i].value);
232 void vcard_free(struct vCard *v) {
235 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
237 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
238 free(v->prop[i].name);
239 free(v->prop[i].value);
242 if (v->prop != NULL) free(v->prop);
244 memset(v, 0, sizeof(struct vCard));
252 * Set a name/value pair in the card
253 * v vCard to manipulate
255 * value the value to assign to key
256 * append if nonzero, append rather than replace if this key already exists.
258 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
261 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
263 /* If this key is already present, replace it */
264 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
265 if (!strcasecmp(v->prop[i].name, name)) {
266 free(v->prop[i].name);
267 free(v->prop[i].value);
268 v->prop[i].name = strdup(name);
269 v->prop[i].value = strdup(value);
274 /* Otherwise, append it */
276 v->prop = realloc(v->prop,
277 (v->numprops * sizeof(struct vCardProp)) );
278 v->prop[v->numprops-1].name = strdup(name);
279 v->prop[v->numprops-1].value = strdup(value);
286 * Serialize a 'struct vcard' into an actual vcard.
288 char *vcard_serialize(struct vCard *v)
295 if (v == NULL) return NULL; /* self check */
296 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
298 /* Set the vCard version number to 2.1 at this time. */
299 vcard_set_prop(v, "VERSION", "2.1", 0);
301 /* Figure out how big a buffer we need to allocate */
302 len = 64; /* for begin, end, and a little padding for safety */
303 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
305 strlen(v->prop[i].name) +
306 strlen(v->prop[i].value) + 16;
310 if (ser == NULL) return NULL;
312 safestrncpy(ser, "begin:vcard\r\n", len);
313 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
314 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
316 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
317 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
321 strcat(ser, v->prop[i].name);
323 strcat(ser, ";charset=UTF-8");
326 strcat(ser, v->prop[i].value);
330 strcat(ser, "end:vcard\r\n");
338 * Convert FN (Friendly Name) into N (Name)
340 * vname Supplied friendly-name
341 * n Target buffer to store Name
342 * vname_size Size of buffer
344 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
347 char middlename[256];
348 char honorific_prefixes[256];
349 char honorific_suffixes[256];
352 safestrncpy(buf, n, sizeof buf);
354 /* Try to intelligently convert the screen name to a
355 * fully expanded vCard name based on the number of
358 safestrncpy(lastname, "", sizeof lastname);
359 safestrncpy(firstname, "", sizeof firstname);
360 safestrncpy(middlename, "", sizeof middlename);
361 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
362 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
364 /* Honorific suffixes */
365 if (num_tokens(buf, ',') > 1) {
366 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
367 sizeof honorific_suffixes);
368 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
371 /* Find a last name */
372 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
373 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
375 /* Find honorific prefixes */
376 if (num_tokens(buf, ' ') > 2) {
377 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
378 remove_token(buf, 0, ' ');
381 /* Find a middle name */
382 if (num_tokens(buf, ' ') > 1) {
383 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
384 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
387 /* Anything left is probably the first name */
388 safestrncpy(firstname, buf, sizeof firstname);
391 /* Compose the structured name */
392 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
393 honorific_prefixes, honorific_suffixes);