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