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