Added TYPE='less parsing support to SilcVCard.
[silc.git] / lib / silcutil / silcvcard.c
1 /*
2
3   silcvcard.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20 /* Implementation of the VCard (RFC 2426) */
21
22 #include "silcincludes.h"
23
24 #define VCARD_HEADER "BEGIN:VCARD\n"
25 #define VCARD_VERSION "VERSION:3.0\n"
26 #define VCARD_FOOTER "END:VCARD\n"
27
28 /* Encode VCard */
29
30 unsigned char *silc_vcard_encode(SilcVCard vcard, SilcUInt32 *vcard_len)
31 {
32   SilcBufferStruct buffer;
33   int i;
34
35   if (!vcard->full_name || !vcard->family_name || !vcard->first_name)
36     return NULL;
37
38   memset(&buffer, 0, sizeof(buffer));
39   silc_buffer_strformat(
40        &buffer,
41        VCARD_HEADER,
42        VCARD_VERSION,
43        "FN:", vcard->full_name, "\n",
44        "N:", vcard->family_name, ";", vcard->first_name, ";",
45        vcard->middle_names, ";", vcard->prefix, ";", vcard->suffix, "\n",
46        SILC_STR_END);
47
48   if (vcard->nickname)
49     silc_buffer_strformat(&buffer,
50                           "NICKNAME:", vcard->nickname, "\n",
51                           SILC_STR_END);
52   if (vcard->bday)
53     silc_buffer_strformat(&buffer,
54                           "BDAY:", vcard->bday, "\n",
55                           SILC_STR_END);
56   if (vcard->title)
57     silc_buffer_strformat(&buffer,
58                           "TITLE:", vcard->title, "\n",
59                           SILC_STR_END);
60   if (vcard->role)
61     silc_buffer_strformat(&buffer,
62                           "ROLE:", vcard->role, "\n",
63                           SILC_STR_END);
64   if (vcard->org_name)
65     silc_buffer_strformat(&buffer,
66                           "ORG:", vcard->org_name, ";", vcard->org_unit, "\n",
67                           SILC_STR_END);
68   if (vcard->categories)
69     silc_buffer_strformat(&buffer,
70                           "CATEGORIES:", vcard->categories, "\n",
71                           SILC_STR_END);
72   if (vcard->class)
73     silc_buffer_strformat(&buffer,
74                           "CLASS:", vcard->class, "\n",
75                           SILC_STR_END);
76   if (vcard->url)
77     silc_buffer_strformat(&buffer,
78                           "URL:", vcard->url, "\n",
79                           SILC_STR_END);
80   if (vcard->label)
81     silc_buffer_strformat(&buffer,
82                           "LABEL;", vcard->url, "\n",
83                           SILC_STR_END);
84   for (i = 0; i < vcard->num_addrs; i++) {
85     silc_buffer_strformat(&buffer,
86                           "ADR;TYPE=",
87                           vcard->addrs[i].type, ":",
88                           vcard->addrs[i].pbox, ";",
89                           vcard->addrs[i].ext_addr, ";",
90                           vcard->addrs[i].street_addr, ";",
91                           vcard->addrs[i].city, ";",
92                           vcard->addrs[i].state, ";",
93                           vcard->addrs[i].code, ";",
94                           vcard->addrs[i].country, "\n",
95                           SILC_STR_END);
96   }
97   for (i = 0; i < vcard->num_tels; i++) {
98     silc_buffer_strformat(&buffer,
99                           "TEL;TYPE=",
100                           vcard->tels[i].type, ":",
101                           vcard->tels[i].tel, "\n",
102                           SILC_STR_END);
103   }
104   for (i = 0; i < vcard->num_emails; i++) {
105     silc_buffer_strformat(&buffer,
106                           "EMAIL;TYPE=",
107                           vcard->emails[i].type, ":",
108                           vcard->emails[i].address, "\n",
109                           SILC_STR_END);
110   }
111   if (vcard->note)
112     silc_buffer_strformat(&buffer,
113                           "NOTE:", vcard->note, "\n",
114                           SILC_STR_END);
115   if (vcard->rev)
116     silc_buffer_strformat(&buffer,
117                           "REV:", vcard->rev, "\n",
118                           SILC_STR_END);
119
120   silc_buffer_strformat(&buffer, VCARD_FOOTER, SILC_STR_END);
121
122   if (vcard_len)
123     *vcard_len = buffer.truelen;
124
125   return buffer.head;
126 }
127
128 /* Take one token */
129 #define VCARD_TOKEN(x)                          \
130   if (!(x)) {                                   \
131     (x) = silc_memdup(val + off, i - off);      \
132     off = i + 1;                                \
133     continue;                                   \
134   }
135
136 /* Take on TYPE= token and prepare for next token, accept the
137    type also without TYPE= as it is possible */
138 #define VCARD_TYPETOKEN(x)                                      \
139   if (!(x)) {                                                   \
140     int tmpi = 0;                                               \
141     if (!strcasecmp(val + off, "TYPE="))                        \
142       tmpi = 5;                                                 \
143     (x) = silc_memdup(val + off + tmpi, i - off - tmpi - 1);    \
144     tmpi = off + tmpi + strlen((x)) + 1;                        \
145     off = i;                                                    \
146     i = tmpi;                                                   \
147   }
148
149 /* Take last token */
150 #define VCARD_LASTTOKEN(x)                      \
151   if (!(x)) {                                   \
152     if (off < len)                              \
153       (x) = silc_memdup(val + off, len - off);  \
154   }                                             \
155
156 /* Decode VCard */
157
158 bool silc_vcard_decode(const unsigned char *data, SilcUInt32 data_len,
159                        SilcVCard vcard)
160 {
161   unsigned char *val;
162   bool has_begin = FALSE, has_end = FALSE;
163   int len, i, off = 0;
164   
165   val = (unsigned char *)data;
166   while (val) {
167     len = 0;
168     for (i = (val - data); i < data_len; i++) {
169       if (data[i] == '\0' || data[i] == '\n') {
170         len = i - (val - data);
171         break;
172       }
173     }
174     if (!len || len > data_len - (val - data))
175       break;
176
177     if (!strncasecmp(val, VCARD_HEADER, strlen(VCARD_HEADER))) {
178       has_begin = TRUE;
179     } else if (!strncasecmp(val, "FN:", 3)) {
180       if (vcard->full_name)
181         break;
182       if (len - 3)
183         vcard->full_name = silc_memdup(val + 3, len - 3);
184     } else if (!strncasecmp(val, "N:", 2)) {
185       if (vcard->family_name)
186         break;
187       if (len - 2) {
188         off = 2;
189         for (i = off; i < len; i++)
190           if (val[i] == ';') {
191             VCARD_TOKEN(vcard->family_name);
192             VCARD_TOKEN(vcard->first_name);
193             VCARD_TOKEN(vcard->middle_names);
194             VCARD_TOKEN(vcard->prefix);
195           }
196         VCARD_LASTTOKEN(vcard->suffix);
197       }
198     } else if (!strncasecmp(val, "NICKNAME:", 9)) {
199       if (vcard->nickname)
200         continue;
201       if (len - 9)
202         vcard->nickname = silc_memdup(val + 9, len - 9);
203     } else if (!strncasecmp(val, "BDAY:", 5)) {
204       if (vcard->bday)
205         continue;
206       if (len - 5)
207         vcard->bday = silc_memdup(val + 5, len - 5);
208     } else if (!strncasecmp(val, "TITLE:", 6)) {
209       if (vcard->title)
210         continue;
211       if (len - 6)
212         vcard->title = silc_memdup(val + 6, len - 6);
213     } else if (!strncasecmp(val, "ROLE:", 5)) {
214       if (vcard->role)
215         continue;
216       if (len - 5)
217         vcard->role = silc_memdup(val + 5, len - 5);
218     } else if (!strncasecmp(val, "ORG:", 4)) {
219       if (vcard->org_name)
220         continue;
221       if (len - 4) {
222         off = 4;
223         for (i = off; i < len; i++) {
224           if (val[i] == ';') {
225             VCARD_TOKEN(vcard->org_name);
226             break;
227           }
228         }
229         /* It's possible to have ORG without last ';', so check for it */
230         if (!vcard->org_name) {
231           VCARD_LASTTOKEN(vcard->org_name);
232         } else {
233           VCARD_LASTTOKEN(vcard->org_unit);
234         }
235       }
236     } else if (!strncasecmp(val, "CATEGORIES:", 11)) {
237       if (vcard->categories)
238         continue;
239       if (len - 11)
240         vcard->categories = silc_memdup(val + 11, len - 11);
241     } else if (!strncasecmp(val, "CLASS:", 6)) {
242       if (vcard->class)
243         continue;
244       if (len - 6)
245         vcard->class = silc_memdup(val + 6, len - 6);
246     } else if (!strncasecmp(val, "URL:", 4)) {
247       if (vcard->url)
248         continue;
249       if (len - 4)
250         vcard->url = silc_memdup(val + 4, len - 4);
251     } else if (!strncasecmp(val, "LABEL;", 6)) {
252       if (vcard->label)
253         continue;
254       if (len - 6)
255         vcard->label = silc_memdup(val + 6, len - 6);
256     } else if (!strncasecmp(val, "ADR;", 4)) {
257       vcard->addrs = silc_realloc(vcard->addrs, sizeof(*vcard->addrs) *
258                                   (vcard->num_addrs + 1));
259       memset(&vcard->addrs[vcard->num_addrs], 0, sizeof(*vcard->addrs));
260       if (len - 4) {
261         off = 4;
262         for (i = off; i < len; i++)
263           if (val[i] == ';') {
264             VCARD_TYPETOKEN(vcard->addrs[vcard->num_addrs].type);
265             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].pbox);
266             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].ext_addr);
267             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].street_addr);
268             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].city);
269             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].state);
270             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].code);
271           }
272         VCARD_LASTTOKEN(vcard->addrs[vcard->num_addrs].country);
273       }
274       vcard->num_addrs++;
275     } else if (!strncasecmp(val, "TEL;", 4)) {
276       vcard->tels = silc_realloc(vcard->tels, sizeof(*vcard->tels) *
277                                  (vcard->num_tels + 1));
278       memset(&vcard->tels[vcard->num_tels], 0, sizeof(*vcard->tels));
279       if (len - 4) {
280         off = 4;
281         for (i = off; i < len; i++)
282           if (val[i] == ':') {
283             i++;
284             VCARD_TYPETOKEN(vcard->tels[vcard->num_tels].type);
285             break;
286           }
287         VCARD_LASTTOKEN(vcard->tels[vcard->num_tels].tel);
288       }
289       vcard->num_tels++;
290     } else if (!strncasecmp(val, "EMAIL;", 6)) {
291       vcard->emails = silc_realloc(vcard->emails, sizeof(*vcard->emails) *
292                                    (vcard->num_emails + 1));
293       memset(&vcard->emails[vcard->num_emails], 0, sizeof(*vcard->emails));
294       if (len - 6) {
295         off = 6;
296         for (i = off; i < len; i++)
297           if (val[i] == ':') {
298             i++;
299             VCARD_TYPETOKEN(vcard->emails[vcard->num_emails].type);
300             break;
301           }
302         VCARD_LASTTOKEN(vcard->emails[vcard->num_emails].address);
303       }
304       vcard->num_emails++;
305     } else if (!strncasecmp(val, "NOTE:", 5)) {
306       if (vcard->note)
307         continue;
308       if (len - 5)
309         vcard->note = silc_memdup(val + 5, len - 5);
310     } else if (!strncasecmp(val, "REV:", 4)) {
311       if (vcard->rev)
312         continue;
313       if (len - 4)
314         vcard->rev = silc_memdup(val + 4, len - 4);
315     } else if (!strncasecmp(val, VCARD_FOOTER, strlen(VCARD_FOOTER))) {
316       has_end = TRUE;
317       break;
318     }
319
320     val = strchr(val, '\n');
321     if (!val || !(*val))
322       break;
323     val++;
324     if (!val || !(*val))
325       break;
326   }
327
328   if (!has_begin || !has_end) {
329     silc_vcard_free(vcard);
330     return FALSE;
331   }
332
333   return TRUE;
334 }
335
336 /* Allocate vcard context */
337
338 SilcVCard silc_vcard_alloc(void)
339 {
340   SilcVCard vcard = silc_calloc(1, sizeof(*vcard));
341   if (!vcard)
342     return NULL;
343   vcard->dynamic = TRUE;
344   return vcard;
345 }
346
347 /* Free the vcard structure */
348
349 void silc_vcard_free(SilcVCard vcard)
350 {
351   int i;
352
353   silc_free(vcard->full_name);
354   silc_free(vcard->family_name);
355   silc_free(vcard->first_name);
356   silc_free(vcard->middle_names);
357   silc_free(vcard->prefix);
358   silc_free(vcard->suffix);
359   silc_free(vcard->nickname);
360   silc_free(vcard->bday);
361   silc_free(vcard->title);
362   silc_free(vcard->role);
363   silc_free(vcard->org_name);
364   silc_free(vcard->org_unit);
365   silc_free(vcard->categories);
366   silc_free(vcard->class);
367   silc_free(vcard->url);
368   silc_free(vcard->label);
369   for (i = 0; i < vcard->num_addrs; i++) {
370     silc_free(vcard->addrs[i].type);
371     silc_free(vcard->addrs[i].pbox);
372     silc_free(vcard->addrs[i].ext_addr);
373     silc_free(vcard->addrs[i].street_addr);
374     silc_free(vcard->addrs[i].city);
375     silc_free(vcard->addrs[i].state);
376     silc_free(vcard->addrs[i].code);
377     silc_free(vcard->addrs[i].country);
378   }
379   silc_free(vcard->addrs);
380   for (i = 0; i < vcard->num_tels; i++) {
381     silc_free(vcard->tels[i].type);
382     silc_free(vcard->tels[i].tel);
383   }
384   silc_free(vcard->tels);
385   for (i = 0; i < vcard->num_emails; i++) {
386     silc_free(vcard->emails[i].type);
387     silc_free(vcard->emails[i].address);
388   }
389   silc_free(vcard->emails);
390   silc_free(vcard->note);
391   silc_free(vcard->rev);
392   if (!vcard->dynamic)
393     memset(vcard, 0, sizeof(*vcard));
394
395   if (vcard->dynamic) {
396     memset(vcard, 0, sizeof(*vcard));
397     silc_free(vcard);
398   }
399 }
400
401 /* Print card to file stream */
402
403 void silc_vcard_fprintf(SilcVCard vcard, FILE *stream)
404 {
405   int i;
406   fprintf(stream, "%s", VCARD_HEADER);
407   fprintf(stream, "%s", VCARD_VERSION);
408   if (vcard->full_name)
409     fprintf(stream, "FN:%s\n", vcard->full_name);
410   if (vcard->family_name)
411     fprintf(stream, "N:%s;%s;%s;%s;%s\n",
412             vcard->family_name,
413             vcard->first_name ? vcard->first_name : "",
414             vcard->middle_names ? vcard->middle_names : "",
415             vcard->prefix ? vcard->prefix : "",
416             vcard->suffix ? vcard->suffix : "");
417   if (vcard->nickname)
418     fprintf(stream, "NICKNAME:%s\n", vcard->nickname);
419   if (vcard->bday)
420     fprintf(stream, "BDAY:%s\n", vcard->bday);
421   if (vcard->title)
422     fprintf(stream, "TITLE:%s\n", vcard->title);
423   if (vcard->role)
424     fprintf(stream, "ROLE:%s\n", vcard->role);
425   if (vcard->org_name)
426     fprintf(stream, "ORG:%s;%s\n", vcard->org_name,
427             vcard->org_unit ? vcard->org_unit : "");
428   if (vcard->categories)
429     fprintf(stream, "CATEGORIES:%s\n", vcard->categories);
430   if (vcard->class)
431     fprintf(stream, "CLASS:%s\n", vcard->class);
432   if (vcard->url)
433     fprintf(stream, "URL:%s\n", vcard->url);
434   if (vcard->label)
435     fprintf(stream, "LABEL;%s\n", vcard->label);
436   for (i = 0; i < vcard->num_addrs; i++) {
437     fprintf(stream, "ADR;TYPE=%s:%s;%s;%s;%s;%s;%s;%s\n",
438             vcard->addrs[i].type,
439             vcard->addrs[i].pbox ? vcard->addrs[i].pbox : "",
440             vcard->addrs[i].ext_addr ? vcard->addrs[i].ext_addr : "",
441             vcard->addrs[i].street_addr ? vcard->addrs[i].street_addr : "",
442             vcard->addrs[i].city ? vcard->addrs[i].city : "",
443             vcard->addrs[i].state ? vcard->addrs[i].state : "",
444             vcard->addrs[i].code ? vcard->addrs[i].code : "",
445             vcard->addrs[i].country ? vcard->addrs[i].country : "");
446   }
447   for (i = 0; i < vcard->num_tels; i++) {
448     fprintf(stream, "TEL;TYPE=%s:%s\n",
449             vcard->tels[i].type,
450             vcard->tels[i].tel ? vcard->tels[i].tel : "");
451   }
452   for (i = 0; i < vcard->num_emails; i++) {
453     fprintf(stream, "EMAIL;TYPE=%s:%s\n",
454             vcard->emails[i].type,
455             vcard->emails[i].address ? vcard->emails[i].address : "");
456   }
457   if (vcard->note)
458     fprintf(stream, "NOTE:%s\n", vcard->note);
459   if (vcard->rev)
460     fprintf(stream, "REV:%s\n", vcard->rev);
461   fprintf(stream, "%s", VCARD_FOOTER);
462   fflush(stream);
463 }