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>
46 * \brief Constructor (empty vCard)
47 * \return an empty vcard
49 struct vCard *vcard_new() {
52 v = (struct vCard *) malloc(sizeof(struct vCard));
53 if (v == NULL) return v;
55 v->magic = CTDL_VCARD_MAGIC;
63 * \brief Remove the "charset=" attribute from a vCard property name
65 * \param strbuf The property name string to be stripped
67 void remove_charset_attribute(char *strbuf)
72 t = num_tokens(strbuf, ';');
74 extract_token(compare, strbuf, i, ';', sizeof compare);
76 if (!strncasecmp(compare, "charset=", 8)) {
77 remove_token(strbuf, i, ';');
80 if (!IsEmptyStr(strbuf)) {
81 if (strbuf[strlen(strbuf)-1] == ';') {
82 strbuf[strlen(strbuf)-1] = 0;
89 * \brief Add a property to a vCard
91 * \param v vCard structure to which we are adding
92 * \param propname name of new property
93 * \param propvalue value of new property
95 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
97 v->prop = realloc(v->prop,
98 (v->numprops * sizeof(struct vCardProp)) );
99 v->prop[v->numprops-1].name = strdup(propname);
100 v->prop[v->numprops-1].value = strdup(propvalue);
106 * \brief Constructor (supply serialized vCard)
107 * \param vtext the text to parse into the new vcard
108 * \return the parsed VCard
110 struct vCard *vcard_load(char *vtext) {
114 char *namebuf, *valuebuf;
118 if (vtext == NULL) return vcard_new();
119 mycopy = strdup(vtext);
120 if (mycopy == NULL) return NULL;
123 * First, fix this big pile o' vCard to make it more parseable.
124 * To make it easier to parse, we convert CRLF to LF, and unfold any
125 * multi-line fields into single lines.
127 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
128 if (!strncmp(&mycopy[i], "\r\n", 2)) {
129 strcpy(&mycopy[i], &mycopy[i+1]);
131 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
132 strcpy(&mycopy[i], &mycopy[i+1]);
137 if (v == NULL) return v;
140 while (!IsEmptyStr(ptr)) {
143 colonpos = pattern2(ptr, ":");
144 nlpos = pattern2(ptr, "\n");
146 if ((nlpos > colonpos) && (colonpos > 0)) {
147 namebuf = malloc(colonpos + 1);
148 valuebuf = malloc(nlpos - colonpos + 1);
149 strncpy(namebuf, ptr, colonpos);
150 namebuf[colonpos] = 0;
151 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
152 valuebuf[nlpos-colonpos-1] = 0;
154 if (!strcasecmp(namebuf, "end")) {
157 if ( (!strcasecmp(namebuf, "begin"))
158 && (!strcasecmp(valuebuf, "vcard"))
163 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
164 remove_charset_attribute(namebuf);
166 v->prop = realloc(v->prop,
167 (v->numprops * sizeof(struct vCardProp))
169 v->prop[v->numprops-1].name = namebuf;
170 v->prop[v->numprops-1].value = valuebuf;
179 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
182 if (*ptr == '\n') ++ptr;
191 * \brief Fetch the value of a particular key.
192 * If is_partial is set to 1, a partial match is ok (for example,
193 * a key of "tel;home" will satisfy a search for "tel").
194 * Set "instance" to a value higher than 0 to return subsequent instances
196 * Set "get_propname" to nonzero to fetch the property name instead of value.
197 * \param v vCard to get keyvalue from
198 * \param propname key to retrieve
199 * \param is_partial dunno???
200 * \param instance if >0 return a later token of the value
201 * \param get_propname if nonzero get the real property name???
202 * \return the requested value / token / propertyname
204 char *vcard_get_prop(struct vCard *v, char *propname,
205 int is_partial, int instance, int get_propname) {
207 int found_instance = 0;
209 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
210 if ( (!strcasecmp(v->prop[i].name, propname))
211 || (propname[0] == 0)
212 || ( (!strncasecmp(v->prop[i].name,
213 propname, strlen(propname)))
214 && (v->prop[i].name[strlen(propname)] == ';')
215 && (is_partial) ) ) {
216 if (instance == found_instance++) {
218 return(v->prop[i].name);
221 return(v->prop[i].value);
236 * \param v the vCard to purge from memory
238 void vcard_free(struct vCard *v) {
241 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
243 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
244 free(v->prop[i].name);
245 free(v->prop[i].value);
248 if (v->prop != NULL) free(v->prop);
250 memset(v, 0, sizeof(struct vCard));
258 * \brief Set a name/value pair in the card
259 * \param v vCard to inspect
260 * \param name key to set
261 * \param value the value to assign to key
262 * \param append should we append the value to an existing one?
264 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
267 if (v->magic != CTDL_VCARD_MAGIC) return; /** Self-check */
269 /** If this key is already present, replace it */
270 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
271 if (!strcasecmp(v->prop[i].name, name)) {
272 free(v->prop[i].name);
273 free(v->prop[i].value);
274 v->prop[i].name = strdup(name);
275 v->prop[i].value = strdup(value);
280 /** Otherwise, append it */
282 v->prop = realloc(v->prop,
283 (v->numprops * sizeof(struct vCardProp)) );
284 v->prop[v->numprops-1].name = strdup(name);
285 v->prop[v->numprops-1].value = strdup(value);
292 * \brief Serialize a 'struct vcard' into an actual vcard.
293 * \param v vCard to serialize
294 * \return the serialized vCard
296 char *vcard_serialize(struct vCard *v)
303 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /** self check */
305 /** Figure out how big a buffer we need to allocate */
306 len = 64; /** for begin, end, and a little padding for safety */
307 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
309 strlen(v->prop[i].name) +
310 strlen(v->prop[i].value) + 16;
314 if (ser == NULL) return NULL;
316 safestrncpy(ser, "begin:vcard\r\n", len);
317 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
318 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
320 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
321 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
325 strcat(ser, v->prop[i].name);
327 strcat(ser, ";charset=UTF-8");
330 strcat(ser, v->prop[i].value);
334 strcat(ser, "end:vcard\r\n");
342 * \brief Convert FN (Friendly Name) into N (Name)
344 * \param vname Supplied friendly-name
345 * \param n Target buffer to store Name
346 * \param vname_size Size of buffer
348 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
351 char middlename[256];
352 char honorific_prefixes[256];
353 char honorific_suffixes[256];
356 safestrncpy(buf, n, sizeof buf);
358 /* Try to intelligently convert the screen name to a
359 * fully expanded vCard name based on the number of
362 safestrncpy(lastname, "", sizeof lastname);
363 safestrncpy(firstname, "", sizeof firstname);
364 safestrncpy(middlename, "", sizeof middlename);
365 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
366 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
368 /* Honorific suffixes */
369 if (num_tokens(buf, ',') > 1) {
370 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
371 sizeof honorific_suffixes);
372 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
375 /* Find a last name */
376 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
377 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
379 /* Find honorific prefixes */
380 if (num_tokens(buf, ' ') > 2) {
381 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
382 remove_token(buf, 0, ' ');
385 /* Find a middle name */
386 if (num_tokens(buf, ' ') > 1) {
387 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
388 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
391 /* Anything left is probably the first name */
392 safestrncpy(firstname, buf, sizeof firstname);
395 /* Compose the structured name */
396 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
397 honorific_prefixes, honorific_suffixes);