6 CtxType CTX_VNOTE = CTX_NONE;
8 int pastel_palette[9][3] = {
22 * Fetch a message from the server and extract a vNote from it
24 struct vnote *vnote_new_from_msg(long msgnum, int unread) {
29 char uid_from_headers[256];
30 char mime_partnum[256];
31 char mime_filename[256];
32 char mime_content_type[256];
33 char mime_disposition[256];
34 char relevant_partnum[256];
35 int phase = 0; /* 0 = citadel headers, 1 = mime headers, 2 = body */
36 char msg4_content_type[256] = "";
37 char msg4_content_encoding[256] = "";
38 int msg4_content_length = 0;
39 struct vnote *vnote_from_body = NULL;
40 int vnote_inline = 0; /* 1 = MSG4 gave us a text/x-vnote top level */
42 relevant_partnum[0] = '\0';
43 serv_printf("MSG4 %ld", msgnum); /* we need the mime headers */
45 StrBuf_ServGetln(Buf);
46 if (GetServerStatus(Buf, NULL) != 1) {
50 while ((StrBuf_ServGetln(Buf) >= 0) && !Done) {
51 if ((StrLength(Buf) == 3) && !strcmp(ChrPtr(Buf), "000")) {
58 if (!strncasecmp(bptr, "exti=", 5)) {
59 safestrncpy(uid_from_headers, &(ChrPtr(Buf)[5]), sizeof uid_from_headers);
61 else if (!strncasecmp(bptr, "part=", 5)) {
62 extract_token(mime_filename, &bptr[5], 1, '|', sizeof mime_filename);
63 extract_token(mime_partnum, &bptr[5], 2, '|', sizeof mime_partnum);
64 extract_token(mime_disposition, &bptr[5], 3, '|', sizeof mime_disposition);
65 extract_token(mime_content_type, &bptr[5], 4, '|', sizeof mime_content_type);
67 if (!strcasecmp(mime_content_type, "text/vnote")) {
68 strcpy(relevant_partnum, mime_partnum);
71 else if ((phase == 0) && (!strncasecmp(bptr, "text", 4))) {
76 if (!IsEmptyStr(bptr)) {
77 if (!strncasecmp(bptr, "Content-type: ", 14)) {
78 safestrncpy(msg4_content_type, &bptr[14], sizeof msg4_content_type);
79 striplt(msg4_content_type);
81 else if (!strncasecmp(bptr, "Content-transfer-encoding: ", 27)) {
82 safestrncpy(msg4_content_encoding, &bptr[27], sizeof msg4_content_encoding);
83 striplt(msg4_content_type);
85 else if ((!strncasecmp(bptr, "Content-length: ", 16))) {
86 msg4_content_length = atoi(&bptr[16]);
92 if ((msg4_content_length > 0)
93 && (!strcasecmp(msg4_content_encoding, "7bit"))
94 && (!strcasecmp(msg4_content_type, "text/vnote"))
101 Data = NewStrBufPlain(NULL, msg4_content_length * 2);
102 if (msg4_content_length > 0) {
103 StrBuf_ServGetBLOBBuffered(Data, msg4_content_length);
107 StrBufAppendBuf(Data, Buf, 0);
108 StrBufAppendBufPlain(Data, "\r\n", 1, 0);
113 StrBufAppendBuf(Data, Buf, 0);
119 /* If MSG4 didn't give us the part we wanted, but we know that we can find it
120 * as one of the other MIME parts, attempt to load it now.
122 if ((!vnote_inline) && (!IsEmptyStr(relevant_partnum))) {
123 Data = load_mimepart(msgnum, relevant_partnum);
126 if (StrLength(Data) > 0) {
127 if (IsEmptyStr(uid_from_headers)) {
128 /* Convert an old-style note to a vNote */
129 vnote_from_body = vnote_new();
130 vnote_from_body->uid = strdup(uid_from_headers);
131 vnote_from_body->color_red = pastel_palette[3][0];
132 vnote_from_body->color_green = pastel_palette[3][1];
133 vnote_from_body->color_blue = pastel_palette[3][2];
134 vnote_from_body->body = malloc(StrLength(Data) + 1);
135 vnote_from_body->body[0] = 0;
136 memcpy(vnote_from_body->body, ChrPtr(Data), StrLength(Data) + 1);
138 return vnote_from_body;
141 char *Buf = SmashStrBuf(&Data);
143 struct vnote *v = vnote_new_from_str(Buf);
154 * Serialize a vnote and write it to the server
156 void write_vnote_to_server(struct vnote *v) {
162 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%s--%04x--%04x",
163 ChrPtr(WC->serv_info->serv_fqdn), getpid(), ++seq);
165 serv_puts("ENT0 1|||4");
166 serv_getln(buf, sizeof buf);
168 /* Remember, serv_printf() appends an extra newline */
169 serv_printf("Content-type: multipart/alternative; " "boundary=\"%s\"\n", boundary);
170 serv_printf("This is a multipart message in MIME format.\n");
171 serv_printf("--%s", boundary);
173 serv_puts("Content-type: text/plain; charset=utf-8");
174 serv_puts("Content-Transfer-Encoding: 7bit");
179 serv_printf("--%s", boundary);
180 serv_puts("Content-type: text/vnote");
181 serv_puts("Content-Transfer-Encoding: 7bit");
183 pch = vnote_serialize(v);
186 serv_printf("--%s--", boundary);
195 * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
197 void ajax_update_note(void) {
201 struct vnote *v = NULL;
203 if (!havebstr("note_uid")) {
204 begin_ajax_response();
205 wc_printf("Received ajax_update_note() request without a note UID.");
210 serv_printf("EUID %s", bstr("note_uid"));
211 serv_getln(buf, sizeof buf);
213 begin_ajax_response();
214 wc_printf("Cannot find message containing vNote with the requested uid!");
218 msgnum = atol(&buf[4]);
220 /* Was this request a delete operation? If so, nuke it... */
221 if (havebstr("deletenote")) {
222 if (!strcasecmp(bstr("deletenote"), "yes")) {
223 serv_printf("DELE %d", msgnum);
224 serv_getln(buf, sizeof buf);
225 begin_ajax_response();
226 wc_printf("%s", buf);
232 /* If we get to this point it's an update, not a delete */
233 v = vnote_new_from_msg(msgnum, 0);
235 begin_ajax_response();
236 wc_printf("Cannot locate a vNote within message %d\n", msgnum);
241 /* Make any requested changes */
242 if (havebstr("top")) {
243 v->pos_top = atoi(bstr("top"));
245 if (havebstr("left")) {
246 v->pos_left = atoi(bstr("left"));
248 if (havebstr("height")) {
249 v->pos_height = atoi(bstr("height"));
251 if (havebstr("width")) {
252 v->pos_width = atoi(bstr("width"));
254 if (havebstr("red")) {
255 v->color_red = atoi(bstr("red"));
257 if (havebstr("green")) {
258 v->color_green = atoi(bstr("green"));
260 if (havebstr("blue")) {
261 v->color_blue = atoi(bstr("blue"));
263 if (havebstr("value")) { /* I would have preferred 'body' but InPlaceEditor hardcodes 'value' */
266 v->body = strdup(bstr("value"));
269 /* Serialize it and save it to the message base. Server will delete the old one. */
270 write_vnote_to_server(v);
272 begin_ajax_response();
285 * display sticky notes
287 * msgnum = Message number on the local server of the note to be displayed
290 /*TODO: wrong hook */
291 int notes_LoadMsgFromServer(SharedMessageStatus * Stat, void **ViewSpecific, message_summary * Msg, int is_new, int i) {
295 memset(&TP, 0, sizeof(WCTemplputParams));
296 TP.Filter.ContextType = CTX_VNOTE;
297 v = vnote_new_from_msg(Msg->msgnum, is_new);
300 DoTemplate(HKEY("vnoteitem"), WC->WBuf, &TP);
303 /* uncomment these lines to see ugly debugging info
304 StrBufAppendPrintf(WC->trailing_javascript,
305 "document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
306 StrBufAppendPrintf(WC->trailing_javascript,
307 "document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
308 StrBufAppendPrintf(WC->trailing_javascript,
309 "document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
310 StrBufAppendPrintf(WC->trailing_javascript,
311 "document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
324 void add_new_note(void) {
329 v->uid = malloc(128);
330 generate_uuid(v->uid);
331 v->color_red = pastel_palette[3][0];
332 v->color_green = pastel_palette[3][1];
333 v->color_blue = pastel_palette[3][2];
334 v->body = strdup(_("Click on any note to edit it."));
335 write_vnote_to_server(v);
339 readloop(readfwd, eUseDefault);
343 void tmpl_vcard_put_posleft(StrBuf * Target, WCTemplputParams * TP) {
344 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
345 StrBufAppendPrintf(Target, "%d", v->pos_left);
348 void tmpl_vcard_put_postop(StrBuf * Target, WCTemplputParams * TP) {
349 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
350 StrBufAppendPrintf(Target, "%d", v->pos_top);
353 void tmpl_vcard_put_poswidth(StrBuf * Target, WCTemplputParams * TP) {
354 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
355 StrBufAppendPrintf(Target, "%d", v->pos_width);
358 void tmpl_vcard_put_posheight(StrBuf * Target, WCTemplputParams * TP) {
359 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
360 StrBufAppendPrintf(Target, "%d", v->pos_height);
363 void tmpl_vcard_put_posheight2(StrBuf * Target, WCTemplputParams * TP) {
364 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
365 StrBufAppendPrintf(Target, "%d", (v->pos_height / 16) - 5);
368 void tmpl_vcard_put_width2(StrBuf * Target, WCTemplputParams * TP) {
369 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
370 StrBufAppendPrintf(Target, "%d", (v->pos_width / 9) - 1);
373 void tmpl_vcard_put_color(StrBuf * Target, WCTemplputParams * TP) {
374 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
375 StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red, v->color_green, v->color_blue);
378 void tmpl_vcard_put_bgcolor(StrBuf * Target, WCTemplputParams * TP) {
379 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
380 StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red / 2, v->color_green / 2, v->color_blue / 2);
383 void tmpl_vcard_put_message(StrBuf * Target, WCTemplputParams * TP) {
384 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
385 StrEscAppend(Target, NULL, v->body, 0, 0); /*TODO? */
388 void tmpl_vcard_put_uid(StrBuf * Target, WCTemplputParams * TP) {
389 struct vnote *v = (struct vnote *) CTX(CTX_VNOTE);
390 StrBufAppendBufPlain(Target, v->uid, -1, 0);
396 int notes_GetParamsGetServerCall(SharedMessageStatus * Stat,
397 void **ViewSpecific, long oper, char *cmd, long len, char *filter, long flen) {
398 strcpy(cmd, "MSGS ALL");
399 Stat->maxmsgs = 32767;
400 wc_printf("<div id=\"new_notes_here\"></div>\n");
405 int notes_Cleanup(void **ViewSpecific) {
410 void render_MIME_VNote(StrBuf * Target, WCTemplputParams * TP, StrBuf * FoundCharset) {
411 wc_mime_attachment *Mime = CTX(CTX_MIME_ATACH);
413 if (StrLength(Mime->Data) == 0)
415 if (StrLength(Mime->Data) > 0) {
421 vcard = SmashStrBuf(&Mime->Data);
422 v = vnote_new_from_str(vcard);
427 memset(&TP, 0, sizeof(WCTemplputParams));
428 TP.Filter.ContextType = CTX_VNOTE;
430 DoTemplate(HKEY("mail_vnoteitem"), Buf, &TP);
436 if (Mime->Data == NULL)
437 Mime->Data = NewStrBuf();
439 FlushStrBuf(Mime->Data);
446 void InitModule_NOTES(void) {
447 RegisterCTX(CTX_VNOTE);
449 RegisterReadLoopHandlerset(VIEW_NOTES,
450 notes_GetParamsGetServerCall,
451 NULL, NULL, NULL, notes_LoadMsgFromServer, NULL, notes_Cleanup, NULL);
453 WebcitAddUrlHandler(HKEY("add_new_note"), "", 0, add_new_note, 0);
454 WebcitAddUrlHandler(HKEY("ajax_update_note"), "", 0, ajax_update_note, 0);
456 RegisterNamespace("VNOTE:POS:LEFT", 0, 0, tmpl_vcard_put_posleft, NULL, CTX_VNOTE);
457 RegisterNamespace("VNOTE:POS:TOP", 0, 0, tmpl_vcard_put_postop, NULL, CTX_VNOTE);
458 RegisterNamespace("VNOTE:POS:WIDTH", 0, 0, tmpl_vcard_put_poswidth, NULL, CTX_VNOTE);
459 RegisterNamespace("VNOTE:POS:HEIGHT", 0, 0, tmpl_vcard_put_posheight, NULL, CTX_VNOTE);
460 RegisterNamespace("VNOTE:POS:HEIGHT2", 0, 0, tmpl_vcard_put_posheight2, NULL, CTX_VNOTE);
461 RegisterNamespace("VNOTE:POS:WIDTH2", 0, 0, tmpl_vcard_put_width2, NULL, CTX_VNOTE);
462 RegisterNamespace("VNOTE:COLOR", 0, 0, tmpl_vcard_put_color, NULL, CTX_VNOTE);
463 RegisterNamespace("VNOTE:BGCOLOR", 0, 0, tmpl_vcard_put_bgcolor, NULL, CTX_VNOTE);
464 RegisterNamespace("VNOTE:MSG", 0, 1, tmpl_vcard_put_message, NULL, CTX_VNOTE);
465 RegisterNamespace("VNOTE:UID", 0, 0, tmpl_vcard_put_uid, NULL, CTX_VNOTE);
467 RegisterMimeRenderer(HKEY("text/vnote"), render_MIME_VNote, 1, 300);