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>
43 * \brief Constructor (empty vCard)
44 * \return an empty vcard
46 struct vCard *vcard_new() {
49 v = (struct vCard *) malloc(sizeof(struct vCard));
50 if (v == NULL) return v;
52 v->magic = CTDL_VCARD_MAGIC;
60 * \brief Remove the "charset=" attribute from a vCard property name
62 * \param strbuf The property name string to be stripped
64 void remove_charset_attribute(char *strbuf)
69 t = num_tokens(strbuf, ';');
71 extract_token(compare, strbuf, i, ';', sizeof compare);
73 if (!strncasecmp(compare, "charset=", 8)) {
74 remove_token(strbuf, i, ';');
77 if (strlen(strbuf) > 0) {
78 if (strbuf[strlen(strbuf)-1] == ';') {
79 strbuf[strlen(strbuf)-1] = 0;
86 * \brief Add a property to a vCard
88 * \param v vCard structure to which we are adding
89 * \param propname name of new property
90 * \param propvalue value of new property
92 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
94 v->prop = realloc(v->prop,
95 (v->numprops * sizeof(struct vCardProp)) );
96 v->prop[v->numprops-1].name = strdup(propname);
97 v->prop[v->numprops-1].value = strdup(propvalue);
103 * \brief Constructor (supply serialized vCard)
104 * \param vtext the text to parse into the new vcard
105 * \return the parsed VCard
107 struct vCard *vcard_load(char *vtext) {
111 char *namebuf, *valuebuf;
115 if (vtext == NULL) return vcard_new();
116 mycopy = strdup(vtext);
117 if (mycopy == NULL) return NULL;
120 * First, fix this big pile o' vCard to make it more parseable.
121 * To make it easier to parse, we convert CRLF to LF, and unfold any
122 * multi-line fields into single lines.
124 for (i=0; i<strlen(mycopy); ++i) {
125 if (!strncmp(&mycopy[i], "\r\n", 2)) {
126 strcpy(&mycopy[i], &mycopy[i+1]);
128 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
129 strcpy(&mycopy[i], &mycopy[i+1]);
134 if (v == NULL) return v;
137 while (strlen(ptr)>0) {
140 colonpos = pattern2(ptr, ":");
141 nlpos = pattern2(ptr, "\n");
143 if ((nlpos > colonpos) && (colonpos > 0)) {
144 namebuf = malloc(colonpos + 1);
145 valuebuf = malloc(nlpos - colonpos + 1);
146 strncpy(namebuf, ptr, colonpos);
147 namebuf[colonpos] = 0;
148 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
149 valuebuf[nlpos-colonpos-1] = 0;
151 if (!strcasecmp(namebuf, "end")) {
154 if ( (!strcasecmp(namebuf, "begin"))
155 && (!strcasecmp(valuebuf, "vcard"))
160 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
161 remove_charset_attribute(namebuf);
163 v->prop = realloc(v->prop,
164 (v->numprops * sizeof(struct vCardProp))
166 v->prop[v->numprops-1].name = namebuf;
167 v->prop[v->numprops-1].value = valuebuf;
176 while ( (*ptr != '\n') && (strlen(ptr)>0) ) {
179 if (*ptr == '\n') ++ptr;
188 * \brief Fetch the value of a particular key.
189 * If is_partial is set to 1, a partial match is ok (for example,
190 * a key of "tel;home" will satisfy a search for "tel").
191 * Set "instance" to a value higher than 0 to return subsequent instances
193 * Set "get_propname" to nonzero to fetch the property name instead of value.
194 * \param v vCard to get keyvalue from
195 * \param propname key to retrieve
196 * \param is_partial dunno???
197 * \param instance if >0 return a later token of the value
198 * \param get_propname if nonzero get the real property name???
199 * \return the requested value / token / propertyname
201 char *vcard_get_prop(struct vCard *v, char *propname,
202 int is_partial, int instance, int get_propname) {
204 int found_instance = 0;
206 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
207 if ( (!strcasecmp(v->prop[i].name, propname))
208 || (propname[0] == 0)
209 || ( (!strncasecmp(v->prop[i].name,
210 propname, strlen(propname)))
211 && (v->prop[i].name[strlen(propname)] == ';')
212 && (is_partial) ) ) {
213 if (instance == found_instance++) {
215 return(v->prop[i].name);
218 return(v->prop[i].value);
233 * \param v the vCard to purge from memory
235 void vcard_free(struct vCard *v) {
238 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
240 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
241 free(v->prop[i].name);
242 free(v->prop[i].value);
245 if (v->prop != NULL) free(v->prop);
247 memset(v, 0, sizeof(struct vCard));
255 * \brief Set a name/value pair in the card
256 * \param v vCard to inspect
257 * \param name key to set
258 * \param value the value to assign to key
259 * \param append should we append the value to an existing one?
261 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
264 if (v->magic != CTDL_VCARD_MAGIC) return; /** Self-check */
266 /** If this key is already present, replace it */
267 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
268 if (!strcasecmp(v->prop[i].name, name)) {
269 free(v->prop[i].name);
270 free(v->prop[i].value);
271 v->prop[i].name = strdup(name);
272 v->prop[i].value = strdup(value);
277 /** Otherwise, append it */
279 v->prop = realloc(v->prop,
280 (v->numprops * sizeof(struct vCardProp)) );
281 v->prop[v->numprops-1].name = strdup(name);
282 v->prop[v->numprops-1].value = strdup(value);
289 * \brief Serialize a struct vcard into a standard text/x-vcard MIME type.
290 * \param v vCard to serialize
291 * \return the serialized vCard
293 char *vcard_serialize(struct vCard *v)
300 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /** self check */
302 /** Figure out how big a buffer we need to allocate */
303 len = 64; /** for begin, end, and a little padding for safety */
304 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
306 strlen(v->prop[i].name) +
307 strlen(v->prop[i].value) + 16;
311 if (ser == NULL) return NULL;
313 safestrncpy(ser, "begin:vcard\r\n", len);
314 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
315 if (strcasecmp(v->prop[i].name, "end")) {
317 for (j=0; i<strlen(v->prop[i].value); ++i) {
318 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
322 strcat(ser, v->prop[i].name);
324 strcat(ser, ";charset=UTF-8");
327 strcat(ser, v->prop[i].value);
331 strcat(ser, "end:vcard\r\n");
339 * \brief Convert FN (Friendly Name) into N (Name)
341 * \param vname Supplied friendly-name
342 * \param n Target buffer to store Name
343 * \param vname_size Size of buffer
345 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
348 char middlename[256];
349 char honorific_prefixes[256];
350 char honorific_suffixes[256];
353 safestrncpy(buf, n, sizeof buf);
355 /* Try to intelligently convert the screen name to a
356 * fully expanded vCard name based on the number of
359 safestrncpy(lastname, "", sizeof lastname);
360 safestrncpy(firstname, "", sizeof firstname);
361 safestrncpy(middlename, "", sizeof middlename);
362 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
363 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
365 /* Honorific suffixes */
366 if (num_tokens(buf, ',') > 1) {
367 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
368 sizeof honorific_suffixes);
369 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
372 /* Find a last name */
373 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
374 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
376 /* Find honorific prefixes */
377 if (num_tokens(buf, ' ') > 2) {
378 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
379 remove_token(buf, 0, ' ');
382 /* Find a middle name */
383 if (num_tokens(buf, ' ') > 1) {
384 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
385 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
388 /* Anything left is probably the first name */
389 safestrncpy(firstname, buf, sizeof firstname);
392 /* Compose the structured name */
393 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
394 honorific_prefixes, honorific_suffixes);