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