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)) {
145 colonpos = pattern2(ptr, ":");
146 nlpos = pattern2(ptr, "\n");
148 if ((nlpos > colonpos) && (colonpos > 0)) {
149 namebuf = malloc(colonpos + 1);
150 valuebuf = malloc(nlpos - colonpos + 1);
151 memcpy(namebuf, ptr, colonpos);
152 namebuf[colonpos] = '\0';
153 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
154 valuebuf[nlpos-colonpos-1] = '\0';
156 if (!strcasecmp(namebuf, "end")) {
159 if ( (!strcasecmp(namebuf, "begin"))
160 && (!strcasecmp(valuebuf, "vcard"))
165 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
166 remove_charset_attribute(namebuf);
168 v->prop = realloc(v->prop,
169 (v->numprops * sizeof(struct vCardProp))
171 v->prop[v->numprops-1].name = namebuf;
172 v->prop[v->numprops-1].value = valuebuf;
181 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
184 if (*ptr == '\n') ++ptr;
193 * Fetch the value of a particular key.
194 * If is_partial is set to 1, a partial match is ok (for example,
195 * a key of "tel;home" will satisfy a search for "tel").
196 * Set "instance" to a value higher than 0 to return subsequent instances
199 * Set "get_propname" to nonzero to fetch the property name instead of value.
200 * v vCard to get keyvalue from
201 * propname key to retrieve
203 * instance if nonzero return a later token of the value
204 * get_propname if nonzero get the real property name???
206 * returns the requested value / token / propertyname
208 char *vcard_get_prop(struct vCard *v, char *propname,
209 int is_partial, int instance, int get_propname) {
211 int found_instance = 0;
213 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
214 if ( (!strcasecmp(v->prop[i].name, propname))
215 || (propname[0] == 0)
216 || ( (!strncasecmp(v->prop[i].name,
217 propname, strlen(propname)))
218 && (v->prop[i].name[strlen(propname)] == ';')
219 && (is_partial) ) ) {
220 if (instance == found_instance++) {
222 return(v->prop[i].name);
225 return(v->prop[i].value);
240 void vcard_free(struct vCard *v) {
243 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
245 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
246 free(v->prop[i].name);
247 free(v->prop[i].value);
250 if (v->prop != NULL) free(v->prop);
252 memset(v, 0, sizeof(struct vCard));
260 * Set a name/value pair in the card
261 * v vCard to manipulate
263 * value the value to assign to key
264 * append if nonzero, append rather than replace if this key already exists.
266 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
269 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
271 /* If this key is already present, replace it */
272 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
273 if (!strcasecmp(v->prop[i].name, name)) {
274 free(v->prop[i].name);
275 free(v->prop[i].value);
276 v->prop[i].name = strdup(name);
277 v->prop[i].value = strdup(value);
282 /* Otherwise, append it */
284 v->prop = realloc(v->prop,
285 (v->numprops * sizeof(struct vCardProp)) );
286 v->prop[v->numprops-1].name = strdup(name);
287 v->prop[v->numprops-1].value = strdup(value);
294 * Serialize a 'struct vcard' into an actual vcard.
296 char *vcard_serialize(struct vCard *v)
303 if (v == NULL) return NULL; /* self check */
304 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
306 /* Set the vCard version number to 2.1 at this time. */
307 vcard_set_prop(v, "VERSION", "2.1", 0);
309 /* Figure out how big a buffer we need to allocate */
310 len = 64; /* for begin, end, and a little padding for safety */
311 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
313 strlen(v->prop[i].name) +
314 strlen(v->prop[i].value) + 16;
318 if (ser == NULL) return NULL;
320 safestrncpy(ser, "begin:vcard\r\n", len);
321 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
322 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
324 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
325 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
329 strcat(ser, v->prop[i].name);
331 strcat(ser, ";charset=UTF-8");
334 strcat(ser, v->prop[i].value);
338 strcat(ser, "end:vcard\r\n");
346 * Convert FN (Friendly Name) into N (Name)
348 * vname Supplied friendly-name
349 * n Target buffer to store Name
350 * vname_size Size of buffer
352 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
355 char middlename[256];
356 char honorific_prefixes[256];
357 char honorific_suffixes[256];
360 safestrncpy(buf, n, sizeof buf);
362 /* Try to intelligently convert the screen name to a
363 * fully expanded vCard name based on the number of
366 safestrncpy(lastname, "", sizeof lastname);
367 safestrncpy(firstname, "", sizeof firstname);
368 safestrncpy(middlename, "", sizeof middlename);
369 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
370 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
372 /* Honorific suffixes */
373 if (num_tokens(buf, ',') > 1) {
374 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
375 sizeof honorific_suffixes);
376 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
379 /* Find a last name */
380 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
381 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
383 /* Find honorific prefixes */
384 if (num_tokens(buf, ' ') > 2) {
385 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
386 remove_token(buf, 0, ' ');
389 /* Find a middle name */
390 if (num_tokens(buf, ' ') > 1) {
391 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
392 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
395 /* Anything left is probably the first name */
396 safestrncpy(firstname, buf, sizeof firstname);
399 /* Compose the structured name */
400 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
401 honorific_prefixes, honorific_suffixes);