* Cleaned up the rcs/cvs Id tags and leading comments at the top of some files
[citadel.git] / webcit / vcard.c
1 /*
2  * $Id$
3  *
4  * vCard data type implementation for Citadel/UX
5  *
6  * Copyright (C) 1999-2004 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) && (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  * Set "get_propname" to nonzero to fetch the property name instead of value.
137  */
138 char *vcard_get_prop(struct vCard *v, char *propname,
139                         int is_partial, int instance, int get_propname) {
140         int i;
141         int found_instance = 0;
142
143         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
144                 if ( (!strcasecmp(v->prop[i].name, propname))
145                    || (propname[0] == 0)
146                    || (  (!strncasecmp(v->prop[i].name,
147                                         propname, strlen(propname)))
148                          && (v->prop[i].name[strlen(propname)] == ';')
149                          && (is_partial) ) ) {
150                         if (instance == found_instance++) {
151                                 if (get_propname) {
152                                         return(v->prop[i].name);
153                                 }
154                                 else {
155                                         return(v->prop[i].value);
156                                 }
157                         }
158                 }
159         }
160
161         return NULL;
162 }
163
164
165
166
167 /*
168  * Destructor
169  */
170 void vcard_free(struct vCard *v) {
171         int i;
172         
173         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
174         
175         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
176                 free(v->prop[i].name);
177                 free(v->prop[i].value);
178         }
179
180         if (v->prop != NULL) free(v->prop);
181         
182         memset(v, 0, sizeof(struct vCard));
183         free(v);
184 }
185
186
187
188
189 /*
190  * Set a name/value pair in the card
191  */
192 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
193         int i;
194
195         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
196
197         /* If this key is already present, replace it */
198         if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
199                 if (!strcasecmp(v->prop[i].name, name)) {
200                         free(v->prop[i].name);
201                         free(v->prop[i].value);
202                         v->prop[i].name = strdup(name);
203                         v->prop[i].value = strdup(value);
204                         return;
205                 }
206         }
207
208         /* Otherwise, append it */
209         ++v->numprops;
210         v->prop = realloc(v->prop,
211                 (v->numprops * sizeof(char *) * 2) );
212         v->prop[v->numprops-1].name = strdup(name);
213         v->prop[v->numprops-1].value = strdup(value);
214 }
215
216
217
218
219 /*
220  * Serialize a struct vcard into a standard text/x-vcard MIME type.
221  *
222  */
223 char *vcard_serialize(struct vCard *v)
224 {
225         char *ser;
226         int i;
227         size_t len;
228
229         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /* self check */
230
231         /* Figure out how big a buffer we need to allocate */
232         len = 64;       /* for begin, end, and a little padding for safety */
233         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
234                 len = len +
235                         strlen(v->prop[i].name) +
236                         strlen(v->prop[i].value) + 4;
237         }
238
239         ser = malloc(len);
240         if (ser == NULL) return NULL;
241
242         strcpy(ser, "begin:vcard\r\n");
243         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
244                 strcat(ser, v->prop[i].name);
245                 strcat(ser, ":");
246                 strcat(ser, v->prop[i].value);
247                 strcat(ser, "\r\n");
248         }
249         strcat(ser, "end:vcard\r\n");
250
251         return ser;
252 }