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