4 * vCard implementation for Citadel
6 * Copyright (C) 1999-2005 by Art Cancro
7 * This code is freely redistributable under the terms of the GNU General
8 * Public License. All other rights reserved.
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
34 #include <libcitadel.h>
45 * \brief Constructor (empty vCard)
46 * \return an empty vcard
48 struct vCard *vcard_new() {
51 v = (struct vCard *) malloc(sizeof(struct vCard));
52 if (v == NULL) return v;
54 v->magic = CTDL_VCARD_MAGIC;
62 * \brief Remove the "charset=" attribute from a vCard property name
64 * \param strbuf The property name string to be stripped
66 void remove_charset_attribute(char *strbuf)
71 t = num_tokens(strbuf, ';');
73 extract_token(compare, strbuf, i, ';', sizeof compare);
75 if (!strncasecmp(compare, "charset=", 8)) {
76 remove_token(strbuf, i, ';');
79 if (!IsEmptyStr(strbuf)) {
80 if (strbuf[strlen(strbuf)-1] == ';') {
81 strbuf[strlen(strbuf)-1] = 0;
88 * \brief Add a property to a vCard
90 * \param v vCard structure to which we are adding
91 * \param propname name of new property
92 * \param propvalue value of new property
94 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
96 v->prop = realloc(v->prop,
97 (v->numprops * sizeof(struct vCardProp)) );
98 v->prop[v->numprops-1].name = strdup(propname);
99 v->prop[v->numprops-1].value = strdup(propvalue);
105 * \brief Constructor (supply serialized vCard)
106 * \param vtext the text to parse into the new vcard
107 * \return the parsed VCard
109 struct vCard *vcard_load(char *vtext) {
113 char *namebuf, *valuebuf;
117 if (vtext == NULL) return vcard_new();
118 mycopy = strdup(vtext);
119 if (mycopy == NULL) return NULL;
122 * First, fix this big pile o' vCard to make it more parseable.
123 * To make it easier to parse, we convert CRLF to LF, and unfold any
124 * multi-line fields into single lines.
126 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
127 if (!strncmp(&mycopy[i], "\r\n", 2)) {
128 strcpy(&mycopy[i], &mycopy[i+1]);
130 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
131 strcpy(&mycopy[i], &mycopy[i+1]);
136 if (v == NULL) return v;
139 while (!IsEmptyStr(ptr)) {
142 colonpos = pattern2(ptr, ":");
143 nlpos = pattern2(ptr, "\n");
145 if ((nlpos > colonpos) && (colonpos > 0)) {
146 namebuf = malloc(colonpos + 1);
147 valuebuf = malloc(nlpos - colonpos + 1);
148 strncpy(namebuf, ptr, colonpos);
149 namebuf[colonpos] = 0;
150 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
151 valuebuf[nlpos-colonpos-1] = 0;
153 if (!strcasecmp(namebuf, "end")) {
156 if ( (!strcasecmp(namebuf, "begin"))
157 && (!strcasecmp(valuebuf, "vcard"))
162 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
163 remove_charset_attribute(namebuf);
165 v->prop = realloc(v->prop,
166 (v->numprops * sizeof(struct vCardProp))
168 v->prop[v->numprops-1].name = namebuf;
169 v->prop[v->numprops-1].value = valuebuf;
178 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
181 if (*ptr == '\n') ++ptr;
190 * \brief Fetch the value of a particular key.
191 * If is_partial is set to 1, a partial match is ok (for example,
192 * a key of "tel;home" will satisfy a search for "tel").
193 * Set "instance" to a value higher than 0 to return subsequent instances
195 * Set "get_propname" to nonzero to fetch the property name instead of value.
196 * \param v vCard to get keyvalue from
197 * \param propname key to retrieve
198 * \param is_partial dunno???
199 * \param instance if >0 return a later token of the value
200 * \param get_propname if nonzero get the real property name???
201 * \return the requested value / token / propertyname
203 char *vcard_get_prop(struct vCard *v, char *propname,
204 int is_partial, int instance, int get_propname) {
206 int found_instance = 0;
208 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
209 if ( (!strcasecmp(v->prop[i].name, propname))
210 || (propname[0] == 0)
211 || ( (!strncasecmp(v->prop[i].name,
212 propname, strlen(propname)))
213 && (v->prop[i].name[strlen(propname)] == ';')
214 && (is_partial) ) ) {
215 if (instance == found_instance++) {
217 return(v->prop[i].name);
220 return(v->prop[i].value);
235 * \param v the vCard to purge from memory
237 void vcard_free(struct vCard *v) {
240 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
242 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
243 free(v->prop[i].name);
244 free(v->prop[i].value);
247 if (v->prop != NULL) free(v->prop);
249 memset(v, 0, sizeof(struct vCard));
257 * \brief Set a name/value pair in the card
258 * \param v vCard to inspect
259 * \param name key to set
260 * \param value the value to assign to key
261 * \param append should we append the value to an existing one?
263 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
266 if (v->magic != CTDL_VCARD_MAGIC) return; /** Self-check */
268 /** If this key is already present, replace it */
269 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
270 if (!strcasecmp(v->prop[i].name, name)) {
271 free(v->prop[i].name);
272 free(v->prop[i].value);
273 v->prop[i].name = strdup(name);
274 v->prop[i].value = strdup(value);
279 /** Otherwise, append it */
281 v->prop = realloc(v->prop,
282 (v->numprops * sizeof(struct vCardProp)) );
283 v->prop[v->numprops-1].name = strdup(name);
284 v->prop[v->numprops-1].value = strdup(value);
291 * \brief Serialize a 'struct vcard' into an actual vcard.
292 * \param v vCard to serialize
293 * \return the serialized vCard
295 char *vcard_serialize(struct vCard *v)
302 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /** self check */
304 /** Figure out how big a buffer we need to allocate */
305 len = 64; /** for begin, end, and a little padding for safety */
306 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
308 strlen(v->prop[i].name) +
309 strlen(v->prop[i].value) + 16;
313 if (ser == NULL) return NULL;
315 safestrncpy(ser, "begin:vcard\r\n", len);
316 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
317 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
319 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
320 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
324 strcat(ser, v->prop[i].name);
326 strcat(ser, ";charset=UTF-8");
329 strcat(ser, v->prop[i].value);
333 strcat(ser, "end:vcard\r\n");
341 * \brief Convert FN (Friendly Name) into N (Name)
343 * \param vname Supplied friendly-name
344 * \param n Target buffer to store Name
345 * \param vname_size Size of buffer
347 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
350 char middlename[256];
351 char honorific_prefixes[256];
352 char honorific_suffixes[256];
355 safestrncpy(buf, n, sizeof buf);
357 /* Try to intelligently convert the screen name to a
358 * fully expanded vCard name based on the number of
361 safestrncpy(lastname, "", sizeof lastname);
362 safestrncpy(firstname, "", sizeof firstname);
363 safestrncpy(middlename, "", sizeof middlename);
364 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
365 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
367 /* Honorific suffixes */
368 if (num_tokens(buf, ',') > 1) {
369 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
370 sizeof honorific_suffixes);
371 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
374 /* Find a last name */
375 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
376 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
378 /* Find honorific prefixes */
379 if (num_tokens(buf, ' ') > 2) {
380 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
381 remove_token(buf, 0, ' ');
384 /* Find a middle name */
385 if (num_tokens(buf, ' ') > 1) {
386 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
387 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
390 /* Anything left is probably the first name */
391 safestrncpy(firstname, buf, sizeof firstname);
394 /* Compose the structured name */
395 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
396 honorific_prefixes, honorific_suffixes);