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