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