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