* swap I/O to the new functions
[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         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.")
89         );
90
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
95
96
97
98
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
101          */
102         wprintf("<div id=\"palette-%s\" ", v->uid);     // begin stickynote_palette div
103         wprintf("class=\"stickynote_palette\">");
104
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
120                 );
121                 wprintf("bgcolor=\"#%02x%02x%02x\"> </td>",
122                         pastel_palette[i][0],
123                         pastel_palette[i][1],
124                         pastel_palette[i][2]
125                 );
126                 if (((i+1)%3)==0) wprintf("</tr>");
127         }
128         wprintf("</table>");
129
130         wprintf("</div>");                              // end stickynote_palette div
131
132
133
134
135
136         wprintf("</div>\n");                            // end outer div
137 }
138
139
140
141 /*
142  * Fetch a message from the server and extract a vNote from it
143  */
144 struct vnote *vnote_new_from_msg(long msgnum,int unread) 
145 {
146         StrBuf *Buf;
147         StrBuf *Data;
148         const char *bptr;
149         int Done = 0;
150         char from[128] = "";
151         char uid_from_headers[256];
152         char mime_partnum[256];
153         char mime_filename[256];
154         char mime_content_type[256];
155         char mime_disposition[256];
156         int mime_length;
157         char relevant_partnum[256];
158         int phase = 0;                          /* 0 = citadel headers, 1 = mime headers, 2 = body */
159         char msg4_content_type[256] = "";
160         char msg4_content_encoding[256] = "";
161         int msg4_content_length = 0;
162         struct vnote *vnote_from_body = NULL;
163
164         relevant_partnum[0] = '\0';
165         serv_printf("MSG4 %ld", msgnum);        /* we need the mime headers */
166         Buf = NewStrBuf();
167         StrBuf_ServGetlnBuffered(Buf);
168         if (GetServerStatus(Buf, NULL) != 1) {
169                 FreeStrBuf (&Buf);
170                 return NULL;
171         }
172         while ((StrBuf_ServGetlnBuffered(Buf)>=0) && !Done) {
173                 if ( (StrLength(Buf)==3) && 
174                      !strcmp(ChrPtr(Buf), "000")) {
175                         Done = 1;
176                         break;
177                 }
178                 bptr = ChrPtr(Buf);
179                 switch (phase) {
180                 case 0:
181                         if (!strncasecmp(bptr, "exti=", 5)) {
182                                 safestrncpy(uid_from_headers, &(ChrPtr(Buf)[5]), sizeof uid_from_headers);
183                         }
184                         else if (!strncasecmp(bptr, "part=", 5)) {
185                                 extract_token(mime_filename, &bptr[5], 1, '|', sizeof mime_filename);
186                                 extract_token(mime_partnum, &bptr[5], 2, '|', sizeof mime_partnum);
187                                 extract_token(mime_disposition, &bptr[5], 3, '|', sizeof mime_disposition);
188                                 extract_token(mime_content_type, &bptr[5], 4, '|', sizeof mime_content_type);
189                                 mime_length = extract_int(&bptr[5], 5);
190
191                                 if (  (!strcasecmp(mime_content_type, "text/calendar"))
192                                       || (!strcasecmp(mime_content_type, "application/ics"))
193                                       || (!strcasecmp(mime_content_type, "text/vtodo"))
194                                         ) {
195                                         strcpy(relevant_partnum, mime_partnum);
196                                 }
197                         }
198                         else if (!strncasecmp(bptr, "from=", 4)) {
199                                 extract_token(from, bptr, 1, '=', sizeof(from));
200                         }
201                         else if ((phase == 0) && (!strncasecmp(bptr, "text", 4))) {
202                                 phase = 1;
203                         }
204                 break;
205                 case 1:
206                         if (!IsEmptyStr(bptr)) {
207                                 if (!strncasecmp(bptr, "Content-type: ", 14)) {
208                                         safestrncpy(msg4_content_type, &bptr[14], sizeof msg4_content_type);
209                                         striplt(msg4_content_type);
210                                 }
211                                 else if (!strncasecmp(bptr, "Content-transfer-encoding: ", 27)) {
212                                         safestrncpy(msg4_content_encoding, &bptr[27], sizeof msg4_content_encoding);
213                                         striplt(msg4_content_type);
214                                 }
215                                 else if ((!strncasecmp(bptr, "Content-length: ", 16))) {
216                                         msg4_content_length = atoi(&bptr[16]);
217                                 }
218                                 break;
219                         }
220                         else {
221                                 phase++;
222                                 
223                                 if ((msg4_content_length > 0)
224                                     && ( !strcasecmp(msg4_content_encoding, "7bit"))
225                                     && ((!strcasecmp(mime_content_type, "text/calendar"))
226                                         || (!strcasecmp(mime_content_type, "application/ics"))
227                                         || (!strcasecmp(mime_content_type, "text/vtodo"))
228                                             )
229                                         ) 
230                                 {
231                                 }
232                         }
233                 case 2:
234                         Data = NewStrBufPlain(NULL, msg4_content_length * 2);
235                         if (msg4_content_length > 0) {
236                                 StrBuf_ServGetBLOBBuffered(Data, msg4_content_length);
237                                 phase ++;
238                         }
239                         else {
240                                 StrBufAppendBuf(Data, Buf, 0);
241                                 StrBufAppendBufPlain(Data, "\r\n", 1, 0);
242                         }
243                 case 3:
244                         StrBufAppendBuf(Data, Buf, 0);
245                 }
246         }
247         FreeStrBuf(&Buf);
248
249         /* If MSG4 didn't give us the part we wanted, but we know that we can find it
250          * as one of the other MIME parts, attempt to load it now.
251          */
252         if ((Data == NULL) && (!IsEmptyStr(relevant_partnum))) {
253                 Data = load_mimepart(msgnum, relevant_partnum);
254         }
255
256
257         if ((IsEmptyStr(relevant_partnum)) && (StrLength(Data) > 0 )) {
258                 if (!IsEmptyStr(uid_from_headers)) {
259                         // Convert an old-style note to a vNote
260                         vnote_from_body = vnote_new();
261                         vnote_from_body->uid = strdup(uid_from_headers);
262                         vnote_from_body->color_red = pastel_palette[3][0];
263                         vnote_from_body->color_green = pastel_palette[3][1];
264                         vnote_from_body->color_blue = pastel_palette[3][2];
265                         vnote_from_body->body = malloc(StrLength(Data) + 1);
266                         vnote_from_body->body[0] = 0;
267                         memcpy(vnote_from_body->body, ChrPtr(Data), StrLength(Data) + 1);
268                         return vnote_from_body;
269                 }
270                 else {
271                         struct vnote *v = vnote_new_from_str(ChrPtr(Data));
272                         return(v);
273                 }
274         }
275         return NULL;
276 }
277
278
279
280 /*
281  * Serialize a vnote and write it to the server
282  */
283 void write_vnote_to_server(struct vnote *v) 
284 {
285         char buf[1024];
286         char *pch;
287
288         serv_puts("ENT0 1|||4");
289         serv_getln(buf, sizeof buf);
290         if (buf[0] == '4') {
291                 serv_puts("Content-type: text/vnote");
292                 serv_puts("");
293                 pch = vnote_serialize(v);
294                 serv_puts(pch);
295                 free(pch);
296                 serv_puts("000");
297         }
298 }
299
300
301
302
303 /*
304  * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
305  */
306 void ajax_update_note(void) {
307
308         char buf[1024];
309         int msgnum;
310         struct vnote *v = NULL;
311
312         if (!havebstr("note_uid")) {
313                 begin_ajax_response();
314                 wprintf("Received ajax_update_note() request without a note UID.");
315                 end_ajax_response();
316                 return;
317         }
318
319         // lprintf(9, "Note UID = %s\n", bstr("note_uid"));
320         serv_printf("EUID %s", bstr("note_uid"));
321         serv_getln(buf, sizeof buf);
322         if (buf[0] != '2') {
323                 begin_ajax_response();
324                 wprintf("Cannot find message containing vNote with the requested uid!");
325                 end_ajax_response();
326                 return;
327         }
328         msgnum = atol(&buf[4]);
329         // lprintf(9, "Note msg = %ld\n", msgnum);
330
331         // Was this request a delete operation?  If so, nuke it...
332         if (havebstr("deletenote")) {
333                 if (!strcasecmp(bstr("deletenote"), "yes")) {
334                         serv_printf("DELE %d", msgnum);
335                         serv_getln(buf, sizeof buf);
336                         begin_ajax_response();
337                         wprintf("%s", buf);
338                         end_ajax_response();
339                         return;
340                 }
341         }
342
343         // If we get to this point it's an update, not a delete
344         v = vnote_new_from_msg(msgnum, 0);
345         if (!v) {
346                 begin_ajax_response();
347                 wprintf("Cannot locate a vNote within message %d\n", msgnum);
348                 end_ajax_response();
349                 return;
350         }
351
352         /* Make any requested changes */
353         if (havebstr("top")) {
354                 v->pos_top = atoi(bstr("top"));
355         }
356         if (havebstr("left")) {
357                 v->pos_left = atoi(bstr("left"));
358         }
359         if (havebstr("height")) {
360                 v->pos_height = atoi(bstr("height"));
361         }
362         if (havebstr("width")) {
363                 v->pos_width = atoi(bstr("width"));
364         }
365         if (havebstr("red")) {
366                 v->color_red = atoi(bstr("red"));
367         }
368         if (havebstr("green")) {
369                 v->color_green = atoi(bstr("green"));
370         }
371         if (havebstr("blue")) {
372                 v->color_blue = atoi(bstr("blue"));
373         }
374         if (havebstr("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
375                 if (v->body) free(v->body);
376                 v->body = strdup(bstr("value"));
377         }
378
379         /* Serialize it and save it to the message base.  Server will delete the old one. */
380         write_vnote_to_server(v);
381
382         begin_ajax_response();
383         if (v->body) {
384                 escputs(v->body);
385         }
386         end_ajax_response();
387
388         vnote_free(v);
389 }
390
391
392
393
394 /*
395  * display sticky notes
396  *
397  * msgnum = Message number on the local server of the note to be displayed
398  */
399 void display_note(message_summary *Msg, int unread) {
400         struct vnote *v;
401         WCTemplputParams TP;
402
403         memset(&TP, 0, sizeof(WCTemplputParams));
404         TP.Filter.ContextType = CTX_VNOTE;
405         v = vnote_new_from_msg(Msg->msgnum, unread);
406         if (v) {
407 //              display_vnote_div(v);
408                 TP.Context = v;
409                 DoTemplate(HKEY("vnoteitem"),
410                            WC->WBuf, &TP);
411                         
412
413                 /* uncomment these lines to see ugly debugging info 
414                 StrBufAppendPrintf(WC->trailing_javascript,
415                         "document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
416                 StrBufAppendPrintf(WC->trailing_javascript,
417                         "document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
418                 StrBufAppendPrintf(WC->trailing_javascript,
419                         "document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
420                 StrBufAppendPrintf(WC->trailing_javascript,
421                         "document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
422                 */
423
424                 vnote_free(v);
425         }
426 }
427
428
429
430 /*
431  * Create a new note
432  */
433 void add_new_note(void) {
434         struct vnote *v;
435
436         v = vnote_new();
437         if (v) {
438                 v->uid = malloc(128);
439                 generate_uuid(v->uid);
440                 v->color_red = pastel_palette[3][0];
441                 v->color_green = pastel_palette[3][1];
442                 v->color_blue = pastel_palette[3][2];
443                 v->body = strdup(_("Click on any note to edit it."));
444                 write_vnote_to_server(v);
445                 vnote_free(v);
446         }
447         
448         readloop(readfwd);
449 }
450
451
452 void tmpl_vcard_put_posleft(StrBuf *Target, WCTemplputParams *TP)
453 {
454         struct vnote *v = (struct vnote *) CTX;
455         StrBufAppendPrintf(Target, "%d", v->pos_left);
456 }
457
458 void tmpl_vcard_put_postop(StrBuf *Target, WCTemplputParams *TP)
459 {
460         struct vnote *v = (struct vnote *) CTX;
461         StrBufAppendPrintf(Target, "%d", v->pos_top);
462 }
463
464 void tmpl_vcard_put_poswidth(StrBuf *Target, WCTemplputParams *TP)
465 {
466         struct vnote *v = (struct vnote *) CTX;
467         StrBufAppendPrintf(Target, "%d", v->pos_width);
468 }
469
470 void tmpl_vcard_put_posheight(StrBuf *Target, WCTemplputParams *TP)
471 {
472         struct vnote *v = (struct vnote *) CTX;
473         StrBufAppendPrintf(Target, "%d", v->pos_height);
474 }
475
476 void tmpl_vcard_put_posheight2(StrBuf *Target, WCTemplputParams *TP)
477 {
478         struct vnote *v = (struct vnote *) CTX;
479         StrBufAppendPrintf(Target, "%d", (v->pos_height / 16) - 5);
480 }
481
482 void tmpl_vcard_put_width2(StrBuf *Target, WCTemplputParams *TP)
483 {
484         struct vnote *v = (struct vnote *) CTX;
485         StrBufAppendPrintf(Target, "%d", (v->pos_width / 9) - 1);
486 }
487
488 void tmpl_vcard_put_color(StrBuf *Target, WCTemplputParams *TP)
489 {
490         struct vnote *v = (struct vnote *) CTX;
491         StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red, v->color_green, v->color_blue);
492 }
493
494 void tmpl_vcard_put_bgcolor(StrBuf *Target, WCTemplputParams *TP)
495 {
496         struct vnote *v = (struct vnote *) CTX;
497         StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red/2, v->color_green/2, v->color_blue/2);
498 }
499
500 void tmpl_vcard_put_message(StrBuf *Target, WCTemplputParams *TP)
501 {
502         struct vnote *v = (struct vnote *) CTX;
503         StrEscAppend(Target, NULL, v->body, 0, 0); ///TODO?
504 }
505
506 void tmpl_vcard_put_uid(StrBuf *Target, WCTemplputParams *TP)
507 {
508         struct vnote *v = (struct vnote *) CTX;
509         StrBufAppendBufPlain(Target, v->uid, -1, 0);
510 }
511
512 void 
513 InitModule_NOTES
514 (void)
515 {
516         WebcitAddUrlHandler(HKEY("add_new_note"), add_new_note, 0);
517         WebcitAddUrlHandler(HKEY("ajax_update_note"), ajax_update_note, 0);
518
519         RegisterNamespace("VNOTE:POS:LEFT", 0, 0, tmpl_vcard_put_posleft, CTX_VNOTE);
520         RegisterNamespace("VNOTE:POS:TOP", 0, 0, tmpl_vcard_put_postop, CTX_VNOTE);
521         RegisterNamespace("VNOTE:POS:WIDTH", 0, 0, tmpl_vcard_put_poswidth, CTX_VNOTE);
522         RegisterNamespace("VNOTE:POS:HEIGHT", 0, 0, tmpl_vcard_put_posheight, CTX_VNOTE);
523         RegisterNamespace("VNOTE:POS:HEIGHT2", 0, 0, tmpl_vcard_put_posheight2, CTX_VNOTE);
524         RegisterNamespace("VNOTE:POS:WIDTH2", 0, 0, tmpl_vcard_put_width2, CTX_VNOTE);
525         RegisterNamespace("VNOTE:COLOR", 0, 0, tmpl_vcard_put_color, CTX_VNOTE);
526         RegisterNamespace("VNOTE:BGCOLOR", 0, 0,tmpl_vcard_put_bgcolor, CTX_VNOTE);
527         RegisterNamespace("VNOTE:MSG", 0, 1, tmpl_vcard_put_message, CTX_VNOTE);
528         RegisterNamespace("VNOTE:UID", 0, 0, tmpl_vcard_put_uid, CTX_VNOTE);
529 }