4 * vCard data type implementation for the Citadel system.
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.
12 #include "webserver.h"
16 * Constructor (empty vCard)
18 struct vCard *vcard_new() {
21 v = (struct vCard *) malloc(sizeof(struct vCard));
22 if (v == NULL) return v;
24 v->magic = CTDL_VCARD_MAGIC;
33 * Constructor (supply serialized vCard)
35 struct vCard *vcard_load(char *vtext) {
39 char *namebuf, *valuebuf;
43 if (vtext == NULL) return vcard_new();
44 mycopy = strdup(vtext);
45 if (mycopy == NULL) return NULL;
47 /* First, fix this big pile o' vCard to make it more parseable.
48 * To make it easier to parse, we convert CRLF to LF, and unfold any
49 * multi-line fields into single lines.
51 for (i=0; i<strlen(mycopy); ++i) {
52 if (!strncmp(&mycopy[i], "\r\n", 2)) {
53 strcpy(&mycopy[i], &mycopy[i+1]);
55 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
56 strcpy(&mycopy[i], &mycopy[i+1]);
61 if (v == NULL) return v;
64 while (strlen(ptr)>0) {
67 colonpos = pattern2(ptr, ":");
68 nlpos = pattern2(ptr, "\n");
70 if ((nlpos > colonpos) && (colonpos > 0)) {
71 namebuf = malloc(colonpos + 1);
72 valuebuf = malloc(nlpos - colonpos + 1);
73 strncpy(namebuf, ptr, colonpos);
74 namebuf[colonpos] = 0;
75 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
76 valuebuf[nlpos-colonpos-1] = 0;
78 if (!strcasecmp(namebuf, "end")) {
81 if ( (!strcasecmp(namebuf, "begin"))
82 && (!strcasecmp(valuebuf, "vcard"))
87 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
89 v->prop = realloc(v->prop,
90 (v->numprops * sizeof(struct vCardProp))
92 v->prop[v->numprops-1].name = namebuf;
93 v->prop[v->numprops-1].value = valuebuf;
102 while ( (*ptr != '\n') && (strlen(ptr)>0) ) {
105 if (*ptr == '\n') ++ptr;
114 * Fetch the value of a particular key.
115 * If is_partial is set to 1, a partial match is ok (for example,
116 * a key of "tel;home" will satisfy a search for "tel").
117 * Set "instance" to a value higher than 0 to return subsequent instances
119 * Set "get_propname" to nonzero to fetch the property name instead of value.
121 char *vcard_get_prop(struct vCard *v, char *propname,
122 int is_partial, int instance, int get_propname) {
124 int found_instance = 0;
126 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
127 if ( (!strcasecmp(v->prop[i].name, propname))
128 || (propname[0] == 0)
129 || ( (!strncasecmp(v->prop[i].name,
130 propname, strlen(propname)))
131 && (v->prop[i].name[strlen(propname)] == ';')
132 && (is_partial) ) ) {
133 if (instance == found_instance++) {
135 return(v->prop[i].name);
138 return(v->prop[i].value);
153 void vcard_free(struct vCard *v) {
156 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
158 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
159 free(v->prop[i].name);
160 free(v->prop[i].value);
163 if (v->prop != NULL) free(v->prop);
165 memset(v, 0, sizeof(struct vCard));
173 * Set a name/value pair in the card
175 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
178 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
180 /* If this key is already present, replace it */
181 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
182 if (!strcasecmp(v->prop[i].name, name)) {
183 free(v->prop[i].name);
184 free(v->prop[i].value);
185 v->prop[i].name = strdup(name);
186 v->prop[i].value = strdup(value);
191 /* Otherwise, append it */
193 v->prop = realloc(v->prop,
194 (v->numprops * sizeof(struct vCardProp)) );
195 v->prop[v->numprops-1].name = strdup(name);
196 v->prop[v->numprops-1].value = strdup(value);
203 * Serialize a struct vcard into a standard text/x-vcard MIME type.
206 char *vcard_serialize(struct vCard *v)
212 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
214 /* Figure out how big a buffer we need to allocate */
215 len = 64; /* for begin, end, and a little padding for safety */
216 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
218 strlen(v->prop[i].name) +
219 strlen(v->prop[i].value) + 4;
223 if (ser == NULL) return NULL;
225 safestrncpy(ser, "begin:vcard\r\n", len);
226 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
227 strcat(ser, v->prop[i].name);
229 strcat(ser, v->prop[i].value);
232 strcat(ser, "end:vcard\r\n");