2 * vCard implementation for Citadel
4 * Copyright (C) 1999-2008 by the citadel.org development team.
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
33 #include <libcitadel.h>
37 * Constructor (empty vCard)
38 * Returns an empty vcard
40 struct vCard *vcard_new() {
43 v = (struct vCard *) malloc(sizeof(struct vCard));
44 if (v == NULL) return v;
46 v->magic = CTDL_VCARD_MAGIC;
54 * Remove the "charset=" attribute from a vCard property name
57 void remove_charset_attribute(char *strbuf)
62 t = num_tokens(strbuf, ';');
64 extract_token(compare, strbuf, i, ';', sizeof compare);
66 if (!strncasecmp(compare, "charset=", 8)) {
67 remove_token(strbuf, i, ';');
70 if (!IsEmptyStr(strbuf)) {
71 if (strbuf[strlen(strbuf)-1] == ';') {
72 strbuf[strlen(strbuf)-1] = 0;
79 * Add a property to a vCard
81 * v vCard structure to which we are adding
82 * propname name of new property
83 * propvalue value of new property
85 void vcard_add_prop(struct vCard *v, const char *propname, const char *propvalue) {
87 v->prop = realloc(v->prop,
88 (v->numprops * sizeof(struct vCardProp)) );
89 v->prop[v->numprops-1].name = strdup(propname);
90 v->prop[v->numprops-1].value = strdup(propvalue);
94 * Constructor - returns a new struct vcard given a serialized vcard
96 struct vCard *VCardLoad(StrBuf *vbtext) {
97 return vcard_load((char*)ChrPtr(vbtext));
101 * Constructor - returns a new struct vcard given a serialized vcard
103 struct vCard *vcard_load(char *vtext) {
107 char *namebuf, *valuebuf;
111 if (vtext == NULL) return vcard_new();
112 mycopy = strdup(vtext);
113 if (mycopy == NULL) return NULL;
116 * First, fix this big pile o' vCard to make it more parseable.
117 * To make it easier to parse, we convert CRLF to LF, and unfold any
118 * multi-line fields into single lines.
120 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
121 if (!strncmp(&mycopy[i], "\r\n", 2)) {
122 strcpy(&mycopy[i], &mycopy[i+1]);
124 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
125 strcpy(&mycopy[i], &mycopy[i+1]);
137 while (!IsEmptyStr(ptr)) {
138 colonpos = pattern2(ptr, ":");
139 nlpos = pattern2(ptr, "\n");
141 if ((nlpos > colonpos) && (colonpos > 0)) {
142 namebuf = malloc(colonpos + 1);
143 valuebuf = malloc(nlpos - colonpos + 1);
144 memcpy(namebuf, ptr, colonpos);
145 namebuf[colonpos] = '\0';
146 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
147 valuebuf[nlpos-colonpos-1] = '\0';
149 if (!strcasecmp(namebuf, "end")) {
152 if ( (!strcasecmp(namebuf, "begin"))
153 && (!strcasecmp(valuebuf, "vcard"))
158 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
159 remove_charset_attribute(namebuf);
161 v->prop = realloc(v->prop,
162 (v->numprops * sizeof(struct vCardProp))
164 v->prop[v->numprops-1].name = namebuf;
165 v->prop[v->numprops-1].value = valuebuf;
174 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
177 if (*ptr == '\n') ++ptr;
186 * Fetch the value of a particular key.
187 * If is_partial is set to 1, a partial match is ok (for example,
188 * a key of "tel;home" will satisfy a search for "tel").
189 * Set "instance" to a value higher than 0 to return subsequent instances
192 * Set "get_propname" to nonzero to fetch the property name instead of value.
193 * v vCard to get keyvalue from
194 * propname key to retrieve
196 * instance if nonzero return a later token of the value
197 * get_propname if nonzero get the real property name???
199 * returns the requested value / token / propertyname
201 char *vcard_get_prop(struct vCard *v, char *propname,
202 int is_partial, int instance, int get_propname) {
204 int found_instance = 0;
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++) {
215 return(v->prop[i].name);
218 return(v->prop[i].value);
233 void vcard_free(struct vCard *v) {
236 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
238 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
239 free(v->prop[i].name);
240 free(v->prop[i].value);
243 if (v->prop != NULL) free(v->prop);
245 memset(v, 0, sizeof(struct vCard));
253 * Set a name/value pair in the card
254 * v vCard to manipulate
256 * value the value to assign to key
257 * append if nonzero, append rather than replace if this key already exists.
259 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
262 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
264 /* If this key is already present, replace it */
265 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
266 if (!strcasecmp(v->prop[i].name, name)) {
267 free(v->prop[i].name);
268 free(v->prop[i].value);
269 v->prop[i].name = strdup(name);
270 v->prop[i].value = strdup(value);
275 /* Otherwise, append it */
277 v->prop = realloc(v->prop,
278 (v->numprops * sizeof(struct vCardProp)) );
279 v->prop[v->numprops-1].name = strdup(name);
280 v->prop[v->numprops-1].value = strdup(value);
287 * Serialize a 'struct vcard' into an actual vcard.
289 char *vcard_serialize(struct vCard *v)
296 if (v == NULL) return NULL; /* self check */
297 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
299 /* Set the vCard version number to 2.1 at this time. */
300 vcard_set_prop(v, "VERSION", "2.1", 0);
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) {
306 strlen(v->prop[i].name) +
307 strlen(v->prop[i].value) + 16;
311 if (ser == NULL) return NULL;
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")) && (v->prop[i].value != NULL) ) {
317 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
318 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
322 strcat(ser, v->prop[i].name);
324 strcat(ser, ";charset=UTF-8");
327 strcat(ser, v->prop[i].value);
331 strcat(ser, "end:vcard\r\n");
339 * Convert FN (Friendly Name) into N (Name)
341 * vname Supplied friendly-name
342 * n Target buffer to store Name
343 * vname_size Size of buffer
345 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
348 char middlename[256];
349 char honorific_prefixes[256];
350 char honorific_suffixes[256];
353 safestrncpy(buf, n, sizeof buf);
355 /* Try to intelligently convert the screen name to a
356 * fully expanded vCard name based on the number of
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);
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), ',');
372 /* Find a last name */
373 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
374 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
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, ' ');
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), ' ');
388 /* Anything left is probably the first name */
389 safestrncpy(firstname, buf, sizeof firstname);
390 string_trim(firstname);
392 /* Compose the structured name */
393 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
394 honorific_prefixes, honorific_suffixes);