2 * vNote implementation for Citadel
4 * Copyright (C) 1999-2007 by the citadel.org development team.
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
33 #include <libcitadel.h>
36 struct vnote *vnote_new(void) {
39 v = (struct vnote *) malloc(sizeof(struct vnote));
41 memset(v, 0, sizeof(struct vnote));
42 v->magic = CTDL_VNOTE_MAGIC;
43 v->pos_left = rand() % 256;
44 v->pos_top = rand() % 256;
48 v->color_green = 0xFF;
54 struct vnote *vnote_new_from_str(char *s) {
62 int is_quoted_printable;
68 while (*ptr) { // keep going until we hit a null terminator
70 nexteol = strchr(ptr, '\n');
72 thisline_len = (nexteol-ptr);
73 thisline = malloc(thisline_len + 2);
74 memcpy(thisline, ptr, thisline_len);
75 thisline[thisline_len] = '\0';
79 thisline = strdup(ptr);
80 thisline_len = strlen(thisline);
85 if (thisline_len > 1) {
86 if (thisline[thisline_len - 1] == '\r') {
87 thisline[thisline_len - 1] = 0;
92 /* locate the colon separator */
93 encoded_value = strchr(thisline, ':');
97 /* any qualifiers? (look for a semicolon) */
98 is_base64 = bmstrcasestr(thisline, "encoding=base64") ? 1 : 0;
99 is_quoted_printable = bmstrcasestr(thisline, "encoding=quoted-printable") ? 1 : 0;
101 char *semicolon_pos = strchr(thisline, ';');
106 decoded_value = malloc(thisline_len);
108 CtdlDecodeBase64(decoded_value,
110 strlen(encoded_value));
112 else if (is_quoted_printable) {
113 CtdlDecodeQuotedPrintable(decoded_value,
115 strlen(encoded_value));
118 strcpy(decoded_value, encoded_value);
121 if (!strcasecmp(thisline, "UID")) {
122 if (v->uid) free(v->uid);
123 v->uid = decoded_value;
125 else if (!strcasecmp(thisline, "SUMMARY")) {
126 if (v->summary) free(v->summary);
127 v->summary = decoded_value;
129 else if ( (!strcasecmp(thisline, "NOTE"))
130 || (!strcasecmp(thisline, "BODY")) ) {
131 if (v->body) free(v->body);
132 v->body = decoded_value;
134 else if (!strcasecmp(thisline, "X-OUTLOOK-WIDTH")) {
135 v->pos_width = atoi(decoded_value);
138 else if (!strcasecmp(thisline, "X-OUTLOOK-HEIGHT")) {
139 v->pos_height = atoi(decoded_value);
142 else if (!strcasecmp(thisline, "X-OUTLOOK-LEFT")) {
143 v->pos_left = atoi(decoded_value);
146 else if (!strcasecmp(thisline, "X-OUTLOOK-TOP")) {
147 v->pos_top = atoi(decoded_value);
150 else if ( (!strcasecmp(thisline, "X-OUTLOOK-COLOR"))
151 && (strlen(decoded_value) == 7)
152 && (decoded_value[0] == '#') ) {
153 sscanf(&decoded_value[1], "%2x%2x%2x",
160 free(decoded_value); // throw it away
163 /* FIXME still need to handle these:
164 * X-OUTLOOK-CREATE-TIME:20070611T204615Z
165 * REV:20070611T204621Z
175 void vnote_free(struct vnote *v) {
177 if (v->magic != CTDL_VNOTE_MAGIC) return;
179 if (v->uid) free(v->uid);
180 if (v->summary) free(v->summary);
181 if (v->body) free(v->body);
183 memset(v, 0, sizeof(struct vnote));
188 /* helper function for vnote_serialize() */
189 void vnote_serialize_output_field(char *append_to, char *field, char *label) {
198 if (!append_to) return;
202 mydup = malloc((strlen(field) * 3) + 1);
206 while (ptr[pos] != 0) {
207 ch = (unsigned char)(ptr[pos++]);
210 mydup[output_len++] = ch;
212 else if ( (ch >= 32) && (ch <= 60) ) {
213 mydup[output_len++] = ch;
215 else if ( (ch >= 62) && (ch <= 126) ) {
216 mydup[output_len++] = ch;
219 sprintf((char *)&mydup[output_len], "=%02X", ch);
224 mydup[output_len] = 0;
226 sprintf(&append_to[strlen(append_to)], "%s%s:%s\r\n",
228 (is_qp ? ";ENCODING=QUOTED-PRINTABLE" : ""),
234 char *vnote_serialize(struct vnote *v) {
236 int bytes_needed = 0;
239 if (v->magic != CTDL_VNOTE_MAGIC) return NULL;
242 if (v->summary) bytes_needed += strlen(v->summary);
243 if (v->body) bytes_needed += strlen(v->body);
244 s = malloc(bytes_needed);
248 vnote_serialize_output_field(s, "vnote", "BEGIN");
249 vnote_serialize_output_field(s, "//Citadel//vNote handler library//EN", "PRODID");
250 vnote_serialize_output_field(s, "1.1", "VERSION");
251 vnote_serialize_output_field(s, "PUBLIC", "CLASS");
252 vnote_serialize_output_field(s, v->uid, "UID");
253 vnote_serialize_output_field(s, v->summary, "SUMMARY");
254 vnote_serialize_output_field(s, v->body, "BODY");
255 vnote_serialize_output_field(s, v->body, "NOTE");
256 sprintf(&s[strlen(s)], "X-OUTLOOK-COLOR:#%02X%02X%02X\r\n",
257 v->color_red, v->color_green, v->color_blue);
258 sprintf(&s[strlen(s)], "X-OUTLOOK-LEFT:%d\r\n", v->pos_left);
259 sprintf(&s[strlen(s)], "X-OUTLOOK-TOP:%d\r\n", v->pos_top);
260 sprintf(&s[strlen(s)], "X-OUTLOOK-WIDTH:%d\r\n", v->pos_width);
261 sprintf(&s[strlen(s)], "X-OUTLOOK-HEIGHT:%d\r\n", v->pos_height);
262 vnote_serialize_output_field(s, "vnote", "END");