4 * vNote implementation for Citadel
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.
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
34 #include <libcitadel.h>
37 /* move this into the header file when we're done */
38 #define CTDL_VNOTE_MAGIC 0xa1fa
54 struct vnote *vnote_new(void) {
57 v = (struct vnote *) malloc(sizeof(struct vnote));
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;
66 v->color_green = 0xFF;
72 struct vnote *vnote_new_from_str(char *s) {
80 int is_quoted_printable;
86 while (*ptr) { // keep going until we hit a null terminator
88 nexteol = strchr(ptr, '\n');
90 thisline = malloc((nexteol - ptr) + 2);
91 strncpy(thisline, ptr, (nexteol-ptr));
92 thisline_len = (nexteol-ptr);
93 thisline[thisline_len] = 0;
97 thisline = strdup(ptr);
98 thisline_len = strlen(thisline);
103 if (thisline_len > 1) {
104 if (thisline[thisline_len - 1] == '\r') {
105 thisline[thisline_len - 1] = 0;
110 /* locate the colon separator */
111 encoded_value = strchr(thisline, ':');
113 *encoded_value++ = 0;
115 /* any qualifiers? (look for a semicolon) */
117 is_quoted_printable = 0;
120 decoded_value = malloc(thisline_len);
123 else if (is_quoted_printable) {
126 strcpy(decoded_value, thisline_len);
132 free(decoded_value); // throw it away
144 void vnote_free(struct vnote *v) {
146 if (v->magic != CTDL_VNOTE_MAGIC) return;
148 if (v->uid) free(v->uid);
149 if (v->summary) free(v->summary);
150 if (v->body) free(v->body);
152 memset(v, 0, sizeof(struct vnote));
157 /* helper function for vnote_serialize() */
158 void vnote_serialize_output_field(char *append_to, char *field, char *label) {
167 if (!append_to) return;
171 mydup = malloc((strlen(field) * 3) + 1);
175 while (ptr[pos] != 0) {
176 ch = (unsigned char)(ptr[pos++]);
179 mydup[output_len++] = ch;
181 else if ( (ch >= 32) && (ch <= 60) ) {
182 mydup[output_len++] = ch;
184 else if ( (ch >= 62) && (ch <= 126) ) {
185 mydup[output_len++] = ch;
188 sprintf((char *)&mydup[output_len], "=%02X", ch);
193 mydup[output_len] = 0;
195 sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
197 (is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
203 char *vnote_serialize(struct vnote *v) {
205 int bytes_needed = 0;
208 if (v->magic != CTDL_VNOTE_MAGIC) return NULL;
211 if (v->summary) bytes_needed += strlen(v->summary);
212 if (v->body) bytes_needed += strlen(v->body);
213 s = malloc(bytes_needed);
217 vnote_serialize_output_field(s, "vnote", "BEGIN");
218 vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
219 vnote_serialize_output_field(s, "1.1", "VERSION");
220 vnote_serialize_output_field(s, "PUBLIC", "CLASS");
221 vnote_serialize_output_field(s, v->uid, "UID");
222 vnote_serialize_output_field(s, v->body, "BODY");
223 vnote_serialize_output_field(s, v->body, "NOTE");
224 sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n",
225 v->color_red, v->color_green, v->color_blue);
226 sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
227 sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
228 sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
229 sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
230 vnote_serialize_output_field(s, "vnote", "END");
235 #ifdef VNOTE_TEST_HARNESS
237 char *bynari_sample =
240 "PRODID://Bynari Insight Connector 3.1.3-0605191//Import from Outlook//EN\n"
242 "UID:040000008200E00074C5B7101A82E00800000000000000000000000000820425CE8571864B8D141CB3FB8CAC62\n"
243 "NOTE;ENCODING=QUOTED-PRINTABLE:blah blah blah=0D=0A=0D=0A\n"
244 "SUMMARY:blah blah blah=0D=0A=0D=0A\n"
245 "X-OUTLOOK-COLOR:#FFFF00\n"
246 "X-OUTLOOK-WIDTH:200\n"
247 "X-OUTLOOK-HEIGHT:166\n"
248 "X-OUTLOOK-LEFT:80\n"
250 "X-OUTLOOK-CREATE-TIME:20070611T204615Z\n"
251 "REV:20070611T204621Z\n"
258 "UID:20061129111109.7chx73xdok1s at 172.16.45.2\n"
260 "DCREATED:20061129T101109Z\n"
269 printf("Before:\n-------------\n%s-------------\nAfter:\n-----------\n", bynari_sample);
270 v = vnote_new_from_str(bynari_sample);
271 s = vnote_serialize(v);