* Notes now display properly, regardless of whether we received the text/vnote from...
[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  * Fetch a message from the server and extract a vNote from it
25  */
26 struct vnote *vnote_new_from_msg(long msgnum,int unread) 
27 {
28         StrBuf *Buf;
29         StrBuf *Data = NULL;
30         const char *bptr;
31         int Done = 0;
32         char uid_from_headers[256];
33         char mime_partnum[256];
34         char mime_filename[256];
35         char mime_content_type[256];
36         char mime_disposition[256];
37         int mime_length;
38         char relevant_partnum[256];
39         int phase = 0;                          /* 0 = citadel headers, 1 = mime headers, 2 = body */
40         char msg4_content_type[256] = "";
41         char msg4_content_encoding[256] = "";
42         int msg4_content_length = 0;
43         struct vnote *vnote_from_body = NULL;
44         int vnote_inline = 0;                   /* 1 = MSG4 gave us a text/x-vnote top level */
45
46         relevant_partnum[0] = '\0';
47         serv_printf("MSG4 %ld", msgnum);        /* we need the mime headers */
48         Buf = NewStrBuf();
49         StrBuf_ServGetln(Buf);
50         if (GetServerStatus(Buf, NULL) != 1) {
51                 FreeStrBuf (&Buf);
52                 return NULL;
53         }
54         while ((StrBuf_ServGetln(Buf)>=0) && !Done) {
55                 if ( (StrLength(Buf)==3) && 
56                      !strcmp(ChrPtr(Buf), "000")) {
57                         Done = 1;
58                         break;
59                 }
60                 bptr = ChrPtr(Buf);
61                 switch (phase) {
62                 case 0:
63                         if (!strncasecmp(bptr, "exti=", 5)) {
64                                 safestrncpy(uid_from_headers, &(ChrPtr(Buf)[5]), sizeof uid_from_headers);
65                         }
66                         else if (!strncasecmp(bptr, "part=", 5)) {
67                                 extract_token(mime_filename, &bptr[5], 1, '|', sizeof mime_filename);
68                                 extract_token(mime_partnum, &bptr[5], 2, '|', sizeof mime_partnum);
69                                 extract_token(mime_disposition, &bptr[5], 3, '|', sizeof mime_disposition);
70                                 extract_token(mime_content_type, &bptr[5], 4, '|', sizeof mime_content_type);
71                                 mime_length = extract_int(&bptr[5], 5);
72
73                                 if (!strcasecmp(mime_content_type, "text/vnote")) {
74                                         strcpy(relevant_partnum, mime_partnum);
75                                 }
76                         }
77                         else if ((phase == 0) && (!strncasecmp(bptr, "text", 4))) {
78                                 phase = 1;
79                         }
80                 break;
81                 case 1:
82                         if (!IsEmptyStr(bptr)) {
83                                 if (!strncasecmp(bptr, "Content-type: ", 14)) {
84                                         safestrncpy(msg4_content_type, &bptr[14], sizeof msg4_content_type);
85                                         striplt(msg4_content_type);
86                                 }
87                                 else if (!strncasecmp(bptr, "Content-transfer-encoding: ", 27)) {
88                                         safestrncpy(msg4_content_encoding, &bptr[27], sizeof msg4_content_encoding);
89                                         striplt(msg4_content_type);
90                                 }
91                                 else if ((!strncasecmp(bptr, "Content-length: ", 16))) {
92                                         msg4_content_length = atoi(&bptr[16]);
93                                 }
94                                 break;
95                         }
96                         else {
97                                 phase++;
98                                 
99                                 if ((msg4_content_length > 0)
100                                     && ( !strcasecmp(msg4_content_encoding, "7bit"))
101                                     && (!strcasecmp(mime_content_type, "text/vnote"))
102                                 ) { 
103                                         vnote_inline = 1;
104                                 }
105                         }
106                 case 2:
107                         if (vnote_inline) {
108                                 Data = NewStrBufPlain(NULL, msg4_content_length * 2);
109                                 if (msg4_content_length > 0) {
110                                         StrBuf_ServGetBLOBBuffered(Data, msg4_content_length);
111                                         phase ++;
112                                 }
113                                 else {
114                                         StrBufAppendBuf(Data, Buf, 0);
115                                         StrBufAppendBufPlain(Data, "\r\n", 1, 0);
116                                 }
117                         }
118                 case 3:
119                         if (vnote_inline) {
120                                 StrBufAppendBuf(Data, Buf, 0);
121                         }
122                 }
123         }
124         FreeStrBuf(&Buf);
125
126         /* If MSG4 didn't give us the part we wanted, but we know that we can find it
127          * as one of the other MIME parts, attempt to load it now.
128          */
129         if ((!vnote_inline) && (!IsEmptyStr(relevant_partnum))) {
130                 Data = load_mimepart(msgnum, relevant_partnum);
131         }
132
133         if (StrLength(Data) > 0) {
134                 if (IsEmptyStr(uid_from_headers)) {
135                         // Convert an old-style note to a vNote
136                         vnote_from_body = vnote_new();
137                         vnote_from_body->uid = strdup(uid_from_headers);
138                         vnote_from_body->color_red = pastel_palette[3][0];
139                         vnote_from_body->color_green = pastel_palette[3][1];
140                         vnote_from_body->color_blue = pastel_palette[3][2];
141                         vnote_from_body->body = malloc(StrLength(Data) + 1);
142                         vnote_from_body->body[0] = 0;
143                         memcpy(vnote_from_body->body, ChrPtr(Data), StrLength(Data) + 1);
144                         FreeStrBuf(&Data);
145                         return vnote_from_body;
146                 }
147                 else {
148                         struct vnote *v = vnote_new_from_str(ChrPtr(Data));
149                         FreeStrBuf(&Data);
150                         return(v);
151                 }
152         }
153         return NULL;
154 }
155
156
157
158 /*
159  * Serialize a vnote and write it to the server
160  */
161 void write_vnote_to_server(struct vnote *v) 
162 {
163         char buf[1024];
164         char *pch;
165
166         serv_puts("ENT0 1|||4");
167         serv_getln(buf, sizeof buf);
168         if (buf[0] == '4') {
169                 serv_puts("Content-type: text/vnote");
170                 serv_puts("");
171                 pch = vnote_serialize(v);
172                 serv_puts(pch);
173                 free(pch);
174                 serv_puts("000");
175         }
176 }
177
178
179
180
181 /*
182  * Background ajax call to receive updates from the browser when a note is moved, resized, or updated.
183  */
184 void ajax_update_note(void) {
185
186         char buf[1024];
187         int msgnum;
188         struct vnote *v = NULL;
189
190         if (!havebstr("note_uid")) {
191                 begin_ajax_response();
192                 wprintf("Received ajax_update_note() request without a note UID.");
193                 end_ajax_response();
194                 return;
195         }
196
197         // lprintf(9, "Note UID = %s\n", bstr("note_uid"));
198         serv_printf("EUID %s", bstr("note_uid"));
199         serv_getln(buf, sizeof buf);
200         if (buf[0] != '2') {
201                 begin_ajax_response();
202                 wprintf("Cannot find message containing vNote with the requested uid!");
203                 end_ajax_response();
204                 return;
205         }
206         msgnum = atol(&buf[4]);
207         // lprintf(9, "Note msg = %ld\n", msgnum);
208
209         // Was this request a delete operation?  If so, nuke it...
210         if (havebstr("deletenote")) {
211                 if (!strcasecmp(bstr("deletenote"), "yes")) {
212                         serv_printf("DELE %d", msgnum);
213                         serv_getln(buf, sizeof buf);
214                         begin_ajax_response();
215                         wprintf("%s", buf);
216                         end_ajax_response();
217                         return;
218                 }
219         }
220
221         // If we get to this point it's an update, not a delete
222         v = vnote_new_from_msg(msgnum, 0);
223         if (!v) {
224                 begin_ajax_response();
225                 wprintf("Cannot locate a vNote within message %d\n", msgnum);
226                 end_ajax_response();
227                 return;
228         }
229
230         /* Make any requested changes */
231         if (havebstr("top")) {
232                 v->pos_top = atoi(bstr("top"));
233         }
234         if (havebstr("left")) {
235                 v->pos_left = atoi(bstr("left"));
236         }
237         if (havebstr("height")) {
238                 v->pos_height = atoi(bstr("height"));
239         }
240         if (havebstr("width")) {
241                 v->pos_width = atoi(bstr("width"));
242         }
243         if (havebstr("red")) {
244                 v->color_red = atoi(bstr("red"));
245         }
246         if (havebstr("green")) {
247                 v->color_green = atoi(bstr("green"));
248         }
249         if (havebstr("blue")) {
250                 v->color_blue = atoi(bstr("blue"));
251         }
252         if (havebstr("value")) {        // I would have preferred 'body' but InPlaceEditor hardcodes 'value'
253                 if (v->body) free(v->body);
254                 v->body = strdup(bstr("value"));
255         }
256
257         /* Serialize it and save it to the message base.  Server will delete the old one. */
258         write_vnote_to_server(v);
259
260         begin_ajax_response();
261         if (v->body) {
262                 escputs(v->body);
263         }
264         end_ajax_response();
265
266         vnote_free(v);
267 }
268
269
270
271
272 /*
273  * display sticky notes
274  *
275  * msgnum = Message number on the local server of the note to be displayed
276  */
277 void display_note(message_summary *Msg, int unread) {
278         struct vnote *v;
279         WCTemplputParams TP;
280
281         memset(&TP, 0, sizeof(WCTemplputParams));
282         TP.Filter.ContextType = CTX_VNOTE;
283         v = vnote_new_from_msg(Msg->msgnum, unread);
284         if (v) {
285                 TP.Context = v;
286                 DoTemplate(HKEY("vnoteitem"),
287                            WC->WBuf, &TP);
288                         
289
290                 /* uncomment these lines to see ugly debugging info 
291                 StrBufAppendPrintf(WC->trailing_javascript,
292                         "document.write('L: ' + $('note-%s').style.left + '; ');", v->uid);
293                 StrBufAppendPrintf(WC->trailing_javascript,
294                         "document.write('T: ' + $('note-%s').style.top + '; ');", v->uid);
295                 StrBufAppendPrintf(WC->trailing_javascript,
296                         "document.write('W: ' + $('note-%s').style.width + '; ');", v->uid);
297                 StrBufAppendPrintf(WC->trailing_javascript,
298                         "document.write('H: ' + $('note-%s').style.height + '<br>');", v->uid);
299                 */
300
301                 vnote_free(v);
302         }
303 }
304
305
306
307 /*
308  * Create a new note
309  */
310 void add_new_note(void) {
311         struct vnote *v;
312
313         v = vnote_new();
314         if (v) {
315                 v->uid = malloc(128);
316                 generate_uuid(v->uid);
317                 v->color_red = pastel_palette[3][0];
318                 v->color_green = pastel_palette[3][1];
319                 v->color_blue = pastel_palette[3][2];
320                 v->body = strdup(_("Click on any note to edit it."));
321                 write_vnote_to_server(v);
322                 vnote_free(v);
323         }
324         
325         readloop(readfwd);
326 }
327
328
329 void tmpl_vcard_put_posleft(StrBuf *Target, WCTemplputParams *TP)
330 {
331         struct vnote *v = (struct vnote *) CTX;
332         StrBufAppendPrintf(Target, "%d", v->pos_left);
333 }
334
335 void tmpl_vcard_put_postop(StrBuf *Target, WCTemplputParams *TP)
336 {
337         struct vnote *v = (struct vnote *) CTX;
338         StrBufAppendPrintf(Target, "%d", v->pos_top);
339 }
340
341 void tmpl_vcard_put_poswidth(StrBuf *Target, WCTemplputParams *TP)
342 {
343         struct vnote *v = (struct vnote *) CTX;
344         StrBufAppendPrintf(Target, "%d", v->pos_width);
345 }
346
347 void tmpl_vcard_put_posheight(StrBuf *Target, WCTemplputParams *TP)
348 {
349         struct vnote *v = (struct vnote *) CTX;
350         StrBufAppendPrintf(Target, "%d", v->pos_height);
351 }
352
353 void tmpl_vcard_put_posheight2(StrBuf *Target, WCTemplputParams *TP)
354 {
355         struct vnote *v = (struct vnote *) CTX;
356         StrBufAppendPrintf(Target, "%d", (v->pos_height / 16) - 5);
357 }
358
359 void tmpl_vcard_put_width2(StrBuf *Target, WCTemplputParams *TP)
360 {
361         struct vnote *v = (struct vnote *) CTX;
362         StrBufAppendPrintf(Target, "%d", (v->pos_width / 9) - 1);
363 }
364
365 void tmpl_vcard_put_color(StrBuf *Target, WCTemplputParams *TP)
366 {
367         struct vnote *v = (struct vnote *) CTX;
368         StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red, v->color_green, v->color_blue);
369 }
370
371 void tmpl_vcard_put_bgcolor(StrBuf *Target, WCTemplputParams *TP)
372 {
373         struct vnote *v = (struct vnote *) CTX;
374         StrBufAppendPrintf(Target, "%02X%02X%02X", v->color_red/2, v->color_green/2, v->color_blue/2);
375 }
376
377 void tmpl_vcard_put_message(StrBuf *Target, WCTemplputParams *TP)
378 {
379         struct vnote *v = (struct vnote *) CTX;
380         StrEscAppend(Target, NULL, v->body, 0, 0); ///TODO?
381 }
382
383 void tmpl_vcard_put_uid(StrBuf *Target, WCTemplputParams *TP)
384 {
385         struct vnote *v = (struct vnote *) CTX;
386         StrBufAppendBufPlain(Target, v->uid, -1, 0);
387 }
388
389 void 
390 InitModule_NOTES
391 (void)
392 {
393         WebcitAddUrlHandler(HKEY("add_new_note"), add_new_note, 0);
394         WebcitAddUrlHandler(HKEY("ajax_update_note"), ajax_update_note, 0);
395
396         RegisterNamespace("VNOTE:POS:LEFT", 0, 0, tmpl_vcard_put_posleft, CTX_VNOTE);
397         RegisterNamespace("VNOTE:POS:TOP", 0, 0, tmpl_vcard_put_postop, CTX_VNOTE);
398         RegisterNamespace("VNOTE:POS:WIDTH", 0, 0, tmpl_vcard_put_poswidth, CTX_VNOTE);
399         RegisterNamespace("VNOTE:POS:HEIGHT", 0, 0, tmpl_vcard_put_posheight, CTX_VNOTE);
400         RegisterNamespace("VNOTE:POS:HEIGHT2", 0, 0, tmpl_vcard_put_posheight2, CTX_VNOTE);
401         RegisterNamespace("VNOTE:POS:WIDTH2", 0, 0, tmpl_vcard_put_width2, CTX_VNOTE);
402         RegisterNamespace("VNOTE:COLOR", 0, 0, tmpl_vcard_put_color, CTX_VNOTE);
403         RegisterNamespace("VNOTE:BGCOLOR", 0, 0,tmpl_vcard_put_bgcolor, CTX_VNOTE);
404         RegisterNamespace("VNOTE:MSG", 0, 1, tmpl_vcard_put_message, CTX_VNOTE);
405         RegisterNamespace("VNOTE:UID", 0, 0, tmpl_vcard_put_uid, CTX_VNOTE);
406 }