* Attempting to add a smart host in webcit was instead adding it as an RBL host....
[citadel.git] / webcit / notes.c
blob9e00be85f693977fcae1ffd91fd5c902897b707b
1 /*
2 * $Id$
4 */
6 #include "webcit.h"
7 #include "groupdav.h"
8 #include "webserver.h"
10 int pastel_palette[9][3] = {
11 { 0x80, 0x80, 0x80 },
12 { 0xff, 0x80, 0x80 },
13 { 0x80, 0x80, 0xff },
14 { 0xff, 0xff, 0x80 },
15 { 0x80, 0xff, 0x80 },
16 { 0xff, 0x80, 0xff },
17 { 0x80, 0xff, 0xff },
18 { 0xff, 0x80, 0x80 },
19 { 0x80, 0x80, 0x80 }
24 * Display a <div> containing a rendered sticky note.
26 void display_vnote_div(struct vnote *v) {
27 int i;
30 wprintf("<div id=\"note-%s\" ", v->uid); /* begin outer div */
31 wprintf("class=\"stickynote_outer\" ");
32 wprintf("style=\"");
33 wprintf("left: %dpx; ", v->pos_left);
34 wprintf("top: %dpx; ", v->pos_top);
35 wprintf("width: %dpx; ", v->pos_width);
36 wprintf("height: %dpx; ", v->pos_height);
37 wprintf("background-color: #%02X%02X%02X ", v->color_red, v->color_green, v->color_blue);
38 wprintf("\">");
44 wprintf("<div id=\"titlebar-%s\" ", v->uid); /* begin title bar div */
45 wprintf("class=\"stickynote_titlebar\" ");
46 wprintf("onMouseDown=\"NotesDragMouseDown(event,'%s')\" ", v->uid);
47 wprintf("style=\"");
48 wprintf("background-color: #%02X%02X%02X ", v->color_red/2, v->color_green/2, v->color_blue/2);
49 wprintf("\">");
51 wprintf("<table border=0 cellpadding=0 cellspacing=0 valign=middle width=100%%><tr>");
53 wprintf("<td align=left valign=middle>");
54 wprintf("<img onclick=\"NotesClickPalette(event,'%s')\" ", v->uid);
55 wprintf("src=\"static/8paint16.gif\">");
57 wprintf("</td>");
59 wprintf("<td></td>"); // nothing in the title bar, it's just for dragging
61 wprintf("<td align=right valign=middle>");
62 wprintf("<img onclick=\"DeleteStickyNote(event,'%s','%s')\" ", v->uid, _("Delete this note?") );
63 wprintf("src=\"static/closewindow.gif\">");
64 wprintf("</td></tr></table>");
66 wprintf("</div>\n"); // end title bar div
72 wprintf("<div id=\"notebody-%s\" ", v->uid); // begin body div
73 wprintf("class=\"stickynote_body\"");
74 wprintf(">");
75 escputs(v->body);
76 wprintf("</div>\n"); // end body div
78 StrBufAppendPrintf(WC->trailing_javascript,
79 " new Ajax.InPlaceEditor('notebody-%s', 'ajax_update_note?note_uid=%s', "
80 "{rows:%d,cols:%d,onEnterHover:false,onLeaveHover:false,"
81 "okText:'%s',cancelText:'%s',clickToEditText:'%s'});",
82 v->uid,
83 v->uid,
84 (v->pos_height / 16) - 5,
85 (v->pos_width / 9) - 1,
86 _("Save"),
87 _("Cancel"),
88 _("Click on any note to edit it.")
91 wprintf("<div id=\"resize-%s\" ", v->uid); // begin resize handle div
92 wprintf("class=\"stickynote_resize\" ");
93 wprintf("onMouseDown=\"NotesResizeMouseDown(event,'%s')\"", v->uid);
94 wprintf("> </div>"); // end resize handle div
99 /* embed color selector - it doesn't have to be inside the title bar html because
100 * it's a separate div placed by css
102 wprintf("<div id=\"palette-%s\" ", v->uid); // begin stickynote_palette div
103 wprintf("class=\"stickynote_palette\">");
105 wprintf("<table border=0 cellpadding=0 cellspacing=0>");
106 for (i=0; i<9; ++i) {
107 if ((i%3)==0) wprintf("<tr>");
108 wprintf("<td ");
109 wprintf("onClick=\"NotesClickColor(event,'%s',%d,%d,%d,'#%02x%02x%02x','#%02x%02x%02x')\" ",
110 v->uid,
111 pastel_palette[i][0], // color values to pass to ajax call
112 pastel_palette[i][1],
113 pastel_palette[i][2],
114 pastel_palette[i][0], // new color of note
115 pastel_palette[i][1],
116 pastel_palette[i][2],
117 pastel_palette[i][0] / 2, // new color of title bar
118 pastel_palette[i][1] / 2,
119 pastel_palette[i][2] / 2
121 wprintf("bgcolor=\"#%02x%02x%02x\"> </td>",
122 pastel_palette[i][0],
123 pastel_palette[i][1],
124 pastel_palette[i][2]
126 if (((i+1)%3)==0) wprintf("</tr>");
128 wprintf("</table>");
130 wprintf("</div>"); // end stickynote_palette div
136 wprintf("</div>\n"); // end outer div
142 * Fetch a message from the server and extract a vNote from it
144 struct vnote *vnote_new_from_msg(long msgnum) {
145 char buf[1024];
146 char mime_partnum[256];
147 char mime_filename[256];
148 char mime_content_type[256];
149 char mime_disposition[256];
150 int mime_length;
151 char relevant_partnum[256];
152 char *relevant_source = NULL;
153 char uid_from_headers[256];
154 int in_body = 0;
155 int body_line_len = 0;
156 int body_len = 0;
157 struct vnote *vnote_from_body = NULL;
159 relevant_partnum[0] = 0;
160 uid_from_headers[0] = 0;
161 sprintf(buf, "MSG4 %ld", msgnum); /* we need the mime headers */
162 serv_puts(buf);
163 serv_getln(buf, sizeof buf);
164 if (buf[0] != '1') return NULL;
166 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
167 if (!strncasecmp(buf, "exti=", 5)) {
168 safestrncpy(uid_from_headers, &buf[5], sizeof uid_from_headers);
170 else if (!strncasecmp(buf, "part=", 5)) {
171 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
172 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
173 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
174 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
175 mime_length = extract_int(&buf[5], 5);
177 if (!strcasecmp(mime_content_type, "text/vnote")) {
178 strcpy(relevant_partnum, mime_partnum);
181 else if ((in_body) && (IsEmptyStr(relevant_partnum)) && (!IsEmptyStr(uid_from_headers))) {
182 // Convert an old-style note to a vNote
183 if (!vnote_from_body) {
184 vnote_from_body = vnote_new();
185 vnote_from_body->uid = strdup(uid_from_headers);
186 vnote_from_body->color_red = pastel_palette[3][0];
187 vnote_from_body->color_green = pastel_palette[3][1];
188 vnote_from_body->color_blue = pastel_palette[3][2];
189 vnote_from_body->body = malloc(32768);
190 vnote_from_body->body[0] = 0;
191 body_len = 0;
193 body_line_len = strlen(buf);
194 if ((body_len + body_line_len + 10) < 32768) {
195 strcpy(&vnote_from_body->body[body_len++], " ");
196 strcpy(&vnote_from_body->body[body_len], buf);
197 body_len += body_line_len;
200 else if (IsEmptyStr(buf)) {
201 in_body = 1;
205 if (!IsEmptyStr(relevant_partnum)) {
206 relevant_source = load_mimepart(msgnum, relevant_partnum);
207 if (relevant_source != NULL) {
208 struct vnote *v = vnote_new_from_str(relevant_source);
209 free(relevant_source);
210 return(v);
214 if (vnote_from_body) {
215 return(vnote_from_body);
217 return NULL;
222 * Serialize a vnote and write it to the server
224 void write_vnote_to_server(struct vnote *v)
226 char buf[1024];
227 char *pch;
229 serv_puts("ENT0 1|||4");
230 serv_getln(buf, sizeof buf);
231 if (buf[0] == '4') {
232 serv_puts("Content-type: text/vnote");
233 serv_puts("");
234 pch = vnote_serialize(v);
235 serv_puts(pch);
236 free(pch);
237 serv_puts("000");
245 * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
247 void ajax_update_note(void) {
249 char buf[1024];
250 int msgnum;
251 struct vnote *v = NULL;
253 if (!havebstr("note_uid")) {
254 begin_ajax_response();
255 wprintf("Received ajax_update_note() request without a note UID.");
256 end_ajax_response();
257 return;
260 // lprintf(9, "Note UID = %s\n", bstr("note_uid"));
261 serv_printf("EUID %s", bstr("note_uid"));
262 serv_getln(buf, sizeof buf);
263 if (buf[0] != '2') {
264 begin_ajax_response();
265 wprintf("Cannot find message containing vNote with the requested uid!");
266 end_ajax_response();
267 return;
269 msgnum = atol(&buf[4]);
270 // lprintf(9, "Note msg = %ld\n", msgnum);
272 // Was this request a delete operation? If so, nuke it...
273 if (havebstr("deletenote")) {
274 if (!strcasecmp(bstr("deletenote"), "yes")) {
275 serv_printf("DELE %d", msgnum);
276 serv_getln(buf, sizeof buf);
277 begin_ajax_response();
278 wprintf("%s", buf);
279 end_ajax_response();
280 return;
284 // If we get to this point it's an update, not a delete
285 v = vnote_new_from_msg(msgnum);
286 if (!v) {
287 begin_ajax_response();
288 wprintf("Cannot locate a vNote within message %d\n", msgnum);
289 end_ajax_response();
290 return;
293 /* Make any requested changes */
294 if (havebstr("top")) {
295 v->pos_top = atoi(bstr("top"));
297 if (havebstr("left")) {
298 v->pos_left = atoi(bstr("left"));
300 if (havebstr("height")) {
301 v->pos_height = atoi(bstr("height"));
303 if (havebstr("width")) {
304 v->pos_width = atoi(bstr("width"));
306 if (havebstr("red")) {
307 v->color_red = atoi(bstr("red"));
309 if (havebstr("green")) {
310 v->color_green = atoi(bstr("green"));
312 if (havebstr("blue")) {
313 v->color_blue = atoi(bstr("blue"));
315 if (havebstr("value")) { // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
316 if (v->body) free(v->body);
317 v->body = strdup(bstr("value"));
320 /* Serialize it and save it to the message base. Server will delete the old one. */
321 write_vnote_to_server(v);
323 begin_ajax_response();
324 if (v->body) {
325 escputs(v->body);
327 end_ajax_response();
329 vnote_free(v);
336 * display sticky notes
338 * msgnum = Message number on the local server of the note to be displayed
340 void display_note(message_summary *Msg, int unread) {
341 struct vnote *v;
342 WCTemplputParams TP;
344 memset(&TP, 0, sizeof(WCTemplputParams));
345 TP.Filter.ContextType = CTX_VNOTE;
346 v = vnote_new_from_msg(Msg->msgnum);
347 if (v) {
348 // display_vnote_div(v);
349 TP.Context = v;
350 DoTemplate(HKEY("vnoteitem"),
351 WC->WBuf, &TP);
354 /* uncomment these lines to see ugly debugging info
355 StrBufAppendPrintf(WC->trailing_javascript,
356 "document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
357 StrBufAppendPrintf(WC->trailing_javascript,
358 "document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
359 StrBufAppendPrintf(WC->trailing_javascript,
360 "document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
361 StrBufAppendPrintf(WC->trailing_javascript,
362 "document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
365 vnote_free(v);
372 * Create a new note
374 void add_new_note(void) {
375 struct vnote *v;
377 v = vnote_new();
378 if (v) {
379 v->uid = malloc(128);
380 generate_uuid(v->uid);
381 v->color_red = pastel_palette[3][0];
382 v->color_green = pastel_palette[3][1];
383 v->color_blue = pastel_palette[3][2];
384 v->body = strdup(_("Click on any note to edit it."));
385 write_vnote_to_server(v);
386 vnote_free(v);
389 readloop(readfwd);
393 void tmpl_vcard_put_posleft(StrBuf *Target, WCTemplputParams *TP)
395 struct vnote *v = (struct vnote *) CTX;
396 StrBufAppendPrintf(Target, "%d", v->pos_left);
399 void tmpl_vcard_put_postop(StrBuf *Target, WCTemplputParams *TP)
401 struct vnote *v = (struct vnote *) CTX;
402 StrBufAppendPrintf(Target, "%d", v->pos_top);
405 void tmpl_vcard_put_poswidth(StrBuf *Target, WCTemplputParams *TP)
407 struct vnote *v = (struct vnote *) CTX;
408 StrBufAppendPrintf(Target, "%d", v->pos_width);
411 void tmpl_vcard_put_posheight(StrBuf *Target, WCTemplputParams *TP)
413 struct vnote *v = (struct vnote *) CTX;
414 StrBufAppendPrintf(Target, "%d", v->pos_height);
417 void tmpl_vcard_put_posheight2(StrBuf *Target, WCTemplputParams *TP)
419 struct vnote *v = (struct vnote *) CTX;
420 StrBufAppendPrintf(Target, "%d", (v->pos_height / 16) - 5);
423 void tmpl_vcard_put_width2(StrBuf *Target, WCTemplputParams *TP)
425 struct vnote *v = (struct vnote *) CTX;
426 StrBufAppendPrintf(Target, "%d", (v->pos_width / 9) - 1);
429 void tmpl_vcard_put_color(StrBuf *Target, WCTemplputParams *TP)
431 struct vnote *v = (struct vnote *) CTX;
432 StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red, v->color_green, v->color_blue);
435 void tmpl_vcard_put_bgcolor(StrBuf *Target, WCTemplputParams *TP)
437 struct vnote *v = (struct vnote *) CTX;
438 StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red/2, v->color_green/2, v->color_blue/2);
441 void tmpl_vcard_put_message(StrBuf *Target, WCTemplputParams *TP)
443 struct vnote *v = (struct vnote *) CTX;
444 StrEscAppend(Target, NULL, v->body, 0, 0); ///TODO?
447 void tmpl_vcard_put_uid(StrBuf *Target, WCTemplputParams *TP)
449 struct vnote *v = (struct vnote *) CTX;
450 StrBufAppendBufPlain(Target, v->uid, -1, 0);
453 void
454 InitModule_NOTES
455 (void)
457 WebcitAddUrlHandler(HKEY("add_new_note"), add_new_note, 0);
458 WebcitAddUrlHandler(HKEY("ajax_update_note"), ajax_update_note, 0);
460 RegisterNamespace("VNOTE:POS:LEFT", 0, 0, tmpl_vcard_put_posleft, CTX_VNOTE);
461 RegisterNamespace("VNOTE:POS:TOP", 0, 0, tmpl_vcard_put_postop, CTX_VNOTE);
462 RegisterNamespace("VNOTE:POS:WIDTH", 0, 0, tmpl_vcard_put_poswidth, CTX_VNOTE);
463 RegisterNamespace("VNOTE:POS:HEIGHT", 0, 0, tmpl_vcard_put_posheight, CTX_VNOTE);
464 RegisterNamespace("VNOTE:POS:HEIGHT2", 0, 0, tmpl_vcard_put_posheight2, CTX_VNOTE);
465 RegisterNamespace("VNOTE:POS:WIDTH2", 0, 0, tmpl_vcard_put_width2, CTX_VNOTE);
466 RegisterNamespace("VNOTE:COLOR", 0, 0, tmpl_vcard_put_color, CTX_VNOTE);
467 RegisterNamespace("VNOTE:BGCOLOR", 0, 0,tmpl_vcard_put_bgcolor, CTX_VNOTE);
468 RegisterNamespace("VNOTE:MSG", 0, 1, tmpl_vcard_put_message, CTX_VNOTE);
469 RegisterNamespace("VNOTE:UID", 0, 0, tmpl_vcard_put_uid, CTX_VNOTE);