* remove old (replaced) code
[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 const char* SortIcons[3] = {
39         "static/up_pointer.gif",
40         "static/down_pointer.gif",
41         "static/sort_none.gif"
42 };
43
44 enum  {/// SortByEnum
45         eDate,
46         eRDate,
47         eSubject,
48         eRSubject,
49         eSender,
50         eRSender,
51         eReverse,
52         eUnSet
53 }; 
54
55 /* SortEnum to plain string representation */
56 static const char* SortByStrings[] = {
57         "date",
58         "rdate",
59         "subject", 
60         "rsubject", 
61         "sender",
62         "rsender",
63         "reverse",
64         "unset"
65 };
66
67 /* SortEnum to sort-Function Table */
68 const CompareFunc SortFuncs[eUnSet] = {
69         summcmp_date,
70         summcmp_rdate,
71         summcmp_subj,
72         summcmp_rsubj,
73         summcmp_sender,
74         summcmp_rsender,
75         summcmp_rdate
76 };
77
78 /* given a SortEnum, which icon should we choose? */
79 const int SortDateToIcon[eUnSet] = { eUp, eDown, eNone, eNone, eNone, eNone, eNone};
80 const int SortSubjectToIcon[eUnSet] = { eNone, eNone, eUp, eDown, eNone, eNone, eNone};
81 const int SortSenderToIcon[eUnSet] = { eNone, eNone, eNone, eNone, eUp, eDown, eNone};
82
83 /* given a SortEnum, which would be the "opposite" search option? */
84 const int DateInvertSortString[eUnSet] =  { eRDate, eDate, eDate, eDate, eDate, eDate, eDate};
85 const int SubjectInvertSortString[eUnSet] =  { eSubject, eSubject, eRSubject, eUnSet, eSubject, eSubject, eSubject};
86 const int SenderInvertSortString[eUnSet] =  { eSender, eSender, eSender, eSender, eRSender, eUnSet, eSender};
87
88
89
90 /*----------------------------------------------------------------------------*/
91
92 /*
93  * Translates sortoption String to its SortEnum representation 
94  * returns the enum matching the string; defaults to RDate
95  */
96 //SortByEnum 
97 int StrToESort (const StrBuf *sortby)
98 {////todoo: hash
99         int result = eDate;
100
101         if (!IsEmptyStr(ChrPtr(sortby))) while (result < eUnSet){
102                         if (!strcasecmp(ChrPtr(sortby), 
103                                         SortByStrings[result])) 
104                                 return result;
105                         result ++;
106                 }
107         return eRDate;
108 }
109
110
111 typedef int (*QSortFunction) (const void*, const void*);
112
113 /*
114  * qsort() compatible function to compare two longs in descending order.
115  */
116 int longcmp_r(const void *s1, const void *s2) {
117         long l1;
118         long l2;
119
120         l1 = *(long *)GetSearchPayload(s1);
121         l2 = *(long *)GetSearchPayload(s2);
122
123         if (l1 > l2) return(-1);
124         if (l1 < l2) return(+1);
125         return(0);
126 }
127
128 /*
129  * qsort() compatible function to compare two longs in descending order.
130  */
131 int qlongcmp_r(const void *s1, const void *s2) {
132         long l1 = (long) s1;
133         long l2 = (long) s2;
134
135         if (l1 > l2) return(-1);
136         if (l1 < l2) return(+1);
137         return(0);
138 }
139
140  
141 /*
142  * qsort() compatible function to compare two message summary structs by ascending subject.
143  */
144 int summcmp_subj(const void *s1, const void *s2) {
145         message_summary *summ1;
146         message_summary *summ2;
147         
148         summ1 = (message_summary *)GetSearchPayload(s1);
149         summ2 = (message_summary *)GetSearchPayload(s2);
150         return strcasecmp(ChrPtr(summ1->subj), ChrPtr(summ2->subj));
151 }
152
153 /*
154  * qsort() compatible function to compare two message summary structs by descending subject.
155  */
156 int summcmp_rsubj(const void *s1, const void *s2) {
157         message_summary *summ1;
158         message_summary *summ2;
159         
160         summ1 = (message_summary *)GetSearchPayload(s1);
161         summ2 = (message_summary *)GetSearchPayload(s2);
162         return strcasecmp(ChrPtr(summ2->subj), ChrPtr(summ1->subj));
163 }
164
165 /*
166  * qsort() compatible function to compare two message summary structs by ascending sender.
167  */
168 int summcmp_sender(const void *s1, const void *s2) {
169         message_summary *summ1;
170         message_summary *summ2;
171         
172         summ1 = (message_summary *)GetSearchPayload(s1);
173         summ2 = (message_summary *)GetSearchPayload(s2);
174         return strcasecmp(ChrPtr(summ1->from), ChrPtr(summ2->from));
175 }
176
177 /*
178  * qsort() compatible function to compare two message summary structs by descending sender.
179  */
180 int summcmp_rsender(const void *s1, const void *s2) {
181         message_summary *summ1;
182         message_summary *summ2;
183         
184         summ1 = (message_summary *)GetSearchPayload(s1);
185         summ2 = (message_summary *)GetSearchPayload(s2);
186         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from));
187 }
188
189 /*
190  * qsort() compatible function to compare two message summary structs by ascending date.
191  */
192 int summcmp_date(const void *s1, const void *s2) {
193         message_summary *summ1;
194         message_summary *summ2;
195         
196         summ1 = (message_summary *)GetSearchPayload(s1);
197         summ2 = (message_summary *)GetSearchPayload(s2);
198
199         if (summ1->date < summ2->date) return -1;
200         else if (summ1->date > summ2->date) return +1;
201         else return 0;
202 }
203
204 /*
205  * qsort() compatible function to compare two message summary structs by descending date.
206  */
207 int summcmp_rdate(const void *s1, const void *s2) {
208         message_summary *summ1;
209         message_summary *summ2;
210         
211         summ1 = (message_summary *)GetSearchPayload(s1);
212         summ2 = (message_summary *)GetSearchPayload(s2);
213
214         if (summ1->date < summ2->date) return +1;
215         else if (summ1->date > summ2->date) return -1;
216         else return 0;
217 }
218
219
220
221
222
223
224 /*
225  * I wanna SEE that message!
226  *
227  * msgnum               Message number to display
228  * printable_view       Nonzero to display a printable view
229  * section              Optional for encapsulated message/rfc822 submessage
230  */
231 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, int printable_view, const StrBuf *PartNum) {
232         StrBuf *Buf;
233         StrBuf *HdrToken;
234         StrBuf *FoundCharset;
235         HashPos  *it;
236         void *vMime;
237         message_summary *Msg = NULL;
238         headereval *Hdr;
239         void *vHdr;
240         char buf[SIZ];
241 //      char mime_submessages[256] = "";
242         char reply_references[1024] = "";
243         int Done = 0;
244         int state=0;
245         long len;
246         const char *Key;
247
248         Buf = NewStrBuf();
249         lprintf(1, "----------%s---------MSG4 %ld|%s--------------\n", tmpl, msgnum, ChrPtr(PartNum));
250         serv_printf("MSG4 %ld|%s", msgnum, ChrPtr(PartNum));
251         StrBuf_ServGetln(Buf);
252         if (GetServerStatus(Buf, NULL) != 1) {
253                 StrBufAppendPrintf(Target, "<strong>");
254                 StrBufAppendPrintf(Target, _("ERROR:"));
255                 StrBufAppendPrintf(Target, "</strong> %s<br />\n", &buf[4]);
256                 FreeStrBuf(&Buf);
257                 return 0;
258         }
259
260         /** begin everythingamundo table */
261
262
263         HdrToken = NewStrBuf();
264         Msg = (message_summary *)malloc(sizeof(message_summary));
265         memset(Msg, 0, sizeof(message_summary));
266         Msg->msgnum = msgnum;
267         Msg->PartNum = PartNum;
268         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
269         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
270         FoundCharset = NewStrBuf();
271         while ((StrBuf_ServGetln(Buf)>=0) && !Done) {
272                 if ( (StrLength(Buf)==3) && 
273                     !strcmp(ChrPtr(Buf), "000")) 
274                 {
275                         Done = 1;
276                         if (state < 2) {
277                                 lprintf(1, _("unexpected end of message"));
278                                 StrBufAppendPrintf(Target, "<i>");
279                                 StrBufAppendPrintf(Target, _("unexpected end of message"));
280                                 StrBufAppendPrintf(Target, " (1)</i><br /><br />\n");
281                                 StrBufAppendPrintf(Target, "</div>\n");
282                                 FreeStrBuf(&Buf);
283                                 FreeStrBuf(&HdrToken);
284                                 DestroyMessageSummary(Msg);
285                                 FreeStrBuf(&FoundCharset);
286                                 return 0;
287                         }
288                         else {
289                                 break;
290                         }
291                 }
292                 switch (state) {
293                 case 0:/* Citadel Message Headers */
294                         if (StrLength(Buf) == 0) {
295                                 state ++;
296                                 break;
297                         }
298                         StrBufExtract_token(HdrToken, Buf, 0, '=');
299                         StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
300                         
301                         lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
302                         if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
303                             (vHdr != NULL)) {
304                                 Hdr = (headereval*)vHdr;
305                                 Hdr->evaluator(Msg, Buf, FoundCharset);
306                                 if (Hdr->Type == 1) {
307                                         state++;
308                                 }
309                         }
310                         else lprintf(1, "don't know how to handle message header[%s]\n", ChrPtr(HdrToken));
311                         break;
312                 case 1:/* Message Mime Header */
313                         if (StrLength(Buf) == 0) {
314                                 state++;
315                                 if (Msg->MsgBody->ContentType == NULL)
316                                         /* end of header or no header? */
317                                         Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
318                                  /* usual end of mime header */
319                         }
320                         else
321                         {
322                                 StrBufExtract_token(HdrToken, Buf, 0, ':');
323                                 if (StrLength(HdrToken) > 0) {
324                                         StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
325                                         lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
326                                         if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
327                                             (vHdr != NULL)) {
328                                                 Hdr = (headereval*)vHdr;
329                                                 Hdr->evaluator(Msg, Buf, FoundCharset);
330                                         }
331                                         break;
332                                 }
333                         }
334                 case 2: /* Message Body */
335                         
336                         if (Msg->MsgBody->size_known > 0) {
337                                 StrBuf_ServGetBLOB(Msg->MsgBody->Data, Msg->MsgBody->length);
338                                 state ++;
339                                         /// todo: check next line, if not 000, append following lines
340                         }
341                         else if (1){
342                                 if (StrLength(Msg->MsgBody->Data) > 0)
343                                         StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
344                                 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
345                         }
346                         break;
347                 case 3:
348                         StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
349                         break;
350                 }
351         }
352
353         if (Msg->AllAttach == NULL)
354                 Msg->AllAttach = NewHash(1,NULL);
355         Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
356
357         
358         /* strip the bare contenttype, so we ommit charset etc. */
359         StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
360         StrBufTrim(Buf);
361         if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
362             (vHdr != NULL)) {
363                 RenderMimeFunc Render;
364                 Render = (RenderMimeFunc)vHdr;
365                 Render(Msg->MsgBody, NULL, FoundCharset);
366         }
367
368         if (StrLength(Msg->reply_references)> 0) {
369                 /* Trim down excessively long lists of thread references.  We eliminate the
370                  * second one in the list so that the thread root remains intact.
371                  */
372                 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
373                 int rrlen = StrLength(Msg->reply_references);
374                 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
375                         remove_token(reply_references, 1, '|');////todo
376                 }
377         }
378
379         /* Generate a reply-to address */
380         if (StrLength(Msg->Rfca) > 0) {
381                 if (Msg->reply_to == NULL)
382                         Msg->reply_to = NewStrBuf();
383                 if (StrLength(Msg->from) > 0) {
384                         StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
385                 }
386                 else {
387                         FlushStrBuf(Msg->reply_to);
388                         StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
389                 }
390         }
391         else 
392         {
393                 if ((StrLength(Msg->OtherNode)>0) && 
394                     (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_nodename)) &&
395                     (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_humannode)) ) 
396                 {
397                         if (Msg->reply_to == NULL)
398                                 Msg->reply_to = NewStrBuf();
399                         StrBufPrintf(Msg->reply_to, 
400                                      "%s @ %s",
401                                      ChrPtr(Msg->from), 
402                                      ChrPtr(Msg->OtherNode));
403                 }
404                 else {
405                         if (Msg->reply_to == NULL)
406                                 Msg->reply_to = NewStrBuf();
407                         FlushStrBuf(Msg->reply_to);
408                         StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
409                 }
410         }
411         it = GetNewHashPos();
412         while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) && 
413                (vMime != NULL)) {
414                 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
415                 evaluate_mime_part(Msg, Mime);
416         }
417         DeleteHashPos(&it);
418
419         DoTemplate(tmpl, tmpllen, Target, Msg, CTX_MAILSUM);
420
421         DestroyMessageSummary(Msg);
422         FreeStrBuf(&FoundCharset);
423         FreeStrBuf(&HdrToken);
424         FreeStrBuf(&Buf);
425         return 1;
426 }
427
428
429
430 /*
431  * Unadorned HTML output of an individual message, suitable
432  * for placing in a hidden iframe, for printing, or whatever
433  *
434  * msgnum_as_string == Message number, as a string instead of as a long int
435  */
436 void embed_message(void) {
437         long msgnum = 0L;
438         const StrBuf *Tmpl = sbstr("template");
439
440         msgnum = StrTol(WC->UrlFragment1);
441         if (StrLength(Tmpl) > 0) 
442                 read_message(WC->WBuf, SKEY(Tmpl), msgnum, 0, NULL);
443         else 
444                 read_message(WC->WBuf, HKEY("view_message"), msgnum, 0, NULL);
445 }
446
447
448 /*
449  * Printable view of a message
450  *
451  * msgnum_as_string == Message number, as a string instead of as a long int
452  */
453 void print_message(void) {
454         long msgnum = 0L;
455
456         msgnum = StrTol(WC->UrlFragment1);
457         output_headers(0, 0, 0, 0, 0, 0);
458
459         hprintf("Content-type: text/html\r\n"
460                 "Server: " PACKAGE_STRING "\r\n"
461                 "Connection: close\r\n");
462
463         begin_burst();
464
465         read_message(WC->WBuf, HKEY("view_message_print"), msgnum, 1, NULL);
466
467         wDumpContent(0);
468 }
469
470 /* 
471  * Mobile browser view of message
472  *
473  * @param msg_num_as_string Message number as a string instead of as a long int 
474  */
475 void mobile_message_view(void) {
476   long msgnum = 0L;
477   msgnum = StrTol(WC->UrlFragment1);
478   output_headers(1, 0, 0, 0, 0, 1);
479   begin_burst();
480   do_template("msgcontrols", NULL);
481   read_message(WC->WBuf, HKEY("view_message"), msgnum,1, NULL);
482   wDumpContent(0);
483 }
484
485 /**
486  * \brief Display a message's headers
487  *
488  * \param msgnum_as_string Message number, as a string instead of as a long int
489  */
490 void display_headers(void) {
491         long msgnum = 0L;
492         char buf[1024];
493
494         msgnum = StrTol(WC->UrlFragment1);
495         output_headers(0, 0, 0, 0, 0, 0);
496
497         hprintf("Content-type: text/plain\r\n"
498                 "Server: %s\r\n"
499                 "Connection: close\r\n",
500                 PACKAGE_STRING);
501         begin_burst();
502
503         serv_printf("MSG2 %ld|3", msgnum);
504         serv_getln(buf, sizeof buf);
505         if (buf[0] == '1') {
506                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
507                         wprintf("%s\n", buf);
508                 }
509         }
510
511         wDumpContent(0);
512 }
513
514
515
516 /**
517  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
518  *      or 'reply quoted' operations.
519  *
520  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
521  *       in this function.  Doing so would throw a JavaScript error in the
522  *       'supplied text' argument to the editor.
523  *
524  * \param msgnum Message number of the message we want to quote
525  * \param forward_attachments Nonzero if we want attachments to be forwarded
526  */
527 void pullquote_message(long msgnum, int forward_attachments, int include_headers) {
528         struct wcsession *WCC = WC;
529         char buf[SIZ];
530         char mime_partnum[256];
531         char mime_filename[256];
532         char mime_content_type[256];
533         char mime_charset[256];
534         char mime_disposition[256];
535         int mime_length;
536         char *attachments = NULL;
537         char *ptr = NULL;
538         int num_attachments = 0;
539         wc_attachment *att;
540         char m_subject[1024];
541         char from[256];
542         char node[256];
543         char rfca[256];
544         char to[256];
545         char reply_to[512];
546         char now[256];
547         int format_type = 0;
548         int nhdr = 0;
549         int bq = 0;
550         int i = 0;
551 #ifdef HAVE_ICONV
552         iconv_t ic = (iconv_t)(-1) ;
553         char *ibuf;                /**< Buffer of characters to be converted */
554         char *obuf;                /**< Buffer for converted characters      */
555         size_t ibuflen;    /**< Length of input buffer         */
556         size_t obuflen;    /**< Length of output buffer       */
557         char *osav;                /**< Saved pointer to output buffer       */
558 #endif
559
560         strcpy(from, "");
561         strcpy(node, "");
562         strcpy(rfca, "");
563         strcpy(reply_to, "");
564         strcpy(mime_content_type, "text/plain");
565         strcpy(mime_charset, "us-ascii");
566
567         serv_printf("MSG4 %ld", msgnum);
568         serv_getln(buf, sizeof buf);
569         if (buf[0] != '1') {
570                 wprintf(_("ERROR:"));
571                 wprintf("%s<br />", &buf[4]);
572                 return;
573         }
574
575         strcpy(m_subject, "");
576
577         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
578                 if (!strcmp(buf, "000")) {
579                         wprintf("%s (3)", _("unexpected end of message"));
580                         return;
581                 }
582                 if (include_headers) {
583                         if (!strncasecmp(buf, "nhdr=yes", 8))
584                                 nhdr = 1;
585                         if (nhdr == 1)
586                                 buf[0] = '_';
587                         if (!strncasecmp(buf, "type=", 5))
588                                 format_type = atoi(&buf[5]);
589                         if (!strncasecmp(buf, "from=", 5)) {
590                                 strcpy(from, &buf[5]);
591                                 wprintf(_("from "));
592                                 utf8ify_rfc822_string(from);
593                                 msgescputs(from);
594                         }
595                         if (!strncasecmp(buf, "subj=", 5)) {
596                                 strcpy(m_subject, &buf[5]);
597                         }
598                         if ((!strncasecmp(buf, "hnod=", 5))
599                             && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
600                                 wprintf("(%s) ", &buf[5]);
601                         }
602                         if ((!strncasecmp(buf, "room=", 5))
603                             && (strcasecmp(&buf[5], WC->wc_roomname))
604                             && (!IsEmptyStr(&buf[5])) ) {
605                                 wprintf(_("in "));
606                                 wprintf("%s&gt; ", &buf[5]);
607                         }
608                         if (!strncasecmp(buf, "rfca=", 5)) {
609                                 strcpy(rfca, &buf[5]);
610                                 wprintf("&lt;");
611                                 msgescputs(rfca);
612                                 wprintf("&gt; ");
613                         }
614                         if (!strncasecmp(buf, "node=", 5)) {
615                                 strcpy(node, &buf[5]);
616                                 if ( ((WC->room_flags & QR_NETWORK)
617                                 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
618                                 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
619                                 && (IsEmptyStr(rfca))
620                                 ) {
621                                         wprintf("@%s ", &buf[5]);
622                                 }
623                         }
624                         if (!strncasecmp(buf, "rcpt=", 5)) {
625                                 wprintf(_("to "));
626                                 strcpy(to, &buf[5]);
627                                 utf8ify_rfc822_string(to);
628                                 wprintf("%s ", to);
629                         }
630                         if (!strncasecmp(buf, "time=", 5)) {
631                                 webcit_fmt_date(now, atol(&buf[5]), 0);
632                                 wprintf("%s ", now);
633                         }
634                 }
635
636                 /**
637                  * Save attachment info for later.  We can't start downloading them
638                  * yet because we're in the middle of a server transaction.
639                  */
640                 if (!strncasecmp(buf, "part=", 5)) {
641                         ptr = malloc( (strlen(buf) + ((attachments != NULL) ? strlen(attachments) : 0)) ) ;
642                         if (ptr != NULL) {
643                                 ++num_attachments;
644                                 sprintf(ptr, "%s%s\n",
645                                         ((attachments != NULL) ? attachments : ""),
646                                         &buf[5]
647                                 );
648                                 free(attachments);
649                                 attachments = ptr;
650                                 lprintf(9, "attachments=<%s>\n", attachments);
651                         }
652                 }
653
654         }
655
656         if (include_headers) {
657                 wprintf("<br>");
658
659                 utf8ify_rfc822_string(m_subject);
660                 if (!IsEmptyStr(m_subject)) {
661                         wprintf(_("Subject:"));
662                         wprintf(" ");
663                         msgescputs(m_subject);
664                         wprintf("<br />");
665                 }
666
667                 /**
668                  * Begin body
669                  */
670                 wprintf("<br />");
671         }
672
673         /**
674          * Learn the content type
675          */
676         strcpy(mime_content_type, "text/plain");
677         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
678                 if (!strcmp(buf, "000")) {
679                         wprintf("%s (4)", _("unexpected end of message"));
680                         goto ENDBODY;
681                 }
682                 if (!strncasecmp(buf, "Content-type: ", 14)) {
683                         int len;
684                         safestrncpy(mime_content_type, &buf[14],
685                                 sizeof(mime_content_type));
686                         for (i=0; i<strlen(mime_content_type); ++i) {
687                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
688                                         safestrncpy(mime_charset, &mime_content_type[i+8],
689                                                 sizeof mime_charset);
690                                 }
691                         }
692                         len = strlen(mime_content_type);
693                         for (i=0; i<len; ++i) {
694                                 if (mime_content_type[i] == ';') {
695                                         mime_content_type[i] = 0;
696                                         len = i - 1;
697                                 }
698                         }
699                         len = strlen(mime_charset);
700                         for (i=0; i<len; ++i) {
701                                 if (mime_charset[i] == ';') {
702                                         mime_charset[i] = 0;
703                                         len = i - 1;
704                                 }
705                         }
706                 }
707         }
708
709         /** Set up a character set conversion if we need to (and if we can) */
710 #ifdef HAVE_ICONV
711         if ( (strcasecmp(mime_charset, "us-ascii"))
712            && (strcasecmp(mime_charset, "UTF-8"))
713            && (strcasecmp(mime_charset, ""))
714         ) {
715                 ctdl_iconv_open("UTF-8", mime_charset, &ic);
716                 if (ic == (iconv_t)(-1) ) {
717                         lprintf(5, "%s:%d iconv_open(%s, %s) failed: %s\n",
718                                 __FILE__, __LINE__, "UTF-8", mime_charset, strerror(errno));
719                 }
720         }
721 #endif
722
723         /** Messages in legacy Citadel variformat get handled thusly... */
724         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
725                 pullquote_fmout();
726         }
727
728         /* Boring old 80-column fixed format text gets handled this way... */
729         else if (!strcasecmp(mime_content_type, "text/plain")) {
730                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
731                         int len;
732                         len = strlen(buf);
733                         if ((len > 0) && (buf[len-1] == '\n')) buf[--len] = 0;
734                         if ((len > 0) && (buf[len-1] == '\r')) buf[--len] = 0;
735
736 #ifdef HAVE_ICONV
737                         if (ic != (iconv_t)(-1) ) {
738                                 ibuf = buf;
739                                 ibuflen = len;
740                                 obuflen = SIZ;
741                                 obuf = (char *) malloc(obuflen);
742                                 osav = obuf;
743                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
744                                 osav[SIZ-obuflen] = 0;
745                                 safestrncpy(buf, osav, sizeof buf);
746                                 free(osav);
747                         }
748 #endif
749
750                         len = strlen(buf);
751                         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1]))) 
752                                 buf[--len] = 0;
753                         if ((bq == 0) &&
754                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
755                                 wprintf("<blockquote>");
756                                 bq = 1;
757                         } else if ((bq == 1) &&
758                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
759                                 wprintf("</blockquote>");
760                                 bq = 0;
761                         }
762                         wprintf("<tt>");
763                         url(buf, sizeof(buf));
764                         msgescputs1(buf);
765                         wprintf("</tt><br />");
766                 }
767                 wprintf("</i><br />");
768         }
769
770         /** HTML just gets escaped and stuffed back into the editor */
771         else if (!strcasecmp(mime_content_type, "text/html")) {
772                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
773                         strcat(buf, "\n");
774                         msgescputs(buf);
775                 }
776         }//// TODO: charset? utf8?
777
778         /** Unknown weirdness ... don't know how to handle this content type */
779         else {
780                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
781         }
782
783 ENDBODY:
784         /** end of body handler */
785
786         /*
787          * If there were attachments, we have to download them and insert them
788          * into the attachment chain for the forwarded message we are composing.
789          */
790         if ( (forward_attachments) && (num_attachments) ) {
791                 for (i=0; i<num_attachments; ++i) {
792                         extract_token(buf, attachments, i, '\n', sizeof buf);
793                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
794                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
795                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
796                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
797                         mime_length = extract_int(buf, 5);
798
799                         /*
800                          * tracing  ... uncomment if necessary
801                          *
802                          */
803                         lprintf(9, "fwd filename: %s\n", mime_filename);
804                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
805                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
806                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
807                         lprintf(9, "fwd length  : %d\n", mime_length);
808
809                         if ( (!strcasecmp(mime_disposition, "inline"))
810                            || (!strcasecmp(mime_disposition, "attachment")) ) {
811                 
812                                 int n;
813                                 char N[64];
814                                 /* Create an attachment struct from this mime part... */
815                                 att = malloc(sizeof(wc_attachment));
816                                 memset(att, 0, sizeof(wc_attachment));
817                                 att->length = mime_length;
818                                 att->content_type = NewStrBufPlain(mime_content_type, -1);
819                                 att->filename = NewStrBufPlain(mime_filename, -1);
820                                 att->data = load_mimepart(msgnum, mime_partnum);
821                 
822                                 if (WCC->attachments == NULL)
823                                         WCC->attachments = NewHash(1, NULL);
824                                 /* And add it to the list. */
825                                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
826                                 Put(WCC->attachments, N, n, att, free_attachment);
827                         }
828
829                 }
830         }
831
832 #ifdef HAVE_ICONV
833         if (ic != (iconv_t)(-1) ) {
834                 iconv_close(ic);
835         }
836 #endif
837
838         if (attachments != NULL) {
839                 free(attachments);
840         }
841 }
842
843
844
845
846 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum) 
847 {
848         void                 *vEval;
849         MsgPartEvaluatorFunc  Eval;
850         message_summary      *Msg;
851         StrBuf *Buf;
852         const char *buf;
853         const char *ebuf;
854         int nBuf;
855         long len;
856         
857         Buf = NewStrBuf();
858
859         serv_printf("MSG0 %ld|1", MsgNum);      /* ask for headers only */
860         
861         StrBuf_ServGetln(Buf);
862         if (GetServerStatus(Buf, NULL) == 1) {
863                 FreeStrBuf(&Buf);
864                 return NULL;
865         }
866
867         Msg = (message_summary*)malloc(sizeof(message_summary));
868         memset(Msg, 0, sizeof(message_summary));
869         while (len = StrBuf_ServGetln(Buf),
870                ((len != 3)  ||
871                 strcmp(ChrPtr(Buf), "000")== 0)){
872                 buf = ChrPtr(Buf);
873                 ebuf = strchr(ChrPtr(Buf), '=');
874                 nBuf = ebuf - buf;
875                 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
876                         Eval = (MsgPartEvaluatorFunc) vEval;
877                         StrBufCutLeft(Buf, nBuf + 1);
878                         Eval(Msg, Buf);
879                 }
880                 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
881         }
882         return Msg;
883 }
884
885
886 /**
887  * \brief display the adressbook overview
888  * \param msgnum the citadel message number
889  * \param alpha what????
890  */
891 void display_addressbook(long msgnum, char alpha) {
892         //char buf[SIZ];
893         /* char mime_partnum[SIZ]; */
894 /*      char mime_filename[SIZ]; */
895 /*      char mime_content_type[SIZ]; */
896         ///char mime_disposition[SIZ];
897         //int mime_length;
898         char vcard_partnum[SIZ];
899         char *vcard_source = NULL;
900         message_summary summ;////TODO: this will leak
901
902         memset(&summ, 0, sizeof(summ));
903         ///safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
904 ///Load Message headers
905 //      Msg = 
906         if (!IsEmptyStr(vcard_partnum)) {
907                 vcard_source = load_mimepart(msgnum, vcard_partnum);
908                 if (vcard_source != NULL) {
909
910                         /** Display the summary line */
911                         display_vcard(WC->WBuf, vcard_source, alpha, 0, NULL,msgnum);
912
913                         /** If it's my vCard I can edit it */
914                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
915                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
916                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
917                         ) {
918                                 wprintf("<a href=\"edit_vcard?"
919                                         "msgnum=%ld&partnum=%s\">",
920                                         msgnum, vcard_partnum);
921                                 wprintf("[%s]</a>", _("edit"));
922                         }
923
924                         free(vcard_source);
925                 }
926         }
927
928 }
929
930
931
932 /**
933  * \brief  If it's an old "Firstname Lastname" style record, try to convert it.
934  * \param namebuf name to analyze, reverse if nescessary
935  */
936 void lastfirst_firstlast(char *namebuf) {
937         char firstname[SIZ];
938         char lastname[SIZ];
939         int i;
940
941         if (namebuf == NULL) return;
942         if (strchr(namebuf, ';') != NULL) return;
943
944         i = num_tokens(namebuf, ' ');
945         if (i < 2) return;
946
947         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
948         remove_token(namebuf, i-1, ' ');
949         strcpy(firstname, namebuf);
950         sprintf(namebuf, "%s; %s", lastname, firstname);
951 }
952
953 /**
954  * \brief fetch what??? name
955  * \param msgnum the citadel message number
956  * \param namebuf where to put the name in???
957  */
958 void fetch_ab_name(message_summary *Msg, char *namebuf) {
959         char buf[SIZ];
960         char mime_partnum[SIZ];
961         char mime_filename[SIZ];
962         char mime_content_type[SIZ];
963         char mime_disposition[SIZ];
964         int mime_length;
965         char vcard_partnum[SIZ];
966         char *vcard_source = NULL;
967         int i, len;
968         message_summary summ;/// TODO this will lak
969
970         if (namebuf == NULL) return;
971         strcpy(namebuf, "");
972
973         memset(&summ, 0, sizeof(summ));
974         //////safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
975
976         sprintf(buf, "MSG0 %ld|0", Msg->msgnum);        /** unfortunately we need the mime info now */
977         serv_puts(buf);
978         serv_getln(buf, sizeof buf);
979         if (buf[0] != '1') return;
980
981         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
982                 if (!strncasecmp(buf, "part=", 5)) {
983                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
984                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
985                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
986                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
987                         mime_length = extract_int(&buf[5], 5);
988
989                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
990                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
991                                 strcpy(vcard_partnum, mime_partnum);
992                         }
993
994                 }
995         }
996
997         if (!IsEmptyStr(vcard_partnum)) {
998                 vcard_source = load_mimepart(Msg->msgnum, vcard_partnum);
999                 if (vcard_source != NULL) {
1000
1001                         /* Grab the name off the card */
1002                         display_vcard(WC->WBuf, vcard_source, 0, 0, namebuf, Msg->msgnum);
1003
1004                         free(vcard_source);
1005                 }
1006         }
1007
1008         lastfirst_firstlast(namebuf);
1009         striplt(namebuf);
1010         len = strlen(namebuf);
1011         for (i=0; i<len; ++i) {
1012                 if (namebuf[i] != ';') return;
1013         }
1014         strcpy(namebuf, _("(no name)"));
1015 }
1016
1017
1018
1019
1020
1021 /*
1022  * load message pointers from the server for a "read messages" operation
1023  *
1024  * servcmd:             the citadel command to send to the citserver
1025  * with_headers:        also include some of the headers with the message numbers (more expensive)
1026  */
1027 int load_msg_ptrs(char *servcmd, int with_headers)
1028 {
1029         StrBuf* FoundCharset = NULL;
1030         struct wcsession *WCC = WC;
1031         message_summary *Msg;
1032         StrBuf *Buf, *Buf2;
1033         ///char buf[1024];
1034         ///time_t datestamp;
1035         //char fullname[128];
1036         //char nodename[128];
1037         //char inetaddr[128];
1038         //char subject[1024];
1039         ///char *ptr;
1040         int nummsgs;
1041         ////int sbjlen;
1042         int maxload = 0;
1043         long len;
1044
1045         ////int num_summ_alloc = 0;
1046
1047         if (WCC->summ != NULL) {
1048                 if (WCC->summ != NULL)
1049                         DeleteHash(&WCC->summ);
1050         }
1051         WCC->summ = NewHash(1, Flathash);
1052         nummsgs = 0;
1053         maxload = 1000;/// TODO
1054         
1055         Buf = NewStrBuf();
1056         serv_puts(servcmd);
1057         StrBuf_ServGetln(Buf);
1058         if (GetServerStatus(Buf, NULL) != 1) {
1059                 FreeStrBuf(&Buf);
1060                 return (nummsgs);
1061         }
1062 // TODO                         if (with_headers) { //// TODO: Have Attachments?
1063         Buf2 = NewStrBuf();
1064         while (len = StrBuf_ServGetln(Buf),
1065                ((len != 3)  ||
1066                 strcmp(ChrPtr(Buf), "000")!= 0))
1067         {
1068                 if (nummsgs < maxload) {
1069                         Msg = (message_summary*)malloc(sizeof(message_summary));
1070                         memset(Msg, 0, sizeof(message_summary));
1071
1072                         Msg->msgnum = StrBufExtract_long(Buf, 0, '|');
1073                         Msg->date = StrBufExtract_long(Buf, 1, '|');
1074
1075                         Msg->from = NewStrBufPlain(NULL, StrLength(Buf));
1076                         StrBufExtract_token(Buf2, Buf, 2, '|');
1077                         if (StrLength(Buf2) != 0) {
1078                                 /** Handle senders with RFC2047 encoding */
1079                                 StrBuf_RFC822_to_Utf8(Msg->from, Buf2, WCC->DefaultCharset, FoundCharset);
1080                         }
1081                         
1082                         /** Nodename */
1083                         StrBufExtract_token(Buf2, Buf, 3, '|');
1084                         if ((StrLength(Buf2) !=0 ) &&
1085                             ( ((WCC->room_flags & QR_NETWORK)
1086                                || ((strcasecmp(ChrPtr(Buf2), serv_info.serv_nodename)
1087                                     && (strcasecmp(ChrPtr(Buf2), serv_info.serv_fqdn)))))))
1088                         {
1089                                 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
1090                                 StrBufAppendBuf(Msg->from, Buf2, 0);
1091                         }
1092
1093                         /** Not used:
1094                         StrBufExtract_token(Msg->inetaddr, Buf, 4, '|');
1095                         */
1096
1097                         Msg->subj = NewStrBufPlain(NULL, StrLength(Buf));
1098                         StrBufExtract_token(Buf2,  Buf, 5, '|');
1099                         if (StrLength(Buf2) == 0)
1100                                 StrBufAppendBufPlain(Msg->subj, _("(no subj)"), 0, -1);
1101                         else {
1102                                 StrBuf_RFC822_to_Utf8(Msg->subj, Buf2, WCC->DefaultCharset, FoundCharset);
1103                                 if ((StrLength(Msg->subj) > 75) && 
1104                                     (StrBuf_Utf8StrLen(Msg->subj) > 75)) {
1105                                         StrBuf_Utf8StrCut(Msg->subj, 72);
1106                                         StrBufAppendBufPlain(Msg->subj, HKEY("..."), 0);
1107                                 }
1108                         }
1109
1110
1111                         if ((StrLength(Msg->from) > 25) && 
1112                             (StrBuf_Utf8StrLen(Msg->from) > 25)) {
1113                                 StrBuf_Utf8StrCut(Msg->from, 23);
1114                                 StrBufAppendBufPlain(Msg->from, HKEY("..."), 0);
1115                         }
1116                         Put(WCC->summ, (const char*)&Msg->msgnum, sizeof(Msg->msgnum), Msg, DestroyMessageSummary);
1117                 }
1118                 nummsgs++;
1119         }
1120         FreeStrBuf(&Buf2);
1121         FreeStrBuf(&Buf);
1122         return (nummsgs);
1123 }
1124
1125
1126
1127
1128
1129
1130 /*
1131  * command loop for reading messages
1132  *
1133  * Set oper to "readnew" or "readold" or "readfwd" or "headers"
1134  */
1135 void readloop(char *oper)
1136 {
1137         void *vMsg;
1138         message_summary *Msg;
1139         char cmd[256] = "";
1140         char buf[SIZ];
1141         char old_msgs[SIZ];
1142         int a = 0;
1143         int b = 0;
1144         int nummsgs;
1145         long startmsg;
1146         int maxmsgs;
1147         long *displayed_msgs = NULL;
1148         int num_displayed = 0;
1149         int is_summary = 0;
1150         int is_addressbook = 0;
1151         int is_singlecard = 0;
1152         int is_calendar = 0;
1153         struct calview calv;
1154         int is_tasks = 0;
1155         int is_notes = 0;
1156         int is_bbview = 0;
1157         int lo, hi;
1158         int lowest_displayed = (-1);
1159         int highest_displayed = 0;
1160         addrbookent *addrbook = NULL;
1161         int num_ab = 0;
1162         const StrBuf *sortby = NULL;
1163         //SortByEnum 
1164         int SortBy = eRDate;
1165         const StrBuf *sortpref_value;
1166         int bbs_reverse = 0;
1167         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
1168         HashPos *at;
1169         const char *HashKey;
1170         long HKLen;
1171
1172         if (WCC->wc_view == VIEW_WIKI) {
1173                 sprintf(buf, "wiki?room=%s&page=home", WCC->wc_roomname);
1174                 http_redirect(buf);
1175                 return;
1176         }
1177
1178         startmsg = lbstr("startmsg");
1179         maxmsgs = ibstr("maxmsgs");
1180         is_summary = (ibstr("is_summary") && !WCC->is_mobile);
1181         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1182
1183         sortpref_value = get_room_pref("sort");
1184
1185         sortby = sbstr("sortby");
1186         if ( (!IsEmptyStr(ChrPtr(sortby))) && 
1187              (strcasecmp(ChrPtr(sortby), ChrPtr(sortpref_value)) != 0)) {
1188                 set_room_pref("sort", NewStrBufDup(sortby), 1);
1189                 sortpref_value = NULL;
1190                 sortpref_value = sortby;
1191         }
1192
1193         SortBy = StrToESort(sortpref_value);
1194         /* message board sort */
1195         if (SortBy == eReverse) {
1196                 bbs_reverse = 1;
1197         }
1198         else {
1199                 bbs_reverse = 0;
1200         }
1201
1202         output_headers(1, 1, 1, 0, 0, 0);
1203
1204         /*
1205          * When in summary mode, always show ALL messages instead of just
1206          * new or old.  Otherwise, show what the user asked for.
1207          */
1208         if (!strcmp(oper, "readnew")) {
1209                 strcpy(cmd, "MSGS NEW");
1210         }
1211         else if (!strcmp(oper, "readold")) {
1212                 strcpy(cmd, "MSGS OLD");
1213         }
1214         else if (!strcmp(oper, "do_search")) {
1215                 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
1216         }
1217         else {
1218                 strcpy(cmd, "MSGS ALL");
1219         }
1220
1221         if ((WCC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1) && !WCC->is_mobile) {
1222                 is_summary = 1;
1223                 if (!strcmp(oper, "do_search")) {
1224                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
1225                 }
1226                 else {
1227                         strcpy(cmd, "MSGS ALL");
1228                 }
1229         }
1230
1231         if ((WCC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1232                 is_addressbook = 1;
1233                 if (!strcmp(oper, "do_search")) {
1234                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
1235                 }
1236                 else {
1237                         strcpy(cmd, "MSGS ALL");
1238                 }
1239                 maxmsgs = 9999999;
1240         }
1241
1242         if (is_summary) {                       /**< fetch header summary */
1243                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
1244                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
1245                         (!strcmp(oper, "do_search") ? bstr("query") : "")
1246                 );
1247                 startmsg = 1;
1248                 maxmsgs = 9999999;
1249         } 
1250         if (WCC->is_mobile) {
1251                 maxmsgs = 20;
1252                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
1253                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
1254                         (!strcmp(oper, "do_search") ? bstr("query") : "")
1255                 );
1256                 SortBy =  eRDate;
1257         }
1258
1259         /*
1260          * Are we doing a summary view?  If so, we need to know old messages
1261          * and new messages, so we can do that pretty boldface thing for the
1262          * new messages.
1263          */
1264         strcpy(old_msgs, "");
1265         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
1266                 serv_puts("GTSN");
1267                 serv_getln(buf, sizeof buf);
1268                 if (buf[0] == '2') {
1269                         strcpy(old_msgs, &buf[4]);
1270                 }
1271         }
1272
1273         is_singlecard = ibstr("is_singlecard");
1274
1275         if (WCC->wc_default_view == VIEW_CALENDAR) {            /**< calendar */
1276                 is_calendar = 1;
1277                 strcpy(cmd, "MSGS ALL|||1");
1278                 maxmsgs = 32767;
1279                 parse_calendar_view_request(&calv);
1280         }
1281         if (WCC->wc_default_view == VIEW_TASKS) {               /**< tasks */
1282                 is_tasks = 1;
1283                 strcpy(cmd, "MSGS ALL");
1284                 maxmsgs = 32767;
1285         }
1286         if (WCC->wc_default_view == VIEW_NOTES) {               /**< notes */
1287                 is_notes = 1;
1288                 strcpy(cmd, "MSGS ALL");
1289                 maxmsgs = 32767;
1290         }
1291
1292         if (is_notes) {
1293                 wprintf("<div id=\"new_notes_here\"></div>\n");
1294         }
1295
1296         nummsgs = load_msg_ptrs(cmd, (is_summary || WCC->is_mobile));
1297         if (nummsgs == 0) {
1298
1299                 if ((!is_tasks) && (!is_calendar) && (!is_notes) && (!is_addressbook)) {
1300                         wprintf("<div align=\"center\"><br /><em>");
1301                         if (!strcmp(oper, "readnew")) {
1302                                 wprintf(_("No new messages."));
1303                         } else if (!strcmp(oper, "readold")) {
1304                                 wprintf(_("No old messages."));
1305                         } else {
1306                                 wprintf(_("No messages here."));
1307                         }
1308                         wprintf("</em><br /></div>\n");
1309                 }
1310
1311                 goto DONE;
1312         }
1313
1314         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
1315                 void *vMsg;
1316                 message_summary *Msg;
1317
1318                 at = GetNewHashPos();
1319                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
1320                         /** Are you a new message, or an old message? */
1321                         Msg = (message_summary*) vMsg;
1322                         if (is_summary) {
1323                                 if (is_msg_in_mset(old_msgs, Msg->msgnum)) {
1324                                         Msg->is_new = 0;
1325                                 }
1326                                 else {
1327                                         Msg->is_new = 1;
1328                                 }
1329                         }
1330                 }
1331                 DeleteHashPos(&at);
1332         }
1333
1334         if (startmsg == 0L) {
1335                 if (bbs_reverse) {
1336                         startmsg = WCC->msgarr[(nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0];
1337                 }
1338                 else {
1339                         startmsg = WCC->msgarr[0];
1340                 }
1341         }
1342
1343         if (is_summary || WCC->is_mobile) {
1344                 SortByPayload(WCC->summ, SortFuncs[SortBy]);
1345         }
1346
1347         if (is_summary) {
1348
1349                 wprintf("<script language=\"javascript\" type=\"text/javascript\">"
1350                         " document.onkeydown = CtdlMsgListKeyPress;     "
1351                         " if (document.layers) {                        "
1352                         "       document.captureEvents(Event.KEYPRESS); "
1353                         " }                                             "
1354                         "</script>\n"
1355                 );
1356
1357                 /** note that Date and Delete are now in the same column */
1358                 wprintf("<div id=\"message_list_hdr\">"
1359                         "<div class=\"fix_scrollbar_bug\">"
1360                         "<table cellspacing=0 style=\"width:100%%\">"
1361                         "<tr>"
1362                 );
1363                 wprintf("<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> </th>\n"
1364                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> </th>\n"
1365                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> \n"
1366                         "&nbsp;"
1367                         "<input type=\"submit\" name=\"delete_button\" id=\"delbutton\" "
1368                         " onClick=\"CtdlDeleteSelectedMessages(event)\" "
1369                         " value=\"%s\">"
1370                         "</th>"
1371                         "</tr>\n"
1372                         ,
1373                         SUBJ_COL_WIDTH_PCT,
1374                         _("Subject"),
1375                         SortByStrings[SubjectInvertSortString[SortBy]],
1376                         SortIcons[SortSubjectToIcon[SortBy]],
1377                         SENDER_COL_WIDTH_PCT,
1378                         _("Sender"),
1379                         SortByStrings[SenderInvertSortString[SortBy]],
1380                         SortIcons[SortSenderToIcon[SortBy]],
1381                         DATE_PLUS_BUTTONS_WIDTH_PCT,
1382                         _("Date"),
1383                         SortByStrings[DateInvertSortString[SortBy]],
1384                         SortIcons[SortDateToIcon[SortBy]],
1385                         _("Delete")
1386                 );
1387                 wprintf("</table></div></div>\n");
1388                 wprintf("<div id=\"message_list\">"
1389
1390                         "<div class=\"fix_scrollbar_bug\">\n"
1391                         "<table class=\"mailbox_summary\" id=\"summary_headers\" "
1392                         "cellspacing=0 style=\"width:100%%;-moz-user-select:none;\">"
1393                 );
1394         } else if (WCC->is_mobile) {
1395                 wprintf("<div id=\"message_list\">");
1396         }
1397
1398
1399         /**
1400          * Set the "is_bbview" variable if it appears that we are looking at
1401          * a classic bulletin board view.
1402          */
1403         if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1404               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
1405                 is_bbview = 1;
1406         }
1407
1408         /**
1409          * If we're not currently looking at ALL requested
1410          * messages, then display the selector bar
1411          */
1412         if (is_bbview) {
1413                 /** begin bbview scroller */
1414                 wprintf("<form name=\"msgomatictop\" class=\"selector_top\" > \n <p>");
1415                 wprintf(_("Reading #"));//// TODO this isn't used, should it? : , lowest_displayed, highest_displayed);
1416
1417                 wprintf("<select name=\"whichones\" size=\"1\" "
1418                         "OnChange=\"location.href=msgomatictop.whichones.options"
1419                         "[selectedIndex].value\">\n");
1420
1421                 if (bbs_reverse) {
1422                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
1423                                 hi = b + 1;
1424                                 lo = b - maxmsgs + 2;
1425                                 if (lo < 1) lo = 1;
1426                                 wprintf("<option %s value="
1427                                         "\"%s"
1428                                         "&startmsg=%ld"
1429                                         "&maxmsgs=%d"
1430                                         "&is_summary=%d\">"
1431                                         "%d-%d</option> \n",
1432                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
1433                                         oper,
1434                                         WCC->msgarr[lo-1],
1435                                         maxmsgs,
1436                                         is_summary,
1437                                         hi, lo);
1438                         }
1439                 }
1440                 else {
1441                         for (b=0; b<nummsgs; b = b + maxmsgs) {
1442                                 lo = b + 1;
1443                                 hi = b + maxmsgs + 1;
1444                                 if (hi > nummsgs) hi = nummsgs;
1445                                 wprintf("<option %s value="
1446                                         "\"%s"
1447                                         "&startmsg=%ld"
1448                                         "&maxmsgs=%d"
1449                                         "&is_summary=%d\">"
1450                                         "%d-%d</option> \n",
1451                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
1452                                         oper,
1453                                         WCC->msgarr[lo-1],
1454                                         maxmsgs,
1455                                         is_summary,
1456                                         lo, hi);
1457                         }
1458                 }
1459
1460                 wprintf("<option value=\"%s?startmsg=%ld"
1461                         "&maxmsgs=9999999&is_summary=%d\">",
1462                         oper,
1463                         WCC->msgarr[0], is_summary);
1464                 wprintf(_("All"));
1465                 wprintf("</option>");
1466                 wprintf("</select> ");
1467                 wprintf(_("of %d messages."), nummsgs);
1468
1469                 /** forward/reverse */
1470                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
1471                         "OnChange=\"location.href='%s?sortby=forward'\"",  
1472                         (bbs_reverse ? "" : "checked"),
1473                         oper
1474                 );
1475                 wprintf(">");
1476                 wprintf(_("oldest to newest"));
1477                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
1478
1479                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
1480                         "OnChange=\"location.href='%s?sortby=reverse'\"", 
1481                         (bbs_reverse ? "checked" : ""),
1482                         oper
1483                 );
1484                 wprintf(">");
1485                 wprintf(_("newest to oldest"));
1486                 wprintf("\n");
1487         
1488                 wprintf("</p></form>\n");
1489                 /** end bbview scroller */
1490         }
1491                         
1492         at = GetNewHashPos();
1493         while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
1494                 Msg = (message_summary*) vMsg;          
1495                 if ((Msg->msgnum >= startmsg) && (num_displayed < maxmsgs)) {
1496                                 
1497                         /** Display the message */
1498                         if (is_summary) {
1499                                 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
1500                         }
1501                         else if (is_addressbook) {
1502                                 fetch_ab_name(Msg, buf);
1503                                 ++num_ab;
1504                                 addrbook = realloc(addrbook,
1505                                                    (sizeof(addrbookent) * num_ab) );
1506                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1507                                             sizeof(addrbook[num_ab-1].ab_name));
1508                                 addrbook[num_ab-1].ab_msgnum = Msg->msgnum;
1509                         }
1510                         else if (is_calendar) {
1511                                 load_calendar_item(Msg, Msg->is_new, &calv);
1512                         }
1513                         else if (is_tasks) {
1514                                 display_task(Msg, Msg->is_new);
1515                         }
1516                         else if (is_notes) {
1517                                 display_note(Msg, Msg->is_new);
1518                         } else if (WCC->is_mobile) {
1519                                 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
1520                         }
1521                         else {
1522                                 if (displayed_msgs == NULL) {
1523                                         displayed_msgs = malloc(sizeof(long) *
1524                                                                 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
1525                                 }
1526                                 displayed_msgs[num_displayed] = Msg->msgnum;
1527                         }
1528                         
1529                         if (lowest_displayed < 0) lowest_displayed = a;
1530                         highest_displayed = a;
1531                         
1532                         ++num_displayed;
1533                 }
1534         }
1535         DeleteHashPos(&at);
1536
1537         /** Output loop */
1538         if (displayed_msgs != NULL) {
1539                 if (bbs_reverse) {
1540                         qsort(displayed_msgs, num_displayed, sizeof(long), qlongcmp_r);
1541                 }
1542
1543                 /** if we do a split bbview in the future, begin messages div here */
1544
1545                 for (a=0; a<num_displayed; ++a) {
1546                         read_message(WC->WBuf, HKEY("view_message"), displayed_msgs[a], 0, NULL);
1547                 }
1548
1549                 /** if we do a split bbview in the future, end messages div here */
1550
1551                 free(displayed_msgs);
1552                 displayed_msgs = NULL;
1553         }
1554
1555         if (is_summary) {
1556                 wprintf("</table>"
1557                         "</div>\n");                    /**< end of 'fix_scrollbar_bug' div */
1558                 wprintf("</div>");                      /**< end of 'message_list' div */
1559                 
1560                 /** Here's the grab-it-to-resize-the-message-list widget */
1561                 wprintf("<div id=\"resize_msglist\" "
1562                         "onMouseDown=\"CtdlResizeMsgListMouseDown(event)\">"
1563                         "<div class=\"fix_scrollbar_bug\"> <hr>"
1564                         "</div></div>\n"
1565                 );
1566
1567                 wprintf("<div id=\"preview_pane\">");   /**< The preview pane will initially be empty */
1568         } else if (WCC->is_mobile) {
1569                 wprintf("</div>");
1570         }
1571
1572         /**
1573          * Bump these because although we're thinking in zero base, the user
1574          * is a drooling idiot and is thinking in one base.
1575          */
1576         ++lowest_displayed;
1577         ++highest_displayed;
1578
1579         /**
1580          * If we're not currently looking at ALL requested
1581          * messages, then display the selector bar
1582          */
1583         if (is_bbview) {
1584                 /** begin bbview scroller */
1585                 wprintf("<form name=\"msgomatic\" class=\"selector_bottom\" > \n <p>");
1586                 wprintf(_("Reading #")); /// TODO: this isn't used: , lowest_displayed, highest_displayed);
1587
1588                 wprintf("<select name=\"whichones\" size=\"1\" "
1589                         "OnChange=\"location.href=msgomatic.whichones.options"
1590                         "[selectedIndex].value\">\n");
1591
1592                 if (bbs_reverse) {
1593                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
1594                                 hi = b + 1;
1595                                 lo = b - maxmsgs + 2;
1596                                 if (lo < 1) lo = 1;
1597                                 wprintf("<option %s value="
1598                                         "\"%s"
1599                                         "&startmsg=%ld"
1600                                         "&maxmsgs=%d"
1601                                         "&is_summary=%d\">"
1602                                         "%d-%d</option> \n",
1603                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
1604                                         oper,
1605                                         WCC->msgarr[lo-1],
1606                                         maxmsgs,
1607                                         is_summary,
1608                                         hi, lo);
1609                         }
1610                 }
1611                 else {
1612                         for (b=0; b<nummsgs; b = b + maxmsgs) {
1613                                 lo = b + 1;
1614                                 hi = b + maxmsgs + 1;
1615                                 if (hi > nummsgs) hi = nummsgs;
1616                                 wprintf("<option %s value="
1617                                         "\"%s"
1618                                         "&startmsg=%ld"
1619                                         "&maxmsgs=%d"
1620                                         "&is_summary=%d\">"
1621                                         "%d-%d</option> \n",
1622                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
1623                                         oper,
1624                                         WCC->msgarr[lo-1],
1625                                         maxmsgs,
1626                                         is_summary,
1627                                         lo, hi);
1628                         }
1629                 }
1630
1631                 wprintf("<option value=\"%s&startmsg=%ld"
1632                         "&maxmsgs=9999999&is_summary=%d\">",
1633                         oper,
1634                         WCC->msgarr[0], is_summary);
1635                 wprintf(_("All"));
1636                 wprintf("</option>");
1637                 wprintf("</select> ");
1638                 wprintf(_("of %d messages."), nummsgs);
1639
1640                 /** forward/reverse */
1641                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
1642                         "OnChange=\"location.href='%s&sortby=forward'\"",  
1643                         (bbs_reverse ? "" : "checked"),
1644                         oper
1645                 );
1646                 wprintf(">");
1647                 wprintf(_("oldest to newest"));
1648                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
1649                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
1650                         "OnChange=\"location.href='%s&sortby=reverse'\"", 
1651                         (bbs_reverse ? "checked" : ""),
1652                         oper
1653                 );
1654                 wprintf(">");
1655                 wprintf(_("newest to oldest"));
1656                 wprintf("\n");
1657
1658                 wprintf("</p></form>\n");
1659                 /** end bbview scroller */
1660         }
1661         
1662 DONE:
1663         if (is_tasks) {
1664                 do_tasks_view();        /** Render the task list */
1665         }
1666
1667         if (is_calendar) {
1668                 render_calendar_view(&calv);
1669         }
1670
1671         if (is_addressbook) {
1672                 do_addrbook_view(addrbook, num_ab);     /** Render the address book */
1673         }
1674
1675         /** Note: wDumpContent() will output one additional </div> tag. */
1676         wprintf("</div>\n");            /** end of 'content' div */
1677         wDumpContent(1);
1678
1679         /** free the summary */
1680         if (WCC->summ != NULL) {
1681                 DeleteHash(&WCC->summ);
1682         }
1683         if (addrbook != NULL) free(addrbook);
1684 }
1685
1686
1687 /*
1688  * Back end for post_message()
1689  * ... this is where the actual message gets transmitted to the server.
1690  */
1691 void post_mime_to_server(void) {
1692         struct wcsession *WCC = WC;
1693         char top_boundary[SIZ];
1694         char alt_boundary[SIZ];
1695         int is_multipart = 0;
1696         static int seq = 0;
1697         wc_attachment *att;
1698         char *encoded;
1699         size_t encoded_length;
1700         size_t encoded_strlen;
1701         char *txtmail = NULL;
1702
1703         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
1704                 serv_info.serv_fqdn,
1705                 getpid(),
1706                 ++seq
1707         );
1708         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
1709                 serv_info.serv_fqdn,
1710                 getpid(),
1711                 ++seq
1712         );
1713
1714         /* RFC2045 requires this, and some clients look for it... */
1715         serv_puts("MIME-Version: 1.0");
1716         serv_puts("X-Mailer: " PACKAGE_STRING);
1717
1718         /* If there are attachments, we have to do multipart/mixed */
1719         if (GetCount(WCC->attachments) > 0) {
1720                 is_multipart = 1;
1721         }
1722
1723         if (is_multipart) {
1724                 /* Remember, serv_printf() appends an extra newline */
1725                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
1726                 serv_printf("This is a multipart message in MIME format.\n");
1727                 serv_printf("--%s", top_boundary);
1728         }
1729
1730         /* Remember, serv_printf() appends an extra newline */
1731         serv_printf("Content-type: multipart/alternative; "
1732                 "boundary=\"%s\"\n", alt_boundary);
1733         serv_printf("This is a multipart message in MIME format.\n");
1734         serv_printf("--%s", alt_boundary);
1735
1736         serv_puts("Content-type: text/plain; charset=utf-8");
1737         serv_puts("Content-Transfer-Encoding: quoted-printable");
1738         serv_puts("");
1739         txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
1740         text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
1741         free(txtmail);
1742
1743         serv_printf("--%s", alt_boundary);
1744
1745         serv_puts("Content-type: text/html; charset=utf-8");
1746         serv_puts("Content-Transfer-Encoding: quoted-printable");
1747         serv_puts("");
1748         serv_puts("<html><body>\r\n");
1749         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
1750         serv_puts("</body></html>\r\n");
1751
1752         serv_printf("--%s--", alt_boundary);
1753         
1754         if (is_multipart) {
1755                 long len;
1756                 const char *Key; 
1757                 void *vAtt;
1758                 HashPos  *it;
1759
1760                 /* Add in the attachments */
1761                 it = GetNewHashPos();
1762                 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
1763                         att = (wc_attachment*)vAtt;
1764                         encoded_length = ((att->length * 150) / 100);
1765                         encoded = malloc(encoded_length);
1766                         if (encoded == NULL) break;
1767                         encoded_strlen = CtdlEncodeBase64(encoded, att->data, att->length, 1);
1768
1769                         serv_printf("--%s", top_boundary);
1770                         serv_printf("Content-type: %s", ChrPtr(att->content_type));
1771                         serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->filename));
1772                         serv_puts("Content-transfer-encoding: base64");
1773                         serv_puts("");
1774                         serv_write(encoded, encoded_strlen);
1775                         serv_puts("");
1776                         serv_puts("");
1777                         free(encoded);
1778                 }
1779                 serv_printf("--%s--", top_boundary);
1780                 DeleteHashPos(&it);
1781         }
1782
1783         serv_puts("000");
1784 }
1785
1786
1787 /*
1788  * Post message (or don't post message)
1789  *
1790  * Note regarding the "dont_post" variable:
1791  * A random value (actually, it's just a timestamp) is inserted as a hidden
1792  * field called "postseq" when the display_enter page is generated.  This
1793  * value is checked when posting, using the static variable dont_post.  If a
1794  * user attempts to post twice using the same dont_post value, the message is
1795  * discarded.  This prevents the accidental double-saving of the same message
1796  * if the user happens to click the browser "back" button.
1797  */
1798 void post_message(void)
1799 {
1800         char buf[1024];
1801         StrBuf *encoded_subject = NULL;
1802         static long dont_post = (-1L);
1803         wc_attachment *att;
1804         int is_anonymous = 0;
1805         const StrBuf *display_name = NULL;
1806         struct wcsession *WCC = WC;
1807         
1808         if (havebstr("force_room")) {
1809                 gotoroom(bstr("force_room"));
1810         }
1811
1812         if (havebstr("display_name")) {
1813                 display_name = sbstr("display_name");
1814                 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1815                         display_name = NULL;
1816                         is_anonymous = 1;
1817                 }
1818         }
1819
1820         if (WCC->upload_length > 0) {
1821                 const char *pch;
1822                 int n;
1823                 char N[64];
1824
1825                 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
1826                 /** There's an attachment.  Save it to this struct... */
1827                 att = malloc(sizeof(wc_attachment));
1828                 memset(att, 0, sizeof(wc_attachment));
1829                 att->length = WCC->upload_length;
1830                 att->content_type = NewStrBufPlain(WCC->upload_content_type, -1);
1831                 att->filename = NewStrBufPlain(WCC->upload_filename, -1);
1832                 
1833                 
1834                 if (WCC->attachments == NULL)
1835                         WCC->attachments = NewHash(1, NULL);
1836                 /* And add it to the list. */
1837                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1838                 Put(WCC->attachments, N, n, att, free_attachment);
1839
1840                 /**
1841                  * Mozilla sends a simple filename, which is what we want,
1842                  * but Satan's Browser sends an entire pathname.  Reduce
1843                  * the path to just a filename if we need to.
1844                  */
1845                 pch = strrchr(ChrPtr(att->filename), '/');
1846                 if (pch != NULL) {
1847                         StrBufCutLeft(att->filename, pch - ChrPtr(att->filename));
1848                 }
1849                 pch = strrchr(ChrPtr(att->filename), '\\');
1850                 if (pch != NULL) {
1851                         StrBufCutLeft(att->filename, pch - ChrPtr(att->filename));
1852                 }
1853
1854                 /**
1855                  * Transfer control of this memory from the upload struct
1856                  * to the attachment struct.
1857                  */
1858                 att->data = WCC->upload;
1859                 WCC->upload_length = 0;
1860                 WCC->upload = NULL;
1861                 display_enter();
1862                 return;
1863         }
1864
1865         if (havebstr("cancel_button")) {
1866                 sprintf(WCC->ImportantMessage, 
1867                         _("Cancelled.  Message was not posted."));
1868         } else if (havebstr("attach_button")) {
1869                 display_enter();
1870                 return;
1871         } else if (lbstr("postseq") == dont_post) {
1872                 sprintf(WCC->ImportantMessage, 
1873                         _("Automatically cancelled because you have already "
1874                         "saved this message."));
1875         } else {
1876                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1877                 const StrBuf *Recp = NULL; 
1878                 const StrBuf *Cc = NULL;
1879                 const StrBuf *Bcc = NULL;
1880                 const StrBuf *Wikipage = NULL;
1881                 const StrBuf *my_email_addr = NULL;
1882                 StrBuf *CmdBuf = NULL;;
1883                 StrBuf *references = NULL;
1884
1885                 if (havebstr("references"))
1886                 {
1887                         const StrBuf *ref = sbstr("references");
1888                         references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
1889                         lprintf(9, "Converting: %s\n", ChrPtr(references));
1890                         StrBufReplaceChars(references, '|', '!');
1891                         lprintf(9, "Converted: %s\n", ChrPtr(references));
1892                 }
1893                 if (havebstr("subject")) {
1894                         const StrBuf *Subj;
1895                         /*
1896                          * make enough room for the encoded string; 
1897                          * plus the QP header 
1898                          */
1899                         Subj = sbstr("subject");
1900                         
1901                         StrBufRFC2047encode(&encoded_subject, Subj);
1902                 }
1903                 Recp = sbstr("recp");
1904                 Cc = sbstr("cc");
1905                 Bcc = sbstr("bcc");
1906                 Wikipage = sbstr("wikipage");
1907                 my_email_addr = sbstr("my_email_addr");
1908                 
1909                 CmdBuf = NewStrBufPlain(NULL, 
1910                                         sizeof (CMD) + 
1911                                         StrLength(Recp) + 
1912                                         StrLength(encoded_subject) +
1913                                         StrLength(Cc) +
1914                                         StrLength(Bcc) + 
1915                                         StrLength(Wikipage) +
1916                                         StrLength(my_email_addr) + 
1917                                         StrLength(references));
1918
1919                 StrBufPrintf(CmdBuf, 
1920                              CMD,
1921                              ChrPtr(Recp),
1922                              is_anonymous,
1923                              ChrPtr(encoded_subject),
1924                              ChrPtr(display_name),
1925                              ChrPtr(Cc),
1926                              ChrPtr(Bcc),
1927                              ChrPtr(Wikipage),
1928                              ChrPtr(my_email_addr),
1929                              ChrPtr(references));
1930                 FreeStrBuf(&references);
1931
1932                 lprintf(9, "%s\n", CmdBuf);
1933                 serv_puts(ChrPtr(CmdBuf));
1934                 serv_getln(buf, sizeof buf);
1935                 FreeStrBuf(&CmdBuf);
1936                 FreeStrBuf(&encoded_subject);
1937                 if (buf[0] == '4') {
1938                         post_mime_to_server();
1939                         if (  (havebstr("recp"))
1940                            || (havebstr("cc"  ))
1941                            || (havebstr("bcc" ))
1942                         ) {
1943                                 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
1944                         }
1945                         else {
1946                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
1947                         }
1948                         dont_post = lbstr("postseq");
1949                 } else {
1950                         lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
1951                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
1952                         display_enter();
1953                         return;
1954                 }
1955         }
1956
1957         DeleteHash(&WCC->attachments);
1958
1959         /**
1960          *  We may have been supplied with instructions regarding the location
1961          *  to which we must return after posting.  If found, go there.
1962          */
1963         if (havebstr("return_to")) {
1964                 http_redirect(bstr("return_to"));
1965         }
1966         /**
1967          *  If we were editing a page in a wiki room, go to that page now.
1968          */
1969         else if (havebstr("wikipage")) {
1970                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
1971                 http_redirect(buf);
1972         }
1973         /**
1974          *  Otherwise, just go to the "read messages" loop.
1975          */
1976         else {
1977                 readloop("readnew");
1978         }
1979 }
1980
1981
1982
1983
1984 /**
1985  * \brief display the message entry screen
1986  */
1987 void display_enter(void)
1988 {
1989         char buf[SIZ];
1990         StrBuf *ebuf;
1991         long now;
1992         const StrBuf *display_name = NULL;
1993         /////wc_attachment *att;
1994         int recipient_required = 0;
1995         int subject_required = 0;
1996         int recipient_bad = 0;
1997         int is_anonymous = 0;
1998         long existing_page = (-1L);
1999         struct wcsession *WCC = WC;
2000
2001         now = time(NULL);
2002
2003         if (havebstr("force_room")) {
2004                 gotoroom(bstr("force_room"));
2005         }
2006
2007         display_name = sbstr("display_name");
2008         if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
2009                 display_name = NULL;
2010                 is_anonymous = 1;
2011         }
2012
2013         /** First test to see whether this is a room that requires recipients to be entered */
2014         serv_puts("ENT0 0");
2015         serv_getln(buf, sizeof buf);
2016
2017         if (!strncmp(buf, "570", 3)) {          /** 570 means that we need a recipient here */
2018                 recipient_required = 1;
2019         }
2020         else if (buf[0] != '2') {               /** Any other error means that we cannot continue */
2021                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
2022                 readloop("readnew");
2023                 return;
2024         }
2025
2026         /* Is the server strongly recommending that the user enter a message subject? */
2027         if ((buf[3] != '\0') && (buf[4] != '\0')) {
2028                 subject_required = extract_int(&buf[4], 1);
2029         }
2030
2031         /**
2032          * Are we perhaps in an address book view?  If so, then an "enter
2033          * message" command really means "add new entry."
2034          */
2035         if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
2036                 do_edit_vcard(-1, "", "", WCC->wc_roomname);
2037                 return;
2038         }
2039
2040         /*
2041          * Are we perhaps in a calendar room?  If so, then an "enter
2042          * message" command really means "add new calendar item."
2043          */
2044         if (WCC->wc_default_view == VIEW_CALENDAR) {
2045                 display_edit_event();
2046                 return;
2047         }
2048
2049         /*
2050          * Are we perhaps in a tasks view?  If so, then an "enter
2051          * message" command really means "add new task."
2052          */
2053         if (WCC->wc_default_view == VIEW_TASKS) {
2054                 display_edit_task();
2055                 return;
2056         }
2057
2058         /*
2059          * Otherwise proceed normally.
2060          * Do a custom room banner with no navbar...
2061          */
2062         output_headers(1, 1, 2, 0, 0, 0);
2063
2064 /*
2065         wprintf("<div id=\"banner\">\n");
2066         embed_room_banner(NULL, navbar_none);
2067         wprintf("</div>\n");
2068         wprintf("<div id=\"content\">\n"
2069                 "<div class=\"fix_scrollbar_bug message \">");
2070 */
2071         /* Now check our actual recipients if there are any */
2072         if (recipient_required) {
2073                 const StrBuf *Recp = NULL; 
2074                 const StrBuf *Cc = NULL;
2075                 const StrBuf *Bcc = NULL;
2076                 const StrBuf *Wikipage = NULL;
2077                 StrBuf *CmdBuf = NULL;;
2078                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
2079                 
2080                 Recp = sbstr("recp");
2081                 Cc = sbstr("cc");
2082                 Bcc = sbstr("bcc");
2083                 Wikipage = sbstr("wikipage");
2084                 
2085                 CmdBuf = NewStrBufPlain(NULL, 
2086                                         sizeof (CMD) + 
2087                                         StrLength(Recp) + 
2088                                         StrLength(display_name) +
2089                                         StrLength(Cc) +
2090                                         StrLength(Bcc) + 
2091                                         StrLength(Wikipage));
2092
2093                 StrBufPrintf(CmdBuf, 
2094                              CMD,
2095                              ChrPtr(Recp), 
2096                              is_anonymous,
2097                              ChrPtr(display_name),
2098                              ChrPtr(Cc), 
2099                              ChrPtr(Bcc), 
2100                              ChrPtr(Wikipage));
2101                 serv_puts(ChrPtr(CmdBuf));
2102                 serv_getln(buf, sizeof buf);
2103                 FreeStrBuf(&CmdBuf);
2104
2105                 if (!strncmp(buf, "570", 3)) {  /** 570 means we have an invalid recipient listed */
2106                         if (havebstr("recp") && 
2107                             havebstr("cc"  ) && 
2108                             havebstr("bcc" )) {
2109                                 recipient_bad = 1;
2110                         }
2111                 }
2112                 else if (buf[0] != '2') {       /** Any other error means that we cannot continue */
2113                         wprintf("<em>%s</em><br />\n", &buf[4]);/// -> important message
2114                         goto DONE;
2115                 }
2116         }
2117         svputlong("RCPTREQUIRED", recipient_required);
2118         svputlong("SUBJREQUIRED", recipient_required || subject_required);
2119         DoTemplate(HKEY("edit_message"), NULL, NULL, CTX_NONE);
2120         address_book_popup();
2121         wDumpContent(1);
2122
2123
2124         return;
2125
2126
2127         /** If we got this far, we can display the message entry screen. */
2128
2129         /* begin message entry screen */
2130         wprintf("<form "
2131                 "enctype=\"multipart/form-data\" "
2132                 "method=\"POST\" "
2133                 "accept-charset=\"UTF-8\" "
2134                 "action=\"post\" "
2135                 "name=\"enterform\""
2136                 ">\n");
2137         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n", now);
2138         if (WCC->wc_view == VIEW_WIKI) {
2139                 wprintf("<input type=\"hidden\" name=\"wikipage\" value=\"%s\">\n", bstr("wikipage"));
2140         }
2141         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"%s\">\n", bstr("return_to"));
2142         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
2143         wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
2144         escputs(WCC->wc_roomname);
2145         wprintf("\">\n");
2146         wprintf("<input type=\"hidden\" name=\"references\" value=\"");
2147         escputs(bstr("references"));
2148         wprintf("\">\n");
2149
2150         /** submit or cancel buttons */
2151         wprintf("<p class=\"send_edit_msg\">");
2152         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
2153         if (recipient_required) {
2154                 wprintf(_("Send message"));
2155         } else {
2156                 wprintf(_("Post message"));
2157         }
2158         wprintf("\">&nbsp;"
2159                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
2160         wprintf("</p>");
2161
2162         /** header bar */
2163
2164         wprintf("<img src=\"static/newmess3_24x.gif\" class=\"imgedit\">");
2165         wprintf("  ");  /** header bar */
2166         webcit_fmt_date(buf, now, 0);
2167         wprintf("%s", buf);
2168         wprintf("\n");  /** header bar */
2169
2170         wprintf("<table width=\"100%%\" class=\"edit_msg_table\">");
2171         wprintf("<tr>");
2172         wprintf("<th><label for=\"from_id\" > ");
2173         wprintf(_(" <I>from</I> "));
2174         wprintf("</label></th>");
2175
2176         wprintf("<td colspan=\"2\">");
2177
2178         /* Allow the user to select any of his valid screen names */
2179
2180         wprintf("<select name=\"display_name\" size=1 id=\"from_id\">\n");
2181
2182         serv_puts("GVSN");
2183         serv_getln(buf, sizeof buf);
2184         if (buf[0] == '1') {
2185                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2186                         wprintf("<option %s value=\"",
2187                                 ((!strcasecmp(bstr("display_name"), buf)) ? "selected" : "")
2188                         );
2189                         escputs(buf);
2190                         wprintf("\">");
2191                         escputs(buf);
2192                         wprintf("</option>\n");
2193                 }
2194         }
2195
2196         if (WCC->room_flags & QR_ANONOPT) {
2197                 wprintf("<option %s value=\"__ANONYMOUS__\">%s</option>\n",
2198                         ((!strcasecmp(bstr("__ANONYMOUS__"), WCC->wc_fullname)) ? "selected" : ""),
2199                         _("Anonymous")
2200                 );
2201         }
2202
2203         wprintf("</select>\n");
2204
2205         /* If this is an email (not a post), allow the user to select any of his
2206          * valid email addresses.
2207          */
2208         if (recipient_required) {
2209                 serv_puts("GVEA");
2210                 serv_getln(buf, sizeof buf);
2211                 if (buf[0] == '1') {
2212                         wprintf("<select name=\"my_email_addr\" size=1>\n");
2213                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2214                                 wprintf("<option value=\"");
2215                                 escputs(buf);
2216                                 wprintf("\">&lt;");
2217                                 escputs(buf);
2218                                 wprintf("&gt;</option>\n");
2219                         }
2220                         wprintf("</select>\n");
2221                 }
2222         }
2223
2224         wprintf(_(" <I>in</I> "));
2225         escputs(WCC->wc_roomname);
2226
2227         wprintf("</td></tr>");
2228
2229         if (recipient_required) {
2230                 char *ccraw;
2231                 char *copy;
2232                 size_t len;
2233                 wprintf("<tr><th><label for=\"recp_id\"> ");
2234                 wprintf(_("To:"));
2235                 wprintf("</label></th>"
2236                         "<td><input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_id\" value=\"");
2237                 ccraw = xbstr("recp", &len);
2238                 copy = (char*) malloc(len * 2 + 1);
2239                 memcpy(copy, ccraw, len + 1); 
2240                 utf8ify_rfc822_string(copy);
2241                 escputs(copy);
2242                 free(copy);
2243                 wprintf("\" size=45 maxlength=1000 />");
2244                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
2245                 wprintf("</td><td rowspan=\"3\" align=\"left\" valign=\"top\">");
2246
2247                 /** Pop open an address book -- begin **/
2248                 wprintf(
2249                         "<a href=\"javascript:PopOpenAddressBook('recp_id|%s|cc_id|%s|bcc_id|%s');\" "
2250                         "title=\"%s\">"
2251                         "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
2252                         "&nbsp;%s</a>",
2253                         _("To:"), _("CC:"), _("BCC:"),
2254                         _("Contacts"), _("Contacts")
2255                 );
2256                 /** Pop open an address book -- end **/
2257
2258                 wprintf("</td></tr>");
2259
2260                 wprintf("<tr><th><label for=\"cc_id\"> ");
2261                 wprintf(_("CC:"));
2262                 wprintf("</label></th>"
2263                         "<td><input autocomplete=\"off\" type=\"text\" name=\"cc\" id=\"cc_id\" value=\"");
2264                 ccraw = xbstr("cc", &len);
2265                 copy = (char*) malloc(len * 2 + 1);
2266                 memcpy(copy, ccraw, len + 1); 
2267                 utf8ify_rfc822_string(copy);
2268                 escputs(copy);
2269                 free(copy);
2270                 wprintf("\" size=45 maxlength=1000 />");
2271                 wprintf("<div class=\"auto_complete\" id=\"cc_name_choices\"></div>");
2272                 wprintf("</td></tr>");
2273
2274                 wprintf("<tr><th><label for=\"bcc_id\"> ");
2275                 wprintf(_("BCC:"));
2276                 wprintf("</label></th>"
2277                         "<td><input autocomplete=\"off\" type=\"text\" name=\"bcc\" id=\"bcc_id\" value=\"");
2278                 ccraw = xbstr("bcc", &len);
2279                 copy = (char*) malloc(len * 2 + 1);
2280                 memcpy(copy, ccraw, len + 1); 
2281                 utf8ify_rfc822_string(copy);
2282                 escputs(copy);
2283                 free(copy);
2284                 wprintf("\" size=45 maxlength=1000 />");
2285                 wprintf("<div class=\"auto_complete\" id=\"bcc_name_choices\"></div>");
2286                 wprintf("</td></tr>");
2287
2288                 /** Initialize the autocomplete ajax helpers (found in wclib.js) */
2289                 wprintf("<script type=\"text/javascript\">      \n"
2290                         " activate_entmsg_autocompleters();     \n"
2291                         "</script>                              \n"
2292                 );
2293
2294         }
2295
2296         wprintf("<tr><th><label for=\"subject_id\" > ");
2297         if (recipient_required || subject_required) {
2298                 wprintf(_("Subject:"));
2299         }
2300         else {
2301                 wprintf(_("Subject (optional):"));
2302         }
2303         wprintf("</label></th>"
2304                 "<td colspan=\"2\">"
2305                 "<input type=\"text\" name=\"subject\" id=\"subject_id\" value=\"");
2306         escputs(bstr("subject"));
2307         wprintf("\" size=45 maxlength=70>\n");
2308         wprintf("</td></tr>");
2309
2310         wprintf("<tr><td colspan=\"3\">\n");
2311
2312         wprintf("<textarea name=\"msgtext\" cols=\"80\" rows=\"15\">");
2313
2314         /** If we're continuing from a previous edit, put our partially-composed message back... */
2315         msgescputs(bstr("msgtext"));
2316
2317         /* If we're forwarding a message, insert it here... */
2318         if (lbstr("fwdquote") > 0L) {
2319                 wprintf("<br><div align=center><i>");
2320                 wprintf(_("--- forwarded message ---"));
2321                 wprintf("</i></div><br>");
2322                 pullquote_message(lbstr("fwdquote"), 1, 1);
2323         }
2324
2325         /** If we're replying quoted, insert the quote here... */
2326         else if (lbstr("replyquote") > 0L) {
2327                 wprintf("<br>"
2328                         "<blockquote>");
2329                 pullquote_message(lbstr("replyquote"), 0, 1);
2330                 wprintf("</blockquote><br>");
2331         }
2332
2333         /** If we're editing a wiki page, insert the existing page here... */
2334         else if (WCC->wc_view == VIEW_WIKI) {
2335                 safestrncpy(buf, bstr("wikipage"), sizeof buf);
2336                 str_wiki_index(buf);
2337                 existing_page = locate_message_by_uid(buf);
2338                 if (existing_page >= 0L) {
2339                         pullquote_message(existing_page, 1, 0);
2340                 }
2341         }
2342
2343         /** Insert our signature if appropriate... */
2344         if ( (WCC->is_mailbox) && !yesbstr("sig_inserted") ) {
2345                 int UseSig;
2346                 get_pref_yesno("use_sig", &UseSig, 0);
2347                 if (UseSig) {
2348                         StrBuf *Sig;
2349                         const char *sig, *esig;
2350
2351                         get_preference("signature", &ebuf);
2352                         Sig = NewStrBuf();
2353                         StrBufEUid_unescapize(Sig, ebuf);
2354                         sig = ChrPtr(Sig);
2355                         esig = sig + StrLength(Sig);
2356                         wprintf("<br>--<br>");
2357                         while (sig <= esig) {
2358                                 if (*sig == '\n') {
2359                                         wprintf("<br>");
2360                                 }
2361                                 else if (*sig == '<') {
2362                                         wprintf("&lt;");
2363                                 }
2364                                 else if (*sig == '>') {
2365                                         wprintf("&gt;");
2366                                 }
2367                                 else if (*sig == '&') {
2368                                         wprintf("&amp;");
2369                                 }
2370                                 else if (*sig == '\"') {
2371                                         wprintf("&quot;");
2372                                 }
2373                                 else if (*sig == '\'') {
2374                                         wprintf("&#39;");
2375                                 }
2376                                 else /* since we're utf 8, is this a good idea? if (isprint(*sig))*/ {
2377                                         wprintf("%c", *sig);
2378                                 } 
2379                                 sig ++;
2380                         }
2381                         FreeStrBuf(&Sig);
2382                 }
2383         }
2384
2385         wprintf("</textarea>\n");
2386
2387         /** Make sure we only insert our signature once */
2388         /** We don't care if it was there or not before, it needs to be there now. */
2389         wprintf("<input type=\"hidden\" name=\"sig_inserted\" value=\"yes\">\n");
2390         
2391         /**
2392          * The following template embeds the TinyMCE richedit control, and automatically
2393          * transforms the textarea into a richedit textarea.
2394          */
2395         do_template("richedit", NULL);
2396
2397         /** Enumerate any attachments which are already in place... */
2398         wprintf("<div class=\"attachment buttons\"><img src=\"static/diskette_24x.gif\" class=\"imgedit\" > ");
2399         wprintf(_("Attachments:"));
2400         wprintf(" ");
2401         wprintf("<select name=\"which_attachment\" size=1>");
2402 /*
2403         for (att = WCC->first_attachment; att != NULL; att = att->next) {
2404                 wprintf("<option value=\"");
2405                 urlescputs(att->filename);
2406                 wprintf("\">");
2407                 escputs(att->filename);
2408                 / * wprintf(" (%s, %d bytes)",att->content_type,att->length); * /
2409                 wprintf("</option>\n");
2410         }
2411 */
2412         wprintf("</select>");
2413
2414         /** Now offer the ability to attach additional files... */
2415         wprintf("&nbsp;&nbsp;&nbsp;");
2416         wprintf(_("Attach file:"));
2417         wprintf(" <input name=\"attachfile\" class=\"attachfile\" "
2418                 "size=16 type=\"file\">\n&nbsp;&nbsp;"
2419                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
2420         wprintf("</div>");      /* End of "attachment buttons" div */
2421
2422
2423         wprintf("</td></tr></table>");
2424         
2425         wprintf("</form>\n");
2426         wprintf("</div>\n");    /* end of "fix_scrollbar_bug" div */
2427
2428         /* NOTE: address_book_popup() will close the "content" div.  Don't close it here. */
2429 DONE:   address_book_popup();
2430         wDumpContent(1);
2431 }
2432
2433
2434 /**
2435  * \brief delete a message
2436  */
2437 void delete_msg(void)
2438 {
2439         long msgid;
2440         char buf[SIZ];
2441
2442         msgid = lbstr("msgid");
2443
2444         if (WC->wc_is_trash) {  /** Delete from Trash is a real delete */
2445                 serv_printf("DELE %ld", msgid); 
2446         }
2447         else {                  /** Otherwise move it to Trash */
2448                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
2449         }
2450
2451         serv_getln(buf, sizeof buf);
2452         sprintf(WC->ImportantMessage, "%s", &buf[4]);
2453
2454         readloop("readnew");
2455 }
2456
2457
2458 /**
2459  * \brief move a message to another folder
2460  */
2461 void move_msg(void)
2462 {
2463         long msgid;
2464         char buf[SIZ];
2465
2466         msgid = lbstr("msgid");
2467
2468         if (havebstr("move_button")) {
2469                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
2470                 serv_puts(buf);
2471                 serv_getln(buf, sizeof buf);
2472                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
2473         } else {
2474                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
2475         }
2476
2477         readloop("readnew");
2478 }
2479
2480
2481
2482
2483
2484 /*
2485  * Confirm move of a message
2486  */
2487 void confirm_move_msg(void)
2488 {
2489         long msgid;
2490         char buf[SIZ];
2491         char targ[SIZ];
2492
2493         msgid = lbstr("msgid");
2494
2495
2496         output_headers(1, 1, 2, 0, 0, 0);
2497         wprintf("<div id=\"banner\">\n");
2498         wprintf("<h1>");
2499         wprintf(_("Confirm move of message"));
2500         wprintf("</h1>");
2501         wprintf("</div>\n");
2502
2503         wprintf("<div id=\"content\" class=\"service\">\n");
2504
2505         wprintf("<CENTER>");
2506
2507         wprintf(_("Move this message to:"));
2508         wprintf("<br />\n");
2509
2510         wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
2511         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
2512         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
2513
2514         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
2515         serv_puts("LKRA");
2516         serv_getln(buf, sizeof buf);
2517         if (buf[0] == '1') {
2518                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2519                         extract_token(targ, buf, 0, '|', sizeof targ);
2520                         wprintf("<OPTION>");
2521                         escputs(targ);
2522                         wprintf("\n");
2523                 }
2524         }
2525         wprintf("</SELECT>\n");
2526         wprintf("<br />\n");
2527
2528         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
2529         wprintf("&nbsp;");
2530         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
2531         wprintf("</form></CENTER>\n");
2532
2533         wprintf("</CENTER>\n");
2534         wDumpContent(1);
2535 }
2536
2537 void readnew(void) { readloop("readnew");}
2538 void readold(void) { readloop("readold");}
2539 void readfwd(void) { readloop("readfwd");}
2540 void headers(void) { readloop("headers");}
2541 void do_search(void) { readloop("do_search");}
2542
2543
2544
2545
2546
2547
2548 void 
2549 InitModule_MSG
2550 (void)
2551 {
2552         WebcitAddUrlHandler(HKEY("readnew"), readnew, 0);
2553         WebcitAddUrlHandler(HKEY("readold"), readold, 0);
2554         WebcitAddUrlHandler(HKEY("readfwd"), readfwd, 0);
2555         WebcitAddUrlHandler(HKEY("headers"), headers, 0);
2556         WebcitAddUrlHandler(HKEY("do_search"), do_search, 0);
2557         WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
2558         WebcitAddUrlHandler(HKEY("post"), post_message, 0);
2559         WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
2560         WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
2561         WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
2562         WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
2563         WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
2564         WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
2565         WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
2566
2567         return ;
2568 }