3 * Copyright (C) 1999-2006 by Art Cancro
4 * This code is freely redistributable under the terms of the GNU General
5 * Public License. All other rights reserved.
8 * \defgroup VCardMain vCard data type implementation for the Citadel system.
13 #include "webserver.h"
17 * \brief Constructor (empty vCard)
18 * \return an empty vcard
20 struct vCard *vcard_new() {
23 v = (struct vCard *) malloc(sizeof(struct vCard));
24 if (v == NULL) return v;
26 v->magic = CTDL_VCARD_MAGIC;
34 * \brief Remove the "charset=" attribute from a vCard property name
36 * \param strbuf The property name string to be stripped
38 void remove_charset_attribute(char *strbuf)
43 t = num_tokens(strbuf, ';');
45 extract_token(compare, strbuf, i, ';', sizeof compare);
47 if (!strncasecmp(compare, "charset=", 8)) {
48 remove_token(strbuf, i, ';');
51 if (!IsEmptyStr(strbuf)) {
54 if (strbuf[len-1] == ';') {
62 * \brief Add a property to a vCard
64 * \param v vCard structure to which we are adding
65 * \param propname name of new property
66 * \param propvalue value of new property
68 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
70 v->prop = realloc(v->prop,
71 (v->numprops * sizeof(struct vCardProp)) );
72 v->prop[v->numprops-1].name = strdup(propname);
73 v->prop[v->numprops-1].value = strdup(propvalue);
79 * \brief Constructor (supply serialized vCard)
80 * \param vtext the text to parse into the new vcard
81 * \return the parsed VCard
83 struct vCard *vcard_load(char *vtext) {
87 char *namebuf, *valuebuf;
92 if (vtext == NULL) return vcard_new();
93 mycopy = strdup(vtext);
94 if (mycopy == NULL) return NULL;
97 * First, fix this big pile o' vCard to make it more parseable.
98 * To make it easier to parse, we convert CRLF to LF, and unfold any
99 * multi-line fields into single lines.
101 len = strlen(mycopy);
102 for (i=0; i<len; ++i) {
103 if (!strncmp(&mycopy[i], "\r\n", 2)) {
104 memmove(&mycopy[i], &mycopy[i+1], len - i);
107 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
108 memmove(&mycopy[i], &mycopy[i+1], len - i);
114 if (v == NULL) return v;
117 while (*ptr != '\0') {
120 colonpos = pattern2(ptr, ":");
121 nlpos = pattern2(ptr, "\n");
123 if ((nlpos > colonpos) && (colonpos > 0)) {
124 namebuf = malloc(colonpos + 1);
125 valuebuf = malloc(nlpos - colonpos + 1);
126 strncpy(namebuf, ptr, colonpos);
127 namebuf[colonpos] = 0;
128 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
129 valuebuf[nlpos-colonpos-1] = 0;
131 if (!strcasecmp(namebuf, "end")) {
134 if ( (!strcasecmp(namebuf, "begin"))
135 && (!strcasecmp(valuebuf, "vcard"))
140 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
141 remove_charset_attribute(namebuf);
143 v->prop = realloc(v->prop,
144 (v->numprops * sizeof(struct vCardProp))
146 v->prop[v->numprops-1].name = namebuf;
147 v->prop[v->numprops-1].value = valuebuf;
156 while ( (*ptr != '\n') && (*ptr != '\0') ) {
159 if (*ptr == '\n') ++ptr;
168 * \brief Fetch the value of a particular key.
169 * If is_partial is set to 1, a partial match is ok (for example,
170 * a key of "tel;home" will satisfy a search for "tel").
171 * Set "instance" to a value higher than 0 to return subsequent instances
173 * Set "get_propname" to nonzero to fetch the property name instead of value.
174 * \param v vCard to get keyvalue from
175 * \param propname key to retrieve
176 * \param is_partial dunno???
177 * \param instance if >0 return a later token of the value
178 * \param get_propname if nonzero get the real property name???
179 * \return the requested value / token / propertyname
181 char *vcard_get_prop(struct vCard *v, char *propname,
182 int is_partial, int instance, int get_propname) {
184 int found_instance = 0;
187 len = strlen(propname);
188 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
189 if ( (!strcasecmp(v->prop[i].name, propname))
190 || (propname[0] == 0)
191 || ( (!strncasecmp(v->prop[i].name,
193 && (v->prop[i].name[len] == ';')
194 && (is_partial) ) ) {
195 if (instance == found_instance++) {
197 return(v->prop[i].name);
200 return(v->prop[i].value);
215 * \param v the vCard to purge from memory
217 void vcard_free(struct vCard *v) {
220 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
222 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
223 free(v->prop[i].name);
224 free(v->prop[i].value);
227 if (v->prop != NULL) free(v->prop);
229 memset(v, 0, sizeof(struct vCard));
237 * \brief Set a name/value pair in the card
238 * \param v vCard to inspect
239 * \param name key to set
240 * \param value the value to assign to key
241 * \param append should we append the value to an existing one?
243 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
246 if (v->magic != CTDL_VCARD_MAGIC) return; /** Self-check */
248 /** If this key is already present, replace it */
249 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
250 if (!strcasecmp(v->prop[i].name, name)) {
251 free(v->prop[i].name);
252 free(v->prop[i].value);
253 v->prop[i].name = strdup(name);
254 v->prop[i].value = strdup(value);
259 /** Otherwise, append it */
261 v->prop = realloc(v->prop,
262 (v->numprops * sizeof(struct vCardProp)) );
263 v->prop[v->numprops-1].name = strdup(name);
264 v->prop[v->numprops-1].value = strdup(value);
271 * \brief Serialize a struct vcard into its standard format.
272 * \param v vCard to serialize
273 * \return the serialized vCard
275 char *vcard_serialize(struct vCard *v)
282 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /** self check */
284 /** Figure out how big a buffer we need to allocate */
285 len = 64; /** for begin, end, and a little padding for safety */
286 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
288 strlen(v->prop[i].name) +
289 strlen(v->prop[i].value) + 16;
293 if (ser == NULL) return NULL;
295 safestrncpy(ser, "begin:vcard\r\n", len);
296 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
297 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
299 for (j=0; j<strlen(v->prop[i].value); ++j) {
300 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
304 strcat(ser, v->prop[i].name);
306 strcat(ser, ";charset=UTF-8");
309 strcat(ser, v->prop[i].value);
313 strcat(ser, "end:vcard\r\n");
321 * \brief Convert FN (Friendly Name) into N (Name)
323 * \param vname Supplied friendly-name
324 * \param n Target buffer to store Name
325 * \param vname_size Size of buffer
327 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
330 char middlename[256];
331 char honorific_prefixes[256];
332 char honorific_suffixes[256];
335 safestrncpy(buf, n, sizeof buf);
337 /* Try to intelligently convert the screen name to a
338 * fully expanded vCard name based on the number of
341 safestrncpy(lastname, "", sizeof lastname);
342 safestrncpy(firstname, "", sizeof firstname);
343 safestrncpy(middlename, "", sizeof middlename);
344 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
345 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
347 /* Honorific suffixes */
348 if (num_tokens(buf, ',') > 1) {
349 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
350 sizeof honorific_suffixes);
351 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
354 /* Find a last name */
355 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
356 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
358 /* Find honorific prefixes */
359 if (num_tokens(buf, ' ') > 2) {
360 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
361 remove_token(buf, 0, ' ');
364 /* Find a middle name */
365 if (num_tokens(buf, ' ') > 1) {
366 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
367 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
370 /* Anything left is probably the first name */
371 safestrncpy(firstname, buf, sizeof firstname);
374 /* Compose the structured name */
375 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
376 honorific_prefixes, honorific_suffixes);