2 * vCard implementation for Citadel
4 * Copyright (C) 1999-2008 by the citadel.org development team.
5 * This code is freely redistributable under the terms of the GNU General
6 * Public License. All other rights reserved.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
32 #include <libcitadel.h>
36 * Constructor (empty vCard)
37 * Returns an empty vcard
39 struct vCard *vcard_new() {
42 v = (struct vCard *) malloc(sizeof(struct vCard));
43 if (v == NULL) return v;
45 v->magic = CTDL_VCARD_MAGIC;
53 * Remove the "charset=" attribute from a vCard property name
56 void remove_charset_attribute(char *strbuf)
61 t = num_tokens(strbuf, ';');
63 extract_token(compare, strbuf, i, ';', sizeof compare);
65 if (!strncasecmp(compare, "charset=", 8)) {
66 remove_token(strbuf, i, ';');
69 if (!IsEmptyStr(strbuf)) {
70 if (strbuf[strlen(strbuf)-1] == ';') {
71 strbuf[strlen(strbuf)-1] = 0;
78 * Add a property to a vCard
80 * v vCard structure to which we are adding
81 * propname name of new property
82 * propvalue value of new property
84 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
86 v->prop = realloc(v->prop,
87 (v->numprops * sizeof(struct vCardProp)) );
88 v->prop[v->numprops-1].name = strdup(propname);
89 v->prop[v->numprops-1].value = strdup(propvalue);
93 * Constructor - returns a new struct vcard given a serialized vcard
95 struct vCard *VCardLoad(StrBuf *vbtext) {
96 return vcard_load((char*)ChrPtr(vbtext));
100 * Constructor - returns a new struct vcard given a serialized vcard
102 struct vCard *vcard_load(char *vtext) {
106 char *namebuf, *valuebuf;
110 if (vtext == NULL) return vcard_new();
111 mycopy = strdup(vtext);
112 if (mycopy == NULL) return NULL;
115 * First, fix this big pile o' vCard to make it more parseable.
116 * To make it easier to parse, we convert CRLF to LF, and unfold any
117 * multi-line fields into single lines.
119 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
120 if (!strncmp(&mycopy[i], "\r\n", 2)) {
121 strcpy(&mycopy[i], &mycopy[i+1]);
123 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
124 strcpy(&mycopy[i], &mycopy[i+1]);
129 if (v == NULL) return v;
132 while (!IsEmptyStr(ptr)) {
135 colonpos = pattern2(ptr, ":");
136 nlpos = pattern2(ptr, "\n");
138 if ((nlpos > colonpos) && (colonpos > 0)) {
139 namebuf = malloc(colonpos + 1);
140 valuebuf = malloc(nlpos - colonpos + 1);
141 memcpy(namebuf, ptr, colonpos);
142 namebuf[colonpos] = '\0';
143 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
144 valuebuf[nlpos-colonpos-1] = '\0';
146 if (!strcasecmp(namebuf, "end")) {
149 if ( (!strcasecmp(namebuf, "begin"))
150 && (!strcasecmp(valuebuf, "vcard"))
155 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
156 remove_charset_attribute(namebuf);
158 v->prop = realloc(v->prop,
159 (v->numprops * sizeof(struct vCardProp))
161 v->prop[v->numprops-1].name = namebuf;
162 v->prop[v->numprops-1].value = valuebuf;
171 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
174 if (*ptr == '\n') ++ptr;
183 * Fetch the value of a particular key.
184 * If is_partial is set to 1, a partial match is ok (for example,
185 * a key of "tel;home" will satisfy a search for "tel").
186 * Set "instance" to a value higher than 0 to return subsequent instances
189 * Set "get_propname" to nonzero to fetch the property name instead of value.
190 * v vCard to get keyvalue from
191 * propname key to retrieve
193 * instance if nonzero return a later token of the value
194 * get_propname if nonzero get the real property name???
196 * returns the requested value / token / propertyname
198 char *vcard_get_prop(struct vCard *v, char *propname,
199 int is_partial, int instance, int get_propname) {
201 int found_instance = 0;
203 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
204 if ( (!strcasecmp(v->prop[i].name, propname))
205 || (propname[0] == 0)
206 || ( (!strncasecmp(v->prop[i].name,
207 propname, strlen(propname)))
208 && (v->prop[i].name[strlen(propname)] == ';')
209 && (is_partial) ) ) {
210 if (instance == found_instance++) {
212 return(v->prop[i].name);
215 return(v->prop[i].value);
230 void vcard_free(struct vCard *v) {
233 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
235 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
236 free(v->prop[i].name);
237 free(v->prop[i].value);
240 if (v->prop != NULL) free(v->prop);
242 memset(v, 0, sizeof(struct vCard));
250 * Set a name/value pair in the card
251 * v vCard to manipulate
253 * value the value to assign to key
254 * append if nonzero, append rather than replace if this key already exists.
256 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
259 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
261 /* If this key is already present, replace it */
262 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
263 if (!strcasecmp(v->prop[i].name, name)) {
264 free(v->prop[i].name);
265 free(v->prop[i].value);
266 v->prop[i].name = strdup(name);
267 v->prop[i].value = strdup(value);
272 /* Otherwise, append it */
274 v->prop = realloc(v->prop,
275 (v->numprops * sizeof(struct vCardProp)) );
276 v->prop[v->numprops-1].name = strdup(name);
277 v->prop[v->numprops-1].value = strdup(value);
284 * Serialize a 'struct vcard' into an actual vcard.
286 char *vcard_serialize(struct vCard *v)
293 if (v == NULL) return NULL; /* self check */
294 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
296 /* Set the vCard version number to 2.1 at this time. */
297 vcard_set_prop(v, "VERSION", "2.1", 0);
299 /* Figure out how big a buffer we need to allocate */
300 len = 64; /* for begin, end, and a little padding for safety */
301 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
303 strlen(v->prop[i].name) +
304 strlen(v->prop[i].value) + 16;
308 if (ser == NULL) return NULL;
310 safestrncpy(ser, "begin:vcard\r\n", len);
311 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
312 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
314 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
315 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
319 strcat(ser, v->prop[i].name);
321 strcat(ser, ";charset=UTF-8");
324 strcat(ser, v->prop[i].value);
328 strcat(ser, "end:vcard\r\n");
336 * Convert FN (Friendly Name) into N (Name)
338 * vname Supplied friendly-name
339 * n Target buffer to store Name
340 * vname_size Size of buffer
342 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
345 char middlename[256];
346 char honorific_prefixes[256];
347 char honorific_suffixes[256];
350 safestrncpy(buf, n, sizeof buf);
352 /* Try to intelligently convert the screen name to a
353 * fully expanded vCard name based on the number of
356 safestrncpy(lastname, "", sizeof lastname);
357 safestrncpy(firstname, "", sizeof firstname);
358 safestrncpy(middlename, "", sizeof middlename);
359 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
360 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
362 /* Honorific suffixes */
363 if (num_tokens(buf, ',') > 1) {
364 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
365 sizeof honorific_suffixes);
366 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
369 /* Find a last name */
370 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
371 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
373 /* Find honorific prefixes */
374 if (num_tokens(buf, ' ') > 2) {
375 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
376 remove_token(buf, 0, ' ');
379 /* Find a middle name */
380 if (num_tokens(buf, ' ') > 1) {
381 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
382 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
385 /* Anything left is probably the first name */
386 safestrncpy(firstname, buf, sizeof firstname);
389 /* Compose the structured name */
390 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
391 honorific_prefixes, honorific_suffixes);