HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / vcard.c
1 /*
2  * $Id$
3  *
4  * vCard implementation for Citadel
5  *
6  * Copyright (C) 1999-2005 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 <string.h>
34 #include <libcitadel.h>
35 #include "citadel.h"
36 #include "server.h"
37 #include "support.h"
38 #include "vcard.h"
39
40
41
42
43
44 /** 
45  * \brief Constructor (empty vCard)
46  * \return an empty vcard
47  */
48 struct vCard *vcard_new() {
49         struct vCard *v;
50
51         v = (struct vCard *) malloc(sizeof(struct vCard));
52         if (v == NULL) return v;
53
54         v->magic = CTDL_VCARD_MAGIC;
55         v->numprops = 0;
56         v->prop = NULL;
57
58         return v;
59 }
60
61 /**
62  * \brief       Remove the "charset=" attribute from a vCard property name
63  *
64  * \param       strbuf          The property name string to be stripped
65  */
66 void remove_charset_attribute(char *strbuf)
67 {
68         int i, t;
69         char compare[256];
70
71         t = num_tokens(strbuf, ';');
72         for (i=0; i<t; ++i) {
73                 extract_token(compare, strbuf, i, ';', sizeof compare);
74                 striplt(compare);
75                 if (!strncasecmp(compare, "charset=", 8)) {
76                         remove_token(strbuf, i, ';');
77                 }
78         }
79         if (!IsEmptyStr(strbuf)) {
80                 if (strbuf[strlen(strbuf)-1] == ';') {
81                         strbuf[strlen(strbuf)-1] = 0;
82                 }
83         }
84 }
85
86
87 /*
88  * \brief       Add a property to a vCard
89  *
90  * \param       v               vCard structure to which we are adding
91  * \param       propname        name of new property
92  * \param       propvalue       value of new property
93  */
94 void vcard_add_prop(struct vCard *v, char *propname, char *propvalue) {
95         ++v->numprops;
96         v->prop = realloc(v->prop,
97                 (v->numprops * sizeof(struct vCardProp)) );
98         v->prop[v->numprops-1].name = strdup(propname);
99         v->prop[v->numprops-1].value = strdup(propvalue);
100 }
101
102
103
104 /**
105  * \brief Constructor (supply serialized vCard)
106  * \param vtext the text to parse into the new vcard
107  * \return the parsed VCard
108  */
109 struct vCard *vcard_load(char *vtext) {
110         struct vCard *v;
111         int valid = 0;
112         char *mycopy, *ptr;
113         char *namebuf, *valuebuf;
114         int i;
115         int colonpos, nlpos;
116
117         if (vtext == NULL) return vcard_new();
118         mycopy = strdup(vtext);
119         if (mycopy == NULL) return NULL;
120
121         /**
122          * First, fix this big pile o' vCard to make it more parseable.
123          * To make it easier to parse, we convert CRLF to LF, and unfold any
124          * multi-line fields into single lines.
125          */
126         for (i=0; !IsEmptyStr(&mycopy[i]); ++i) {
127                 if (!strncmp(&mycopy[i], "\r\n", 2)) {
128                         strcpy(&mycopy[i], &mycopy[i+1]);
129                 }
130                 if ( (mycopy[i]=='\n') && (isspace(mycopy[i+1])) ) {
131                         strcpy(&mycopy[i], &mycopy[i+1]);
132                 }
133         }
134
135         v = vcard_new();
136         if (v == NULL) return v;
137
138         ptr = mycopy;
139         while (!IsEmptyStr(ptr)) {
140                 colonpos = (-1);
141                 nlpos = (-1);
142                 colonpos = pattern2(ptr, ":");
143                 nlpos = pattern2(ptr, "\n");
144
145                 if ((nlpos > colonpos) && (colonpos > 0)) {
146                         namebuf = malloc(colonpos + 1);
147                         valuebuf = malloc(nlpos - colonpos + 1);
148                         strncpy(namebuf, ptr, colonpos);
149                         namebuf[colonpos] = 0;
150                         strncpy(valuebuf, &ptr[colonpos+1], nlpos-colonpos-1);
151                         valuebuf[nlpos-colonpos-1] = 0;
152
153                         if (!strcasecmp(namebuf, "end")) {
154                                 valid = 0;
155                         }
156                         if (    (!strcasecmp(namebuf, "begin"))
157                                 && (!strcasecmp(valuebuf, "vcard"))
158                         ) {
159                                 valid = 1;
160                         }
161
162                         if ( (valid) && (strcasecmp(namebuf, "begin")) ) {
163                                 remove_charset_attribute(namebuf);
164                                 ++v->numprops;
165                                 v->prop = realloc(v->prop,
166                                         (v->numprops * sizeof(struct vCardProp))
167                                 );
168                                 v->prop[v->numprops-1].name = namebuf;
169                                 v->prop[v->numprops-1].value = valuebuf;
170                         } 
171                         else {
172                                 free(namebuf);
173                                 free(valuebuf);
174                         }
175
176                 }
177
178                 while ( (*ptr != '\n') && (!IsEmptyStr(ptr)) ) {
179                         ++ptr;
180                 }
181                 if (*ptr == '\n') ++ptr;
182         }
183
184         free(mycopy);
185         return v;
186 }
187
188
189 /**
190  * \brief Fetch the value of a particular key.
191  * If is_partial is set to 1, a partial match is ok (for example,
192  * a key of "tel;home" will satisfy a search for "tel").
193  * Set "instance" to a value higher than 0 to return subsequent instances
194  * of the same key.
195  * Set "get_propname" to nonzero to fetch the property name instead of value.
196  * \param v vCard to get keyvalue from
197  * \param propname key to retrieve
198  * \param is_partial dunno???
199  * \param instance if >0 return a later token of the value
200  * \param get_propname if nonzero get the real property name???
201  * \return the requested value / token / propertyname
202  */
203 char *vcard_get_prop(struct vCard *v, char *propname,
204                         int is_partial, int instance, int get_propname) {
205         int i;
206         int found_instance = 0;
207
208         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
209                 if ( (!strcasecmp(v->prop[i].name, propname))
210                    || (propname[0] == 0)
211                    || (  (!strncasecmp(v->prop[i].name,
212                                         propname, strlen(propname)))
213                          && (v->prop[i].name[strlen(propname)] == ';')
214                          && (is_partial) ) ) {
215                         if (instance == found_instance++) {
216                                 if (get_propname) {
217                                         return(v->prop[i].name);
218                                 }
219                                 else {
220                                         return(v->prop[i].value);
221                                 }
222                         }
223                 }
224         }
225
226         return NULL;
227 }
228
229
230
231
232 /**
233  * \brief Destructor
234  * kill a vCard
235  * \param v the vCard to purge from memory
236  */
237 void vcard_free(struct vCard *v) {
238         int i;
239         
240         if (v->magic != CTDL_VCARD_MAGIC) return;       /* Self-check */
241         
242         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
243                 free(v->prop[i].name);
244                 free(v->prop[i].value);
245         }
246
247         if (v->prop != NULL) free(v->prop);
248         
249         memset(v, 0, sizeof(struct vCard));
250         free(v);
251 }
252
253
254
255
256 /**
257  * \brief Set a name/value pair in the card
258  * \param v vCard to inspect
259  * \param name key to set
260  * \param value the value to assign to key
261  * \param append should we append the value to an existing one?
262  */
263 void vcard_set_prop(struct vCard *v, char *name, char *value, int append) {
264         int i;
265
266         if (v->magic != CTDL_VCARD_MAGIC) return;       /** Self-check */
267
268         /** If this key is already present, replace it */
269         if (!append) if (v->numprops) for (i=0; i<(v->numprops); ++i) {
270                 if (!strcasecmp(v->prop[i].name, name)) {
271                         free(v->prop[i].name);
272                         free(v->prop[i].value);
273                         v->prop[i].name = strdup(name);
274                         v->prop[i].value = strdup(value);
275                         return;
276                 }
277         }
278
279         /** Otherwise, append it */
280         ++v->numprops;
281         v->prop = realloc(v->prop,
282                 (v->numprops * sizeof(struct vCardProp)) );
283         v->prop[v->numprops-1].name = strdup(name);
284         v->prop[v->numprops-1].value = strdup(value);
285 }
286
287
288
289
290 /**
291  * \brief Serialize a 'struct vcard' into an actual vcard.
292  * \param v vCard to serialize
293  * \return the serialized vCard
294  */
295 char *vcard_serialize(struct vCard *v)
296 {
297         char *ser;
298         int i, j;
299         size_t len;
300         int is_utf8 = 0;
301
302         if (v->magic != CTDL_VCARD_MAGIC) return NULL;  /** self check */
303
304         /** Figure out how big a buffer we need to allocate */
305         len = 64;       /** for begin, end, and a little padding for safety */
306         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
307                 len = len +
308                         strlen(v->prop[i].name) +
309                         strlen(v->prop[i].value) + 16;
310         }
311
312         ser = malloc(len);
313         if (ser == NULL) return NULL;
314
315         safestrncpy(ser, "begin:vcard\r\n", len);
316         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
317                 if ( (strcasecmp(v->prop[i].name, "end")) && (v->prop[i].value != NULL) ) {
318                         is_utf8 = 0;
319                         for (j=0; !IsEmptyStr(&v->prop[i].value[j]); ++j) {
320                                 if ( (v->prop[i].value[j] < 32) || (v->prop[i].value[j] > 126) ) {
321                                         is_utf8 = 1;
322                                 }
323                         }
324                         strcat(ser, v->prop[i].name);
325                         if (is_utf8) {
326                                 strcat(ser, ";charset=UTF-8");
327                         }
328                         strcat(ser, ":");
329                         strcat(ser, v->prop[i].value);
330                         strcat(ser, "\r\n");
331                 }
332         }
333         strcat(ser, "end:vcard\r\n");
334
335         return ser;
336 }
337
338
339
340 /*
341  * \brief       Convert FN (Friendly Name) into N (Name)
342  *
343  * \param       vname           Supplied friendly-name
344  * \param       n               Target buffer to store Name
345  * \param       vname_size      Size of buffer
346  */
347 void vcard_fn_to_n(char *vname, char *n, size_t vname_size) {
348         char lastname[256];
349         char firstname[256];
350         char middlename[256];
351         char honorific_prefixes[256];
352         char honorific_suffixes[256];
353         char buf[256];
354
355         safestrncpy(buf, n, sizeof buf);
356
357         /* Try to intelligently convert the screen name to a
358          * fully expanded vCard name based on the number of
359          * words in the name
360          */
361         safestrncpy(lastname, "", sizeof lastname);
362         safestrncpy(firstname, "", sizeof firstname);
363         safestrncpy(middlename, "", sizeof middlename);
364         safestrncpy(honorific_prefixes, "", sizeof honorific_prefixes);
365         safestrncpy(honorific_suffixes, "", sizeof honorific_suffixes);
366
367         /* Honorific suffixes */
368         if (num_tokens(buf, ',') > 1) {
369                 extract_token(honorific_suffixes, buf, (num_tokens(buf, ' ') - 1), ',',
370                         sizeof honorific_suffixes);
371                 remove_token(buf, (num_tokens(buf, ',') - 1), ',');
372         }
373
374         /* Find a last name */
375         extract_token(lastname, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof lastname);
376         remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
377
378         /* Find honorific prefixes */
379         if (num_tokens(buf, ' ') > 2) {
380                 extract_token(honorific_prefixes, buf, 0, ' ', sizeof honorific_prefixes);
381                 remove_token(buf, 0, ' ');
382         }
383
384         /* Find a middle name */
385         if (num_tokens(buf, ' ') > 1) {
386                 extract_token(middlename, buf, (num_tokens(buf, ' ') - 1), ' ', sizeof middlename);
387                 remove_token(buf, (num_tokens(buf, ' ') - 1), ' ');
388         }
389
390         /* Anything left is probably the first name */
391         safestrncpy(firstname, buf, sizeof firstname);
392         striplt(firstname);
393
394         /* Compose the structured name */
395         snprintf(vname, vname_size, "%s;%s;%s;%s;%s", lastname, firstname, middlename,
396                 honorific_prefixes, honorific_suffixes);
397 }
398
399
400
401