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