1 // vCard implementation for Citadel
3 // Copyright (C) 1999-2023 by the citadel.org development team.
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
20 #include <libcitadel.h>
23 // Constructor (empty vCard)
24 // Returns an empty vcard
25 struct vCard *vcard_new() {
28 v = (struct vCard *) malloc(sizeof(struct vCard));
29 if (v == NULL) return v;
31 v->magic = CTDL_VCARD_MAGIC;
38 // Remove the "charset=" attribute from a vCard property name
39 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)) {
52 if (strbuf[strlen(strbuf)-1] == ';') {
53 strbuf[strlen(strbuf)-1] = 0;
59 // Add a property to a vCard
61 // v vCard structure to which we are adding
62 // propname name of new property
63 // propvalue value of new property
64 void vcard_add_prop(struct vCard *v, const char *propname, const char *propvalue) {
66 v->prop = realloc(v->prop, (v->numprops * sizeof(struct vCardProp)) );
67 v->prop[v->numprops-1].name = strdup(propname);
68 v->prop[v->numprops-1].value = strdup(propvalue);
72 // Constructor - returns a new struct vcard given a serialized vcard
73 struct vCard *VCardLoad(StrBuf *vbtext) {
74 return vcard_load((char*)ChrPtr(vbtext));
78 // Constructor - returns a new struct vcard given a serialized vcard
79 struct vCard *vcard_load(char *vtext) {
83 char *namebuf, *valuebuf;
87 if (vtext == NULL) return vcard_new();
88 mycopy = strdup(vtext);
89 if (mycopy == NULL) return NULL;
91 // First, fix this big pile o' vCard to make it more parseable.
92 // To make it easier to parse, we convert CRLF to LF, and unfold any
93 // multi-line fields into single lines.
94 for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
95 if (!strncmp(&mycopy[i], "\r\n", 2)) {
96 strcpy(&mycopy[i], &mycopy[i+1]);
98 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
99 strcpy(&mycopy[i], &mycopy[i+1]);
110 while (!IsEmptyStr(ptr)) {
111 colonpos = pattern2(ptr, ":");
112 nlpos = pattern2(ptr, "\n");
114 if ((nlpos > colonpos) && (colonpos > 0)) {
115 namebuf = malloc(colonpos + 1);
116 valuebuf = malloc(nlpos - colonpos + 1);
117 memcpy(namebuf, ptr, colonpos);
118 namebuf[colonpos] = '\0';
119 memcpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
120 valuebuf[nlpos-colonpos-1] = '\0';
122 if (!strcasecmp(namebuf, "end")) {
125 if ( (!strcasecmp(namebuf, "begin"))
126 && (!strcasecmp(valuebuf, "vcard"))
131 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
132 remove_charset_attribute(namebuf);
134 v->prop = realloc(v->prop,
135 (v->numprops * sizeof(struct vCardProp))
137 v->prop[v->numprops-1].name = namebuf;
138 v->prop[v->numprops-1].value = valuebuf;
147 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
150 if (*ptr == '\n') ++ptr;
158 // Fetch the value of a particular key.
159 // If is_partial is set to 1, a partial match is ok (for example,
160 // a key of "tel;home" will satisfy a search for "tel").
161 // Set "instance" to a value higher than 0 to return subsequent instances
164 // Set "get_propname" to nonzero to fetch the property name instead of value.
165 // v vCard to get keyvalue from
166 // propname key to retrieve
168 // instance if nonzero return a later token of the value
169 // get_propname if nonzero get the real property name???
171 // returns the requested value / token / propertyname
172 char *vcard_get_prop(struct vCard *v, char *propname, int is_partial, int instance, int get_propname) {
174 int found_instance = 0;
176 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
177 if ( (!strcasecmp(v->prop[i].name, propname))
178 || (propname[0] == 0)
179 || ( (!strncasecmp(v->prop[i].name,
180 propname, strlen(propname)))
181 && (v->prop[i].name[strlen(propname)] == ';')
182 && (is_partial) ) ) {
183 if (instance == found_instance++) {
185 return(v->prop[i].name);
188 return(v->prop[i].value);
199 void vcard_free(struct vCard *v) {
202 if (v->magic != CTDL_VCARD_MAGIC) return; // Self-check
204 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
205 free(v->prop[i].name);
206 free(v->prop[i].value);
209 if (v->prop != NULL) free(v->prop);
211 memset(v, 0, sizeof(struct vCard));
216 // Set a name/value pair in the card
217 // v vCard to manipulate
219 // value the value to assign to key
220 // append if nonzero, append rather than replace if this key already exists.
221 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
224 if (v->magic != CTDL_VCARD_MAGIC) return; // Self-check
226 // If this key is already present, replace it
227 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
228 if (!strcasecmp(v->prop[i].name, name)) {
229 free(v->prop[i].name);
230 free(v->prop[i].value);
231 v->prop[i].name = strdup(name);
232 v->prop[i].value = strdup(value);
237 // Otherwise, append it
239 v->prop = realloc(v->prop,
240 (v->numprops * sizeof(struct vCardProp)) );
241 v->prop[v->numprops-1].name = strdup(name);
242 v->prop[v->numprops-1].value = strdup(value);
246 // Serialize a 'struct vcard' into an actual vcard.
247 char *vcard_serialize(struct vCard *v) {
253 if (v == NULL) return NULL; // self check
254 if (v->magic != CTDL_VCARD_MAGIC) return NULL; // self check
256 // Set the vCard version number to 2.1 at this time.
257 vcard_set_prop(v, "VERSION", "2.1", 0);
259 // Figure out how big a buffer we need to allocate
260 len = 64; // for begin, end, and a little padding for safety
261 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
263 strlen(v->prop[i].name) +
264 strlen(v->prop[i].value) + 16;
268 if (ser == NULL) return NULL;
270 safestrncpy(ser, "begin:vcard\r\n", len);
271 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
272 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
274 for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
275 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
279 strcat(ser, v->prop[i].name);
281 strcat(ser, ";charset=UTF-8");
284 strcat(ser, v->prop[i].value);
288 strcat(ser, "end:vcard\r\n");
293 // Convert FN (Friendly Name) into N (Name)
295 // vname Supplied friendly-name
296 // n Target buffer to store Name
297 // vname_size Size of buffer
298 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
301 char middlename[256];
302 char honorific_prefixes[256];
303 char honorific_suffixes[256];
306 safestrncpy(buf, n, sizeof buf);
308 // Try to intelligently convert the screen name to a fully expanded vCard name based on the number of words in the name
309 safestrncpy(lastname, "", sizeof lastname);
310 safestrncpy(firstname, "", sizeof firstname);
311 safestrncpy(middlename, "", sizeof middlename);
312 safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
313 safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
315 // Honorific suffixes
316 if (num_tokens(buf, ',') > 1) {
317 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
318 sizeof honorific_suffixes);
319 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
323 extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
324 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
326 // Find honorific prefixes
327 if (num_tokens(buf, ' ') > 2) {
328 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
329 remove_token(buf, 0, ' ');
332 // Find a middle name
333 if (num_tokens(buf, ' ') > 1) {
334 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
335 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
338 // Anything left is probably the first name
339 safestrncpy(firstname, buf, sizeof firstname);
340 string_trim(firstname);
342 // Compose the structured name
343 snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename, honorific_prefixes, honorific_suffixes);