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