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