* add contexttype, so the subst can precheck the context pointer for us, and bad...
[citadel.git] / webcit / notes.c
1 /*
2  * $Id$
3  *
4  */
5
6 #include "webcit.h"
7 #include "groupdav.h"
8 #include "webserver.h"
9
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 }
20 };
21
22
23 /*
24  * Display a <div> containing a rendered sticky note.
25  */
26 void display_vnote_div(struct vnote *v) {
27         int i;
28
29
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("\">");
39
40
41
42
43
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("\">");
50
51         wprintf("<table border=0 cellpadding=0 cellspacing=0 valign=middle width=100%%><tr>");
52
53         wprintf("<td align=left valign=middle>");
54         wprintf("<img onclick=\"NotesClickPalette(event,'%s')\" ", v->uid);
55         wprintf("src=\"static/8paint16.gif\">");
56
57         wprintf("</td>");
58
59         wprintf("<td></td>");   // nothing in the title bar, it's just for dragging
60
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>");
65
66         wprintf("</div>\n");                            // end title bar div
67
68
69
70
71
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
77
78         wprintf("<script type=\"text/javascript\">");
79         wprintf(" 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.")
89         );
90         wprintf("</script>\n");
91
92
93
94         wprintf("<div id=\"resize-%s\" ", v->uid);      // begin resize handle div
95         wprintf("class=\"stickynote_resize\" ");
96         wprintf("onMouseDown=\"NotesResizeMouseDown(event,'%s')\"", v->uid);
97         wprintf("> </div>");                            // end resize handle div
98
99
100
101
102         /* embed color selector - it doesn't have to be inside the title bar html because
103          * it's a separate div placed by css
104          */
105         wprintf("<div id=\"palette-%s\" ", v->uid);     // begin stickynote_palette div
106         wprintf("class=\"stickynote_palette\">");
107
108         wprintf("<table border=0 cellpadding=0 cellspacing=0>");
109         for (i=0; i<9; ++i) {
110                 if ((i%3)==0) wprintf("<tr>");
111                 wprintf("<td ");
112                 wprintf("onClick=\"NotesClickColor(event,'%s',%d,%d,%d,'#%02x%02x%02x','#%02x%02x%02x')\" ",
113                         v->uid,
114                         pastel_palette[i][0],           // color values to pass to ajax call
115                         pastel_palette[i][1],
116                         pastel_palette[i][2],
117                         pastel_palette[i][0],           // new color of note
118                         pastel_palette[i][1],
119                         pastel_palette[i][2],
120                         pastel_palette[i][0] / 2,       // new color of title bar
121                         pastel_palette[i][1] / 2,
122                         pastel_palette[i][2] / 2
123                 );
124                 wprintf("bgcolor=\"#%02x%02x%02x\"> </td>",
125                         pastel_palette[i][0],
126                         pastel_palette[i][1],
127                         pastel_palette[i][2]
128                 );
129                 if (((i+1)%3)==0) wprintf("</tr>");
130         }
131         wprintf("</table>");
132
133         wprintf("</div>");                              // end stickynote_palette div
134
135
136
137
138
139         wprintf("</div>\n");                            // end outer div
140 }
141
142
143
144 /*
145  * Fetch a message from the server and extract a vNote from it
146  */
147 struct vnote *vnote_new_from_msg(long msgnum) {
148         char buf[1024];
149         char mime_partnum[256];
150         char mime_filename[256];
151         char mime_content_type[256];
152         char mime_disposition[256];
153         int mime_length;
154         char relevant_partnum[256];
155         char *relevant_source = NULL;
156         char uid_from_headers[256];
157         int in_body = 0;
158         int body_line_len = 0;
159         int body_len = 0;
160         struct vnote *vnote_from_body = NULL;
161
162         relevant_partnum[0] = 0;
163         uid_from_headers[0] = 0;
164         sprintf(buf, "MSG4 %ld", msgnum);       /* we need the mime headers */
165         serv_puts(buf);
166         serv_getln(buf, sizeof buf);
167         if (buf[0] != '1') return NULL;
168
169         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
170                 if (!strncasecmp(buf, "exti=", 5)) {
171                         safestrncpy(uid_from_headers, &buf[5], sizeof uid_from_headers);
172                 }
173                 else if (!strncasecmp(buf, "part=", 5)) {
174                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
175                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
176                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
177                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
178                         mime_length = extract_int(&buf[5], 5);
179
180                         if (!strcasecmp(mime_content_type, "text/vnote")) {
181                                 strcpy(relevant_partnum, mime_partnum);
182                         }
183                 }
184                 else if ((in_body) && (IsEmptyStr(relevant_partnum)) && (!IsEmptyStr(uid_from_headers))) {
185                         // Convert an old-style note to a vNote
186                         if (!vnote_from_body) {
187                                 vnote_from_body = vnote_new();
188                                 vnote_from_body->uid = strdup(uid_from_headers);
189                                 vnote_from_body->color_red = pastel_palette[3][0];
190                                 vnote_from_body->color_green = pastel_palette[3][1];
191                                 vnote_from_body->color_blue = pastel_palette[3][2];
192                                 vnote_from_body->body = malloc(32768);
193                                 vnote_from_body->body[0] = 0;
194                                 body_len = 0;
195                         }
196                         body_line_len = strlen(buf);
197                         if ((body_len + body_line_len + 10) < 32768) {
198                                 strcpy(&vnote_from_body->body[body_len++], " ");
199                                 strcpy(&vnote_from_body->body[body_len], buf);
200                                 body_len += body_line_len;
201                         }
202                 }
203                 else if (IsEmptyStr(buf)) {
204                         in_body = 1;
205                 }
206         }
207
208         if (!IsEmptyStr(relevant_partnum)) {
209                 relevant_source = load_mimepart(msgnum, relevant_partnum);
210                 if (relevant_source != NULL) {
211                         struct vnote *v = vnote_new_from_str(relevant_source);
212                         free(relevant_source);
213                         return(v);
214                 }
215         }
216
217         if (vnote_from_body) {
218                 return(vnote_from_body);
219         }
220         return NULL;
221 }
222
223
224 /*
225  * Serialize a vnote and write it to the server
226  */
227 void write_vnote_to_server(struct vnote *v) 
228 {
229         char buf[1024];
230         char *pch;
231
232         serv_puts("ENT0 1|||4");
233         serv_getln(buf, sizeof buf);
234         if (buf[0] == '4') {
235                 serv_puts("Content-type: text/vnote");
236                 serv_puts("");
237                 pch = vnote_serialize(v);
238                 serv_puts(pch);
239                 free(pch);
240                 serv_puts("000");
241         }
242 }
243
244
245
246
247 /*
248  * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
249  */
250 void ajax_update_note(void) {
251
252         char buf[1024];
253         int msgnum;
254         struct vnote *v = NULL;
255
256         if (!havebstr("note_uid")) {
257                 begin_ajax_response();
258                 wprintf("Received ajax_update_note() request without a note UID.");
259                 end_ajax_response();
260                 return;
261         }
262
263         // lprintf(9, "Note UID = %s\n", bstr("note_uid"));
264         serv_printf("EUID %s", bstr("note_uid"));
265         serv_getln(buf, sizeof buf);
266         if (buf[0] != '2') {
267                 begin_ajax_response();
268                 wprintf("Cannot find message containing vNote with the requested uid!");
269                 end_ajax_response();
270                 return;
271         }
272         msgnum = atol(&buf[4]);
273         // lprintf(9, "Note msg = %ld\n", msgnum);
274
275         // Was this request a delete operation?  If so, nuke it...
276         if (havebstr("deletenote")) {
277                 if (!strcasecmp(bstr("deletenote"), "yes")) {
278                         serv_printf("DELE %d", msgnum);
279                         serv_getln(buf, sizeof buf);
280                         begin_ajax_response();
281                         wprintf("%s", buf);
282                         end_ajax_response();
283                         return;
284                 }
285         }
286
287         // If we get to this point it's an update, not a delete
288         v = vnote_new_from_msg(msgnum);
289         if (!v) {
290                 begin_ajax_response();
291                 wprintf("Cannot locate a vNote within message %d\n", msgnum);
292                 end_ajax_response();
293                 return;
294         }
295
296         /* Make any requested changes */
297         if (havebstr("top")) {
298                 v->pos_top = atoi(bstr("top"));
299         }
300         if (havebstr("left")) {
301                 v->pos_left = atoi(bstr("left"));
302         }
303         if (havebstr("height")) {
304                 v->pos_height = atoi(bstr("height"));
305         }
306         if (havebstr("width")) {
307                 v->pos_width = atoi(bstr("width"));
308         }
309         if (havebstr("red")) {
310                 v->color_red = atoi(bstr("red"));
311         }
312         if (havebstr("green")) {
313                 v->color_green = atoi(bstr("green"));
314         }
315         if (havebstr("blue")) {
316                 v->color_blue = atoi(bstr("blue"));
317         }
318         if (havebstr("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
319                 if (v->body) free(v->body);
320                 v->body = strdup(bstr("value"));
321         }
322
323         /* Serialize it and save it to the message base.  Server will delete the old one. */
324         write_vnote_to_server(v);
325
326         begin_ajax_response();
327         if (v->body) {
328                 escputs(v->body);
329         }
330         end_ajax_response();
331
332         vnote_free(v);
333 }
334
335
336
337
338 /*
339  * display sticky notes
340  *
341  * msgnum = Message number on the local server of the note to be displayed
342  */
343 void display_note(long msgnum, int unread) {
344         struct vnote *v;
345
346         v = vnote_new_from_msg(msgnum);
347         if (v) {
348 //              display_vnote_div(v);
349                 DoTemplate(HKEY("vnoteitem"),
350                            v, WC->WBuf, CTX_VNOTE);
351                         
352
353                 /* uncomment these lines to see ugly debugging info 
354                 wprintf("<script type=\"text/javascript\">");
355                 wprintf("document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
356                 wprintf("document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
357                 wprintf("document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
358                 wprintf("document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
359                 wprintf("</script>");
360                 */
361
362                 vnote_free(v);
363         }
364 }
365
366
367
368 /*
369  * Create a new note
370  */
371 void add_new_note(void) {
372         struct vnote *v;
373
374         v = vnote_new();
375         if (v) {
376                 v->uid = malloc(128);
377                 generate_uuid(v->uid);
378                 v->color_red = pastel_palette[3][0];
379                 v->color_green = pastel_palette[3][1];
380                 v->color_blue = pastel_palette[3][2];
381                 v->body = strdup(_("Click on any note to edit it."));
382                 write_vnote_to_server(v);
383                 vnote_free(v);
384         }
385         
386         readloop("readfwd");
387 }
388
389
390 void tmpl_vcard_put_posleft(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
391 {
392         struct vnote *v = (struct vnote *) Context;
393         StrBufAppendPrintf(Target, "%d", v->pos_left);
394 }
395
396 void tmpl_vcard_put_postop(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
397 {
398         struct vnote *v = (struct vnote *) Context;
399         StrBufAppendPrintf(Target, "%d", v->pos_top);
400 }
401
402 void tmpl_vcard_put_poswidth(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
403 {
404         struct vnote *v = (struct vnote *) Context;
405         StrBufAppendPrintf(Target, "%d", v->pos_width);
406 }
407
408 void tmpl_vcard_put_posheight(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
409 {
410         struct vnote *v = (struct vnote *) Context;
411         StrBufAppendPrintf(Target, "%d", v->pos_height);
412 }
413
414 void tmpl_vcard_put_posheight2(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
415 {
416         struct vnote *v = (struct vnote *) Context;
417         StrBufAppendPrintf(Target, "%d", (v->pos_height / 16) - 5);
418 }
419
420 void tmpl_vcard_put_width2(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
421 {
422         struct vnote *v = (struct vnote *) Context;
423         StrBufAppendPrintf(Target, "%d", (v->pos_width / 9) - 1);
424 }
425
426 void tmpl_vcard_put_color(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
427 {
428         struct vnote *v = (struct vnote *) Context;
429         StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red, v->color_green, v->color_blue);
430 }
431
432 void tmpl_vcard_put_bgcolor(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
433 {
434         struct vnote *v = (struct vnote *) Context;
435         StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red/2, v->color_green/2, v->color_blue/2);
436 }
437
438 void tmpl_vcard_put_message(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
439 {
440         struct vnote *v = (struct vnote *) Context;
441         StrEscAppend(Target, NULL, v->body, 0, 0); ///TODO?
442 }
443
444 void tmpl_vcard_put_uid(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
445 {
446         struct vnote *v = (struct vnote *) Context;
447         StrBufAppendBufPlain(Target, v->uid, -1, 0);
448 }
449
450 void 
451 InitModule_NOTES
452 (void)
453 {
454         WebcitAddUrlHandler(HKEY("add_new_note"), add_new_note, 0);
455         WebcitAddUrlHandler(HKEY("ajax_update_note"), ajax_update_note, 0);
456
457         RegisterNamespace("VNOTE:POS:LEFT", 0, 0, tmpl_vcard_put_posleft, CTX_VNOTE);
458         RegisterNamespace("VNOTE:POS:TOP", 0, 0, tmpl_vcard_put_postop, CTX_VNOTE);
459         RegisterNamespace("VNOTE:POS:WIDTH", 0, 0, tmpl_vcard_put_poswidth, CTX_VNOTE);
460         RegisterNamespace("VNOTE:POS:HEIGHT", 0, 0, tmpl_vcard_put_posheight, CTX_VNOTE);
461         RegisterNamespace("VNOTE:POS:HEIGHT2", 0, 0, tmpl_vcard_put_posheight2, CTX_VNOTE);
462         RegisterNamespace("VNOTE:POS:WIDTH2", 0, 0, tmpl_vcard_put_width2, CTX_VNOTE);
463         RegisterNamespace("VNOTE:COLOR", 0, 0, tmpl_vcard_put_color, CTX_VNOTE);
464         RegisterNamespace("VNOTE:BGCOLOR", 0, 0,tmpl_vcard_put_bgcolor, CTX_VNOTE);
465         RegisterNamespace("VNOTE:MSG", 0, 1, tmpl_vcard_put_message, CTX_VNOTE);
466         RegisterNamespace("VNOTE:UID", 0, 0, tmpl_vcard_put_uid, CTX_VNOTE);
467 }