Force wiki room views into the wiki viewer instead of a read loop
[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  * Mobile browser view of message
496  */
497 void mobile_message_view(void) 
498 {
499         long msgnum = 0L;
500         const StrBuf *Mime;
501   
502         msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
503         output_headers(1, 0, 0, 0, 0, 1);
504         begin_burst();
505         do_template("msgcontrols", NULL);
506         read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
507         wDumpContent(0);
508 }
509
510 /*
511  * Display a message's headers
512  */
513 void display_headers(void) {
514         long msgnum = 0L;
515         char buf[1024];
516
517         msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
518         output_headers(0, 0, 0, 0, 0, 0);
519
520         hprintf("Content-type: text/plain\r\n"
521                 "Server: %s\r\n"
522                 "Connection: close\r\n",
523                 PACKAGE_STRING);
524         begin_burst();
525
526         serv_printf("MSG2 %ld|3", msgnum);
527         serv_getln(buf, sizeof buf);
528         if (buf[0] == '1') {
529                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
530                         wc_printf("%s\n", buf);
531                 }
532         }
533
534         wDumpContent(0);
535 }
536
537
538 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum) 
539 {
540         void                 *vEval;
541         MsgPartEvaluatorStruct  *Eval;
542         message_summary      *Msg;
543         StrBuf *Buf;
544         const char *buf;
545         const char *ebuf;
546         int nBuf;
547         long len;
548         
549         Buf = NewStrBuf();
550
551         serv_printf("MSG0 %ld|1", MsgNum);      /* ask for headers only */
552         
553         StrBuf_ServGetln(Buf);
554         if (GetServerStatus(Buf, NULL) == 1) {
555                 FreeStrBuf(&Buf);
556                 return NULL;
557         }
558
559         Msg = (message_summary*)malloc(sizeof(message_summary));
560         memset(Msg, 0, sizeof(message_summary));
561         while (len = StrBuf_ServGetln(Buf),
562                ((len != 3)  ||
563                 strcmp(ChrPtr(Buf), "000")== 0)){
564                 buf = ChrPtr(Buf);
565                 ebuf = strchr(ChrPtr(Buf), '=');
566                 nBuf = ebuf - buf;
567                 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
568                         Eval = (MsgPartEvaluatorStruct*) vEval;
569                         StrBufCutLeft(Buf, nBuf + 1);
570                         Eval->f(Msg, Buf);
571                 }
572                 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
573         }
574         return Msg;
575 }
576
577
578
579
580
581 /*
582  * load message pointers from the server for a "read messages" operation
583  *
584  * servcmd:             the citadel command to send to the citserver
585  */
586 int load_msg_ptrs(const char *servcmd, 
587                   SharedMessageStatus *Stat, 
588                   load_msg_ptrs_detailheaders LH)
589 {
590         wcsession *WCC = WC;
591         message_summary *Msg;
592         StrBuf *Buf, *Buf2;
593         long len;
594         int n;
595         int skipit;
596         const char *Ptr = NULL;
597
598         Stat->lowest_found = LONG_MAX;
599         Stat->highest_found = LONG_MIN;
600
601         if (WCC->summ != NULL) {
602                 DeleteHash(&WCC->summ);
603         }
604         WCC->summ = NewHash(1, Flathash);
605         
606         Buf = NewStrBuf();
607         serv_puts(servcmd);
608         StrBuf_ServGetln(Buf);
609         if (GetServerStatus(Buf, NULL) != 1) {
610                 FreeStrBuf(&Buf);
611                 return (Stat->nummsgs);
612         }
613         Buf2 = NewStrBuf();
614         while (len = StrBuf_ServGetln(Buf), ((len != 3) || strcmp(ChrPtr(Buf), "000")!= 0))
615         {
616                 if (Stat->nummsgs < Stat->maxload) {
617                         skipit = 0;
618                         Ptr = NULL;
619                         Msg = (message_summary*)malloc(sizeof(message_summary));
620                         memset(Msg, 0, sizeof(message_summary));
621
622                         Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
623                         Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
624
625                         if (Stat->nummsgs == 0) {
626                                 if (Msg->msgnum < Stat->lowest_found) {
627                                         Stat->lowest_found = Msg->msgnum;
628                                 }
629                                 if (Msg->msgnum > Stat->highest_found) {
630                                         Stat->highest_found = Msg->msgnum;
631                                 }
632                         }
633
634                         if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
635                                 free(Msg);
636                                 continue;
637                         }
638
639                         /* 
640                          * as citserver probably gives us messages in forward date sorting
641                          * nummsgs should be the same order as the message date.
642                          */
643                         if (Msg->date == 0) {
644                                 Msg->date = Stat->nummsgs;
645                                 if (StrLength(Buf) < 32) 
646                                         skipit = 1;
647                         }
648                         if ((!skipit) && (LH != NULL)) {
649                                 if (!LH(Buf, &Ptr, Msg, Buf2)){
650                                         free(Msg);
651                                         continue;
652                                 }                                       
653                         }
654                         n = Msg->msgnum;
655                         Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
656                 }
657                 Stat->nummsgs++;
658         }
659         FreeStrBuf(&Buf2);
660         FreeStrBuf(&Buf);
661         return (Stat->nummsgs);
662 }
663
664
665
666 /**
667  * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
668  * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
669  * \param MatchMSet MSet we want to flag
670  * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
671  */
672 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
673 {
674         const char *HashKey;
675         long HKLen;
676         HashPos *at;
677         void *vMsg;
678         message_summary *Msg;
679
680         at = GetNewHashPos(ScanMe, 0);
681         while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
682                 /* Are you a new message, or an old message? */
683                 Msg = (message_summary*) vMsg;
684                 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
685                         Msg->Flags = Msg->Flags | FlagToSet;
686                 }
687                 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
688                         Msg->Flags = Msg->Flags | FlagToSet;
689                 }
690         }
691         DeleteHashPos(&at);
692 }
693
694
695 void load_seen_flags(void)
696 {
697         StrBuf *OldMsg;
698         wcsession *WCC = WC;
699         MSet *MatchMSet;
700
701         OldMsg = NewStrBuf();
702         serv_puts("GTSN");
703         StrBuf_ServGetln(OldMsg);
704         if (GetServerStatus(OldMsg, NULL) == 2) {
705                 StrBufCutLeft(OldMsg, 4);
706         }
707         else {
708                 FreeStrBuf(&OldMsg);
709                 return;
710         }
711
712         if (ParseMSet(&MatchMSet, OldMsg))
713         {
714                 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
715         }
716         DeleteMSet(&MatchMSet);
717         FreeStrBuf(&OldMsg);
718 }
719
720 extern readloop_struct rlid[];
721
722 typedef struct _RoomRenderer{
723         int RoomType;
724
725         GetParamsGetServerCall_func GetParamsGetServerCall;
726         PrintViewHeader_func PrintViewHeader;
727         LoadMsgFromServer_func LoadMsgFromServer;
728         RenderView_or_Tail_func RenderView_or_Tail;
729         View_Cleanup_func ViewCleanup;
730         load_msg_ptrs_detailheaders LHParse;
731 } RoomRenderer;
732
733
734 /*
735  * command loop for reading messages
736  *
737  * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
738  */
739 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
740 {
741         RoomRenderer *ViewMsg;
742         void *vViewMsg;
743         void *vMsg;
744         message_summary *Msg;
745         char cmd[256] = "";
746         int i, r;
747         wcsession *WCC = WC;
748         HashPos *at;
749         const char *HashKey;
750         long HKLen;
751         WCTemplputParams SubTP;
752         SharedMessageStatus Stat;
753         void *ViewSpecific;
754
755         if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
756                 WCC->CurRoom.view = VIEW_MAILBOX;
757         }
758
759         if (havebstr("is_ajax") && (1 == (ibstr("is_ajax")))) {
760                 WCC->is_ajax = 1;
761         }
762
763         if ((oper == do_search) && (WCC->CurRoom.view == VIEW_WIKI)) {
764                 display_wiki_pagelist();
765                 return;
766         }
767
768         if (WCC->CurRoom.view == VIEW_WIKI) {
769                 http_redirect("wiki?page=home");
770                 return;
771         }
772
773         memset(&Stat, 0, sizeof(SharedMessageStatus));
774         Stat.maxload = 10000;
775         Stat.lowest_found = (-1);
776         Stat.highest_found = (-1);
777         if (ForceRenderer == eUseDefault)
778                 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
779         else 
780                 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
781         if (vViewMsg == NULL) {
782                 WCC->CurRoom.view = VIEW_BBS;
783                 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
784         }
785         if (vViewMsg == NULL) {
786                 return;                 // TODO: print message
787         }
788
789         ViewMsg = (RoomRenderer*) vViewMsg;
790         if (!WCC->is_ajax) {
791                 output_headers(1, 1, 1, 0, 0, 0);
792         } else if (WCC->CurRoom.view == VIEW_MAILBOX) {
793                 jsonMessageListHdr();
794         }
795
796         if (ViewMsg->GetParamsGetServerCall != NULL) {
797                 r = ViewMsg->GetParamsGetServerCall(
798                        &Stat,
799                        &ViewSpecific,
800                        oper,
801                        cmd, sizeof(cmd)
802                 );
803         } else {
804                 r = 0;
805         }
806         switch(r)
807         {
808         case 400:
809         case 404:
810
811                 return;
812         case 300: /* the callback hook should do the work for us here, since he knows what to do. */
813                 return;
814         case 200:
815         default:
816                 break;
817         }
818         if (!IsEmptyStr(cmd))
819                 Stat.nummsgs = load_msg_ptrs(cmd, &Stat, ViewMsg->LHParse);
820
821         if (Stat.sortit) {
822                 CompareFunc SortIt;
823                 memset(&SubTP, 0, sizeof(WCTemplputParams));
824                 SubTP.Filter.ContextType = CTX_MAILSUM;
825                 SubTP.Context = NULL;
826                 SortIt =  RetrieveSort(&SubTP, NULL, 0,
827                                        HKEY("date"), Stat.defaultsortorder);
828                 if (SortIt != NULL)
829                         SortByPayload(WCC->summ, SortIt);
830         }
831         if (Stat.startmsg < 0) {
832                 Stat.startmsg =  0;
833         }
834
835         if (Stat.load_seen) load_seen_flags();
836         
837         /*
838          * Print any inforation above the message list...
839          */
840         if (ViewMsg->PrintViewHeader != NULL)
841                 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
842
843         WCC->startmsg =  Stat.startmsg;
844         WCC->maxmsgs = Stat.maxmsgs;
845         WCC->num_displayed = 0;
846
847         /* Put some helpful data in vars for mailsummary_json */
848         {
849                 StrBuf *Foo;
850                 
851                 Foo = NewStrBuf ();
852                 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
853                 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
854                 StrBufPrintf(Foo, "%ld", Stat.startmsg);
855                 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
856         }
857
858         /*
859          * iterate over each message. if we need to load an attachment, do it here. 
860          */
861
862         if ((ViewMsg->LoadMsgFromServer != NULL) && 
863             (!IsEmptyStr(cmd)))
864         {
865                 at = GetNewHashPos(WCC->summ, 0);
866                 Stat.num_displayed = i = 0;
867                 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
868                         Msg = (message_summary*) vMsg;          
869                         if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
870                                 ViewMsg->LoadMsgFromServer(&Stat, 
871                                                            &ViewSpecific, 
872                                                            Msg, 
873                                                            (Msg->Flags & MSGFLAG_READ) != 0, 
874                                                            i);
875                         } 
876                         i++;
877                 }
878                 DeleteHashPos(&at);
879         }
880
881         /*
882          * Done iterating the message list. now tasks we want to do after.
883          */
884         if (ViewMsg->RenderView_or_Tail != NULL)
885                 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
886
887         if (ViewMsg->ViewCleanup != NULL)
888                 ViewMsg->ViewCleanup(&ViewSpecific);
889
890         WCC->startmsg = 0;
891         WCC->maxmsgs = 0;
892         if (WCC->summ != NULL) {
893                 DeleteHash(&WCC->summ);
894         }
895 }
896
897
898 /*
899  * Back end for post_message()
900  * ... this is where the actual message gets transmitted to the server.
901  */
902 void post_mime_to_server(void) {
903         wcsession *WCC = WC;
904         char top_boundary[SIZ];
905         char alt_boundary[SIZ];
906         int is_multipart = 0;
907         static int seq = 0;
908         wc_mime_attachment *att;
909         char *encoded;
910         size_t encoded_length;
911         size_t encoded_strlen;
912         char *txtmail = NULL;
913         int include_text_alt = 0;       /* Set to nonzero to include multipart/alternative text/plain */
914
915         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
916                 ChrPtr(WCC->serv_info->serv_fqdn),
917                 getpid(),
918                 ++seq
919         );
920         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
921                 ChrPtr(WCC->serv_info->serv_fqdn),
922                 getpid(),
923                 ++seq
924         );
925
926         /* RFC2045 requires this, and some clients look for it... */
927         serv_puts("MIME-Version: 1.0");
928         serv_puts("X-Mailer: " PACKAGE_STRING);
929
930         /* If there are attachments, we have to do multipart/mixed */
931         if (GetCount(WCC->attachments) > 0) {
932                 is_multipart = 1;
933         }
934
935         /* Only do multipart/alternative for mailboxes.  BBS and Wiki rooms don't need it. */
936         if (WC->CurRoom.view == VIEW_MAILBOX) {
937                 include_text_alt = 1;
938         }
939
940         if (is_multipart) {
941                 /* Remember, serv_printf() appends an extra newline */
942                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
943                 serv_printf("This is a multipart message in MIME format.\n");
944                 serv_printf("--%s", top_boundary);
945         }
946
947         /* Remember, serv_printf() appends an extra newline */
948         if (include_text_alt) {
949                 serv_printf("Content-type: multipart/alternative; "
950                         "boundary=\"%s\"\n", alt_boundary);
951                 serv_printf("This is a multipart message in MIME format.\n");
952                 serv_printf("--%s", alt_boundary);
953
954                 serv_puts("Content-type: text/plain; charset=utf-8");
955                 serv_puts("Content-Transfer-Encoding: quoted-printable");
956                 serv_puts("");
957                 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
958                 text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
959                 free(txtmail);
960
961                 serv_printf("--%s", alt_boundary);
962         }
963
964         serv_puts("Content-type: text/html; charset=utf-8");
965         serv_puts("Content-Transfer-Encoding: quoted-printable");
966         serv_puts("");
967         serv_puts("<html><body>\r\n");
968         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
969         serv_puts("</body></html>\r\n");
970
971         if (include_text_alt) {
972                 serv_printf("--%s--", alt_boundary);
973         }
974         
975         if (is_multipart) {
976                 long len;
977                 const char *Key; 
978                 void *vAtt;
979                 HashPos  *it;
980
981                 /* Add in the attachments */
982                 it = GetNewHashPos(WCC->attachments, 0);
983                 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
984                         att = (wc_mime_attachment *)vAtt;
985                         if (att->length == 0)
986                                 continue;
987                         encoded_length = ((att->length * 150) / 100);
988                         encoded = malloc(encoded_length);
989                         if (encoded == NULL) break;
990                         encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
991
992                         serv_printf("--%s", top_boundary);
993                         serv_printf("Content-type: %s", ChrPtr(att->ContentType));
994                         serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
995                         serv_puts("Content-transfer-encoding: base64");
996                         serv_puts("");
997                         serv_write(encoded, encoded_strlen);
998                         serv_puts("");
999                         serv_puts("");
1000                         free(encoded);
1001                 }
1002                 serv_printf("--%s--", top_boundary);
1003                 DeleteHashPos(&it);
1004         }
1005
1006         serv_puts("000");
1007 }
1008
1009
1010 /*
1011  * Post message (or don't post message)
1012  *
1013  * Note regarding the "dont_post" variable:
1014  * A random value (actually, it's just a timestamp) is inserted as a hidden
1015  * field called "postseq" when the display_enter page is generated.  This
1016  * value is checked when posting, using the static variable dont_post.  If a
1017  * user attempts to post twice using the same dont_post value, the message is
1018  * discarded.  This prevents the accidental double-saving of the same message
1019  * if the user happens to click the browser "back" button.
1020  */
1021 void post_message(void)
1022 {
1023         StrBuf *UserName;
1024         StrBuf *EmailAddress;
1025         StrBuf *EncBuf;
1026         char buf[1024];
1027         StrBuf *encoded_subject = NULL;
1028         static long dont_post = (-1L);
1029         int is_anonymous = 0;
1030         const StrBuf *display_name = NULL;
1031         wcsession *WCC = WC;
1032         StrBuf *Buf;
1033         
1034         if (havebstr("force_room")) {
1035                 gotoroom(sbstr("force_room"));
1036         }
1037
1038         if (havebstr("display_name")) {
1039                 display_name = sbstr("display_name");
1040                 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1041                         display_name = NULL;
1042                         is_anonymous = 1;
1043                 }
1044         }
1045
1046         if (!strcasecmp(bstr("submit_action"), "cancel")) {
1047                 sprintf(WCC->ImportantMessage, 
1048                         _("Cancelled.  Message was not posted."));
1049         } else if (lbstr("postseq") == dont_post) {
1050                 sprintf(WCC->ImportantMessage, 
1051                         _("Automatically cancelled because you have already "
1052                           "saved this message."));
1053         } else {
1054                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1055                 StrBuf *Recp = NULL; 
1056                 StrBuf *Cc = NULL;
1057                 StrBuf *Bcc = NULL;
1058                 const StrBuf *Wikipage = NULL;
1059                 const StrBuf *my_email_addr = NULL;
1060                 StrBuf *CmdBuf = NULL;
1061                 StrBuf *references = NULL;
1062                 int save_to_drafts;
1063                 long HeaderLen;
1064
1065                 save_to_drafts = !strcasecmp(bstr("submit_action"), "drafts");
1066                 Buf = NewStrBuf();
1067
1068                 if (save_to_drafts) {
1069                         /* temporarily change to the drafts room */
1070                         serv_puts("GOTO _DRAFTS_");
1071                         StrBuf_ServGetln(Buf);
1072                         if (GetServerStatus(Buf, NULL) != 2) {
1073                                 /* You probably don't even have a dumb Drafts folder */
1074                                 StrBufCutLeft(Buf, 4);
1075                                 lprintf(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf));
1076                                 StrBufAppendBufPlain(WCC->ImportantMsg, _("Saved to Drafts failed: "), -1, 0);
1077                                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 0);
1078                                 display_enter();
1079                                 FreeStrBuf(&Buf);
1080                                 return;
1081                         }
1082                 }
1083
1084                 if (havebstr("references"))
1085                 {
1086                         const StrBuf *ref = sbstr("references");
1087                         references = NewStrBufDup(ref);
1088                         if (*ChrPtr(references) == '|') {       /* remove leading '|' if present */
1089                                 StrBufCutLeft(references, 1);
1090                         }
1091                         StrBufReplaceChars(references, '|', '!');
1092                 }
1093                 if (havebstr("subject")) {
1094                         const StrBuf *Subj;
1095                         /*
1096                          * make enough room for the encoded string; 
1097                          * plus the QP header 
1098                          */
1099                         Subj = sbstr("subject");
1100                         
1101                         StrBufRFC2047encode(&encoded_subject, Subj);
1102                 }
1103                 UserName = NewStrBuf();
1104                 EmailAddress = NewStrBuf();
1105                 EncBuf = NewStrBuf();
1106
1107                 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1108                 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1109                 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1110
1111                 FreeStrBuf(&UserName);
1112                 FreeStrBuf(&EmailAddress);
1113                 FreeStrBuf(&EncBuf);
1114
1115                 Wikipage = sbstr("page");
1116                 my_email_addr = sbstr("my_email_addr");
1117                 
1118                 HeaderLen = StrLength(Recp) + 
1119                         StrLength(encoded_subject) +
1120                         StrLength(Cc) +
1121                         StrLength(Bcc) + 
1122                         StrLength(Wikipage) +
1123                         StrLength(my_email_addr) + 
1124                         StrLength(references);
1125                 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1126                 StrBufPrintf(CmdBuf, 
1127                              CMD,
1128                              save_to_drafts?"":ChrPtr(Recp),
1129                              is_anonymous,
1130                              ChrPtr(encoded_subject),
1131                              ChrPtr(display_name),
1132                              save_to_drafts?"":ChrPtr(Cc),
1133                              save_to_drafts?"":ChrPtr(Bcc),
1134                              ChrPtr(Wikipage),
1135                              ChrPtr(my_email_addr),
1136                              ChrPtr(references));
1137                 FreeStrBuf(&references);
1138                 FreeStrBuf(&encoded_subject);
1139
1140                 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) && 
1141                     (GetCount(WCC->attachments) == 0)){
1142                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Refusing to post empty message.\n"), -1, 0);
1143                         FreeStrBuf(&CmdBuf);
1144                                 
1145                 }
1146                 else 
1147                 {
1148                         lprintf(9, "%s\n", ChrPtr(CmdBuf));
1149                         serv_puts(ChrPtr(CmdBuf));
1150                         FreeStrBuf(&CmdBuf);
1151
1152                         StrBuf_ServGetln(Buf);
1153                         if (GetServerStatus(Buf, NULL) == 4) {
1154                                 if (save_to_drafts) {
1155                                         if (  (havebstr("recp"))
1156                                               || (havebstr("cc"  ))
1157                                               || (havebstr("bcc" )) ) {
1158                                                 /* save recipient headers or room to post to */
1159                                                 serv_printf("To: %s", ChrPtr(Recp));
1160                                                 serv_printf("Cc: %s", ChrPtr(Cc));
1161                                                 serv_printf("Bcc: %s", ChrPtr(Bcc));
1162                                         } else {
1163                                                 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1164                                         }
1165                                 }
1166                                 post_mime_to_server();
1167                                 if (save_to_drafts) {
1168                                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been saved to Drafts.\n"), -1, 0);
1169                                         gotoroom(WCC->CurRoom.name);
1170                                         display_enter();
1171                                         FreeStrBuf(&Buf);
1172                                         return;
1173                                 } else if (  (havebstr("recp"))
1174                                              || (havebstr("cc"  ))
1175                                              || (havebstr("bcc" ))
1176                                         ) {
1177                                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been sent.\n"), -1, 0);
1178                                 }
1179                                 else {
1180                                         StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been posted.\n"), -1, 0);
1181                                 }
1182                                 dont_post = lbstr("postseq");
1183                         } else {
1184                                 StrBufCutLeft(Buf, 4);
1185
1186                                 lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, ChrPtr(Buf));
1187                                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 0);
1188                                 if (save_to_drafts) gotoroom(WCC->CurRoom.name);
1189                                 display_enter();
1190                                 FreeStrBuf(&Buf);
1191                                 FreeStrBuf(&Cc);
1192                                 FreeStrBuf(&Bcc);
1193                                 return;
1194                         }
1195                 }
1196                 FreeStrBuf(&Recp);
1197                 FreeStrBuf(&Buf);
1198                 FreeStrBuf(&Cc);
1199                 FreeStrBuf(&Bcc);
1200         }
1201
1202         DeleteHash(&WCC->attachments);
1203
1204         /*
1205          *  We may have been supplied with instructions regarding the location
1206          *  to which we must return after posting.  If found, go there.
1207          */
1208         if (havebstr("return_to")) {
1209                 http_redirect(bstr("return_to"));
1210         }
1211         /*
1212          *  If we were editing a page in a wiki room, go to that page now.
1213          */
1214         else if (havebstr("page")) {
1215                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1216                 http_redirect(buf);
1217         }
1218         /*
1219          *  Otherwise, just go to the "read messages" loop.
1220          */
1221         else {
1222                 readloop(readnew, eUseDefault);
1223         }
1224 }
1225
1226
1227 /*
1228  * Client is uploading an attachment
1229  */
1230 void upload_attachment(void) {
1231         wcsession *WCC = WC;
1232         const char *pch;
1233         int n;
1234         const char *newn;
1235         long newnlen;
1236         void *v;
1237         wc_mime_attachment *att;
1238
1239         lprintf(9, "upload_attachment()\n");
1240         wc_printf("upload_attachment()<br>\n");
1241
1242         if (WCC->upload_length <= 0) {
1243                 lprintf(9, "ERROR no attachment was uploaded\n");
1244                 wc_printf("ERROR no attachment was uploaded<br>\n");
1245                 return;
1246         }
1247
1248         lprintf(9, "Client is uploading %d bytes\n", WCC->upload_length);
1249         wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1250         att = malloc(sizeof(wc_mime_attachment));
1251         memset(att, 0, sizeof(wc_mime_attachment ));
1252         att->length = WCC->upload_length;
1253         att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1254         att->FileName = NewStrBufDup(WCC->upload_filename);
1255         
1256         if (WCC->attachments == NULL) {
1257                 WCC->attachments = NewHash(1, Flathash);
1258         }
1259
1260         /* And add it to the list. */
1261         n = 0;
1262         if ((GetCount(WCC->attachments) > 0) && 
1263             GetHashAt(WCC->attachments, 
1264                       GetCount(WCC->attachments) -1, 
1265                       &newnlen, &newn, &v))
1266             n = *((int*) newn) + 1;
1267         Put(WCC->attachments, IKEY(n), att, DestroyMime);
1268
1269         /*
1270          * Mozilla sends a simple filename, which is what we want,
1271          * but Satan's Browser sends an entire pathname.  Reduce
1272          * the path to just a filename if we need to.
1273          */
1274         pch = strrchr(ChrPtr(att->FileName), '/');
1275         if (pch != NULL) {
1276                 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1277         }
1278         pch = strrchr(ChrPtr(att->FileName), '\\');
1279         if (pch != NULL) {
1280                 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1281         }
1282
1283         /*
1284          * Transfer control of this memory from the upload struct
1285          * to the attachment struct.
1286          */
1287         att->Data = WCC->upload;
1288         WCC->upload = NULL;
1289         WCC->upload_length = 0;
1290 }
1291
1292
1293 /*
1294  * Remove an attachment from the message currently being composed.
1295  *
1296  * Currently we identify the attachment to be removed by its filename.
1297  * There is probably a better way to do this.
1298  */
1299 void remove_attachment(void) {
1300         wcsession *WCC = WC;
1301         wc_mime_attachment *att;
1302         void *vAtt;
1303         StrBuf *WhichAttachment;
1304         HashPos *at;
1305         long len;
1306         const char *key;
1307
1308         WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1309         StrBufUnescape(WhichAttachment, 0);
1310         at = GetNewHashPos(WCC->attachments, 0);
1311         do {
1312                 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1313         
1314                 att = (wc_mime_attachment*) vAtt;
1315                 if ((att != NULL) && 
1316                     (strcmp(ChrPtr(WhichAttachment), 
1317                             ChrPtr(att->FileName)   ) == 0))
1318                 {
1319                         DeleteEntryFromHash(WCC->attachments, at);
1320                         break;
1321                 }
1322         }
1323         while (NextHashPos(WCC->attachments, at));
1324         FreeStrBuf(&WhichAttachment);
1325         wc_printf("remove_attachment() completed\n");
1326 }
1327
1328
1329 /*
1330  * display the message entry screen
1331  */
1332 void display_enter(void)
1333 {
1334         char buf[SIZ];
1335         long now;
1336         const StrBuf *display_name = NULL;
1337         int recipient_required = 0;
1338         int subject_required = 0;
1339         int recipient_bad = 0;
1340         int is_anonymous = 0;
1341         wcsession *WCC = WC;
1342
1343         now = time(NULL);
1344
1345         if (havebstr("force_room")) {
1346                 gotoroom(sbstr("force_room"));
1347         }
1348
1349         display_name = sbstr("display_name");
1350         if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1351                 display_name = NULL;
1352                 is_anonymous = 1;
1353         }
1354
1355         /* First test to see whether this is a room that requires recipients to be entered */
1356         serv_puts("ENT0 0");
1357         serv_getln(buf, sizeof buf);
1358
1359         if (!strncmp(buf, "570", 3)) {          /* 570 means that we need a recipient here */
1360                 recipient_required = 1;
1361         }
1362         else if (buf[0] != '2') {               /* Any other error means that we cannot continue */
1363                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1364                 readloop(readnew, eUseDefault);
1365                 return;
1366         }
1367
1368         /* Is the server strongly recommending that the user enter a message subject? */
1369         if ((buf[3] != '\0') && (buf[4] != '\0')) {
1370                 subject_required = extract_int(&buf[4], 1);
1371         }
1372
1373         /*
1374          * Are we perhaps in an address book view?  If so, then an "enter
1375          * message" command really means "add new entry."
1376          */
1377         if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1378                 do_edit_vcard(-1, "", NULL, NULL, "",  ChrPtr(WCC->CurRoom.name));
1379                 return;
1380         }
1381
1382         /*
1383          * Are we perhaps in a calendar room?  If so, then an "enter
1384          * message" command really means "add new calendar item."
1385          */
1386         if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1387                 display_edit_event();
1388                 return;
1389         }
1390
1391         /*
1392          * Are we perhaps in a tasks view?  If so, then an "enter
1393          * message" command really means "add new task."
1394          */
1395         if (WCC->CurRoom.defview == VIEW_TASKS) {
1396                 display_edit_task();
1397                 return;
1398         }
1399
1400         /*
1401          * Otherwise proceed normally.
1402          * Do a custom room banner with no navbar...
1403          */
1404
1405         if (recipient_required) {
1406                 const StrBuf *Recp = NULL; 
1407                 const StrBuf *Cc = NULL;
1408                 const StrBuf *Bcc = NULL;
1409                 const StrBuf *Wikipage = NULL;
1410                 StrBuf *CmdBuf = NULL;
1411                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1412                 
1413                 Recp = sbstr("recp");
1414                 Cc = sbstr("cc");
1415                 Bcc = sbstr("bcc");
1416                 Wikipage = sbstr("page");
1417                 
1418                 CmdBuf = NewStrBufPlain(NULL, 
1419                                         sizeof (CMD) + 
1420                                         StrLength(Recp) + 
1421                                         StrLength(display_name) +
1422                                         StrLength(Cc) +
1423                                         StrLength(Bcc) + 
1424                                         StrLength(Wikipage));
1425
1426                 StrBufPrintf(CmdBuf, 
1427                              CMD,
1428                              ChrPtr(Recp), 
1429                              is_anonymous,
1430                              ChrPtr(display_name),
1431                              ChrPtr(Cc), 
1432                              ChrPtr(Bcc), 
1433                              ChrPtr(Wikipage));
1434                 serv_puts(ChrPtr(CmdBuf));
1435                 serv_getln(buf, sizeof buf);
1436                 FreeStrBuf(&CmdBuf);
1437
1438                 if (!strncmp(buf, "570", 3)) {  /* 570 means we have an invalid recipient listed */
1439                         if (havebstr("recp") && 
1440                             havebstr("cc"  ) && 
1441                             havebstr("bcc" )) {
1442                                 recipient_bad = 1;
1443                         }
1444                 }
1445                 else if (buf[0] != '2') {       /* Any other error means that we cannot continue */
1446                         wc_printf("<em>%s</em><br>\n", &buf[4]);        /* TODO -> important message */
1447                         return;
1448                 }
1449         }
1450         if (recipient_required)
1451                 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1452         if (recipient_required || subject_required)
1453                 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1454
1455         begin_burst();
1456         output_headers(1, 0, 0, 0, 1, 0);
1457         DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1458         end_burst();
1459
1460         return;
1461 }
1462
1463 /*
1464  * delete a message
1465  */
1466 void delete_msg(void)
1467 {
1468         long msgid;
1469         char buf[SIZ];
1470
1471         msgid = lbstr("msgid");
1472
1473         if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) {  /* Delete from Trash is a real delete */
1474                 serv_printf("DELE %ld", msgid); 
1475         }
1476         else {                  /* Otherwise move it to Trash */
1477                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1478         }
1479
1480         serv_getln(buf, sizeof buf);
1481         sprintf(WC->ImportantMessage, "%s", &buf[4]);
1482         readloop(readnew, eUseDefault);
1483 }
1484
1485
1486 /*
1487  * move a message to another room
1488  */
1489 void move_msg(void)
1490 {
1491         long msgid;
1492         char buf[SIZ];
1493
1494         msgid = lbstr("msgid");
1495
1496         if (havebstr("move_button")) {
1497                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1498                 serv_puts(buf);
1499                 serv_getln(buf, sizeof buf);
1500                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1501         } else {
1502                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1503         }
1504
1505         readloop(readnew, eUseDefault);
1506 }
1507
1508
1509 /*
1510  * Confirm move of a message
1511  */
1512 void confirm_move_msg(void)
1513 {
1514         long msgid;
1515         char buf[SIZ];
1516         char targ[SIZ];
1517
1518         msgid = lbstr("msgid");
1519
1520
1521         output_headers(1, 1, 2, 0, 0, 0);
1522         wc_printf("<div id=\"banner\">\n");
1523         wc_printf("<h1>");
1524         wc_printf(_("Confirm move of message"));
1525         wc_printf("</h1>");
1526         wc_printf("</div>\n");
1527
1528         wc_printf("<div id=\"content\" class=\"service\">\n");
1529
1530         wc_printf("<CENTER>");
1531
1532         wc_printf(_("Move this message to:"));
1533         wc_printf("<br>\n");
1534
1535         wc_printf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1536         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1537         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1538
1539         wc_printf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1540         serv_puts("LKRA");
1541         serv_getln(buf, sizeof buf);
1542         if (buf[0] == '1') {
1543                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1544                         extract_token(targ, buf, 0, '|', sizeof targ);
1545                         wc_printf("<OPTION>");
1546                         escputs(targ);
1547                         wc_printf("\n");
1548                 }
1549         }
1550         wc_printf("</SELECT>\n");
1551         wc_printf("<br>\n");
1552
1553         wc_printf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1554         wc_printf("&nbsp;");
1555         wc_printf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1556         wc_printf("</form></CENTER>\n");
1557
1558         wc_printf("</CENTER>\n");
1559         wDumpContent(1);
1560 }
1561
1562
1563 /*
1564  * Generic function to output an arbitrary MIME attachment from
1565  * message being composed
1566  *
1567  * partnum              The MIME part to be output
1568  * filename             Fake filename to give
1569  * force_download       Nonzero to force set the Content-Type: header to "application/octet-stream"
1570  */
1571 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1572 {
1573         void *vPart;
1574         StrBuf *content_type;
1575         wc_mime_attachment *part;
1576         int i;
1577
1578         i = StrToi(partnum);
1579         if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1580             (vPart != NULL)) {
1581                 part = (wc_mime_attachment*) vPart;
1582                 if (force_download) {
1583                         content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1584                 }
1585                 else {
1586                         content_type = NewStrBufDup(part->ContentType);
1587                 }
1588                 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1589                 http_transmit_thing(ChrPtr(content_type), 0);
1590         } else {
1591                 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1592                 output_headers(0, 0, 0, 0, 0, 0);
1593                 hprintf("Content-Type: text/plain\r\n");
1594                 begin_burst();
1595                 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"), 
1596                         ChrPtr(partnum), ChrPtr(filename));
1597                 end_burst();
1598         }
1599         FreeStrBuf(&content_type);
1600 }
1601
1602
1603 /*
1604  * Generic function to output an arbitrary MIME part from an arbitrary
1605  * message number on the server.
1606  *
1607  * msgnum               Number of the item on the citadel server
1608  * partnum              The MIME part to be output
1609  * force_download       Nonzero to force set the Content-Type: header to "application/octet-stream"
1610  */
1611 void mimepart(int force_download)
1612 {
1613         long msgnum;
1614         StrBuf *att;
1615         wcsession *WCC = WC;
1616         StrBuf *Buf;
1617         off_t bytes;
1618         StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1619         const char *CT;
1620
1621         att = Buf = NewStrBuf();
1622         msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1623         StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1624
1625         serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1626         StrBuf_ServGetln(Buf);
1627         if (GetServerStatus(Buf, NULL) == 2) {
1628                 StrBufCutLeft(Buf, 4);
1629                 bytes = StrBufExtract_long(Buf, 0, '|');
1630                 if (!force_download) {
1631                         StrBufExtract_token(ContentType, Buf, 3, '|');
1632                 }
1633
1634                 serv_read_binary(WCC->WBuf, bytes, Buf);
1635                 serv_puts("CLOS");
1636                 StrBuf_ServGetln(Buf);
1637                 CT = ChrPtr(ContentType);
1638
1639                 if (!force_download) {
1640                         if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1641                                 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1642                                 CT = GuessMimeByFilename(SKEY(Buf));
1643                         }
1644                         if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1645                                 CT = GuessMimeType(SKEY(WCC->WBuf));
1646                         }
1647                 }
1648                 http_transmit_thing(CT, 0);
1649         } else {
1650                 StrBufCutLeft(Buf, 4);
1651                 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1652                 output_headers(0, 0, 0, 0, 0, 0);
1653                 hprintf("Content-Type: text/plain\r\n");
1654                 begin_burst();
1655                 wc_printf(_("An error occurred while retrieving this part: %s\n"), 
1656                         ChrPtr(Buf));
1657                 end_burst();
1658         }
1659         FreeStrBuf(&ContentType);
1660         FreeStrBuf(&Buf);
1661 }
1662
1663
1664 /*
1665  * Read any MIME part of a message, from the server, into memory.
1666  */
1667 StrBuf *load_mimepart(long msgnum, char *partnum)
1668 {
1669         off_t bytes;
1670         StrBuf *Buf;
1671         
1672         Buf = NewStrBuf();
1673         serv_printf("DLAT %ld|%s", msgnum, partnum);
1674         StrBuf_ServGetln(Buf);
1675         if (GetServerStatus(Buf, NULL) == 6) {
1676                 StrBufCutLeft(Buf, 4);
1677                 bytes = StrBufExtract_long(Buf, 0, '|');
1678                 FreeStrBuf(&Buf);
1679                 Buf = NewStrBuf();
1680                 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1681                 return(Buf);
1682         }
1683         else {
1684                 FreeStrBuf(&Buf);
1685                 return(NULL);
1686         }
1687 }
1688
1689 /*
1690  * Read any MIME part of a message, from the server, into memory.
1691  */
1692 void MimeLoadData(wc_mime_attachment *Mime)
1693 {
1694         StrBuf *Buf;
1695         const char *Ptr;
1696         off_t bytes;
1697         /* TODO: is there a chance the content type is different from the one we know? */
1698
1699         serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1700         Buf = NewStrBuf();
1701         StrBuf_ServGetln(Buf);
1702         if (GetServerStatus(Buf, NULL) == 6) {
1703                 Ptr = &(ChrPtr(Buf)[4]);
1704                 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1705                 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3);  /* filename, cbtype, mimetype */
1706                 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1707                 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1708                 
1709                 if (Mime->Data == NULL)
1710                         Mime->Data = NewStrBufPlain(NULL, bytes);
1711                 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1712         }
1713         else {
1714                 FlushStrBuf(Mime->Data);
1715                 /* TODO XImportant message */
1716         }
1717         FreeStrBuf(&Buf);
1718 }
1719
1720
1721 void view_mimepart(void) {
1722         mimepart(0);
1723 }
1724
1725 void download_mimepart(void) {
1726         mimepart(1);
1727 }
1728
1729 void view_postpart(void) {
1730         StrBuf *filename = NewStrBuf();
1731         StrBuf *partnum = NewStrBuf();
1732
1733         StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1734         StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1735
1736         postpart(partnum, filename, 0);
1737
1738         FreeStrBuf(&filename);
1739         FreeStrBuf(&partnum);
1740 }
1741
1742 void download_postpart(void) {
1743         StrBuf *filename = NewStrBuf();
1744         StrBuf *partnum = NewStrBuf();
1745
1746         StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1747         StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1748
1749         postpart(partnum, filename, 1);
1750
1751         FreeStrBuf(&filename);
1752         FreeStrBuf(&partnum);
1753 }
1754
1755
1756
1757 void show_num_attachments(void) {
1758         wc_printf("%d", GetCount(WC->attachments));
1759 }
1760
1761
1762 void h_readnew(void) { readloop(readnew, eUseDefault);}
1763 void h_readold(void) { readloop(readold, eUseDefault);}
1764 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1765 void h_headers(void) { readloop(headers, eUseDefault);}
1766 void h_do_search(void) { readloop(do_search, eUseDefault);}
1767 void h_readgt(void) { readloop(readgt, eUseDefault);}
1768 void h_readlt(void) { readloop(readlt, eUseDefault);}
1769
1770 void jsonMessageListHdr(void) 
1771 {
1772         /* TODO: make a generic function */
1773         hprintf("HTTP/1.1 200 OK\r\n");
1774         hprintf("Content-type: application/json; charset=utf-8\r\n");
1775         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1776         hprintf("Connection: close\r\n");
1777         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1778         begin_burst();
1779 }
1780
1781
1782 /* Output message list in JSON format */
1783 void jsonMessageList(void) {
1784         const StrBuf *room = sbstr("room");
1785         long oper = (havebstr("query")) ? do_search : readnew;
1786         WC->is_ajax = 1; 
1787         gotoroom(room);
1788         readloop(oper, eUseDefault);
1789         WC->is_ajax = 0;
1790 }
1791
1792 void RegisterReadLoopHandlerset(
1793         int RoomType,
1794         GetParamsGetServerCall_func GetParamsGetServerCall,
1795         PrintViewHeader_func PrintViewHeader,
1796         load_msg_ptrs_detailheaders LH,
1797         LoadMsgFromServer_func LoadMsgFromServer,
1798         RenderView_or_Tail_func RenderView_or_Tail,
1799         View_Cleanup_func ViewCleanup
1800         )
1801 {
1802         RoomRenderer *Handler;
1803
1804         Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1805
1806         Handler->RoomType = RoomType;
1807         Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1808         Handler->PrintViewHeader = PrintViewHeader;
1809         Handler->LoadMsgFromServer = LoadMsgFromServer;
1810         Handler->RenderView_or_Tail = RenderView_or_Tail;
1811         Handler->ViewCleanup = ViewCleanup;
1812         Handler->LHParse = LH;
1813
1814         Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1815 }
1816
1817 void 
1818 InitModule_MSG
1819 (void)
1820 {
1821         RegisterPreference("use_sig",
1822                            _("Attach signature to email messages?"), 
1823                            PRF_YESNO, 
1824                            NULL);
1825         RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1826         RegisterPreference("default_header_charset", 
1827                            _("Default character set for email headers:"), 
1828                            PRF_STRING, 
1829                            NULL);
1830         RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1831         RegisterPreference("defaultname", 
1832                            _("Preferred display name for email messages"), 
1833                            PRF_STRING, 
1834                            NULL);
1835         RegisterPreference("defaulthandle", 
1836                            _("Preferred display name for bulletin board posts"), 
1837                            PRF_STRING, 
1838                            NULL);
1839         RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1840
1841         WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1842         WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1843         WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1844         WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1845         WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1846         WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1847         WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1848         WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1849         WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1850         WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1851         WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1852         WebcitAddUrlHandler(HKEY("confirm_move_msg"), "", 0, confirm_move_msg, PROHIBIT_STARTPAGE);
1853         WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1854         WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1855         WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1856         WebcitAddUrlHandler(HKEY("mobilemsg"), "", 0, mobile_message_view, NEED_URL);
1857         WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1858
1859         WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1860         WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1861         WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1862         WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1863         WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1864         WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1865         WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
1866
1867         /* json */
1868         WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
1869         return ;
1870 }
1871
1872 void
1873 SessionDetachModule_MSG
1874 (wcsession *sess)
1875 {
1876         DeleteHash(&sess->summ);
1877 }