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]);
148 while (!IsEmptyStr(ptr)) {
149 colonpos = pattern2(ptr, ":");
150 nlpos = pattern2(ptr, "\n");
152 if ((nlpos > colonpos) && (colonpos > 0)) {
153 namebuf = malloc(colonpos + 1);
154 valuebuf = malloc(nlpos - colonpos + 1);
155 memcpy(namebuf, ptr, colonpos);
156 namebuf[colonpos] = '\0';
157 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
158 valuebuf[nlpos-colonpos-1] = '\0';
160 if (!strcasecmp(namebuf, "end")) {
163 if ( (!strcasecmp(namebuf, "begin"))
164 && (!strcasecmp(valuebuf, "vcard"))
169 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
170 remove_charset_attribute(namebuf);
172 v->prop = realloc(v->prop,
173 (v->numprops * sizeof(struct vCardProp))
175 v->prop[v->numprops-1].name = namebuf;
176 v->prop[v->numprops-1].value = valuebuf;
185 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
188 if (*ptr == '\n') ++ptr;
197 * Fetch the value of a particular key.
198 * If is_partial is set to 1, a partial match is ok (for example,
199 * a key of "tel;home" will satisfy a search for "tel").
200 * Set "instance" to a value higher than 0 to return subsequent instances
203 * Set "get_propname" to nonzero to fetch the property name instead of value.
204 * v vCard to get keyvalue from
205 * propname key to retrieve
207 * instance if nonzero return a later token of the value
208 * get_propname if nonzero get the real property name???
210 * returns the requested value / token / propertyname
212 char *vcard_get_prop(struct vCard *v, char *propname,
213 int is_partial, int instance, int get_propname) {
215 int found_instance = 0;
217 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
218 if ( (!strcasecmp(v->prop[i].name, propname))
219 || (propname[0] == 0)
220 || ( (!strncasecmp(v->prop[i].name,
221 propname, strlen(propname)))
222 && (v->prop[i].name[strlen(propname)] == ';')
223 && (is_partial) ) ) {
224 if (instance == found_instance++) {
226 return(v->prop[i].name);
229 return(v->prop[i].value);
244 void vcard_free(struct vCard *v) {
247 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
249 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
250 free(v->prop[i].name);
251 free(v->prop[i].value);
254 if (v->prop != NULL) free(v->prop);
256 memset(v, 0, sizeof(struct vCard));
264 * Set a name/value pair in the card
265 * v vCard to manipulate
267 * value the value to assign to key
268 * append if nonzero, append rather than replace if this key already exists.
270 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
273 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
275 /* If this key is already present, replace it */
276 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
277 if (!strcasecmp(v->prop[i].name, name)) {
278 free(v->prop[i].name);
279 free(v->prop[i].value);
280 v->prop[i].name = strdup(name);
281 v->prop[i].value = strdup(value);
286 /* Otherwise, append it */
288 v->prop = realloc(v->prop,
289 (v->numprops * sizeof(struct vCardProp)) );
290 v->prop[v->numprops-1].name = strdup(name);
291 v->prop[v->numprops-1].value = strdup(value);
298 * Serialize a 'struct vcard' into an actual vcard.
300 char *vcard_serialize(struct vCard *v)
307 if (v == NULL) return NULL; /* self check */
308 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
310 /* Set the vCard version number to 2.1 at this time. */
311 vcard_set_prop(v, "VERSION", "2.1", 0);
313 /* Figure out how big a buffer we need to allocate */
314 len = 64; /* for begin, end, and a little padding for safety */
315 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
317 strlen(v->prop[i].name) +
318 strlen(v->prop[i].value) + 16;
322 if (ser == NULL) return NULL;
324 safestrncpy(ser, "begin:vcard\r\n", len);
325 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
326 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
328 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
329 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
333 strcat(ser, v->prop[i].name);
335 strcat(ser, ";charset=UTF-8");
338 strcat(ser, v->prop[i].value);
342 strcat(ser, "end:vcard\r\n");
350 * Convert FN (Friendly Name) into N (Name)
352 * vname Supplied friendly-name
353 * n Target buffer to store Name
354 * vname_size Size of buffer
356 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
359 char middlename[256];
360 char honorific_prefixes[256];
361 char honorific_suffixes[256];
364 safestrncpy(buf, n, sizeof buf);
366 /* Try to intelligently convert the screen name to a
367 * fully expanded vCard name based on the number of
370 safestrncpy(lastname, "", sizeof lastname);
371 safestrncpy(firstname, "", sizeof firstname);
372 safestrncpy(middlename, "", sizeof middlename);
373 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
374 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
376 /* Honorific suffixes */
377 if (num_tokens(buf, ',') > 1) {
378 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
379 sizeof honorific_suffixes);
380 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
383 /* Find a last name */
384 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
385 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
387 /* Find honorific prefixes */
388 if (num_tokens(buf, ' ') > 2) {
389 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
390 remove_token(buf, 0, ' ');
393 /* Find a middle name */
394 if (num_tokens(buf, ' ') > 1) {
395 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
396 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
399 /* Anything left is probably the first name */
400 safestrncpy(firstname, buf, sizeof firstname);
403 /* Compose the structured name */
404 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
405 honorific_prefixes, honorific_suffixes);