* Added some rudimentary support for displaying vCards as card-looking things
[citadel.git] / webcit / vcard.c
1 /*
2  * $Id$
3  *
4  * vCard implementation for Citadel/UX
5  *
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.
9  */
10
11
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <syslog.h>
34
35 #include "webcit.h"
36 #include "vcard.h"
37
38 /* 
39  * Constructor (empty vCard)
40  */
41 struct vCard *vcard_new() {
42         struct vCard *v;
43
44         v = (struct vCard *) malloc(sizeof(struct vCard));
45         if (v == NULL) return v;
46
47         v->magic = CTDL_VCARD_MAGIC;
48         v->numprops = 0;
49         v->prop = NULL;
50
51         return v;
52 }
53
54
55 /*
56  * Constructor (supply serialized vCard)
57  */
58 struct vCard *vcard_load(char *vtext) {
59         struct vCard *v;
60         int valid = 0;
61         char *mycopy, *ptr;
62         char *namebuf, *valuebuf;
63         int i;
64         int colonpos, nlpos;
65
66         mycopy = strdup(vtext);
67         if (mycopy == NULL) return NULL;
68
69         /* First, fix this big pile o' vCard to make it more parseable.
70          * To make it easier to parse, we convert CRLF to LF, and unfold any
71          * multi-line fields into single lines.
72          */
73         for (i=0; i<strlen(mycopy); ++i) {
74                 if (!strncmp(&mycopy[i], "\r\n", 2)) {
75                         strcpy(&mycopy[i], &mycopy[i+1]);
76                 }
77                 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
78                         strcpy(&mycopy[i], &mycopy[i+1]);
79                 }
80         }
81
82         v = vcard_new();
83         if (v == NULL) return v;
84
85         ptr = mycopy;
86         while (strlen(ptr)>0) {
87                 colonpos = (-1);
88                 nlpos = (-1);
89                 colonpos = pattern2(ptr, ":");
90                 nlpos = pattern2(ptr, "\n");
91
92                 if (nlpos > colonpos > 0) {
93                         namebuf = malloc(colonpos + 1);
94                         valuebuf = malloc(nlpos - colonpos + 1);
95                         strncpy(namebuf, ptr, colonpos);
96                         namebuf[colonpos] = 0;
97                         strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
98                         valuebuf[nlpos-colonpos-1] = 0;
99
100                         if ( (!strcasecmp(namebuf, "end"))
101                            && (!strcasecmp(valuebuf, "vcard")) )  valid = 0;
102                         if ( (!strcasecmp(namebuf, "begin"))
103                            && (!strcasecmp(valuebuf, "vcard")) )  valid = 1;
104
105                         if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
106                                 ++v->numprops;
107                                 v->prop = realloc(v->prop,
108                                         (v->numprops * sizeof(char *) * 2) );
109                                 v->prop[v->numprops-1].name = namebuf;
110                                 v->prop[v->numprops-1].value = valuebuf;
111                         } 
112                         else {
113                                 free(namebuf);
114                                 free(valuebuf);
115                         }
116
117                 }
118
119                 while ( (*ptr != '\n') && (strlen(ptr)>0) ) {
120                         ++ptr;
121                 }
122                 if (*ptr == '\n') ++ptr;
123         }
124
125         free(mycopy);
126         return v;
127 }
128
129
130 /*
131  * Fetch the value of a particular key
132  * If is_partial is set to 1, a partial match is ok (for example,
133  * a key of "tel;home" will satisfy a search for "tel")
134  * Set "instance" to a value higher than 0 to return subsequent instances
135  * of the same key
136  */
137 char *vcard_get_prop(struct vCard *v, char *propname,
138                         int is_partial, int instance) {
139         int i;
140         int found_instance = 0;
141
142         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
143                 if ( (!strcasecmp(v->prop[i].name, propname))
144                    || (  (!strncasecmp(v->prop[i].name,
145                                         propname, strlen(propname)))
146                          && (v->prop[i].name[strlen(propname)] == ';')
147                          && (is_partial) ) ) {
148                         if (instance == found_instance++) {
149                                 return(v->prop[i].value);
150                         }
151                 }
152         }
153
154         return NULL;
155 }
156
157
158
159
160 /*
161  * Destructor
162  */
163 void vcard_free(struct vCard *v) {
164         int i;
165         
166         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
167         
168         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
169                 free(v->prop[i].name);
170                 free(v->prop[i].value);
171         }
172
173         if (v->prop != NULL) free(v->prop);
174         
175         memset(v, 0, sizeof(struct vCard));
176         free(v);
177 }
178
179
180
181
182 /*
183  * Set a name/value pair in the card
184  */
185 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
186         int i;
187
188         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
189
190         /* If this key is already present, replace it */
191         if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
192                 if (!strcasecmp(v->prop[i].name, name)) {
193                         free(v->prop[i].name);
194                         free(v->prop[i].value);
195                         v->prop[i].name = strdup(name);
196                         v->prop[i].value = strdup(value);
197                         return;
198                 }
199         }
200
201         /* Otherwise, append it */
202         ++v->numprops;
203         v->prop = realloc(v->prop,
204                 (v->numprops * sizeof(char *) * 2) );
205         v->prop[v->numprops-1].name = strdup(name);
206         v->prop[v->numprops-1].value = strdup(value);
207 }
208
209
210
211
212 /*
213  * Serialize a struct vcard into a standard text/x-vcard MIME type.
214  *
215  */
216 char *vcard_serialize(struct vCard *v)
217 {
218         char *ser;
219         int i;
220         size_t len;
221
222         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /* self check */
223
224         /* Figure out how big a buffer we need to allocate */
225         len = 64;       /* for begin, end, and a little padding for safety */
226         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
227                 len = len +
228                         strlen(v->prop[i].name) +
229                         strlen(v->prop[i].value) + 4;
230         }
231
232         ser = malloc(len);
233         if (ser == NULL) return NULL;
234
235         strcpy(ser, "begin:vcard\r\n");
236         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
237                 strcat(ser, v->prop[i].name);
238                 strcat(ser, ":");
239                 strcat(ser, v->prop[i].value);
240                 strcat(ser, "\r\n");
241         }
242         strcat(ser, "end:vcard\r\n");
243
244         return ser;
245 }