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