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