]> code.citadel.org Git - citadel.git/blob - libcitadel/lib/vnote.c
vnote_serialize() is now (probably) complete.
[citadel.git] / libcitadel / lib / vnote.c
1 /*
2  * $Id$
3  *
4  * vNote implementation for Citadel
5  *
6  * Copyright (C) 1999-2007 by the citadel.org development team.
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
36
37 /* move this into the header file when we're done */
38 #define CTDL_VNOTE_MAGIC        0xa1fa
39 struct vnote {
40         int magic;
41         char *uid;
42         char *summary;
43         char *body;
44         int pos_left;
45         int pos_top;
46         int pos_width;
47         int pos_height;
48         int color_red;
49         int color_green;
50         int color_blue;
51 };
52
53
54 struct vnote *vnote_new(void) {
55         struct vnote *v;
56
57         v = (struct vnote *) malloc(sizeof(struct vnote));
58         if (v) {
59                 memset(v, 0, sizeof(struct vnote));
60                 v->magic = CTDL_VNOTE_MAGIC;
61                 v->pos_left = rand() % 256;
62                 v->pos_top = rand() % 256;
63                 v->pos_width = 200;
64                 v->pos_height = 150;
65                 v->color_red = 0xFF;
66                 v->color_green = 0xFF;
67                 v->color_blue = 0x00;
68         }
69         return v;
70 }
71
72 struct vnote *vnote_new_from_str(char *s) {
73         struct vnote *v;
74
75         v = vnote_new();
76         if (!v) return NULL;
77
78         /* FIXME finish this */
79
80         return(v);
81 }
82
83 void vnote_free(struct vnote *v) {
84         if (!v) return;
85         if (v->magic != CTDL_VNOTE_MAGIC) return;
86
87         if (v->uid) free(v->uid);
88         if (v->summary) free(v->summary);
89         if (v->body) free(v->body);
90         
91         memset(v, 0, sizeof(struct vnote));
92         free(v);
93 }
94
95
96 /* helper function for vnote_serialize() */
97 void vnote_serialize_output_field(char *append_to, char *field, char *label) {
98
99         char *mydup;
100         int output_len = 0;
101         int is_qp = 0;
102         char *ptr = field;
103         unsigned char ch;
104         int pos = 0;
105
106         if (!append_to) return;
107         if (!field) return;
108         if (!label) return;
109
110         mydup = malloc((strlen(field) * 3) + 1);
111         if (!mydup) return;
112         strcpy(mydup, "");
113
114         while (ptr[pos] != 0) {
115                 ch = (unsigned char)(ptr[pos++]);
116
117                 if (ch == 9) {
118                         mydup[output_len++] = ch;
119                 }
120                 else if ( (ch >= 32) && (ch <= 60) ) {
121                         mydup[output_len++] = ch;
122                 }
123                 else if ( (ch >= 62) && (ch <= 126) ) {
124                         mydup[output_len++] = ch;
125                 }
126                 else {
127                         sprintf((char *)&mydup[output_len], "=%02X", ch);
128                         output_len += 3;
129                         is_qp = 1;
130                 }
131         }
132         mydup[output_len] = 0;
133
134         sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
135                 label,
136                 (is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
137                 mydup);
138         free(mydup);
139 }
140
141
142 char *vnote_serialize(struct vnote *v) {
143         char *s;
144         int bytes_needed = 0;
145
146         if (!v) return NULL;
147         if (v->magic != CTDL_VNOTE_MAGIC) return NULL;
148
149         bytes_needed = 1024;
150         if (v->summary) bytes_needed += strlen(v->summary);
151         if (v->body) bytes_needed += strlen(v->body);
152         s = malloc(bytes_needed);
153         if (!s) return NULL;
154
155         strcpy(s, "");
156         vnote_serialize_output_field(s, "vnote", "BEGIN");
157         vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
158         vnote_serialize_output_field(s, "1.1", "VERSION");
159         vnote_serialize_output_field(s, "PUBLIC", "CLASS");
160         vnote_serialize_output_field(s, v->uid, "UID");
161         vnote_serialize_output_field(s, v->body, "BODY");
162         vnote_serialize_output_field(s, v->body, "NOTE");
163         sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n",
164                 v->color_red, v->color_green, v->color_blue);
165         sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
166         sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
167         sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
168         sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
169         vnote_serialize_output_field(s, "vnote", "END");
170         return(s);
171 }
172
173
174 #ifdef VNOTE_TEST_HARNESS
175
176 char *bynari_sample =
177         "BEGIN:vnote\n"
178         "VERSION:1.1\n"
179         "PRODID://Bynari Insight Connector 3.1.3-0605191//Import from Outlook//EN\n"
180         "CLASS:PUBLIC\n"
181         "UID:040000008200E00074C5B7101A82E00800000000000000000000000000820425CE8571864B8D141CB3FB8CAC62\n"
182         "NOTE;ENCODING=QUOTED-PRINTABLE:blah blah blah=0D=0A=0D=0A\n"
183         "SUMMARY:blah blah blah=0D=0A=0D=0A\n"
184         "X-OUTLOOK-COLOR:#FFFF00\n"
185         "X-OUTLOOK-WIDTH:200\n"
186         "X-OUTLOOK-HEIGHT:166\n"
187         "X-OUTLOOK-LEFT:80\n"
188         "X-OUTLOOK-TOP:80\n"
189         "X-OUTLOOK-CREATE-TIME:20070611T204615Z\n"
190         "REV:20070611T204621Z\n"
191         "END:vnote\n"
192 ;
193
194 char *horde_sample =
195         "BEGIN:VNOTE\n"
196         "VERSION:1.1\n"
197         "UID:20061129111109.7chx73xdok1s at 172.16.45.2\n"
198         "BODY:HORDE_1\n"
199         "DCREATED:20061129T101109Z\n"
200         "END:VNOTE\n"
201 ;
202
203
204 main() {
205         char *s;
206         struct vnote *v;
207
208         printf("Before:\n-------------\n%s-------------\nAfter:\n-----------\n", bynari_sample);
209         v = vnote_new_from_str(bynari_sample);
210         s = vnote_serialize(v);
211         vnote_free(v);
212         if (s) {
213                 printf("%s", s);
214                 free(s);
215         }
216
217         exit(0);
218 }
219 #endif
220
221
222
223
224