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