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 struct vnote *vnote_new(void) {
40 v = (struct vnote *) malloc(sizeof(struct vnote));
42 memset(v, 0, sizeof(struct vnote));
43 v->magic = CTDL_VNOTE_MAGIC;
44 v->pos_left = rand() % 256;
45 v->pos_top = rand() % 256;
49 v->color_green = 0xFF;
55 struct vnote *vnote_new_from_str(char *s) {
63 int is_quoted_printable;
69 while (*ptr) { // keep going until we hit a null terminator
71 nexteol = strchr(ptr, '\n');
73 thisline = malloc((nexteol - ptr) + 2);
74 strncpy(thisline, ptr, (nexteol-ptr));
75 thisline_len = (nexteol-ptr);
76 thisline[thisline_len] = 0;
80 thisline = strdup(ptr);
81 thisline_len = strlen(thisline);
86 if (thisline_len > 1) {
87 if (thisline[thisline_len - 1] == '\r') {
88 thisline[thisline_len - 1] = 0;
93 /* locate the colon separator */
94 encoded_value = strchr(thisline, ':');
98 /* any qualifiers? (look for a semicolon) */
99 is_base64 = (int) bmstrcasestr(thisline, "encoding=base64");
100 is_quoted_printable = (int) bmstrcasestr(thisline,
101 "encoding=quoted-printable");
103 char *semicolon_pos = strchr(thisline, ';');
108 decoded_value = malloc(thisline_len);
110 CtdlDecodeBase64(decoded_value,
112 strlen(encoded_value));
114 else if (is_quoted_printable) {
115 CtdlDecodeQuotedPrintable(decoded_value,
117 strlen(encoded_value));
120 strcpy(decoded_value, encoded_value);
123 if (!strcasecmp(thisline, "UID")) {
124 if (v->uid) free(v->uid);
125 v->uid = decoded_value;
127 else if (!strcasecmp(thisline, "SUMMARY")) {
128 if (v->summary) free(v->summary);
129 v->summary = decoded_value;
131 else if ( (!strcasecmp(thisline, "NOTE"))
132 || (!strcasecmp(thisline, "BODY")) ) {
133 if (v->body) free(v->body);
134 v->body = decoded_value;
136 else if (!strcasecmp(thisline, "X-OUTLOOK-WIDTH")) {
137 v->pos_width = atoi(decoded_value);
140 else if (!strcasecmp(thisline, "X-OUTLOOK-HEIGHT")) {
141 v->pos_height = atoi(decoded_value);
144 else if (!strcasecmp(thisline, "X-OUTLOOK-LEFT")) {
145 v->pos_left = atoi(decoded_value);
148 else if (!strcasecmp(thisline, "X-OUTLOOK-TOP")) {
149 v->pos_top = atoi(decoded_value);
152 else if ( (!strcasecmp(thisline, "X-OUTLOOK-COLOR"))
153 && (strlen(decoded_value) == 7)
154 && (decoded_value[0] == '#') ) {
155 sscanf(&decoded_value[1], "%2x%2x%2x",
162 free(decoded_value); // throw it away
165 /* FIXME still need to handle these:
166 * X-OUTLOOK-CREATE-TIME:20070611T204615Z
167 * REV:20070611T204621Z
177 void vnote_free(struct vnote *v) {
179 if (v->magic != CTDL_VNOTE_MAGIC) return;
181 if (v->uid) free(v->uid);
182 if (v->summary) free(v->summary);
183 if (v->body) free(v->body);
185 memset(v, 0, sizeof(struct vnote));
190 /* helper function for vnote_serialize() */
191 void vnote_serialize_output_field(char *append_to, char *field, char *label) {
200 if (!append_to) return;
204 mydup = malloc((strlen(field) * 3) + 1);
208 while (ptr[pos] != 0) {
209 ch = (unsigned char)(ptr[pos++]);
212 mydup[output_len++] = ch;
214 else if ( (ch >= 32) && (ch <= 60) ) {
215 mydup[output_len++] = ch;
217 else if ( (ch >= 62) && (ch <= 126) ) {
218 mydup[output_len++] = ch;
221 sprintf((char *)&mydup[output_len], "=%02X", ch);
226 mydup[output_len] = 0;
228 sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
230 (is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
236 char *vnote_serialize(struct vnote *v) {
238 int bytes_needed = 0;
241 if (v->magic != CTDL_VNOTE_MAGIC) return NULL;
244 if (v->summary) bytes_needed += strlen(v->summary);
245 if (v->body) bytes_needed += strlen(v->body);
246 s = malloc(bytes_needed);
250 vnote_serialize_output_field(s, "vnote", "BEGIN");
251 vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
252 vnote_serialize_output_field(s, "1.1", "VERSION");
253 vnote_serialize_output_field(s, "PUBLIC", "CLASS");
254 vnote_serialize_output_field(s, v->uid, "UID");
255 vnote_serialize_output_field(s, v->summary, "SUMMARY");
256 vnote_serialize_output_field(s, v->body, "BODY");
257 vnote_serialize_output_field(s, v->body, "NOTE");
258 sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n",
259 v->color_red, v->color_green, v->color_blue);
260 sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
261 sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
262 sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
263 sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
264 vnote_serialize_output_field(s, "vnote", "END");