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