4 * vCard data type implementation for Citadel/UX
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>
39 * Constructor (empty vCard)
41 struct vCard *vcard_new() {
44 v = (struct vCard *) malloc(sizeof(struct vCard));
45 if (v == NULL) return v;
47 v->magic = CTDL_VCARD_MAGIC;
56 * Constructor (supply serialized vCard)
58 struct vCard *vcard_load(char *vtext) {
62 char *namebuf, *valuebuf;
66 if (vtext == NULL) return vcard_new();
67 mycopy = strdup(vtext);
68 if (mycopy == NULL) return NULL;
70 /* First, fix this big pile o' vCard to make it more parseable.
71 * To make it easier to parse, we convert CRLF to LF, and unfold any
72 * multi-line fields into single lines.
74 for (i=0; i<strlen(mycopy); ++i) {
75 if (!strncmp(&mycopy[i], "\r\n", 2)) {
76 strcpy(&mycopy[i], &mycopy[i+1]);
78 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
79 strcpy(&mycopy[i], &mycopy[i+1]);
84 if (v == NULL) return v;
87 while (strlen(ptr)>0) {
90 colonpos = pattern2(ptr, ":");
91 nlpos = pattern2(ptr, "\n");
93 if ((nlpos > colonpos) && (colonpos > 0)) {
94 namebuf = malloc(colonpos + 1);
95 valuebuf = malloc(nlpos - colonpos + 1);
96 strncpy(namebuf, ptr, colonpos);
97 namebuf[colonpos] = 0;
98 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
99 valuebuf[nlpos-colonpos-1] = 0;
101 if (!strcasecmp(namebuf, "end")) {
104 if ( (!strcasecmp(namebuf, "begin"))
105 && (!strcasecmp(valuebuf, "vcard"))
110 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
112 v->prop = realloc(v->prop,
113 (v->numprops * sizeof(struct vCardProp))
115 v->prop[v->numprops-1].name = namebuf;
116 v->prop[v->numprops-1].value = valuebuf;
125 while ( (*ptr != '\n') && (strlen(ptr)>0) ) {
128 if (*ptr == '\n') ++ptr;
137 * Fetch the value of a particular key.
138 * If is_partial is set to 1, a partial match is ok (for example,
139 * a key of "tel;home" will satisfy a search for "tel").
140 * Set "instance" to a value higher than 0 to return subsequent instances
142 * Set "get_propname" to nonzero to fetch the property name instead of value.
144 char *vcard_get_prop(struct vCard *v, char *propname,
145 int is_partial, int instance, int get_propname) {
147 int found_instance = 0;
149 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
150 if ( (!strcasecmp(v->prop[i].name, propname))
151 || (propname[0] == 0)
152 || ( (!strncasecmp(v->prop[i].name,
153 propname, strlen(propname)))
154 && (v->prop[i].name[strlen(propname)] == ';')
155 && (is_partial) ) ) {
156 if (instance == found_instance++) {
158 return(v->prop[i].name);
161 return(v->prop[i].value);
176 void vcard_free(struct vCard *v) {
179 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
181 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
182 free(v->prop[i].name);
183 free(v->prop[i].value);
186 if (v->prop != NULL) free(v->prop);
188 memset(v, 0, sizeof(struct vCard));
196 * Set a name/value pair in the card
198 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
201 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
203 /* If this key is already present, replace it */
204 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
205 if (!strcasecmp(v->prop[i].name, name)) {
206 free(v->prop[i].name);
207 free(v->prop[i].value);
208 v->prop[i].name = strdup(name);
209 v->prop[i].value = strdup(value);
214 /* Otherwise, append it */
216 v->prop = realloc(v->prop,
217 (v->numprops * sizeof(struct vCardProp)) );
218 v->prop[v->numprops-1].name = strdup(name);
219 v->prop[v->numprops-1].value = strdup(value);
226 * Serialize a struct vcard into a standard text/x-vcard MIME type.
229 char *vcard_serialize(struct vCard *v)
235 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
237 /* Figure out how big a buffer we need to allocate */
238 len = 64; /* for begin, end, and a little padding for safety */
239 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
241 strlen(v->prop[i].name) +
242 strlen(v->prop[i].value) + 4;
246 if (ser == NULL) return NULL;
248 safestrncpy(ser, "begin:vcard\r\n", len);
249 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
250 strcat(ser, v->prop[i].name);
252 strcat(ser, v->prop[i].value);
255 strcat(ser, "end:vcard\r\n");