4 * vCard implementation for Citadel
6 * Copyright (C) 1999 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>
40 * Constructor (empty vCard)
42 struct vCard *vcard_new() {
45 v = (struct vCard *) malloc(sizeof(struct vCard));
46 if (v == NULL) return v;
48 v->magic = CTDL_VCARD_MAGIC;
57 * Add a property to a vCard
59 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
61 v->prop = realloc(v->prop,
62 (v->numprops * sizeof(char *) * 2) );
63 v->prop[v->numprops-1].name = strdup(propname);
64 v->prop[v->numprops-1].value = strdup(propvalue);
70 * Constructor (supply serialized vCard)
72 struct vCard *vcard_load(char *vtext) {
76 char *namebuf, *valuebuf;
80 mycopy = strdup(vtext);
81 if (mycopy == NULL) return NULL;
83 /* First, fix this big pile o' vCard to make it more parseable.
84 * To make it easier to parse, we convert CRLF to LF, and unfold any
85 * multi-line fields into single lines.
87 for (i=0; i<strlen(mycopy); ++i) {
88 if (!strncmp(&mycopy[i], "\r\n", 2)) {
89 strcpy(&mycopy[i], &mycopy[i+1]);
91 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
92 strcpy(&mycopy[i], &mycopy[i+1]);
97 if (v == NULL) return v;
100 while (strlen(ptr)>0) {
103 colonpos = pattern2(ptr, ":");
104 nlpos = pattern2(ptr, "\n");
106 if ((nlpos > colonpos) && (colonpos > 0)) {
107 namebuf = malloc(colonpos + 1);
108 valuebuf = malloc(nlpos - colonpos + 1);
109 strncpy(namebuf, ptr, colonpos);
110 namebuf[colonpos] = 0;
111 strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
112 valuebuf[nlpos-colonpos-1] = 0;
114 if ( (!strcasecmp(namebuf, "end"))
115 && (!strcasecmp(valuebuf, "vcard")) ) valid = 0;
116 if ( (!strcasecmp(namebuf, "begin"))
117 && (!strcasecmp(valuebuf, "vcard")) ) valid = 1;
119 if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
121 v->prop = realloc(v->prop,
122 (v->numprops * sizeof(char *) * 2) );
123 v->prop[v->numprops-1].name = namebuf;
124 v->prop[v->numprops-1].value = valuebuf;
133 while ( (*ptr != '\n') && (strlen(ptr)>0) ) {
136 if (*ptr == '\n') ++ptr;
145 * Fetch the value of a particular key.
146 * If is_partial is set to 1, a partial match is ok (for example,
147 * a key of "tel;home" will satisfy a search for "tel").
148 * Set "instance" to a value higher than 0 to return subsequent instances
150 * Set "get_propname" to nonzero to fetch the property name instead of value.
152 char *vcard_get_prop(struct vCard *v, char *propname,
153 int is_partial, int instance, int get_propname) {
155 int found_instance = 0;
157 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
158 if ( (!strcasecmp(v->prop[i].name, propname))
159 || (propname[0] == 0)
160 || ( (!strncasecmp(v->prop[i].name,
161 propname, strlen(propname)))
162 && (v->prop[i].name[strlen(propname)] == ';')
163 && (is_partial) ) ) {
164 if (instance == found_instance++) {
166 return(v->prop[i].name);
169 return(v->prop[i].value);
182 void vcard_free(struct vCard *v) {
185 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
187 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
188 free(v->prop[i].name);
189 free(v->prop[i].value);
192 if (v->prop != NULL) free(v->prop);
194 memset(v, 0, sizeof(struct vCard));
202 * Set a name/value pair in the card
204 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
207 if (v->magic != CTDL_VCARD_MAGIC) return; /* Self-check */
209 /* If this key is already present, replace it */
210 if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
211 if (!strcasecmp(v->prop[i].name, name)) {
212 free(v->prop[i].name);
213 free(v->prop[i].value);
214 v->prop[i].name = strdup(name);
215 v->prop[i].value = strdup(value);
220 /* Otherwise, append it */
222 v->prop = realloc(v->prop,
223 (v->numprops * sizeof(char *) * 2) );
224 v->prop[v->numprops-1].name = strdup(name);
225 v->prop[v->numprops-1].value = strdup(value);
232 * Serialize a struct vcard into a standard text/x-vcard MIME type.
235 char *vcard_serialize(struct vCard *v)
241 if (v->magic != CTDL_VCARD_MAGIC) return NULL; /* self check */
243 /* Figure out how big a buffer we need to allocate */
244 len = 64; /* for begin, end, and a little padding for safety */
245 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
247 strlen(v->prop[i].name) +
248 strlen(v->prop[i].value) + 4;
252 if (ser == NULL) return NULL;
254 strcpy(ser, "begin:vcard\r\n");
255 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
256 strcat(ser, v->prop[i].name);
258 strcat(ser, v->prop[i].value);
261 strcat(ser, "end:vcard\r\n");