Added silc_vcard_alloc. silc_vcard_free now checks for dynamically
[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"
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 */
137 #define VCARD_TYPETOKEN(x)                              \
138   if (!(x)) {                                           \
139     int tmpi;                                           \
140     (x) = silc_memdup(val + off + 5, i - off - 5 - 1);  \
141     tmpi = off + 5 + strlen((x)) + 1;                   \
142     off = i;                                            \
143     i = tmpi;                                           \
144   }
145
146 /* Take last token */
147 #define VCARD_LASTTOKEN(x)                      \
148   if (!(x)) {                                   \
149     if (off < len)                              \
150       (x) = silc_memdup(val + off, len - off);  \
151   }                                             \
152
153 /* Decode VCard */
154
155 bool silc_vcard_decode(const unsigned char *data, SilcUInt32 data_len,
156                        SilcVCard vcard)
157 {
158   unsigned char *val;
159   bool has_begin = FALSE, has_end = FALSE;
160   int len, i, off = 0;
161   
162   val = (unsigned char *)data;
163   while (val) {
164     if (!strchr(val, '\n'))
165       break;
166     len = strchr(val, '\n') - (char *)val;
167     if (len > data_len - (val - data))
168       break;
169
170     if (!strncasecmp(val, VCARD_HEADER, strlen(VCARD_HEADER))) {
171       has_begin = TRUE;
172     } else if (!strncasecmp(val, "FN:", 3)) {
173       if (vcard->full_name)
174         break;
175       if (len - 3)
176         vcard->full_name = silc_memdup(val + 3, len - 3);
177     } else if (!strncasecmp(val, "N:", 2)) {
178       if (vcard->family_name)
179         break;
180       if (len - 2) {
181         off = 2;
182         for (i = off; i < len; i++)
183           if (val[i] == ';') {
184             VCARD_TOKEN(vcard->family_name);
185             VCARD_TOKEN(vcard->first_name);
186             VCARD_TOKEN(vcard->middle_names);
187             VCARD_TOKEN(vcard->prefix);
188           }
189         VCARD_LASTTOKEN(vcard->suffix);
190       }
191     } else if (!strncasecmp(val, "NICKNAME:", 9)) {
192       if (vcard->nickname)
193         continue;
194       if (len - 9)
195         vcard->nickname = silc_memdup(val + 9, len - 9);
196     } else if (!strncasecmp(val, "BDAY:", 5)) {
197       if (vcard->bday)
198         continue;
199       if (len - 5)
200         vcard->bday = silc_memdup(val + 5, len - 5);
201     } else if (!strncasecmp(val, "TITLE:", 6)) {
202       if (vcard->title)
203         continue;
204       if (len - 6)
205         vcard->title = silc_memdup(val + 6, len - 6);
206     } else if (!strncasecmp(val, "ROLE:", 5)) {
207       if (vcard->role)
208         continue;
209       if (len - 5)
210         vcard->role = silc_memdup(val + 5, len - 5);
211     } else if (!strncasecmp(val, "ORG:", 4)) {
212       if (vcard->org_name)
213         continue;
214       if (len - 4) {
215         off = 4;
216         for (i = off; i < len; i++) {
217           if (val[i] == ';') {
218             VCARD_TOKEN(vcard->org_name);
219             break;
220           }
221         }
222         /* It's possible to have ORG without last ';', so check for it */
223         if (!vcard->org_name) {
224           VCARD_LASTTOKEN(vcard->org_name);
225         } else {
226           VCARD_LASTTOKEN(vcard->org_unit);
227         }
228       }
229     } else if (!strncasecmp(val, "CATEGORIES:", 11)) {
230       if (vcard->categories)
231         continue;
232       if (len - 11)
233         vcard->categories = silc_memdup(val + 11, len - 11);
234     } else if (!strncasecmp(val, "CLASS:", 6)) {
235       if (vcard->class)
236         continue;
237       if (len - 6)
238         vcard->class = silc_memdup(val + 6, len - 6);
239     } else if (!strncasecmp(val, "URL:", 4)) {
240       if (vcard->url)
241         continue;
242       if (len - 4)
243         vcard->url = silc_memdup(val + 4, len - 4);
244     } else if (!strncasecmp(val, "LABEL;", 6)) {
245       if (vcard->label)
246         continue;
247       if (len - 6)
248         vcard->label = silc_memdup(val + 6, len - 6);
249     } else if (!strncasecmp(val, "ADR;", 4)) {
250       vcard->addrs = silc_realloc(vcard->addrs, sizeof(*vcard->addrs) *
251                                   (vcard->num_addrs + 1));
252       if (len - 4) {
253         off = 4;
254         for (i = off; i < len; i++)
255           if (val[i] == ';') {
256             VCARD_TYPETOKEN(vcard->addrs[vcard->num_addrs].type);
257             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].pbox);
258             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].ext_addr);
259             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].street_addr);
260             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].city);
261             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].state);
262             VCARD_TOKEN(vcard->addrs[vcard->num_addrs].code);
263           }
264         VCARD_LASTTOKEN(vcard->addrs[vcard->num_addrs].country);
265       }
266       vcard->num_addrs++;
267     } else if (!strncasecmp(val, "TEL;", 4)) {
268       vcard->tels = silc_realloc(vcard->tels, sizeof(*vcard->tels) *
269                                  (vcard->num_tels + 1));
270       if (len - 4) {
271         off = 4;
272         for (i = off; i < len; i++)
273           if (val[i] == ':') {
274             i++;
275             VCARD_TYPETOKEN(vcard->tels[vcard->num_tels].type);
276             break;
277           }
278         VCARD_LASTTOKEN(vcard->tels[vcard->num_tels].tel);
279       }
280       vcard->num_tels++;
281     } else if (!strncasecmp(val, "EMAIL;", 6)) {
282       vcard->emails = silc_realloc(vcard->emails, sizeof(*vcard->emails) *
283                                    (vcard->num_emails + 1));
284       if (len - 6) {
285         off = 6;
286         for (i = off; i < len; i++)
287           if (val[i] == ':') {
288             i++;
289             VCARD_TYPETOKEN(vcard->emails[vcard->num_emails].type);
290             break;
291           }
292         VCARD_LASTTOKEN(vcard->emails[vcard->num_emails].address);
293       }
294       vcard->num_emails++;
295     } else if (!strncasecmp(val, "NOTE:", 5)) {
296       if (vcard->note)
297         continue;
298       if (len - 5)
299         vcard->note = silc_memdup(val + 5, len - 5);
300     } else if (!strncasecmp(val, "REV:", 4)) {
301       if (vcard->rev)
302         continue;
303       if (len - 4)
304         vcard->rev = silc_memdup(val + 4, len - 4);
305     } else if (!strncasecmp(val, VCARD_FOOTER, strlen(VCARD_FOOTER))) {
306       has_end = TRUE;
307       break;
308     }
309
310     val = strchr(val, '\n');
311     if (!val || !(*val))
312       break;
313     val++;
314     if (!val || !(*val))
315       break;
316   }
317
318   if (!has_begin || !has_end) {
319     silc_vcard_free(vcard);
320     return FALSE;
321   }
322
323   return TRUE;
324 }
325
326 /* Allocate vcard context */
327
328 SilcVCard silc_vcard_alloc(void)
329 {
330   SilcVCard vcard = silc_calloc(1, sizeof(*vcard));
331   if (!vcard)
332     return NULL;
333   vcard->dynamic = TRUE;
334   return vcard;
335 }
336
337 /* Free the vcard structure */
338
339 void silc_vcard_free(SilcVCard vcard)
340 {
341   int i;
342
343   silc_free(vcard->full_name);
344   silc_free(vcard->family_name);
345   silc_free(vcard->first_name);
346   silc_free(vcard->middle_names);
347   silc_free(vcard->prefix);
348   silc_free(vcard->suffix);
349   silc_free(vcard->nickname);
350   silc_free(vcard->bday);
351   silc_free(vcard->title);
352   silc_free(vcard->role);
353   silc_free(vcard->org_name);
354   silc_free(vcard->org_unit);
355   silc_free(vcard->categories);
356   silc_free(vcard->class);
357   silc_free(vcard->url);
358   silc_free(vcard->label);
359   for (i = 0; i < vcard->num_addrs; i++) {
360     silc_free(vcard->addrs[i].type);
361     silc_free(vcard->addrs[i].pbox);
362     silc_free(vcard->addrs[i].ext_addr);
363     silc_free(vcard->addrs[i].street_addr);
364     silc_free(vcard->addrs[i].city);
365     silc_free(vcard->addrs[i].state);
366     silc_free(vcard->addrs[i].code);
367     silc_free(vcard->addrs[i].country);
368   }
369   silc_free(vcard->addrs);
370   for (i = 0; i < vcard->num_tels; i++) {
371     silc_free(vcard->tels[i].type);
372     silc_free(vcard->tels[i].tel);
373   }
374   silc_free(vcard->tels);
375   for (i = 0; i < vcard->num_emails; i++) {
376     silc_free(vcard->emails[i].type);
377     silc_free(vcard->emails[i].address);
378   }
379   silc_free(vcard->emails);
380   silc_free(vcard->note);
381   silc_free(vcard->rev);
382
383   if (vcard->dynamic) {
384     memset(vcard, 'F', sizeof(*vcard));
385     silc_free(vcard);
386   }
387 }
388
389 /* Print card to file stream */
390
391 void silc_vcard_fprintf(SilcVCard vcard, FILE *stream)
392 {
393   int i;
394   fprintf(stream, "%s", VCARD_HEADER);
395   fprintf(stream, "%s", VCARD_VERSION);
396   if (vcard->full_name)
397     fprintf(stream, "FN:%s\n", vcard->full_name);
398   if (vcard->family_name)
399     fprintf(stream, "N:%s;%s;%s;%s;%s\n",
400             vcard->family_name,
401             vcard->first_name ? vcard->first_name : "",
402             vcard->middle_names ? vcard->middle_names : "",
403             vcard->prefix ? vcard->prefix : "",
404             vcard->suffix ? vcard->suffix : "");
405   if (vcard->nickname)
406     fprintf(stream, "NICKNAME:%s\n", vcard->nickname);
407   if (vcard->bday)
408     fprintf(stream, "BDAY:%s\n", vcard->bday);
409   if (vcard->title)
410     fprintf(stream, "TITLE:%s\n", vcard->title);
411   if (vcard->role)
412     fprintf(stream, "ROLE:%s\n", vcard->role);
413   if (vcard->org_name)
414     fprintf(stream, "ORG:%s;%s\n", vcard->org_name,
415             vcard->org_unit ? vcard->org_unit : "");
416   if (vcard->categories)
417     fprintf(stream, "CATEGORIES:%s\n", vcard->categories);
418   if (vcard->class)
419     fprintf(stream, "CLASS:%s\n", vcard->class);
420   if (vcard->url)
421     fprintf(stream, "URL:%s\n", vcard->url);
422   if (vcard->label)
423     fprintf(stream, "LABEL;%s\n", vcard->label);
424   for (i = 0; i < vcard->num_addrs; i++) {
425     fprintf(stream, "ADR;TYPE=%s:%s;%s;%s;%s;%s;%s;%s\n",
426             vcard->addrs[i].type,
427             vcard->addrs[i].pbox ? vcard->addrs[i].pbox : "",
428             vcard->addrs[i].ext_addr ? vcard->addrs[i].ext_addr : "",
429             vcard->addrs[i].street_addr ? vcard->addrs[i].street_addr : "",
430             vcard->addrs[i].city ? vcard->addrs[i].city : "",
431             vcard->addrs[i].state ? vcard->addrs[i].state : "",
432             vcard->addrs[i].code ? vcard->addrs[i].code : "",
433             vcard->addrs[i].country ? vcard->addrs[i].country : "");
434   }
435   for (i = 0; i < vcard->num_tels; i++) {
436     fprintf(stream, "TEL;TYPE=%s:%s\n",
437             vcard->tels[i].type,
438             vcard->tels[i].tel ? vcard->tels[i].tel : "");
439   }
440   for (i = 0; i < vcard->num_emails; i++) {
441     fprintf(stream, "EMAIL;TYPE=%s:%s\n",
442             vcard->emails[i].type,
443             vcard->emails[i].address ? vcard->emails[i].address : "");
444   }
445   if (vcard->note)
446     fprintf(stream, "NOTE:%s\n", vcard->note);
447   if (vcard->rev)
448     fprintf(stream, "REV:%s\n", vcard->rev);
449   fprintf(stream, "%s", VCARD_FOOTER);
450   fflush(stream);
451 }