6e30c98d30e2f3f176a2be4df1afdb5cff39ade3
[citadel.git] / webcit / messages.c
1 /*
2  * Functions which deal with the fetching and displaying of messages.
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7 #include "groupdav.h"
8
9 HashList *MsgHeaderHandler = NULL;
10 HashList *MsgEvaluators = NULL;
11 HashList *MimeRenderHandler = NULL;
12 HashList *ReadLoopHandler = NULL;
13 int dbg_analyze_msg = 0;
14
15 #define SUBJ_COL_WIDTH_PCT              50      /* Mailbox view column width */
16 #define SENDER_COL_WIDTH_PCT            30      /* Mailbox view column width */
17 #define DATE_PLUS_BUTTONS_WIDTH_PCT     20      /* Mailbox view column width */
18
19 void jsonMessageListHdr(void);
20
21 void display_enter(void);
22
23 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
24
25 typedef struct _MsgPartEvaluatorStruct {
26         MsgPartEvaluatorFunc f;
27 } MsgPartEvaluatorStruct;
28
29 int load_message(message_summary *Msg, 
30                  StrBuf *FoundCharset,
31                  StrBuf **Error)
32 {
33         wcsession *WCC = WC;
34         StrBuf *Buf;
35         StrBuf *HdrToken;
36         headereval *Hdr;
37         void *vHdr;
38         char buf[SIZ];
39         int Done = 0;
40         int state=0;
41         
42         Buf = NewStrBuf();
43         if (Msg->PartNum != NULL) {
44                 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
45         }
46         else {
47                 serv_printf("MSG4 %ld", Msg->msgnum);
48         }
49
50         StrBuf_ServGetln(Buf);
51         if (GetServerStatus(Buf, NULL) != 1) {
52                 *Error = NewStrBuf();
53                 StrBufAppendPrintf(*Error, "<strong>");
54                 StrBufAppendPrintf(*Error, _("ERROR:"));
55                 StrBufAppendPrintf(*Error, "</strong> %s<br />\n", &buf[4]);
56                 FreeStrBuf(&Buf);
57                 return 0;
58         }
59
60         /* begin everythingamundo table */
61         HdrToken = NewStrBuf();
62         while (!Done && StrBuf_ServGetln(Buf)>=0) {
63                 if ( (StrLength(Buf)==3) && 
64                     !strcmp(ChrPtr(Buf), "000")) 
65                 {
66                         Done = 1;
67                         if (state < 2) {
68                                 if (Msg->MsgBody->Data == NULL)
69                                         Msg->MsgBody->Data = NewStrBuf();
70                                 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
71                                 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
72                                 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
73                                 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br /><br />\n");
74                                 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
75                         }
76                         break;
77                 }
78                 switch (state) {
79                 case 0:/* Citadel Message Headers */
80                         if (StrLength(Buf) == 0) {
81                                 state ++;
82                                 break;
83                         }
84                         StrBufExtract_token(HdrToken, Buf, 0, '=');
85                         StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
86                         
87 #ifdef TECH_PREVIEW
88                         if (dbg_analyze_msg) lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
89 #endif
90                         /* look up one of the examine_* functions to parse the content */
91                         if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
92                             (vHdr != NULL)) {
93                                 Hdr = (headereval*)vHdr;
94                                 Hdr->evaluator(Msg, Buf, FoundCharset);
95                                 if (Hdr->Type == 1) {
96                                         state++;
97                                 }
98                         }/* TODO: 
99                         else LogError(Target, 
100                                       __FUNCTION__,  
101                                       "don't know how to handle message header[%s]\n", 
102                                       ChrPtr(HdrToken));
103                          */
104                         break;
105                 case 1:/* Message Mime Header */
106                         if (StrLength(Buf) == 0) {
107                                 state++;
108                                 if (Msg->MsgBody->ContentType == NULL)
109                                         /* end of header or no header? */
110                                         Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
111                                  /* usual end of mime header */
112                         }
113                         else
114                         {
115                                 StrBufExtract_token(HdrToken, Buf, 0, ':');
116                                 if (StrLength(HdrToken) > 0) {
117                                         StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
118 #ifdef TECH_PREVIEW
119                                         if (dbg_analyze_msg) lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
120 #endif
121                                         /* the examine*'s know how to do with mime headers too... */
122                                         if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
123                                             (vHdr != NULL)) {
124                                                 Hdr = (headereval*)vHdr;
125                                                 Hdr->evaluator(Msg, Buf, FoundCharset);
126                                         }
127                                         break;
128                                 }
129                         }
130                 case 2: /* Message Body */
131                         
132                         if (Msg->MsgBody->size_known > 0) {
133                                 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
134                                 state ++;
135                                 /*/ todo: check next line, if not 000, append following lines */
136                         }
137                         else if (1){
138                                 if (StrLength(Msg->MsgBody->Data) > 0)
139                                         StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
140                                 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
141                         }
142                         break;
143                 case 3:
144                         StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
145                         break;
146                 }
147         }
148
149         if (Msg->AllAttach == NULL)
150                 Msg->AllAttach = NewHash(1,NULL);
151         /* now we put the body mimepart we read above into the mimelist */
152         Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
153         
154         /* Generate a reply-to address */
155         if (StrLength(Msg->Rfca) > 0) {
156                 if (Msg->reply_to == NULL)
157                         Msg->reply_to = NewStrBuf();
158                 if (StrLength(Msg->from) > 0) {
159                         StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
160                 }
161                 else {
162                         FlushStrBuf(Msg->reply_to);
163                         StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
164                 }
165         }
166         else 
167         {
168                 if ((StrLength(Msg->OtherNode)>0) && 
169                     (strcasecmp(ChrPtr(Msg->OtherNode), ChrPtr(WCC->serv_info->serv_nodename))) &&
170                     (strcasecmp(ChrPtr(Msg->OtherNode), ChrPtr(WCC->serv_info->serv_humannode)) ))
171                 {
172                         if (Msg->reply_to == NULL)
173                                 Msg->reply_to = NewStrBuf();
174                         StrBufPrintf(Msg->reply_to, 
175                                      "%s @ %s",
176                                      ChrPtr(Msg->from), 
177                                      ChrPtr(Msg->OtherNode));
178                 }
179                 else {
180                         if (Msg->reply_to == NULL)
181                                 Msg->reply_to = NewStrBuf();
182                         FlushStrBuf(Msg->reply_to);
183                         StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
184                 }
185         }
186         FreeStrBuf(&Buf);
187         FreeStrBuf(&HdrToken);
188         return 1;
189 }
190
191
192
193 /*
194  * I wanna SEE that message!
195  *
196  * msgnum               Message number to display
197  * printable_view       Nonzero to display a printable view
198  * section              Optional for encapsulated message/rfc822 submessage
199  */
200 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime) 
201 {
202         StrBuf *Buf;
203         StrBuf *FoundCharset;
204         HashPos  *it;
205         void *vMime;
206         message_summary *Msg = NULL;
207         void *vHdr;
208         long len;
209         const char *Key;
210         WCTemplputParams SubTP;
211         StrBuf *Error = NULL;
212
213         Buf = NewStrBuf();
214         FoundCharset = NewStrBuf();
215         Msg = (message_summary *)malloc(sizeof(message_summary));
216         memset(Msg, 0, sizeof(message_summary));
217         Msg->msgnum = msgnum;
218         Msg->PartNum = PartNum;
219         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
220         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
221         Msg->MsgBody->msgnum = msgnum;
222
223         if (!load_message(Msg, FoundCharset, &Error)) {
224                 StrBufAppendBuf(Target, Error, 0);
225                 FreeStrBuf(&Error);
226         }
227
228         /* Extract just the content-type (omit attributes such as "charset") */
229         StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
230         StrBufTrim(Buf);
231         StrBufLowerCase(Buf);
232
233         /* Locate a renderer capable of converting this MIME part into HTML */
234         if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
235             (vHdr != NULL)) {
236                 RenderMimeFuncStruct *Render;
237                 Render = (RenderMimeFuncStruct*)vHdr;
238                 Render->f(Msg->MsgBody, NULL, FoundCharset);
239         }
240
241         if (StrLength(Msg->reply_references)> 0) {
242                 /* Trim down excessively long lists of thread references.  We eliminate the
243                  * second one in the list so that the thread root remains intact.
244                  */
245                 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
246                 int rrlen = StrLength(Msg->reply_references);
247                 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
248                         StrBufRemove_token(Msg->reply_references, 1, '|');
249                 }
250         }
251
252         /* now check if we need to translate some mimeparts, and remove the duplicate */
253         it = GetNewHashPos(Msg->AllAttach, 0);
254         while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) && 
255                (vMime != NULL)) {
256                 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
257                 evaluate_mime_part(Msg, Mime);
258         }
259         DeleteHashPos(&it);
260         memset(&SubTP, 0, sizeof(WCTemplputParams));
261         SubTP.Filter.ContextType = CTX_MAILSUM;
262         SubTP.Context = Msg;
263         *OutMime = DoTemplate(tmpl, tmpllen, Target, &SubTP);
264
265         DestroyMessageSummary(Msg);
266         FreeStrBuf(&FoundCharset);
267         FreeStrBuf(&Buf);
268         return 1;
269 }
270
271
272 void
273 HttpStatus(long CitadelStatus)
274 {
275         long httpstatus = 502;
276         
277         switch (MAJORCODE(CitadelStatus))
278         {
279         case LISTING_FOLLOWS:
280         case CIT_OK:
281                 httpstatus = 201;
282                 break;
283         case ERROR:
284                 switch (MINORCODE(CitadelStatus))
285                 {
286                 case INTERNAL_ERROR:
287                         httpstatus = 403;
288                         break;
289                         
290                 case TOO_BIG:
291                 case ILLEGAL_VALUE:
292                 case HIGHER_ACCESS_REQUIRED:
293                 case MAX_SESSIONS_EXCEEDED:
294                 case RESOURCE_BUSY:
295                 case RESOURCE_NOT_OPEN:
296                 case NOT_HERE:
297                 case INVALID_FLOOR_OPERATION:
298                 case FILE_NOT_FOUND:
299                 case ROOM_NOT_FOUND:
300                         httpstatus = 409;
301                         break;
302
303                 case MESSAGE_NOT_FOUND:
304                 case ALREADY_EXISTS:
305                         httpstatus = 403;
306                         break;
307
308                 case NO_SUCH_SYSTEM:
309                         httpstatus = 502;
310                         break;
311
312                 default:
313                 case CMD_NOT_SUPPORTED:
314                 case PASSWORD_REQUIRED:
315                 case ALREADY_LOGGED_IN:
316                 case USERNAME_REQUIRED:
317                 case NOT_LOGGED_IN:
318                 case SERVER_SHUTTING_DOWN:
319                 case NO_SUCH_USER:
320                 case ASYNC_GEXP:
321                         httpstatus = 502;
322                         break;
323                 }
324                 break;
325
326         default:
327         case BINARY_FOLLOWS:
328         case SEND_BINARY:
329         case START_CHAT_MODE:
330         case ASYNC_MSG:
331         case MORE_DATA:
332         case SEND_LISTING:
333                 httpstatus = 502; /* aeh... whut? */
334                 break;
335         }
336
337
338 }
339
340 /*
341  * Unadorned HTML output of an individual message, suitable
342  * for placing in a hidden iframe, for printing, or whatever
343  */
344 void handle_one_message(void) 
345 {
346         long CitStatus;
347         int CopyMessage = 0;
348         const StrBuf *Destination;
349         void *vLine;
350         const StrBuf *Mime;
351         long msgnum = 0L;
352         wcsession *WCC = WC;
353         const StrBuf *Tmpl;
354         StrBuf *CmdBuf = NULL;
355         const char *pMsg;
356
357
358         pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
359         if (pMsg == NULL) {
360                 HttpStatus(CitStatus);
361                 return;
362         }
363
364         msgnum = atol(pMsg + 1);
365         StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
366         gotoroom(WCC->Hdr->HR.ReqLine);
367         switch (WCC->Hdr->HR.eReqType)
368         {
369         case eGET:
370         case ePOST:
371                 Tmpl = sbstr("template");
372                 if (StrLength(Tmpl) > 0) 
373                         read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
374                 else 
375                         read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
376                 http_transmit_thing(ChrPtr(Mime), 0);
377                 break;
378         case eDELETE:
379                 CmdBuf = NewStrBuf ();
380                 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
381                         serv_printf("DELE %ld", msgnum);        
382                 }
383                 else {                  /* Otherwise move it to Trash */
384                         serv_printf("MOVE %ld|_TRASH_|0", msgnum);
385                 }
386                 StrBuf_ServGetln(CmdBuf);
387                 FlushStrBuf(WCC->ImportantMsg);
388                 StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
389                 GetServerStatus(CmdBuf, &CitStatus);
390                 HttpStatus(CitStatus);
391                 break;
392         case eCOPY:
393                 CopyMessage = 1;
394         case eMOVE:
395                 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
396                     (vLine!=NULL)) {
397                         Destination = (StrBuf*) vLine;
398                         serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
399                         StrBuf_ServGetln(CmdBuf);
400                         FlushStrBuf(WCC->ImportantMsg);
401                         StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
402                         GetServerStatus(CmdBuf, &CitStatus);
403                         HttpStatus(CitStatus);
404                 }
405                 else
406                         HttpStatus(500);
407                 break;
408         default:
409                 break;
410
411         }
412 }
413
414
415 /*
416  * Unadorned HTML output of an individual message, suitable
417  * for placing in a hidden iframe, for printing, or whatever
418  */
419 void embed_message(void) {
420         const StrBuf *Mime;
421         long msgnum = 0L;
422         wcsession *WCC = WC;
423         const StrBuf *Tmpl;
424         StrBuf *CmdBuf = NULL;
425
426         msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
427         switch (WCC->Hdr->HR.eReqType)
428         {
429         case eGET:
430         case ePOST:
431                 Tmpl = sbstr("template");
432                 if (StrLength(Tmpl) > 0) 
433                         read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
434                 else 
435                         read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
436                 http_transmit_thing(ChrPtr(Mime), 0);
437                 break;
438         case eDELETE:
439                 CmdBuf = NewStrBuf ();
440                 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
441                         serv_printf("DELE %ld", msgnum);        
442                 }
443                 else {                  /* Otherwise move it to Trash */
444                         serv_printf("MOVE %ld|_TRASH_|0", msgnum);
445                 }
446                 StrBuf_ServGetln(CmdBuf);
447                 FlushStrBuf(WCC->ImportantMsg);
448                 StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
449                 break;
450         default:
451                 break;
452
453         }
454 }
455
456
457 /*
458  * Printable view of a message
459  */
460 void print_message(void) {
461         long msgnum = 0L;
462         const StrBuf *Mime;
463
464         msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
465         output_headers(0, 0, 0, 0, 0, 0);
466
467         hprintf("Content-type: text/html\r\n"
468                 "Server: " PACKAGE_STRING "\r\n"
469                 "Connection: close\r\n");
470
471         begin_burst();
472
473         read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
474
475         wDumpContent(0);
476 }
477
478 /* 
479  * Mobile browser view of message
480  */
481 void mobile_message_view(void) 
482 {
483         long msgnum = 0L;
484         const StrBuf *Mime;
485   
486         msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
487         output_headers(1, 0, 0, 0, 0, 1);
488         begin_burst();
489         do_template("msgcontrols", NULL);
490         read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
491         wDumpContent(0);
492 }
493
494 /*
495  * Display a message's headers
496  */
497 void display_headers(void) {
498         long msgnum = 0L;
499         char buf[1024];
500
501         msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
502         output_headers(0, 0, 0, 0, 0, 0);
503
504         hprintf("Content-type: text/plain\r\n"
505                 "Server: %s\r\n"
506                 "Connection: close\r\n",
507                 PACKAGE_STRING);
508         begin_burst();
509
510         serv_printf("MSG2 %ld|3", msgnum);
511         serv_getln(buf, sizeof buf);
512         if (buf[0] == '1') {
513                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
514                         wc_printf("%s\n", buf);
515                 }
516         }
517
518         wDumpContent(0);
519 }
520
521
522 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum) 
523 {
524         void                 *vEval;
525         MsgPartEvaluatorStruct  *Eval;
526         message_summary      *Msg;
527         StrBuf *Buf;
528         const char *buf;
529         const char *ebuf;
530         int nBuf;
531         long len;
532         
533         Buf = NewStrBuf();
534
535         serv_printf("MSG0 %ld|1", MsgNum);      /* ask for headers only */
536         
537         StrBuf_ServGetln(Buf);
538         if (GetServerStatus(Buf, NULL) == 1) {
539                 FreeStrBuf(&Buf);
540                 return NULL;
541         }
542
543         Msg = (message_summary*)malloc(sizeof(message_summary));
544         memset(Msg, 0, sizeof(message_summary));
545         while (len = StrBuf_ServGetln(Buf),
546                ((len != 3)  ||
547                 strcmp(ChrPtr(Buf), "000")== 0)){
548                 buf = ChrPtr(Buf);
549                 ebuf = strchr(ChrPtr(Buf), '=');
550                 nBuf = ebuf - buf;
551                 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
552                         Eval = (MsgPartEvaluatorStruct*) vEval;
553                         StrBufCutLeft(Buf, nBuf + 1);
554                         Eval->f(Msg, Buf);
555                 }
556                 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
557         }
558         return Msg;
559 }
560
561
562
563
564
565 /*
566  * load message pointers from the server for a "read messages" operation
567  *
568  * servcmd:             the citadel command to send to the citserver
569  */
570 int load_msg_ptrs(const char *servcmd, 
571                   SharedMessageStatus *Stat, 
572                   load_msg_ptrs_detailheaders LH)
573 {
574         wcsession *WCC = WC;
575         message_summary *Msg;
576         StrBuf *Buf, *Buf2;
577         long len;
578         int n;
579         int skipit;
580         const char *Ptr = NULL;
581
582         Stat->lowest_found = LONG_MAX;
583         Stat->highest_found = LONG_MIN;
584
585         if (WCC->summ != NULL) {
586                 DeleteHash(&WCC->summ);
587         }
588         WCC->summ = NewHash(1, Flathash);
589         
590         Buf = NewStrBuf();
591         serv_puts(servcmd);
592         StrBuf_ServGetln(Buf);
593         if (GetServerStatus(Buf, NULL) != 1) {
594                 FreeStrBuf(&Buf);
595                 return (Stat->nummsgs);
596         }
597         Buf2 = NewStrBuf();
598         while (len = StrBuf_ServGetln(Buf), ((len != 3) || strcmp(ChrPtr(Buf), "000")!= 0))
599         {
600                 if (Stat->nummsgs < Stat->maxload) {
601                         skipit = 0;
602                         Ptr = NULL;
603                         Msg = (message_summary*)malloc(sizeof(message_summary));
604                         memset(Msg, 0, sizeof(message_summary));
605
606                         Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
607                         Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
608
609                         if (Stat->nummsgs == 0) {
610                                 if (Msg->msgnum < Stat->lowest_found) {
611                                         Stat->lowest_found = Msg->msgnum;
612                                 }
613                                 if (Msg->msgnum > Stat->highest_found) {
614                                         Stat->highest_found = Msg->msgnum;
615                                 }
616                         }
617
618                         if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
619                                 free(Msg);
620                                 continue;
621                         }
622
623                         /* 
624                          * as citserver probably gives us messages in forward date sorting
625                          * nummsgs should be the same order as the message date.
626                          */
627                         if (Msg->date == 0) {
628                                 Msg->date = Stat->nummsgs;
629                                 if (StrLength(Buf) < 32) 
630                                         skipit = 1;
631                         }
632                         if ((!skipit) && (LH != NULL)) {
633                                 if (!LH(Buf, &Ptr, Msg, Buf2)){
634                                         free(Msg);
635                                         continue;
636                                 }                                       
637                         }
638                         n = Msg->msgnum;
639                         Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
640                 }
641                 Stat->nummsgs++;
642         }
643         FreeStrBuf(&Buf2);
644         FreeStrBuf(&Buf);
645         return (Stat->nummsgs);
646 }
647
648
649
650 /**
651  * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
652  * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
653  * \param MatchMSet MSet we want to flag
654  * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
655  */
656 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
657 {
658         const char *HashKey;
659         long HKLen;
660         HashPos *at;
661         void *vMsg;
662         message_summary *Msg;
663
664         at = GetNewHashPos(ScanMe, 0);
665         while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
666                 /* Are you a new message, or an old message? */
667                 Msg = (message_summary*) vMsg;
668                 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
669                         Msg->Flags = Msg->Flags | FlagToSet;
670                 }
671                 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
672                         Msg->Flags = Msg->Flags | FlagToSet;
673                 }
674         }
675         DeleteHashPos(&at);
676 }
677
678
679 void load_seen_flags(void)
680 {
681         StrBuf *OldMsg;
682         wcsession *WCC = WC;
683         MSet *MatchMSet;
684
685         OldMsg = NewStrBuf();
686         serv_puts("GTSN");
687         StrBuf_ServGetln(OldMsg);
688         if (GetServerStatus(OldMsg, NULL) == 2) {
689                 StrBufCutLeft(OldMsg, 4);
690         }
691         else {
692                 FreeStrBuf(&OldMsg);
693                 return;
694         }
695
696         if (ParseMSet(&MatchMSet, OldMsg))
697         {
698                 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
699         }
700         DeleteMSet(&MatchMSet);
701         FreeStrBuf(&OldMsg);
702 }
703
704 extern readloop_struct rlid[];
705
706 typedef struct _RoomRenderer{
707         int RoomType;
708
709         GetParamsGetServerCall_func GetParamsGetServerCall;
710         PrintViewHeader_func PrintViewHeader;
711         LoadMsgFromServer_func LoadMsgFromServer;
712         RenderView_or_Tail_func RenderView_or_Tail;
713         View_Cleanup_func ViewCleanup;
714         load_msg_ptrs_detailheaders LHParse;
715 } RoomRenderer;
716
717
718 /*
719  * command loop for reading messages
720  *
721  * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
722  */
723 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
724 {
725         RoomRenderer *ViewMsg;
726         void *vViewMsg;
727         void *vMsg;
728         message_summary *Msg;
729         char cmd[256] = "";
730         int i, r;
731         wcsession *WCC = WC;
732         HashPos *at;
733         const char *HashKey;
734         long HKLen;
735         WCTemplputParams SubTP;
736         SharedMessageStatus Stat;
737         void *ViewSpecific;
738
739         if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
740                 WCC->CurRoom.view = VIEW_MAILBOX;
741         }
742
743         if (havebstr("is_ajax") && (1 == (ibstr("is_ajax")))) {
744                 WCC->is_ajax = 1;
745         }
746
747         if ((oper == do_search) && (WCC->CurRoom.view == VIEW_WIKI)) {
748                 display_wiki_pagelist();
749                 return;
750         }
751
752         memset(&Stat, 0, sizeof(SharedMessageStatus));
753         Stat.maxload = 10000;
754         Stat.lowest_found = (-1);
755         Stat.highest_found = (-1);
756         if (ForceRenderer == eUseDefault)
757                 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
758         else 
759                 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
760         if (vViewMsg == NULL) {
761                 WCC->CurRoom.view = VIEW_BBS;
762                 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
763         }
764         if (vViewMsg == NULL) {
765                 return;                 // TODO: print message
766         }
767
768         ViewMsg = (RoomRenderer*) vViewMsg;
769         if (!WCC->is_ajax) {
770                 output_headers(1, 1, 1, 0, 0, 0);
771         } else if (WCC->CurRoom.view == VIEW_MAILBOX) {
772                 jsonMessageListHdr();
773         }
774
775         if (ViewMsg->GetParamsGetServerCall != NULL) {
776                 r = ViewMsg->GetParamsGetServerCall(
777                        &Stat,
778                        &ViewSpecific,
779                        oper,
780                        cmd, sizeof(cmd)
781                 );
782         } else {
783                 r = 0;
784         }
785         switch(r)
786         {
787         case 400:
788         case 404:
789
790                 return;
791         case 300: /* the callback hook should do the work for us here, since he knows what to do. */
792                 return;
793         case 200:
794         default:
795                 break;
796         }
797         if (!IsEmptyStr(cmd))
798                 Stat.nummsgs = load_msg_ptrs(cmd, &Stat, ViewMsg->LHParse);
799
800         if (Stat.sortit) {
801                 CompareFunc SortIt;
802                 memset(&SubTP, 0, sizeof(WCTemplputParams));
803                 SubTP.Filter.ContextType = CTX_MAILSUM;
804                 SubTP.Context = NULL;
805                 SortIt =  RetrieveSort(&SubTP, NULL, 0,
806                                        HKEY("date"), Stat.defaultsortorder);
807                 if (SortIt != NULL)
808                         SortByPayload(WCC->summ, SortIt);
809         }
810         if (Stat.startmsg < 0) {
811                 Stat.startmsg =  0;
812         }
813
814         if (Stat.load_seen) load_seen_flags();
815         
816         /*
817          * Print any inforation above the message list...
818          */
819         if (ViewMsg->PrintViewHeader != NULL)
820                 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
821
822         WCC->startmsg =  Stat.startmsg;
823         WCC->maxmsgs = Stat.maxmsgs;
824         WCC->num_displayed = 0;
825
826         /* Put some helpful data in vars for mailsummary_json */
827         {
828                 StrBuf *Foo;
829                 
830                 Foo = NewStrBuf ();
831                 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
832                 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
833                 StrBufPrintf(Foo, "%ld", Stat.startmsg);
834                 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
835         }
836
837         /*
838          * iterate over each message. if we need to load an attachment, do it here. 
839          */
840
841         if ((ViewMsg->LoadMsgFromServer != NULL) && 
842             (!IsEmptyStr(cmd)))
843         {
844                 at = GetNewHashPos(WCC->summ, 0);
845                 Stat.num_displayed = i = 0;
846                 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
847                         Msg = (message_summary*) vMsg;          
848                         if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
849                                 ViewMsg->LoadMsgFromServer(&Stat, 
850                                                            &ViewSpecific, 
851                                                            Msg, 
852                                                            (Msg->Flags & MSGFLAG_READ) != 0, 
853                                                            i);
854                         } 
855                         i++;
856                 }
857                 DeleteHashPos(&at);
858         }
859
860         /*
861          * Done iterating the message list. now tasks we want to do after.
862          */
863         if (ViewMsg->RenderView_or_Tail != NULL)
864                 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
865
866         if (ViewMsg->ViewCleanup != NULL)
867                 ViewMsg->ViewCleanup(&ViewSpecific);
868
869         WCC->startmsg = 0;
870         WCC->maxmsgs = 0;
871         if (WCC->summ != NULL) {
872                 DeleteHash(&WCC->summ);
873         }
874 }
875
876
877 /*
878  * Back end for post_message()
879  * ... this is where the actual message gets transmitted to the server.
880  */
881 void post_mime_to_server(void) {
882         wcsession *WCC = WC;
883         char top_boundary[SIZ];
884         char alt_boundary[SIZ];
885         int is_multipart = 0;
886         static int seq = 0;
887         wc_mime_attachment *att;
888         char *encoded;
889         size_t encoded_length;
890         size_t encoded_strlen;
891         char *txtmail = NULL;
892         int include_text_alt = 0;       /* Set to nonzero to include multipart/alternative text/plain */
893
894         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
895                 ChrPtr(WCC->serv_info->serv_fqdn),
896                 getpid(),
897                 ++seq
898         );
899         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
900                 ChrPtr(WCC->serv_info->serv_fqdn),
901                 getpid(),
902                 ++seq
903         );
904
905         /* RFC2045 requires this, and some clients look for it... */
906         serv_puts("MIME-Version: 1.0");
907         serv_puts("X-Mailer: " PACKAGE_STRING);
908
909         /* If there are attachments, we have to do multipart/mixed */
910         if (GetCount(WCC->attachments) > 0) {
911                 is_multipart = 1;
912         }
913
914         /* Only do multipart/alternative for mailboxes.  BBS and Wiki rooms don't need it. */
915         if (WC->CurRoom.view == VIEW_MAILBOX) {
916                 include_text_alt = 1;
917         }
918
919         if (is_multipart) {
920                 /* Remember, serv_printf() appends an extra newline */
921                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
922                 serv_printf("This is a multipart message in MIME format.\n");
923                 serv_printf("--%s", top_boundary);
924         }
925
926         /* Remember, serv_printf() appends an extra newline */
927         if (include_text_alt) {
928                 serv_printf("Content-type: multipart/alternative; "
929                         "boundary=\"%s\"\n", alt_boundary);
930                 serv_printf("This is a multipart message in MIME format.\n");
931                 serv_printf("--%s", alt_boundary);
932
933                 serv_puts("Content-type: text/plain; charset=utf-8");
934                 serv_puts("Content-Transfer-Encoding: quoted-printable");
935                 serv_puts("");
936                 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
937                 text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
938                 free(txtmail);
939
940                 serv_printf("--%s", alt_boundary);
941         }
942
943         serv_puts("Content-type: text/html; charset=utf-8");
944         serv_puts("Content-Transfer-Encoding: quoted-printable");
945         serv_puts("");
946         serv_puts("<html><body>\r\n");
947         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
948         serv_puts("</body></html>\r\n");
949
950         if (include_text_alt) {
951                 serv_printf("--%s--", alt_boundary);
952         }
953         
954         if (is_multipart) {
955                 long len;
956                 const char *Key; 
957                 void *vAtt;
958                 HashPos  *it;
959
960                 /* Add in the attachments */
961                 it = GetNewHashPos(WCC->attachments, 0);
962                 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
963                         att = (wc_mime_attachment *)vAtt;
964                         if (att->length == 0)
965                                 continue;
966                         encoded_length = ((att->length * 150) / 100);
967                         encoded = malloc(encoded_length);
968                         if (encoded == NULL) break;
969                         encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
970
971                         serv_printf("--%s", top_boundary);
972                         serv_printf("Content-type: %s", ChrPtr(att->ContentType));
973                         serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
974                         serv_puts("Content-transfer-encoding: base64");
975                         serv_puts("");
976                         serv_write(encoded, encoded_strlen);
977                         serv_puts("");
978                         serv_puts("");
979                         free(encoded);
980                 }
981                 serv_printf("--%s--", top_boundary);
982                 DeleteHashPos(&it);
983         }
984
985         serv_puts("000");
986 }
987
988
989 /*
990  * Post message (or don't post message)
991  *
992  * Note regarding the "dont_post" variable:
993  * A random value (actually, it's just a timestamp) is inserted as a hidden
994  * field called "postseq" when the display_enter page is generated.  This
995  * value is checked when posting, using the static variable dont_post.  If a
996  * user attempts to post twice using the same dont_post value, the message is
997  * discarded.  This prevents the accidental double-saving of the same message
998  * if the user happens to click the browser "back" button.
999  */
1000 void post_message(void)
1001 {
1002         StrBuf *UserName;
1003         StrBuf *EmailAddress;
1004         StrBuf *EncBuf;
1005         char buf[1024];
1006         StrBuf *encoded_subject = NULL;
1007         static long dont_post = (-1L);
1008         int is_anonymous = 0;
1009         const StrBuf *display_name = NULL;
1010         wcsession *WCC = WC;
1011         StrBuf *Buf;
1012         
1013         if (havebstr("force_room")) {
1014                 gotoroom(sbstr("force_room"));
1015         }
1016
1017         if (havebstr("display_name")) {
1018                 display_name = sbstr("display_name");
1019                 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1020                         display_name = NULL;
1021                         is_anonymous = 1;
1022                 }
1023         }
1024
1025         if (!strcasecmp(bstr("submit_action"), "cancel")) {
1026                 sprintf(WCC->ImportantMessage, 
1027                         _("Cancelled.  Message was not posted."));
1028         } else if (lbstr("postseq") == dont_post) {
1029                 sprintf(WCC->ImportantMessage, 
1030                         _("Automatically cancelled because you have already "
1031                           "saved this message."));
1032         } else {
1033                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1034                 StrBuf *Recp = NULL; 
1035                 StrBuf *Cc = NULL;
1036                 StrBuf *Bcc = NULL;
1037                 const StrBuf *Wikipage = NULL;
1038                 const StrBuf *my_email_addr = NULL;
1039                 StrBuf *CmdBuf = NULL;
1040                 StrBuf *references = NULL;
1041                 int save_to_drafts;
1042                 long HeaderLen;
1043
1044                 save_to_drafts = !strcasecmp(bstr("submit_action"), "drafts");
1045                 Buf = NewStrBuf();
1046
1047                 if (save_to_drafts) {
1048                         /* temporarily change to the drafts room */
1049                         serv_puts("GOTO _DRAFTS_");
1050                         StrBuf_ServGetln(Buf);
1051                         if (GetServerStatus(Buf, NULL) != 2) {
1052                                 /* You probably don't even have a dumb Drafts folder */
1053                                 StrBufCutLeft(Buf, 4);
1054                                 lprintf(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf));
1055                                 StrBufAppendBufPlain(WCC->ImportantMsg, _("Saved to Drafts failed: "), -1, 0);
1056                                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 0);
1057                                 display_enter();
1058                                 FreeStrBuf(&Buf);
1059                                 return;
1060                         }
1061                 }
1062
1063                 if (havebstr("references"))
1064                 {
1065                         const StrBuf *ref = sbstr("references");
1066                         references = NewStrBufDup(ref);
1067                         if (*ChrPtr(references) == '|') {       /* remove leading '|' if present */
1068                                 StrBufCutLeft(references, 1);
1069                         }
1070                         StrBufReplaceChars(references, '|', '!');
1071                 }
1072                 if (havebstr("subject")) {
1073                         const StrBuf *Subj;
1074                         /*
1075                          * make enough room for the encoded string; 
1076                          * plus the QP header 
1077                          */
1078                         Subj = sbstr("subject");
1079                         
1080                         StrBufRFC2047encode(&encoded_subject, Subj);
1081                 }
1082                 UserName = NewStrBuf();
1083                 EmailAddress = NewStrBuf();
1084                 EncBuf = NewStrBuf();
1085
1086                 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1087                 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1088                 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1089
1090                 FreeStrBuf(&UserName);
1091                 FreeStrBuf(&EmailAddress);
1092                 FreeStrBuf(&EncBuf);
1093
1094                 Wikipage = sbstr("page");
1095                 my_email_addr = sbstr("my_email_addr");
1096                 
1097                 HeaderLen = StrLength(Recp) + 
1098                         StrLength(encoded_subject) +
1099                         StrLength(Cc) +
1100                         StrLength(Bcc) + 
1101                         StrLength(Wikipage) +
1102                         StrLength(my_email_addr) + 
1103                         StrLength(references);
1104                 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1105                 StrBufPrintf(CmdBuf, 
1106                              CMD,
1107                              save_to_drafts?"":ChrPtr(Recp),
1108                              is_anonymous,
1109                              ChrPtr(encoded_subject),
1110                              ChrPtr(display_name),
1111                              save_to_drafts?"":ChrPtr(Cc),
1112                              save_to_drafts?"":ChrPtr(Bcc),
1113                              ChrPtr(Wikipage),
1114                              ChrPtr(my_email_addr),
1115                              ChrPtr(references));
1116                 FreeStrBuf(&references);
1117                 FreeStrBuf(&encoded_subject);
1118
1119                 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) && 
1120                     (GetCount(WCC->attachments) == 0)){
1121                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Refusing to post empty message.\n"), -1, 0);
1122                         FreeStrBuf(&CmdBuf);
1123                                 
1124                 }
1125                 else 
1126                 {
1127                         lprintf(9, "%s\n", ChrPtr(CmdBuf));
1128                         serv_puts(ChrPtr(CmdBuf));
1129                         FreeStrBuf(&CmdBuf);
1130
1131                         StrBuf_ServGetln(Buf);
1132                         if (GetServerStatus(Buf, NULL) == 4) {
1133                                 if (save_to_drafts) {
1134                                         if (  (havebstr("recp"))
1135                                               || (havebstr("cc"  ))
1136                                               || (havebstr("bcc" )) ) {
1137                                                 /* save recipient headers or room to post to */
1138                                                 serv_printf("To: %s", ChrPtr(Recp));
1139                                                 serv_printf("Cc: %s", ChrPtr(Cc));
1140                                                 serv_printf("Bcc: %s", ChrPtr(Bcc));
1141                                         } else {
1142                                                 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1143                                         }
1144                                 }
1145                                 post_mime_to_server();
1146                                 if (save_to_drafts) {
1147                                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been saved to Drafts.\n"), -1, 0);
1148                                         gotoroom(WCC->CurRoom.name);
1149                                         display_enter();
1150                                         FreeStrBuf(&Buf);
1151                                         return;
1152                                 } else if (  (havebstr("recp"))
1153                                              || (havebstr("cc"  ))
1154                                              || (havebstr("bcc" ))
1155                                         ) {
1156                                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been sent.\n"), -1, 0);
1157                                 }
1158                                 else {
1159                                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been posted.\n"), -1, 0);
1160                                 }
1161                                 dont_post = lbstr("postseq");
1162                         } else {
1163                                 StrBufCutLeft(Buf, 4);
1164
1165                                 lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, ChrPtr(Buf));
1166                                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 0);
1167                                 if (save_to_drafts) gotoroom(WCC->CurRoom.name);
1168                                 display_enter();
1169                                 FreeStrBuf(&Buf);
1170                                 FreeStrBuf(&Cc);
1171                                 FreeStrBuf(&Bcc);
1172                                 return;
1173                         }
1174                 }
1175                 FreeStrBuf(&Recp);
1176                 FreeStrBuf(&Buf);
1177                 FreeStrBuf(&Cc);
1178                 FreeStrBuf(&Bcc);
1179         }
1180
1181         DeleteHash(&WCC->attachments);
1182
1183         /*
1184          *  We may have been supplied with instructions regarding the location
1185          *  to which we must return after posting.  If found, go there.
1186          */
1187         if (havebstr("return_to")) {
1188                 http_redirect(bstr("return_to"));
1189         }
1190         /*
1191          *  If we were editing a page in a wiki room, go to that page now.
1192          */
1193         else if (havebstr("page")) {
1194                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1195                 http_redirect(buf);
1196         }
1197         /*
1198          *  Otherwise, just go to the "read messages" loop.
1199          */
1200         else {
1201                 readloop(readnew, eUseDefault);
1202         }
1203 }
1204
1205
1206 /*
1207  * Client is uploading an attachment
1208  */
1209 void upload_attachment(void) {
1210         wcsession *WCC = WC;
1211         const char *pch;
1212         int n;
1213         const char *newn;
1214         long newnlen;
1215         void *v;
1216         wc_mime_attachment *att;
1217
1218         lprintf(9, "upload_attachment()\n");
1219         wc_printf("upload_attachment()<br>\n");
1220
1221         if (WCC->upload_length <= 0) {
1222                 lprintf(9, "ERROR no attachment was uploaded\n");
1223                 wc_printf("ERROR no attachment was uploaded<br>\n");
1224                 return;
1225         }
1226
1227         lprintf(9, "Client is uploading %d bytes\n", WCC->upload_length);
1228         wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1229         att = malloc(sizeof(wc_mime_attachment));
1230         memset(att, 0, sizeof(wc_mime_attachment ));
1231         att->length = WCC->upload_length;
1232         att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1233         att->FileName = NewStrBufDup(WCC->upload_filename);
1234         
1235         if (WCC->attachments == NULL) {
1236                 WCC->attachments = NewHash(1, Flathash);
1237         }
1238
1239         /* And add it to the list. */
1240         n = 0;
1241         if ((GetCount(WCC->attachments) > 0) && 
1242             GetHashAt(WCC->attachments, 
1243                       GetCount(WCC->attachments) -1, 
1244                       &newnlen, &newn, &v))
1245             n = *((int*) newn) + 1;
1246         Put(WCC->attachments, IKEY(n), att, DestroyMime);
1247
1248         /*
1249          * Mozilla sends a simple filename, which is what we want,
1250          * but Satan's Browser sends an entire pathname.  Reduce
1251          * the path to just a filename if we need to.
1252          */
1253         pch = strrchr(ChrPtr(att->FileName), '/');
1254         if (pch != NULL) {
1255                 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1256         }
1257         pch = strrchr(ChrPtr(att->FileName), '\\');
1258         if (pch != NULL) {
1259                 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1260         }
1261
1262         /*
1263          * Transfer control of this memory from the upload struct
1264          * to the attachment struct.
1265          */
1266         att->Data = WCC->upload;
1267         WCC->upload = NULL;
1268         WCC->upload_length = 0;
1269 }
1270
1271
1272 /*
1273  * Remove an attachment from the message currently being composed.
1274  *
1275  * Currently we identify the attachment to be removed by its filename.
1276  * There is probably a better way to do this.
1277  */
1278 void remove_attachment(void) {
1279         wcsession *WCC = WC;
1280         wc_mime_attachment *att;
1281         void *vAtt;
1282         StrBuf *WhichAttachment;
1283         HashPos *at;
1284         long len;
1285         const char *key;
1286
1287         WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1288         StrBufUnescape(WhichAttachment, 0);
1289         at = GetNewHashPos(WCC->attachments, 0);
1290         do {
1291                 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1292         
1293                 att = (wc_mime_attachment*) vAtt;
1294                 if ((att != NULL) && 
1295                     (strcmp(ChrPtr(WhichAttachment), 
1296                             ChrPtr(att->FileName)   ) == 0))
1297                 {
1298                         DeleteEntryFromHash(WCC->attachments, at);
1299                         break;
1300                 }
1301         }
1302         while (NextHashPos(WCC->attachments, at));
1303         FreeStrBuf(&WhichAttachment);
1304         wc_printf("remove_attachment() completed\n");
1305 }
1306
1307
1308 /*
1309  * display the message entry screen
1310  */
1311 void display_enter(void)
1312 {
1313         char buf[SIZ];
1314         long now;
1315         const StrBuf *display_name = NULL;
1316         int recipient_required = 0;
1317         int subject_required = 0;
1318         int recipient_bad = 0;
1319         int is_anonymous = 0;
1320         wcsession *WCC = WC;
1321
1322         now = time(NULL);
1323
1324         if (havebstr("force_room")) {
1325                 gotoroom(sbstr("force_room"));
1326         }
1327
1328         display_name = sbstr("display_name");
1329         if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1330                 display_name = NULL;
1331                 is_anonymous = 1;
1332         }
1333
1334         /* First test to see whether this is a room that requires recipients to be entered */
1335         serv_puts("ENT0 0");
1336         serv_getln(buf, sizeof buf);
1337
1338         if (!strncmp(buf, "570", 3)) {          /* 570 means that we need a recipient here */
1339                 recipient_required = 1;
1340         }
1341         else if (buf[0] != '2') {               /* Any other error means that we cannot continue */
1342                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1343                 readloop(readnew, eUseDefault);
1344                 return;
1345         }
1346
1347         /* Is the server strongly recommending that the user enter a message subject? */
1348         if ((buf[3] != '\0') && (buf[4] != '\0')) {
1349                 subject_required = extract_int(&buf[4], 1);
1350         }
1351
1352         /*
1353          * Are we perhaps in an address book view?  If so, then an "enter
1354          * message" command really means "add new entry."
1355          */
1356         if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1357                 do_edit_vcard(-1, "", NULL, NULL, "",  ChrPtr(WCC->CurRoom.name));
1358                 return;
1359         }
1360
1361         /*
1362          * Are we perhaps in a calendar room?  If so, then an "enter
1363          * message" command really means "add new calendar item."
1364          */
1365         if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1366                 display_edit_event();
1367                 return;
1368         }
1369
1370         /*
1371          * Are we perhaps in a tasks view?  If so, then an "enter
1372          * message" command really means "add new task."
1373          */
1374         if (WCC->CurRoom.defview == VIEW_TASKS) {
1375                 display_edit_task();
1376                 return;
1377         }
1378
1379         /*
1380          * Otherwise proceed normally.
1381          * Do a custom room banner with no navbar...
1382          */
1383
1384         if (recipient_required) {
1385                 const StrBuf *Recp = NULL; 
1386                 const StrBuf *Cc = NULL;
1387                 const StrBuf *Bcc = NULL;
1388                 const StrBuf *Wikipage = NULL;
1389                 StrBuf *CmdBuf = NULL;
1390                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1391                 
1392                 Recp = sbstr("recp");
1393                 Cc = sbstr("cc");
1394                 Bcc = sbstr("bcc");
1395                 Wikipage = sbstr("page");
1396                 
1397                 CmdBuf = NewStrBufPlain(NULL, 
1398                                         sizeof (CMD) + 
1399                                         StrLength(Recp) + 
1400                                         StrLength(display_name) +
1401                                         StrLength(Cc) +
1402                                         StrLength(Bcc) + 
1403                                         StrLength(Wikipage));
1404
1405                 StrBufPrintf(CmdBuf, 
1406                              CMD,
1407                              ChrPtr(Recp), 
1408                              is_anonymous,
1409                              ChrPtr(display_name),
1410                              ChrPtr(Cc), 
1411                              ChrPtr(Bcc), 
1412                              ChrPtr(Wikipage));
1413                 serv_puts(ChrPtr(CmdBuf));
1414                 serv_getln(buf, sizeof buf);
1415                 FreeStrBuf(&CmdBuf);
1416
1417                 if (!strncmp(buf, "570", 3)) {  /* 570 means we have an invalid recipient listed */
1418                         if (havebstr("recp") && 
1419                             havebstr("cc"  ) && 
1420                             havebstr("bcc" )) {
1421                                 recipient_bad = 1;
1422                         }
1423                 }
1424                 else if (buf[0] != '2') {       /* Any other error means that we cannot continue */
1425                         wc_printf("<em>%s</em><br />\n", &buf[4]);      /* TODO -> important message */
1426                         return;
1427                 }
1428         }
1429         if (recipient_required)
1430                 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1431         if (recipient_required || subject_required)
1432                 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1433
1434         begin_burst();
1435         output_headers(1, 0, 0, 0, 1, 0);
1436         DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1437         end_burst();
1438
1439         return;
1440 }
1441
1442 /*
1443  * delete a message
1444  */
1445 void delete_msg(void)
1446 {
1447         long msgid;
1448         char buf[SIZ];
1449
1450         msgid = lbstr("msgid");
1451
1452         if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) {  /* Delete from Trash is a real delete */
1453                 serv_printf("DELE %ld", msgid); 
1454         }
1455         else {                  /* Otherwise move it to Trash */
1456                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1457         }
1458
1459         serv_getln(buf, sizeof buf);
1460         sprintf(WC->ImportantMessage, "%s", &buf[4]);
1461         readloop(readnew, eUseDefault);
1462 }
1463
1464
1465 /*
1466  * move a message to another room
1467  */
1468 void move_msg(void)
1469 {
1470         long msgid;
1471         char buf[SIZ];
1472
1473         msgid = lbstr("msgid");
1474
1475         if (havebstr("move_button")) {
1476                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1477                 serv_puts(buf);
1478                 serv_getln(buf, sizeof buf);
1479                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1480         } else {
1481                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1482         }
1483
1484         readloop(readnew, eUseDefault);
1485 }
1486
1487
1488 /*
1489  * Confirm move of a message
1490  */
1491 void confirm_move_msg(void)
1492 {
1493         long msgid;
1494         char buf[SIZ];
1495         char targ[SIZ];
1496
1497         msgid = lbstr("msgid");
1498
1499
1500         output_headers(1, 1, 2, 0, 0, 0);
1501         wc_printf("<div id=\"banner\">\n");
1502         wc_printf("<h1>");
1503         wc_printf(_("Confirm move of message"));
1504         wc_printf("</h1>");
1505         wc_printf("</div>\n");
1506
1507         wc_printf("<div id=\"content\" class=\"service\">\n");
1508
1509         wc_printf("<CENTER>");
1510
1511         wc_printf(_("Move this message to:"));
1512         wc_printf("<br />\n");
1513
1514         wc_printf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1515         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1516         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1517
1518         wc_printf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1519         serv_puts("LKRA");
1520         serv_getln(buf, sizeof buf);
1521         if (buf[0] == '1') {
1522                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1523                         extract_token(targ, buf, 0, '|', sizeof targ);
1524                         wc_printf("<OPTION>");
1525                         escputs(targ);
1526                         wc_printf("\n");
1527                 }
1528         }
1529         wc_printf("</SELECT>\n");
1530         wc_printf("<br />\n");
1531
1532         wc_printf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1533         wc_printf("&nbsp;");
1534         wc_printf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1535         wc_printf("</form></CENTER>\n");
1536
1537         wc_printf("</CENTER>\n");
1538         wDumpContent(1);
1539 }
1540
1541
1542 /*
1543  * Generic function to output an arbitrary MIME attachment from
1544  * message being composed
1545  *
1546  * partnum              The MIME part to be output
1547  * filename             Fake filename to give
1548  * force_download       Nonzero to force set the Content-Type: header to "application/octet-stream"
1549  */
1550 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1551 {
1552         void *vPart;
1553         StrBuf *content_type;
1554         wc_mime_attachment *part;
1555         int i;
1556
1557         i = StrToi(partnum);
1558         if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1559             (vPart != NULL)) {
1560                 part = (wc_mime_attachment*) vPart;
1561                 if (force_download) {
1562                         content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1563                 }
1564                 else {
1565                         content_type = NewStrBufDup(part->ContentType);
1566                 }
1567                 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1568                 http_transmit_thing(ChrPtr(content_type), 0);
1569         } else {
1570                 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1571                 output_headers(0, 0, 0, 0, 0, 0);
1572                 hprintf("Content-Type: text/plain\r\n");
1573                 begin_burst();
1574                 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"), 
1575                         ChrPtr(partnum), ChrPtr(filename));
1576                 end_burst();
1577         }
1578         FreeStrBuf(&content_type);
1579 }
1580
1581
1582 /*
1583  * Generic function to output an arbitrary MIME part from an arbitrary
1584  * message number on the server.
1585  *
1586  * msgnum               Number of the item on the citadel server
1587  * partnum              The MIME part to be output
1588  * force_download       Nonzero to force set the Content-Type: header to "application/octet-stream"
1589  */
1590 void mimepart(int force_download)
1591 {
1592         long msgnum;
1593         StrBuf *att;
1594         wcsession *WCC = WC;
1595         StrBuf *Buf;
1596         off_t bytes;
1597         StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1598         const char *CT;
1599
1600         att = Buf = NewStrBuf();
1601         msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1602         StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1603
1604         serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1605         StrBuf_ServGetln(Buf);
1606         if (GetServerStatus(Buf, NULL) == 2) {
1607                 StrBufCutLeft(Buf, 4);
1608                 bytes = StrBufExtract_long(Buf, 0, '|');
1609                 if (!force_download) {
1610                         StrBufExtract_token(ContentType, Buf, 3, '|');
1611                 }
1612
1613                 serv_read_binary(WCC->WBuf, bytes, Buf);
1614                 serv_puts("CLOS");
1615                 StrBuf_ServGetln(Buf);
1616                 CT = ChrPtr(ContentType);
1617
1618                 if (!force_download) {
1619                         if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1620                                 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1621                                 CT = GuessMimeByFilename(SKEY(Buf));
1622                         }
1623                         if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1624                                 CT = GuessMimeType(SKEY(WCC->WBuf));
1625                         }
1626                 }
1627                 http_transmit_thing(CT, 0);
1628         } else {
1629                 StrBufCutLeft(Buf, 4);
1630                 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1631                 output_headers(0, 0, 0, 0, 0, 0);
1632                 hprintf("Content-Type: text/plain\r\n");
1633                 begin_burst();
1634                 wc_printf(_("An error occurred while retrieving this part: %s\n"), 
1635                         ChrPtr(Buf));
1636                 end_burst();
1637         }
1638         FreeStrBuf(&ContentType);
1639         FreeStrBuf(&Buf);
1640 }
1641
1642
1643 /*
1644  * Read any MIME part of a message, from the server, into memory.
1645  */
1646 StrBuf *load_mimepart(long msgnum, char *partnum)
1647 {
1648         off_t bytes;
1649         StrBuf *Buf;
1650         
1651         Buf = NewStrBuf();
1652         serv_printf("DLAT %ld|%s", msgnum, partnum);
1653         StrBuf_ServGetln(Buf);
1654         if (GetServerStatus(Buf, NULL) == 6) {
1655                 StrBufCutLeft(Buf, 4);
1656                 bytes = StrBufExtract_long(Buf, 0, '|');
1657                 FreeStrBuf(&Buf);
1658                 Buf = NewStrBuf();
1659                 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1660                 return(Buf);
1661         }
1662         else {
1663                 FreeStrBuf(&Buf);
1664                 return(NULL);
1665         }
1666 }
1667
1668 /*
1669  * Read any MIME part of a message, from the server, into memory.
1670  */
1671 void MimeLoadData(wc_mime_attachment *Mime)
1672 {
1673         StrBuf *Buf;
1674         const char *Ptr;
1675         off_t bytes;
1676         /* TODO: is there a chance the content type is different from the one we know? */
1677
1678         serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1679         Buf = NewStrBuf();
1680         StrBuf_ServGetln(Buf);
1681         if (GetServerStatus(Buf, NULL) == 6) {
1682                 Ptr = &(ChrPtr(Buf)[4]);
1683                 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1684                 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3);  /* filename, cbtype, mimetype */
1685                 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1686                 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1687                 
1688                 if (Mime->Data == NULL)
1689                         Mime->Data = NewStrBufPlain(NULL, bytes);
1690                 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1691         }
1692         else {
1693                 FlushStrBuf(Mime->Data);
1694                 /* TODO XImportant message */
1695         }
1696         FreeStrBuf(&Buf);
1697 }
1698
1699
1700 void view_mimepart(void) {
1701         mimepart(0);
1702 }
1703
1704 void download_mimepart(void) {
1705         mimepart(1);
1706 }
1707
1708 void view_postpart(void) {
1709         StrBuf *filename = NewStrBuf();
1710         StrBuf *partnum = NewStrBuf();
1711
1712         StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1713         StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1714
1715         postpart(partnum, filename, 0);
1716
1717         FreeStrBuf(&filename);
1718         FreeStrBuf(&partnum);
1719 }
1720
1721 void download_postpart(void) {
1722         StrBuf *filename = NewStrBuf();
1723         StrBuf *partnum = NewStrBuf();
1724
1725         StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1726         StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1727
1728         postpart(partnum, filename, 1);
1729
1730         FreeStrBuf(&filename);
1731         FreeStrBuf(&partnum);
1732 }
1733
1734
1735
1736 void show_num_attachments(void) {
1737         wc_printf("%d", GetCount(WC->attachments));
1738 }
1739
1740
1741 void h_readnew(void) { readloop(readnew, eUseDefault);}
1742 void h_readold(void) { readloop(readold, eUseDefault);}
1743 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1744 void h_headers(void) { readloop(headers, eUseDefault);}
1745 void h_do_search(void) { readloop(do_search, eUseDefault);}
1746 void h_readgt(void) { readloop(readgt, eUseDefault);}
1747 void h_readlt(void) { readloop(readlt, eUseDefault);}
1748
1749 void jsonMessageListHdr(void) 
1750 {
1751         /* TODO: make a generic function */
1752         hprintf("HTTP/1.1 200 OK\r\n");
1753         hprintf("Content-type: application/json; charset=utf-8\r\n");
1754         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1755         hprintf("Connection: close\r\n");
1756         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1757         begin_burst();
1758 }
1759
1760
1761 /* Output message list in JSON format */
1762 void jsonMessageList(void) {
1763         const StrBuf *room = sbstr("room");
1764         long oper = (havebstr("query")) ? do_search : readnew;
1765         WC->is_ajax = 1; 
1766         gotoroom(room);
1767         readloop(oper, eUseDefault);
1768         WC->is_ajax = 0;
1769 }
1770
1771 void RegisterReadLoopHandlerset(
1772         int RoomType,
1773         GetParamsGetServerCall_func GetParamsGetServerCall,
1774         PrintViewHeader_func PrintViewHeader,
1775         load_msg_ptrs_detailheaders LH,
1776         LoadMsgFromServer_func LoadMsgFromServer,
1777         RenderView_or_Tail_func RenderView_or_Tail,
1778         View_Cleanup_func ViewCleanup
1779         )
1780 {
1781         RoomRenderer *Handler;
1782
1783         Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1784
1785         Handler->RoomType = RoomType;
1786         Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1787         Handler->PrintViewHeader = PrintViewHeader;
1788         Handler->LoadMsgFromServer = LoadMsgFromServer;
1789         Handler->RenderView_or_Tail = RenderView_or_Tail;
1790         Handler->ViewCleanup = ViewCleanup;
1791         Handler->LHParse = LH;
1792
1793         Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1794 }
1795
1796 void 
1797 InitModule_MSG
1798 (void)
1799 {
1800         RegisterPreference("use_sig",
1801                            _("Attach signature to email messages?"), 
1802                            PRF_YESNO, 
1803                            NULL);
1804         RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1805         RegisterPreference("default_header_charset", 
1806                            _("Default character set for email headers:"), 
1807                            PRF_STRING, 
1808                            NULL);
1809         RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1810         RegisterPreference("defaultname", 
1811                            _("Preferred display name for email messages"), 
1812                            PRF_STRING, 
1813                            NULL);
1814         RegisterPreference("defaulthandle", 
1815                            _("Preferred display name for bulletin board posts"), 
1816                            PRF_STRING, 
1817                            NULL);
1818         RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1819
1820         WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1821         WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1822         WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1823         WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1824         WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1825         WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1826         WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1827         WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1828         WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1829         WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1830         WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1831         WebcitAddUrlHandler(HKEY("confirm_move_msg"), "", 0, confirm_move_msg, PROHIBIT_STARTPAGE);
1832         WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1833         WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1834         WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1835         WebcitAddUrlHandler(HKEY("mobilemsg"), "", 0, mobile_message_view, NEED_URL);
1836         WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1837
1838         WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1839         WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1840         WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1841         WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1842         WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1843         WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1844         WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
1845
1846         /* json */
1847         WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
1848         return ;
1849 }
1850
1851 void
1852 SessionDetachModule_MSG
1853 (wcsession *sess)
1854 {
1855         DeleteHash(&sess->summ);
1856 }