1 // vNote implementation for Citadel
3 // Copyright (C) 1999-2007 by the citadel.org development team.
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.
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
20 # include <sys/time.h>
31 #include <libcitadel.h>
34 struct vnote *vnote_new(void) {
37 v = (struct vnote *) malloc(sizeof(struct vnote));
39 memset(v, 0, sizeof(struct vnote));
40 v->magic = CTDL_VNOTE_MAGIC;
41 v->pos_left = rand() % 256;
42 v->pos_top = rand() % 256;
46 v->color_green = 0xFF;
52 struct vnote *vnote_new_from_str(char *s) {
60 int is_quoted_printable;
66 while (*ptr) { // keep going until we hit a null terminator
68 nexteol = strchr(ptr, '\n');
70 thisline_len = (nexteol-ptr);
71 thisline = malloc(thisline_len + 2);
72 memcpy(thisline, ptr, thisline_len);
73 thisline[thisline_len] = '\0';
77 thisline = strdup(ptr);
78 thisline_len = strlen(thisline);
83 if (thisline_len > 1) {
84 if (thisline[thisline_len - 1] == '\r') {
85 thisline[thisline_len - 1] = 0;
90 /* locate the colon separator */
91 encoded_value = strchr(thisline, ':');
95 /* any qualifiers? (look for a semicolon) */
96 is_base64 = bmstrcasestr(thisline, "encoding=base64") ? 1 : 0;
97 is_quoted_printable = bmstrcasestr(thisline, "encoding=quoted-printable") ? 1 : 0;
99 char *semicolon_pos = strchr(thisline, ';');
104 decoded_value = malloc(thisline_len);
106 CtdlDecodeBase64(decoded_value,
108 strlen(encoded_value));
110 else if (is_quoted_printable) {
111 CtdlDecodeQuotedPrintable(decoded_value,
113 strlen(encoded_value));
116 strcpy(decoded_value, encoded_value);
119 if (!strcasecmp(thisline, "UID")) {
120 if (v->uid) free(v->uid);
121 v->uid = decoded_value;
123 else if (!strcasecmp(thisline, "SUMMARY")) {
124 if (v->summary) free(v->summary);
125 v->summary = decoded_value;
127 else if ( (!strcasecmp(thisline, "NOTE"))
128 || (!strcasecmp(thisline, "BODY")) ) {
129 if (v->body) free(v->body);
130 v->body = decoded_value;
132 else if (!strcasecmp(thisline, "X-OUTLOOK-WIDTH")) {
133 v->pos_width = atoi(decoded_value);
136 else if (!strcasecmp(thisline, "X-OUTLOOK-HEIGHT")) {
137 v->pos_height = atoi(decoded_value);
140 else if (!strcasecmp(thisline, "X-OUTLOOK-LEFT")) {
141 v->pos_left = atoi(decoded_value);
144 else if (!strcasecmp(thisline, "X-OUTLOOK-TOP")) {
145 v->pos_top = atoi(decoded_value);
148 else if ( (!strcasecmp(thisline, "X-OUTLOOK-COLOR"))
149 && (strlen(decoded_value) == 7)
150 && (decoded_value[0] == '#') ) {
151 sscanf(&decoded_value[1], "%2x%2x%2x",
158 free(decoded_value); // throw it away
161 // FIXME still need to handle these:
162 // X-OUTLOOK-CREATE-TIME:20070611T204615Z
163 // REV:20070611T204621Z
172 void vnote_free(struct vnote *v) {
174 if (v->magic != CTDL_VNOTE_MAGIC) return;
176 if (v->uid) free(v->uid);
177 if (v->summary) free(v->summary);
178 if (v->body) free(v->body);
180 memset(v, 0, sizeof(struct vnote));
185 // helper function for vnote_serialize()
186 void vnote_serialize_output_field(char *append_to, char *field, char *label) {
195 if (!append_to) return;
199 mydup = malloc((strlen(field) * 3) + 1);
203 while (ptr[pos] != 0) {
204 ch = (unsigned char)(ptr[pos++]);
207 mydup[output_len++] = ch;
209 else if ( (ch >= 32) && (ch <= 60) ) {
210 mydup[output_len++] = ch;
212 else if ( (ch >= 62) && (ch <= 126) ) {
213 mydup[output_len++] = ch;
216 sprintf((char *)&mydup[output_len], "=%02X", ch);
221 mydup[output_len] = 0;
223 sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
225 (is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
231 char *vnote_serialize(struct vnote *v) {
233 int bytes_needed = 0;
236 if (v->magic != CTDL_VNOTE_MAGIC) return NULL;
239 if (v->summary) bytes_needed += strlen(v->summary);
240 if (v->body) bytes_needed += strlen(v->body);
241 s = malloc(bytes_needed);
245 vnote_serialize_output_field(s, "vnote", "BEGIN");
246 vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
247 vnote_serialize_output_field(s, "1.1", "VERSION");
248 vnote_serialize_output_field(s, "PUBLIC", "CLASS");
249 vnote_serialize_output_field(s, v->uid, "UID");
250 vnote_serialize_output_field(s, v->summary, "SUMMARY");
251 vnote_serialize_output_field(s, v->body, "BODY");
252 vnote_serialize_output_field(s, v->body, "NOTE");
253 sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n", v->color_red, v->color_green, v->color_blue);
254 sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
255 sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
256 sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
257 sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
258 vnote_serialize_output_field(s, "vnote", "END");