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