2 * vCard implementation for Citadel
4 * Copyright (C) 1999-2008 by the citadel.org development team.
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
44 #include <libcitadel.h>
48 * Constructor (empty vCard)
49 * Returns an empty vcard
51 struct vCard *vcard_new() {
54 v = (struct vCard *) malloc(sizeof(struct vCard));
55 if (v == NULL) return v;
57 v->magic = CTDL_VCARD_MAGIC;
65 * Remove the "charset=" attribute from a vCard property name
68 void remove_charset_attribute(char *strbuf)
73 t = num_tokens(strbuf, ';');
75 extract_token(compare, strbuf, i, ';', sizeof compare);
77 if (!strncasecmp(compare, "charset=", 8)) {
78 remove_token(strbuf, i, ';');
81 if (!IsEmptyStr(strbuf)) {
82 if (strbuf[strlen(strbuf)-1] == ';') {
83 strbuf[strlen(strbuf)-1] = 0;
90 * Add a property to a vCard
92 * v vCard structure to which we are adding
93 * propname name of new property
94 * propvalue value of new property
96 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
98 v->prop = realloc(v->prop,
99 (v->numprops * sizeof(struct vCardProp)) );
100 v->prop[v->numprops-1].name = strdup(propname);
101 v->prop[v->numprops-1].value = strdup(propvalue);
105 * Constructor - returns a new struct vcard given a serialized vcard
107 struct vCard *VCardLoad(StrBuf *vbtext) {
108 return vcard_load((char*)ChrPtr(vbtext));
112 * Constructor - returns a new struct vcard given a serialized vcard
114 struct vCard *vcard_load(char *vtext) {
118 char *namebuf, *valuebuf;
122 if (vtext == NULL) return vcard_new();
123 mycopy = strdup(vtext);
124 if (mycopy == NULL) return NULL;
127 * First, fix this big pile o' vCard to make it more parseable.
128 * To make it easier to parse, we convert CRLF to LF, and unfold any
129 * multi-line fields into single lines.
131 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
132 if (!strncmp(&mycopy[i], "\r\n", 2)) {
133 strcpy(&mycopy[i], &mycopy[i+1]);
135 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
136 strcpy(&mycopy[i], &mycopy[i+1]);
141 if (v == NULL) return v;
144 while (!IsEmptyStr(ptr)) {
147 colonpos = pattern2(ptr, ":");
148 nlpos = pattern2(ptr, "\n");
150 if ((nlpos > colonpos) && (colonpos > 0)) {
151 namebuf = malloc(colonpos + 1);
152 valuebuf = malloc(nlpos - colonpos + 1);
153 memcpy(namebuf, ptr, colonpos);
154 namebuf[colonpos] = '\0';
155 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
156 valuebuf[nlpos-colonpos-1] = '\0';
158 if (!strcasecmp(namebuf, "end")) {
161 if ( (!strcasecmp(namebuf, "begin"))
162 && (!strcasecmp(valuebuf, "vcard"))
167 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
168 remove_charset_attribute(namebuf);
170 v->prop = realloc(v->prop,
171 (v->numprops * sizeof(struct vCardProp))
173 v->prop[v->numprops-1].name = namebuf;
174 v->prop[v->numprops-1].value = valuebuf;
183 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
186 if (*ptr == '\n') ++ptr;
195 * Fetch the value of a particular key.
196 * If is_partial is set to 1, a partial match is ok (for example,
197 * a key of "tel;home" will satisfy a search for "tel").
198 * Set "instance" to a value higher than 0 to return subsequent instances
201 * Set "get_propname" to nonzero to fetch the property name instead of value.
202 * v vCard to get keyvalue from
203 * propname key to retrieve
205 * instance if nonzero return a later token of the value
206 * get_propname if nonzero get the real property name???
208 * returns the requested value / token / propertyname
210 char *vcard_get_prop(struct vCard *v, char *propname,
211 int is_partial, int instance, int get_propname) {
213 int found_instance = 0;
215 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
216 if ( (!strcasecmp(v->prop[i].name, propname))
217 || (propname[0] == 0)
218 || ( (!strncasecmp(v->prop[i].name,
219 propname, strlen(propname)))
220 && (v->prop[i].name[strlen(propname)] == ';')
221 && (is_partial) ) ) {
222 if (instance == found_instance++) {
224 return(v->prop[i].name);
227 return(v->prop[i].value);
242 void vcard_free(struct vCard *v) {
245 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
247 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
248 free(v->prop[i].name);
249 free(v->prop[i].value);
252 if (v->prop != NULL) free(v->prop);
254 memset(v, 0, sizeof(struct vCard));
262 * Set a name/value pair in the card
263 * v vCard to manipulate
265 * value the value to assign to key
266 * append if nonzero, append rather than replace if this key already exists.
268 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
271 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
273 /* If this key is already present, replace it */
274 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
275 if (!strcasecmp(v->prop[i].name, name)) {
276 free(v->prop[i].name);
277 free(v->prop[i].value);
278 v->prop[i].name = strdup(name);
279 v->prop[i].value = strdup(value);
284 /* Otherwise, append it */
286 v->prop = realloc(v->prop,
287 (v->numprops * sizeof(struct vCardProp)) );
288 v->prop[v->numprops-1].name = strdup(name);
289 v->prop[v->numprops-1].value = strdup(value);
296 * Serialize a 'struct vcard' into an actual vcard.
298 char *vcard_serialize(struct vCard *v)
305 if (v == NULL) return NULL; /* self check */
306 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
308 /* Set the vCard version number to 2.1 at this time. */
309 vcard_set_prop(v, "VERSION", "2.1", 0);
311 /* Figure out how big a buffer we need to allocate */
312 len = 64; /* for begin, end, and a little padding for safety */
313 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
315 strlen(v->prop[i].name) +
316 strlen(v->prop[i].value) + 16;
320 if (ser == NULL) return NULL;
322 safestrncpy(ser, "begin:vcard\r\n", len);
323 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
324 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
326 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
327 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
331 strcat(ser, v->prop[i].name);
333 strcat(ser, ";charset=UTF-8");
336 strcat(ser, v->prop[i].value);
340 strcat(ser, "end:vcard\r\n");
348 * Convert FN (Friendly Name) into N (Name)
350 * vname Supplied friendly-name
351 * n Target buffer to store Name
352 * vname_size Size of buffer
354 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
357 char middlename[256];
358 char honorific_prefixes[256];
359 char honorific_suffixes[256];
362 safestrncpy(buf, n, sizeof buf);
364 /* Try to intelligently convert the screen name to a
365 * fully expanded vCard name based on the number of
368 safestrncpy(lastname, "", sizeof lastname);
369 safestrncpy(firstname, "", sizeof firstname);
370 safestrncpy(middlename, "", sizeof middlename);
371 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
372 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
374 /* Honorific suffixes */
375 if (num_tokens(buf, ',') > 1) {
376 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
377 sizeof honorific_suffixes);
378 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
381 /* Find a last name */
382 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
383 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
385 /* Find honorific prefixes */
386 if (num_tokens(buf, ' ') > 2) {
387 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
388 remove_token(buf, 0, ' ');
391 /* Find a middle name */
392 if (num_tokens(buf, ' ') > 1) {
393 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
394 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
397 /* Anything left is probably the first name */
398 safestrncpy(firstname, buf, sizeof firstname);
401 /* Compose the structured name */
402 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
403 honorific_prefixes, honorific_suffixes);