]> code.citadel.org Git - citadel.git/blob - citadel/vcard.c
* "read my vCard" and "write my vCard" are written and tested.
[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 *new_vcard() {
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 *load_vcard(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 = new_vcard();
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 /*
128  * Destructor
129  */
130 void free_vcard(struct vCard *v) {
131         int i;
132
133         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
134         
135         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
136                 phree(v->prop[i].name);
137                 phree(v->prop[i].value);
138         }
139
140         if (v->prop != NULL) phree(v->prop);
141         
142         memset(v, 0, sizeof(struct vCard));
143 }
144
145
146 /*
147  * Set a name/value pair in the card
148  */
149 void set_prop(struct vCard *v, char *name, char *value) {
150         int i;
151
152         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
153
154         /* If this key is already present, replace it */
155         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
156                 if (!strcasecmp(v->prop[i].name, name)) {
157                         phree(v->prop[i].name);
158                         phree(v->prop[i].value);
159                         v->prop[i].name = strdoop(name);
160                         v->prop[i].value = strdoop(value);
161                         return;
162                 }
163         }
164
165         /* Otherwise, append it */
166         ++v->numprops;
167         v->prop = reallok(v->prop,
168                 (v->numprops * sizeof(char *) * 2) );
169         v->prop[v->numprops-1].name = strdoop(name);
170         v->prop[v->numprops-1].value = (value);
171 }
172
173
174
175
176 /*
177  * Serialize a struct vcard into a standard text/x-vcard MIME type.
178  *
179  */
180 char *serialize_vcard(struct vCard *v)
181 {
182         char *ser;
183         int i;
184         size_t len;
185
186         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /* self check */
187
188         /* Figure out how big a buffer we need to allocate */
189         len = 64;       /* for begin, end, and a little padding for safety */
190         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
191                 len = len +
192                         strlen(v->prop[i].name) +
193                         strlen(v->prop[i].value) + 4;
194         }
195
196         ser = mallok(len);
197         if (ser == NULL) return NULL;
198
199         strcpy(ser, "begin:vcard\r\n");
200         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
201                 strcat(ser, v->prop[i].name);
202                 strcat(ser, ":");
203                 strcat(ser, v->prop[i].name);
204                 strcat(ser, "\r\n");
205         }
206         strcat(ser, "end:vcard\r\n");
207
208         return ser;
209 }