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