2 * vNote implementation for Citadel
4 * Copyright (C) 1999-2007 by the citadel.org development team.
5 * This code is freely redistributable under the terms of the GNU General
6 * Public License. All other rights reserved.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
32 #include <libcitadel.h>
35 struct vnote *vnote_new(void) {
38 v = (struct vnote *) malloc(sizeof(struct vnote));
40 memset(v, 0, sizeof(struct vnote));
41 v->magic = CTDL_VNOTE_MAGIC;
42 v->pos_left = rand() % 256;
43 v->pos_top = rand() % 256;
47 v->color_green = 0xFF;
53 struct vnote *vnote_new_from_str(char *s) {
61 int is_quoted_printable;
67 while (*ptr) { // keep going until we hit a null terminator
69 nexteol = strchr(ptr, '\n');
71 thisline = malloc((nexteol - ptr) + 2);
72 strncpy(thisline, ptr, (nexteol-ptr));
73 thisline_len = (nexteol-ptr);
74 thisline[thisline_len] = 0;
78 thisline = strdup(ptr);
79 thisline_len = strlen(thisline);
84 if (thisline_len > 1) {
85 if (thisline[thisline_len - 1] == '\r') {
86 thisline[thisline_len - 1] = 0;
91 /* locate the colon separator */
92 encoded_value = strchr(thisline, ':');
96 /* any qualifiers? (look for a semicolon) */
97 is_base64 = bmstrcasestr(thisline, "encoding=base64") ? 1 : 0;
98 is_quoted_printable = bmstrcasestr(thisline, "encoding=quoted-printable") ? 1 : 0;
100 char *semicolon_pos = strchr(thisline, ';');
105 decoded_value = malloc(thisline_len);
107 CtdlDecodeBase64(decoded_value,
109 strlen(encoded_value));
111 else if (is_quoted_printable) {
112 CtdlDecodeQuotedPrintable(decoded_value,
114 strlen(encoded_value));
117 strcpy(decoded_value, encoded_value);
120 if (!strcasecmp(thisline, "UID")) {
121 if (v->uid) free(v->uid);
122 v->uid = decoded_value;
124 else if (!strcasecmp(thisline, "SUMMARY")) {
125 if (v->summary) free(v->summary);
126 v->summary = decoded_value;
128 else if ( (!strcasecmp(thisline, "NOTE"))
129 || (!strcasecmp(thisline, "BODY")) ) {
130 if (v->body) free(v->body);
131 v->body = decoded_value;
133 else if (!strcasecmp(thisline, "X-OUTLOOK-WIDTH")) {
134 v->pos_width = atoi(decoded_value);
137 else if (!strcasecmp(thisline, "X-OUTLOOK-HEIGHT")) {
138 v->pos_height = atoi(decoded_value);
141 else if (!strcasecmp(thisline, "X-OUTLOOK-LEFT")) {
142 v->pos_left = atoi(decoded_value);
145 else if (!strcasecmp(thisline, "X-OUTLOOK-TOP")) {
146 v->pos_top = atoi(decoded_value);
149 else if ( (!strcasecmp(thisline, "X-OUTLOOK-COLOR"))
150 && (strlen(decoded_value) == 7)
151 && (decoded_value[0] == '#') ) {
152 sscanf(&decoded_value[1], "%2x%2x%2x",
159 free(decoded_value); // throw it away
162 /* FIXME still need to handle these:
163 * X-OUTLOOK-CREATE-TIME:20070611T204615Z
164 * REV:20070611T204621Z
174 void vnote_free(struct vnote *v) {
176 if (v->magic != CTDL_VNOTE_MAGIC) return;
178 if (v->uid) free(v->uid);
179 if (v->summary) free(v->summary);
180 if (v->body) free(v->body);
182 memset(v, 0, sizeof(struct vnote));
187 /* helper function for vnote_serialize() */
188 void vnote_serialize_output_field(char *append_to, char *field, char *label) {
197 if (!append_to) return;
201 mydup = malloc((strlen(field) * 3) + 1);
205 while (ptr[pos] != 0) {
206 ch = (unsigned char)(ptr[pos++]);
209 mydup[output_len++] = ch;
211 else if ( (ch >= 32) && (ch <= 60) ) {
212 mydup[output_len++] = ch;
214 else if ( (ch >= 62) && (ch <= 126) ) {
215 mydup[output_len++] = ch;
218 sprintf((char *)&mydup[output_len], "=%02X", ch);
223 mydup[output_len] = 0;
225 sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
227 (is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
233 char *vnote_serialize(struct vnote *v) {
235 int bytes_needed = 0;
238 if (v->magic != CTDL_VNOTE_MAGIC) return NULL;
241 if (v->summary) bytes_needed += strlen(v->summary);
242 if (v->body) bytes_needed += strlen(v->body);
243 s = malloc(bytes_needed);
247 vnote_serialize_output_field(s, "vnote", "BEGIN");
248 vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
249 vnote_serialize_output_field(s, "1.1", "VERSION");
250 vnote_serialize_output_field(s, "PUBLIC", "CLASS");
251 vnote_serialize_output_field(s, v->uid, "UID");
252 vnote_serialize_output_field(s, v->summary, "SUMMARY");
253 vnote_serialize_output_field(s, v->body, "BODY");
254 vnote_serialize_output_field(s, v->body, "NOTE");
255 sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n",
256 v->color_red, v->color_green, v->color_blue);
257 sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
258 sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
259 sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
260 sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
261 vnote_serialize_output_field(s, "vnote", "END");