* abstract sorting algorithms. abstraction layer so far used in the message view.
[citadel.git] / webcit / messages.c
1 /*
2  * $Id$
3  *
4  * Functions which deal with the fetching and displaying of messages.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12 HashList *MsgHeaderHandler = NULL;
13 HashList *MsgEvaluators = NULL;
14 HashList *MimeRenderHandler = NULL;
15
16 #define SUBJ_COL_WIDTH_PCT              50      /**< Mailbox view column width */
17 #define SENDER_COL_WIDTH_PCT            30      /**< Mailbox view column width */
18 #define DATE_PLUS_BUTTONS_WIDTH_PCT     20      /**< Mailbox view column width */
19
20
21
22 void display_enter(void);
23 int longcmp_r(const void *s1, const void *s2);
24 int summcmp_subj(const void *s1, const void *s2);
25 int summcmp_rsubj(const void *s1, const void *s2);
26 int summcmp_sender(const void *s1, const void *s2);
27 int summcmp_rsender(const void *s1, const void *s2);
28 int summcmp_date(const void *s1, const void *s2);
29 int summcmp_rdate(const void *s1, const void *s2);
30
31 /*----------------------------------------------------------------------------*/
32
33
34 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
35
36
37
38 /*----------------------------------------------------------------------------*/
39
40
41
42 /*
43  * I wanna SEE that message!
44  *
45  * msgnum               Message number to display
46  * printable_view       Nonzero to display a printable view
47  * section              Optional for encapsulated message/rfc822 submessage
48  */
49 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, int printable_view, const StrBuf *PartNum) {
50         StrBuf *Buf;
51         StrBuf *HdrToken;
52         StrBuf *FoundCharset;
53         HashPos  *it;
54         void *vMime;
55         message_summary *Msg = NULL;
56         headereval *Hdr;
57         void *vHdr;
58         char buf[SIZ];
59 //      char mime_submessages[256] = "";
60         char reply_references[1024] = "";
61         int Done = 0;
62         int state=0;
63         long len;
64         const char *Key;
65
66         Buf = NewStrBuf();
67         lprintf(1, "----------%s---------MSG4 %ld|%s--------------\n", tmpl, msgnum, ChrPtr(PartNum));
68         serv_printf("MSG4 %ld|%s", msgnum, ChrPtr(PartNum));
69         StrBuf_ServGetln(Buf);
70         if (GetServerStatus(Buf, NULL) != 1) {
71                 StrBufAppendPrintf(Target, "<strong>");
72                 StrBufAppendPrintf(Target, _("ERROR:"));
73                 StrBufAppendPrintf(Target, "</strong> %s<br />\n", &buf[4]);
74                 FreeStrBuf(&Buf);
75                 return 0;
76         }
77
78         /** begin everythingamundo table */
79
80
81         HdrToken = NewStrBuf();
82         Msg = (message_summary *)malloc(sizeof(message_summary));
83         memset(Msg, 0, sizeof(message_summary));
84         Msg->msgnum = msgnum;
85         Msg->PartNum = PartNum;
86         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
87         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
88         FoundCharset = NewStrBuf();
89         while ((StrBuf_ServGetln(Buf)>=0) && !Done) {
90                 if ( (StrLength(Buf)==3) && 
91                     !strcmp(ChrPtr(Buf), "000")) 
92                 {
93                         Done = 1;
94                         if (state < 2) {
95                                 lprintf(1, _("unexpected end of message"));
96                                 
97                                 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
98                                 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
99                                 StrBufAppendPrintf(Msg->MsgBody->Data, _("unexpected end of message"));
100                                 StrBufAppendPrintf(Msg->MsgBody->Data, " (1)</i><br /><br />\n");
101                                 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
102                         }
103                         break;
104                 }
105                 switch (state) {
106                 case 0:/* Citadel Message Headers */
107                         if (StrLength(Buf) == 0) {
108                                 state ++;
109                                 break;
110                         }
111                         StrBufExtract_token(HdrToken, Buf, 0, '=');
112                         StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
113                         
114                         lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
115                         if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
116                             (vHdr != NULL)) {
117                                 Hdr = (headereval*)vHdr;
118                                 Hdr->evaluator(Msg, Buf, FoundCharset);
119                                 if (Hdr->Type == 1) {
120                                         state++;
121                                 }
122                         }
123                         else lprintf(1, "don't know how to handle message header[%s]\n", ChrPtr(HdrToken));
124                         break;
125                 case 1:/* Message Mime Header */
126                         if (StrLength(Buf) == 0) {
127                                 state++;
128                                 if (Msg->MsgBody->ContentType == NULL)
129                                         /* end of header or no header? */
130                                         Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
131                                  /* usual end of mime header */
132                         }
133                         else
134                         {
135                                 StrBufExtract_token(HdrToken, Buf, 0, ':');
136                                 if (StrLength(HdrToken) > 0) {
137                                         StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
138                                         lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
139                                         if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
140                                             (vHdr != NULL)) {
141                                                 Hdr = (headereval*)vHdr;
142                                                 Hdr->evaluator(Msg, Buf, FoundCharset);
143                                         }
144                                         break;
145                                 }
146                         }
147                 case 2: /* Message Body */
148                         
149                         if (Msg->MsgBody->size_known > 0) {
150                                 StrBuf_ServGetBLOB(Msg->MsgBody->Data, Msg->MsgBody->length);
151                                 state ++;
152                                         /// todo: check next line, if not 000, append following lines
153                         }
154                         else if (1){
155                                 if (StrLength(Msg->MsgBody->Data) > 0)
156                                         StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
157                                 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
158                         }
159                         break;
160                 case 3:
161                         StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
162                         break;
163                 }
164         }
165
166         if (Msg->AllAttach == NULL)
167                 Msg->AllAttach = NewHash(1,NULL);
168         Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
169         
170         /* strip the bare contenttype, so we ommit charset etc. */
171         StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
172         StrBufTrim(Buf);
173         if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
174             (vHdr != NULL)) {
175                 RenderMimeFunc Render;
176                 Render = (RenderMimeFunc)vHdr;
177                 Render(Msg->MsgBody, NULL, FoundCharset);
178         }
179
180         if (StrLength(Msg->reply_references)> 0) {
181                 /* Trim down excessively long lists of thread references.  We eliminate the
182                  * second one in the list so that the thread root remains intact.
183                  */
184                 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
185                 int rrlen = StrLength(Msg->reply_references);
186                 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
187                         remove_token(reply_references, 1, '|');////todo
188                 }
189         }
190
191         /* Generate a reply-to address */
192         if (StrLength(Msg->Rfca) > 0) {
193                 if (Msg->reply_to == NULL)
194                         Msg->reply_to = NewStrBuf();
195                 if (StrLength(Msg->from) > 0) {
196                         StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
197                 }
198                 else {
199                         FlushStrBuf(Msg->reply_to);
200                         StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
201                 }
202         }
203         else 
204         {
205                 if ((StrLength(Msg->OtherNode)>0) && 
206                     (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_nodename)) &&
207                     (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_humannode)) ) 
208                 {
209                         if (Msg->reply_to == NULL)
210                                 Msg->reply_to = NewStrBuf();
211                         StrBufPrintf(Msg->reply_to, 
212                                      "%s @ %s",
213                                      ChrPtr(Msg->from), 
214                                      ChrPtr(Msg->OtherNode));
215                 }
216                 else {
217                         if (Msg->reply_to == NULL)
218                                 Msg->reply_to = NewStrBuf();
219                         FlushStrBuf(Msg->reply_to);
220                         StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
221                 }
222         }
223         it = GetNewHashPos();
224         while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) && 
225                (vMime != NULL)) {
226                 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
227                 evaluate_mime_part(Msg, Mime);
228         }
229         DeleteHashPos(&it);
230
231         DoTemplate(tmpl, tmpllen, Target, Msg, CTX_MAILSUM);
232
233         DestroyMessageSummary(Msg);
234         FreeStrBuf(&FoundCharset);
235         FreeStrBuf(&HdrToken);
236         FreeStrBuf(&Buf);
237         return 1;
238 }
239
240
241
242 /*
243  * Unadorned HTML output of an individual message, suitable
244  * for placing in a hidden iframe, for printing, or whatever
245  *
246  * msgnum_as_string == Message number, as a string instead of as a long int
247  */
248 void embed_message(void) {
249         long msgnum = 0L;
250         const StrBuf *Tmpl = sbstr("template");
251
252         msgnum = StrTol(WC->UrlFragment1);
253         if (StrLength(Tmpl) > 0) 
254                 read_message(WC->WBuf, SKEY(Tmpl), msgnum, 0, NULL);
255         else 
256                 read_message(WC->WBuf, HKEY("view_message"), msgnum, 0, NULL);
257 }
258
259
260 /*
261  * Printable view of a message
262  *
263  * msgnum_as_string == Message number, as a string instead of as a long int
264  */
265 void print_message(void) {
266         long msgnum = 0L;
267
268         msgnum = StrTol(WC->UrlFragment1);
269         output_headers(0, 0, 0, 0, 0, 0);
270
271         hprintf("Content-type: text/html\r\n"
272                 "Server: " PACKAGE_STRING "\r\n"
273                 "Connection: close\r\n");
274
275         begin_burst();
276
277         read_message(WC->WBuf, HKEY("view_message_print"), msgnum, 1, NULL);
278
279         wDumpContent(0);
280 }
281
282 /* 
283  * Mobile browser view of message
284  *
285  * @param msg_num_as_string Message number as a string instead of as a long int 
286  */
287 void mobile_message_view(void) {
288   long msgnum = 0L;
289   msgnum = StrTol(WC->UrlFragment1);
290   output_headers(1, 0, 0, 0, 0, 1);
291   begin_burst();
292   do_template("msgcontrols", NULL);
293   read_message(WC->WBuf, HKEY("view_message"), msgnum,1, NULL);
294   wDumpContent(0);
295 }
296
297 /**
298  * \brief Display a message's headers
299  *
300  * \param msgnum_as_string Message number, as a string instead of as a long int
301  */
302 void display_headers(void) {
303         long msgnum = 0L;
304         char buf[1024];
305
306         msgnum = StrTol(WC->UrlFragment1);
307         output_headers(0, 0, 0, 0, 0, 0);
308
309         hprintf("Content-type: text/plain\r\n"
310                 "Server: %s\r\n"
311                 "Connection: close\r\n",
312                 PACKAGE_STRING);
313         begin_burst();
314
315         serv_printf("MSG2 %ld|3", msgnum);
316         serv_getln(buf, sizeof buf);
317         if (buf[0] == '1') {
318                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
319                         wprintf("%s\n", buf);
320                 }
321         }
322
323         wDumpContent(0);
324 }
325
326
327
328 /**
329  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
330  *      or 'reply quoted' operations.
331  *
332  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
333  *       in this function.  Doing so would throw a JavaScript error in the
334  *       'supplied text' argument to the editor.
335  *
336  * \param msgnum Message number of the message we want to quote
337  * \param forward_attachments Nonzero if we want attachments to be forwarded
338  */
339 void pullquote_message(long msgnum, int forward_attachments, int include_headers) {
340         struct wcsession *WCC = WC;
341         char buf[SIZ];
342         char mime_partnum[256];
343         char mime_filename[256];
344         char mime_content_type[256];
345         char mime_charset[256];
346         char mime_disposition[256];
347         int mime_length;
348         char *attachments = NULL;
349         char *ptr = NULL;
350         int num_attachments = 0;
351         wc_attachment *att;
352         char m_subject[1024];
353         char from[256];
354         char node[256];
355         char rfca[256];
356         char to[256];
357         char reply_to[512];
358         char now[256];
359         int format_type = 0;
360         int nhdr = 0;
361         int bq = 0;
362         int i = 0;
363 #ifdef HAVE_ICONV
364         iconv_t ic = (iconv_t)(-1) ;
365         char *ibuf;                /**< Buffer of characters to be converted */
366         char *obuf;                /**< Buffer for converted characters      */
367         size_t ibuflen;    /**< Length of input buffer         */
368         size_t obuflen;    /**< Length of output buffer       */
369         char *osav;                /**< Saved pointer to output buffer       */
370 #endif
371
372         strcpy(from, "");
373         strcpy(node, "");
374         strcpy(rfca, "");
375         strcpy(reply_to, "");
376         strcpy(mime_content_type, "text/plain");
377         strcpy(mime_charset, "us-ascii");
378
379         serv_printf("MSG4 %ld", msgnum);
380         serv_getln(buf, sizeof buf);
381         if (buf[0] != '1') {
382                 wprintf(_("ERROR:"));
383                 wprintf("%s<br />", &buf[4]);
384                 return;
385         }
386
387         strcpy(m_subject, "");
388
389         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
390                 if (!strcmp(buf, "000")) {
391                         wprintf("%s (3)", _("unexpected end of message"));
392                         return;
393                 }
394                 if (include_headers) {
395                         if (!strncasecmp(buf, "nhdr=yes", 8))
396                                 nhdr = 1;
397                         if (nhdr == 1)
398                                 buf[0] = '_';
399                         if (!strncasecmp(buf, "type=", 5))
400                                 format_type = atoi(&buf[5]);
401                         if (!strncasecmp(buf, "from=", 5)) {
402                                 strcpy(from, &buf[5]);
403                                 wprintf(_("from "));
404                                 utf8ify_rfc822_string(from);
405                                 msgescputs(from);
406                         }
407                         if (!strncasecmp(buf, "subj=", 5)) {
408                                 strcpy(m_subject, &buf[5]);
409                         }
410                         if ((!strncasecmp(buf, "hnod=", 5))
411                             && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
412                                 wprintf("(%s) ", &buf[5]);
413                         }
414                         if ((!strncasecmp(buf, "room=", 5))
415                             && (strcasecmp(&buf[5], WC->wc_roomname))
416                             && (!IsEmptyStr(&buf[5])) ) {
417                                 wprintf(_("in "));
418                                 wprintf("%s&gt; ", &buf[5]);
419                         }
420                         if (!strncasecmp(buf, "rfca=", 5)) {
421                                 strcpy(rfca, &buf[5]);
422                                 wprintf("&lt;");
423                                 msgescputs(rfca);
424                                 wprintf("&gt; ");
425                         }
426                         if (!strncasecmp(buf, "node=", 5)) {
427                                 strcpy(node, &buf[5]);
428                                 if ( ((WC->room_flags & QR_NETWORK)
429                                 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
430                                 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
431                                 && (IsEmptyStr(rfca))
432                                 ) {
433                                         wprintf("@%s ", &buf[5]);
434                                 }
435                         }
436                         if (!strncasecmp(buf, "rcpt=", 5)) {
437                                 wprintf(_("to "));
438                                 strcpy(to, &buf[5]);
439                                 utf8ify_rfc822_string(to);
440                                 wprintf("%s ", to);
441                         }
442                         if (!strncasecmp(buf, "time=", 5)) {
443                                 webcit_fmt_date(now, atol(&buf[5]), 0);
444                                 wprintf("%s ", now);
445                         }
446                 }
447
448                 /**
449                  * Save attachment info for later.  We can't start downloading them
450                  * yet because we're in the middle of a server transaction.
451                  */
452                 if (!strncasecmp(buf, "part=", 5)) {
453                         ptr = malloc( (strlen(buf) + ((attachments != NULL) ? strlen(attachments) : 0)) ) ;
454                         if (ptr != NULL) {
455                                 ++num_attachments;
456                                 sprintf(ptr, "%s%s\n",
457                                         ((attachments != NULL) ? attachments : ""),
458                                         &buf[5]
459                                 );
460                                 free(attachments);
461                                 attachments = ptr;
462                                 lprintf(9, "attachments=<%s>\n", attachments);
463                         }
464                 }
465
466         }
467
468         if (include_headers) {
469                 wprintf("<br>");
470
471                 utf8ify_rfc822_string(m_subject);
472                 if (!IsEmptyStr(m_subject)) {
473                         wprintf(_("Subject:"));
474                         wprintf(" ");
475                         msgescputs(m_subject);
476                         wprintf("<br />");
477                 }
478
479                 /**
480                  * Begin body
481                  */
482                 wprintf("<br />");
483         }
484
485         /**
486          * Learn the content type
487          */
488         strcpy(mime_content_type, "text/plain");
489         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
490                 if (!strcmp(buf, "000")) {
491                         wprintf("%s (4)", _("unexpected end of message"));
492                         goto ENDBODY;
493                 }
494                 if (!strncasecmp(buf, "Content-type: ", 14)) {
495                         int len;
496                         safestrncpy(mime_content_type, &buf[14],
497                                 sizeof(mime_content_type));
498                         for (i=0; i<strlen(mime_content_type); ++i) {
499                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
500                                         safestrncpy(mime_charset, &mime_content_type[i+8],
501                                                 sizeof mime_charset);
502                                 }
503                         }
504                         len = strlen(mime_content_type);
505                         for (i=0; i<len; ++i) {
506                                 if (mime_content_type[i] == ';') {
507                                         mime_content_type[i] = 0;
508                                         len = i - 1;
509                                 }
510                         }
511                         len = strlen(mime_charset);
512                         for (i=0; i<len; ++i) {
513                                 if (mime_charset[i] == ';') {
514                                         mime_charset[i] = 0;
515                                         len = i - 1;
516                                 }
517                         }
518                 }
519         }
520
521         /** Set up a character set conversion if we need to (and if we can) */
522 #ifdef HAVE_ICONV
523         if ( (strcasecmp(mime_charset, "us-ascii"))
524            && (strcasecmp(mime_charset, "UTF-8"))
525            && (strcasecmp(mime_charset, ""))
526         ) {
527                 ctdl_iconv_open("UTF-8", mime_charset, &ic);
528                 if (ic == (iconv_t)(-1) ) {
529                         lprintf(5, "%s:%d iconv_open(%s, %s) failed: %s\n",
530                                 __FILE__, __LINE__, "UTF-8", mime_charset, strerror(errno));
531                 }
532         }
533 #endif
534
535         /** Messages in legacy Citadel variformat get handled thusly... */
536         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
537                 pullquote_fmout();
538         }
539
540         /* Boring old 80-column fixed format text gets handled this way... */
541         else if (!strcasecmp(mime_content_type, "text/plain")) {
542                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
543                         int len;
544                         len = strlen(buf);
545                         if ((len > 0) && (buf[len-1] == '\n')) buf[--len] = 0;
546                         if ((len > 0) && (buf[len-1] == '\r')) buf[--len] = 0;
547
548 #ifdef HAVE_ICONV
549                         if (ic != (iconv_t)(-1) ) {
550                                 ibuf = buf;
551                                 ibuflen = len;
552                                 obuflen = SIZ;
553                                 obuf = (char *) malloc(obuflen);
554                                 osav = obuf;
555                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
556                                 osav[SIZ-obuflen] = 0;
557                                 safestrncpy(buf, osav, sizeof buf);
558                                 free(osav);
559                         }
560 #endif
561
562                         len = strlen(buf);
563                         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1]))) 
564                                 buf[--len] = 0;
565                         if ((bq == 0) &&
566                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
567                                 wprintf("<blockquote>");
568                                 bq = 1;
569                         } else if ((bq == 1) &&
570                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
571                                 wprintf("</blockquote>");
572                                 bq = 0;
573                         }
574                         wprintf("<tt>");
575                         url(buf, sizeof(buf));
576                         msgescputs1(buf);
577                         wprintf("</tt><br />");
578                 }
579                 wprintf("</i><br />");
580         }
581
582         /** HTML just gets escaped and stuffed back into the editor */
583         else if (!strcasecmp(mime_content_type, "text/html")) {
584                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
585                         strcat(buf, "\n");
586                         msgescputs(buf);
587                 }
588         }//// TODO: charset? utf8?
589
590         /** Unknown weirdness ... don't know how to handle this content type */
591         else {
592                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
593         }
594
595 ENDBODY:
596         /** end of body handler */
597
598         /*
599          * If there were attachments, we have to download them and insert them
600          * into the attachment chain for the forwarded message we are composing.
601          */
602         if ( (forward_attachments) && (num_attachments) ) {
603                 for (i=0; i<num_attachments; ++i) {
604                         extract_token(buf, attachments, i, '\n', sizeof buf);
605                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
606                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
607                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
608                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
609                         mime_length = extract_int(buf, 5);
610
611                         /*
612                          * tracing  ... uncomment if necessary
613                          *
614                          */
615                         lprintf(9, "fwd filename: %s\n", mime_filename);
616                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
617                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
618                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
619                         lprintf(9, "fwd length  : %d\n", mime_length);
620
621                         if ( (!strcasecmp(mime_disposition, "inline"))
622                            || (!strcasecmp(mime_disposition, "attachment")) ) {
623                 
624                                 int n;
625                                 char N[64];
626                                 /* Create an attachment struct from this mime part... */
627                                 att = malloc(sizeof(wc_attachment));
628                                 memset(att, 0, sizeof(wc_attachment));
629                                 att->length = mime_length;
630                                 att->content_type = NewStrBufPlain(mime_content_type, -1);
631                                 att->filename = NewStrBufPlain(mime_filename, -1);
632                                 att->data = load_mimepart(msgnum, mime_partnum);
633                 
634                                 if (WCC->attachments == NULL)
635                                         WCC->attachments = NewHash(1, NULL);
636                                 /* And add it to the list. */
637                                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
638                                 Put(WCC->attachments, N, n, att, free_attachment);
639                         }
640
641                 }
642         }
643
644 #ifdef HAVE_ICONV
645         if (ic != (iconv_t)(-1) ) {
646                 iconv_close(ic);
647         }
648 #endif
649
650         if (attachments != NULL) {
651                 free(attachments);
652         }
653 }
654
655
656
657
658 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum) 
659 {
660         void                 *vEval;
661         MsgPartEvaluatorFunc  Eval;
662         message_summary      *Msg;
663         StrBuf *Buf;
664         const char *buf;
665         const char *ebuf;
666         int nBuf;
667         long len;
668         
669         Buf = NewStrBuf();
670
671         serv_printf("MSG0 %ld|1", MsgNum);      /* ask for headers only */
672         
673         StrBuf_ServGetln(Buf);
674         if (GetServerStatus(Buf, NULL) == 1) {
675                 FreeStrBuf(&Buf);
676                 return NULL;
677         }
678
679         Msg = (message_summary*)malloc(sizeof(message_summary));
680         memset(Msg, 0, sizeof(message_summary));
681         while (len = StrBuf_ServGetln(Buf),
682                ((len != 3)  ||
683                 strcmp(ChrPtr(Buf), "000")== 0)){
684                 buf = ChrPtr(Buf);
685                 ebuf = strchr(ChrPtr(Buf), '=');
686                 nBuf = ebuf - buf;
687                 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
688                         Eval = (MsgPartEvaluatorFunc) vEval;
689                         StrBufCutLeft(Buf, nBuf + 1);
690                         Eval(Msg, Buf);
691                 }
692                 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
693         }
694         return Msg;
695 }
696
697
698 /**
699  * \brief display the adressbook overview
700  * \param msgnum the citadel message number
701  * \param alpha what????
702  */
703 void display_addressbook(long msgnum, char alpha) {
704         //char buf[SIZ];
705         /* char mime_partnum[SIZ]; */
706 /*      char mime_filename[SIZ]; */
707 /*      char mime_content_type[SIZ]; */
708         ///char mime_disposition[SIZ];
709         //int mime_length;
710         char vcard_partnum[SIZ];
711         char *vcard_source = NULL;
712         message_summary summ;////TODO: this will leak
713
714         memset(&summ, 0, sizeof(summ));
715         ///safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
716 ///Load Message headers
717 //      Msg = 
718         if (!IsEmptyStr(vcard_partnum)) {
719                 vcard_source = load_mimepart(msgnum, vcard_partnum);
720                 if (vcard_source != NULL) {
721
722                         /** Display the summary line */
723                         display_vcard(WC->WBuf, vcard_source, alpha, 0, NULL,msgnum);
724
725                         /** If it's my vCard I can edit it */
726                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
727                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
728                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
729                         ) {
730                                 wprintf("<a href=\"edit_vcard?"
731                                         "msgnum=%ld&partnum=%s\">",
732                                         msgnum, vcard_partnum);
733                                 wprintf("[%s]</a>", _("edit"));
734                         }
735
736                         free(vcard_source);
737                 }
738         }
739
740 }
741
742
743
744 /**
745  * \brief  If it's an old "Firstname Lastname" style record, try to convert it.
746  * \param namebuf name to analyze, reverse if nescessary
747  */
748 void lastfirst_firstlast(char *namebuf) {
749         char firstname[SIZ];
750         char lastname[SIZ];
751         int i;
752
753         if (namebuf == NULL) return;
754         if (strchr(namebuf, ';') != NULL) return;
755
756         i = num_tokens(namebuf, ' ');
757         if (i < 2) return;
758
759         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
760         remove_token(namebuf, i-1, ' ');
761         strcpy(firstname, namebuf);
762         sprintf(namebuf, "%s; %s", lastname, firstname);
763 }
764
765 /**
766  * \brief fetch what??? name
767  * \param msgnum the citadel message number
768  * \param namebuf where to put the name in???
769  */
770 void fetch_ab_name(message_summary *Msg, char *namebuf) {
771         char buf[SIZ];
772         char mime_partnum[SIZ];
773         char mime_filename[SIZ];
774         char mime_content_type[SIZ];
775         char mime_disposition[SIZ];
776         int mime_length;
777         char vcard_partnum[SIZ];
778         char *vcard_source = NULL;
779         int i, len;
780         message_summary summ;/// TODO this will lak
781
782         if (namebuf == NULL) return;
783         strcpy(namebuf, "");
784
785         memset(&summ, 0, sizeof(summ));
786         //////safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
787
788         sprintf(buf, "MSG0 %ld|0", Msg->msgnum);        /** unfortunately we need the mime info now */
789         serv_puts(buf);
790         serv_getln(buf, sizeof buf);
791         if (buf[0] != '1') return;
792
793         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
794                 if (!strncasecmp(buf, "part=", 5)) {
795                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
796                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
797                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
798                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
799                         mime_length = extract_int(&buf[5], 5);
800
801                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
802                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
803                                 strcpy(vcard_partnum, mime_partnum);
804                         }
805
806                 }
807         }
808
809         if (!IsEmptyStr(vcard_partnum)) {
810                 vcard_source = load_mimepart(Msg->msgnum, vcard_partnum);
811                 if (vcard_source != NULL) {
812
813                         /* Grab the name off the card */
814                         display_vcard(WC->WBuf, vcard_source, 0, 0, namebuf, Msg->msgnum);
815
816                         free(vcard_source);
817                 }
818         }
819
820         lastfirst_firstlast(namebuf);
821         striplt(namebuf);
822         len = strlen(namebuf);
823         for (i=0; i<len; ++i) {
824                 if (namebuf[i] != ';') return;
825         }
826         strcpy(namebuf, _("(no name)"));
827 }
828
829
830
831
832
833 /*
834  * load message pointers from the server for a "read messages" operation
835  *
836  * servcmd:             the citadel command to send to the citserver
837  * with_headers:        also include some of the headers with the message numbers (more expensive)
838  */
839 int load_msg_ptrs(char *servcmd, int with_headers)
840 {
841         StrBuf* FoundCharset = NULL;
842         struct wcsession *WCC = WC;
843         message_summary *Msg;
844         StrBuf *Buf, *Buf2;
845         ///char buf[1024];
846         ///time_t datestamp;
847         //char fullname[128];
848         //char nodename[128];
849         //char inetaddr[128];
850         //char subject[1024];
851         ///char *ptr;
852         int nummsgs;
853         ////int sbjlen;
854         int maxload = 0;
855         long len;
856         int n;
857         ////int num_summ_alloc = 0;
858
859         if (WCC->summ != NULL) {
860                 if (WCC->summ != NULL)
861                         DeleteHash(&WCC->summ);
862         }
863         WCC->summ = NewHash(1, Flathash);
864         nummsgs = 0;
865         maxload = 10000;
866         
867         Buf = NewStrBuf();
868         serv_puts(servcmd);
869         StrBuf_ServGetln(Buf);
870         if (GetServerStatus(Buf, NULL) != 1) {
871                 FreeStrBuf(&Buf);
872                 return (nummsgs);
873         }
874 // TODO                         if (with_headers) { //// TODO: Have Attachments?
875         Buf2 = NewStrBuf();
876         while (len = StrBuf_ServGetln(Buf),
877                ((len != 3)  ||
878                 strcmp(ChrPtr(Buf), "000")!= 0))
879         {
880                 if (nummsgs < maxload) {
881                         Msg = (message_summary*)malloc(sizeof(message_summary));
882                         memset(Msg, 0, sizeof(message_summary));
883
884                         Msg->msgnum = StrBufExtract_long(Buf, 0, '|');
885                         Msg->date = StrBufExtract_long(Buf, 1, '|');
886
887                         Msg->from = NewStrBufPlain(NULL, StrLength(Buf));
888                         StrBufExtract_token(Buf2, Buf, 2, '|');
889                         if (StrLength(Buf2) != 0) {
890                                 /** Handle senders with RFC2047 encoding */
891                                 StrBuf_RFC822_to_Utf8(Msg->from, Buf2, WCC->DefaultCharset, FoundCharset);
892                         }
893                         
894                         /** Nodename */
895                         StrBufExtract_token(Buf2, Buf, 3, '|');
896                         if ((StrLength(Buf2) !=0 ) &&
897                             ( ((WCC->room_flags & QR_NETWORK)
898                                || ((strcasecmp(ChrPtr(Buf2), serv_info.serv_nodename)
899                                     && (strcasecmp(ChrPtr(Buf2), serv_info.serv_fqdn)))))))
900                         {
901                                 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
902                                 StrBufAppendBuf(Msg->from, Buf2, 0);
903                         }
904
905                         /** Not used:
906                         StrBufExtract_token(Msg->inetaddr, Buf, 4, '|');
907                         */
908
909                         Msg->subj = NewStrBufPlain(NULL, StrLength(Buf));
910                         StrBufExtract_token(Buf2,  Buf, 5, '|');
911                         if (StrLength(Buf2) == 0)
912                                 StrBufAppendBufPlain(Msg->subj, _("(no subj)"), 0, -1);
913                         else {
914                                 StrBuf_RFC822_to_Utf8(Msg->subj, Buf2, WCC->DefaultCharset, FoundCharset);
915                                 if ((StrLength(Msg->subj) > 75) && 
916                                     (StrBuf_Utf8StrLen(Msg->subj) > 75)) {
917                                         StrBuf_Utf8StrCut(Msg->subj, 72);
918                                         StrBufAppendBufPlain(Msg->subj, HKEY("..."), 0);
919                                 }
920                         }
921
922
923                         if ((StrLength(Msg->from) > 25) && 
924                             (StrBuf_Utf8StrLen(Msg->from) > 25)) {
925                                 StrBuf_Utf8StrCut(Msg->from, 23);
926                                 StrBufAppendBufPlain(Msg->from, HKEY("..."), 0);
927                         }
928                         n = Msg->msgnum;
929                         Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
930                 }
931                 nummsgs++;
932         }
933         FreeStrBuf(&Buf2);
934         FreeStrBuf(&Buf);
935         return (nummsgs);
936 }
937
938
939 inline message_summary* GetMessagePtrAt(int n, HashList *Summ)
940 {
941         void *vMsg;
942         if (Summ == NULL)
943                 return NULL;
944         GetHash(Summ, (const char*)&n, sizeof(n), &vMsg);
945         return (message_summary*) vMsg;
946 }
947
948
949 /*
950  * command loop for reading messages
951  *
952  * Set oper to "readnew" or "readold" or "readfwd" or "headers"
953  */
954 void readloop(char *oper)
955 {
956         StrBuf *BBViewToolBar = NULL;
957         void *vMsg;
958         message_summary *Msg;
959         char cmd[256] = "";
960         char buf[SIZ];
961         char old_msgs[SIZ];
962         int a = 0;
963         int b = 0;
964         int n;
965         int nummsgs;
966         long startmsg;
967         int maxmsgs;
968         long *displayed_msgs = NULL;
969         int num_displayed = 0;
970         int is_summary = 0;
971         int is_addressbook = 0;
972         int is_singlecard = 0;
973         int is_calendar = 0;
974         struct calview calv;
975         int is_tasks = 0;
976         int is_notes = 0;
977         int is_bbview = 0;
978         int lo, hi;
979         int lowest_displayed = (-1);
980         int highest_displayed = 0;
981         addrbookent *addrbook = NULL;
982         int num_ab = 0;
983         int bbs_reverse = 0;
984         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
985         HashPos *at;
986         const char *HashKey;
987         long HKLen;
988         int care_for_empty_list = 0;
989         int load_seen = 0;
990         int sortit = 0;
991
992         switch (WCC->wc_view) {
993         case VIEW_WIKI:
994                 sprintf(buf, "wiki?room=%s&page=home", WCC->wc_roomname);
995                 http_redirect(buf);
996                 return;
997         case VIEW_CALENDAR:
998                 load_seen = 1;
999                 is_calendar = 1;
1000                 strcpy(cmd, "MSGS ALL|||1");
1001                 maxmsgs = 32767;
1002                 parse_calendar_view_request(&calv);
1003         case VIEW_TASKS:
1004                 is_tasks = 1;
1005                 strcpy(cmd, "MSGS ALL");
1006                 maxmsgs = 32767;
1007         case VIEW_NOTES:
1008                 is_notes = 1;
1009                 strcpy(cmd, "MSGS ALL");
1010                 maxmsgs = 32767;
1011                 wprintf("<div id=\"new_notes_here\"></div>\n");
1012         case VIEW_ADDRESSBOOK:
1013                 is_singlecard = ibstr("is_singlecard");
1014                 if (maxmsgs > 1) {
1015                         is_addressbook = 1;
1016                         if (!strcmp(oper, "do_search")) {
1017                                 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
1018                         }
1019                         else {
1020                                 strcpy(cmd, "MSGS ALL");
1021                         }
1022                         maxmsgs = 9999999;
1023                         break;
1024                 }
1025
1026         default:
1027                 care_for_empty_list = 1;
1028                 startmsg = lbstr("startmsg");
1029                 maxmsgs = ibstr("maxmsgs");
1030                 is_summary = (ibstr("is_summary") && !WCC->is_mobile);
1031                 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1032                 
1033
1034                 
1035                 /*
1036                  * When in summary mode, always show ALL messages instead of just
1037                  * new or old.  Otherwise, show what the user asked for.
1038                  */
1039                 if (!strcmp(oper, "readnew")) {
1040                         strcpy(cmd, "MSGS NEW");
1041                 }
1042                 else if (!strcmp(oper, "readold")) {
1043                         strcpy(cmd, "MSGS OLD");
1044                 }
1045                 else if (!strcmp(oper, "do_search")) {
1046                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
1047                 }
1048                 else {
1049                         strcpy(cmd, "MSGS ALL");
1050                 }
1051                 
1052                 if ((WCC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1) && !WCC->is_mobile) {
1053                         is_summary = 1;
1054                         if (!strcmp(oper, "do_search")) {
1055                                 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
1056                         }
1057                         else {
1058                                 strcpy(cmd, "MSGS ALL");
1059                         }
1060                 }
1061
1062                 is_bbview = !is_summary;
1063                 if (is_summary) {                       /**< fetch header summary */
1064                         load_seen = 1;
1065                         snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
1066                                  (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
1067                                  (!strcmp(oper, "do_search") ? bstr("query") : "")
1068                                 );
1069                         startmsg = 1;
1070                         maxmsgs = 9999999;
1071                 } 
1072                 if (startmsg == 0L) {
1073                         if (bbs_reverse) {
1074                                 Msg = GetMessagePtrAt((nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0, WCC->summ);
1075                                 startmsg = (Msg==NULL)? 0 : Msg->msgnum;
1076                         }
1077                         else {
1078                                 Msg = GetMessagePtrAt(0, WCC->summ);
1079                                 startmsg = (Msg==NULL)? 0 : Msg->msgnum;
1080                         }
1081                 }
1082                 sortit = is_summary || WCC->is_mobile;
1083         }
1084
1085
1086
1087
1088
1089
1090
1091         output_headers(1, 1, 1, 0, 0, 0);
1092
1093         /*
1094         if (WCC->is_mobile) {
1095                 maxmsgs = 20;
1096                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
1097                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
1098                         (!strcmp(oper, "do_search") ? bstr("query") : "")
1099                 );
1100                 SortBy =  eRDate;
1101         }
1102
1103         /*
1104          * Are we doing a summary view?  If so, we need to know old messages
1105          * and new messages, so we can do that pretty boldface thing for the
1106          * new messages.
1107          */
1108
1109
1110         nummsgs = load_msg_ptrs(cmd, (is_summary || WCC->is_mobile));
1111         if (nummsgs == 0) {
1112                 if (care_for_empty_list) {
1113                         wprintf("<div align=\"center\"><br /><em>");
1114                         if (!strcmp(oper, "readnew")) {
1115                                 wprintf(_("No new messages."));
1116                         } else if (!strcmp(oper, "readold")) {
1117                                 wprintf(_("No old messages."));
1118                         } else {
1119                                 wprintf(_("No messages here."));
1120                         }
1121                         wprintf("</em><br /></div>\n");
1122                 }
1123
1124                 goto DONE;
1125         }
1126
1127         if (load_seen){
1128                 void *vMsg;
1129
1130                 strcpy(old_msgs, "");
1131                 serv_puts("GTSN");
1132                 serv_getln(buf, sizeof buf);
1133                 if (buf[0] == '2') {
1134                         strcpy(old_msgs, &buf[4]);
1135                 }
1136                 at = GetNewHashPos();
1137                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
1138                         /** Are you a new message, or an old message? */
1139                         Msg = (message_summary*) vMsg;
1140                         if (is_summary) {
1141                                 if (is_msg_in_mset(old_msgs, Msg->msgnum)) {
1142                                         Msg->is_new = 0;
1143                                 }
1144                                 else {
1145                                         Msg->is_new = 1;
1146                                 }
1147                         }
1148                 }
1149                 DeleteHashPos(&at);
1150         }
1151
1152         if (sortit) {
1153                 CompareFunc SortIt;
1154                 SortIt =  RetrieveSort(CTX_MAILSUM, NULL, 
1155                                        HKEY("date"), 2);
1156                 if (SortIt != NULL)
1157                         SortByPayload(WCC->summ, SortIt);
1158         }
1159
1160         if (is_summary) {
1161                 do_template("summary_header", NULL);
1162         } else if (WCC->is_mobile) {
1163                 wprintf("<div id=\"message_list\">");
1164         }
1165
1166
1167         /**
1168          * If we're not currently looking at ALL requested
1169          * messages, then display the selector bar
1170          */
1171         if (is_bbview) {
1172                 const char *selected;
1173                 StrBuf *Selector = NewStrBuf();
1174                 BBViewToolBar = NewStrBuf();
1175 /////           DoTemplate("bbview_scrollbar");
1176                 /** begin bbview scroller */
1177                 StrBufAppendPrintf(BBViewToolBar, "<form name=\"msgomatictop\" class=\"selector_top\" > \n <p>");
1178                 StrBufAppendPrintf(BBViewToolBar, _("Reading #"));//// TODO this isn't used, should it? : , lowest_displayed, highest_displayed);
1179
1180                 StrBufAppendPrintf(BBViewToolBar, "<select name=\"whichones\" size=\"1\" "
1181                                    "OnChange=\"location.href=msgomatictop.whichones.options"
1182                                    "[selectedIndex].value\">\n");
1183
1184                 if (bbs_reverse) {
1185                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
1186                                 hi = b + 1;
1187                                 lo = b - maxmsgs + 2;
1188                                 if (lo < 1) lo = 1;
1189                                 
1190                                 Msg = GetMessagePtrAt(lo-1, WCC->summ);
1191                                 n = (Msg==NULL)? 0 : Msg->msgnum;
1192                                 selected = ((n == startmsg) ? "selected" : "");
1193                                 
1194                                 StrBufAppendPrintf(Selector, 
1195                                                    "<option %s value="
1196                                                    "\"%s"
1197                                                    "&startmsg=%ld"
1198                                                    "&maxmsgs=%d"
1199                                                    "&is_summary=%d\">"
1200                                                    "%d-%d</option> \n",
1201                                                    selected,
1202                                                    oper,
1203
1204
1205                                                    maxmsgs,
1206                                                    is_summary,
1207                                                    hi, lo);
1208                         }
1209                 }
1210                 else {
1211                         for (b=0; b<nummsgs; b = b + maxmsgs) {
1212                                 lo = b + 1;
1213                                 hi = b + maxmsgs + 1;
1214                                 if (hi > nummsgs) hi = nummsgs;
1215
1216                                 Msg = GetMessagePtrAt(b, WCC->summ);
1217                                 n = (Msg==NULL)? 0 : Msg->msgnum;
1218                                 selected = ((n == startmsg) ? "selected" : "");
1219                                 Msg = GetMessagePtrAt(lo-1,  WCC->summ);
1220                                 StrBufAppendPrintf(Selector, 
1221                                                    "<option %s value="
1222                                                    "\"%s"
1223                                                    "&startmsg=%ld"
1224                                                    "&maxmsgs=%d"
1225                                                    "&is_summary=%d\">"
1226                                                    "%d-%d</option> \n",
1227                                                    selected,
1228                                                    oper,
1229                                                    (Msg==NULL)? 0 : Msg->msgnum,
1230                                                    maxmsgs,
1231                                                    is_summary,
1232                                                    lo, hi);
1233                         }
1234                 }
1235
1236                 StrBufAppendBuf(BBViewToolBar, Selector, 0);
1237
1238                 Msg = GetMessagePtrAt(0,  WCC->summ);
1239
1240                 StrBufAppendPrintf(BBViewToolBar, "<option value=\"%s?startmsg=%ld"
1241                         "&maxmsgs=9999999&is_summary=0\">",
1242                         oper,
1243                         (Msg==NULL)? 0 : Msg->msgnum);
1244                 StrBufAppendPrintf(BBViewToolBar, _("All"));
1245
1246                 StrBufAppendPrintf(BBViewToolBar, "</option>");
1247                 StrBufAppendPrintf(BBViewToolBar, "</select> ");
1248                 StrBufAppendPrintf(BBViewToolBar, _("of %d messages."), nummsgs);
1249
1250                 /** forward/reverse */
1251                 StrBufAppendPrintf(BBViewToolBar, "<input type=\"radio\" %s name=\"direction\" value=\"\""
1252                         "OnChange=\"location.href='%s?sortby=forward'\"",  
1253                         (bbs_reverse ? "" : "checked"),
1254                         oper
1255                 );
1256                 StrBufAppendPrintf(BBViewToolBar, ">");
1257                 StrBufAppendPrintf(BBViewToolBar, _("oldest to newest"));
1258                 StrBufAppendPrintf(BBViewToolBar, "&nbsp;&nbsp;&nbsp;&nbsp;");
1259
1260                 StrBufAppendPrintf(BBViewToolBar, "<input type=\"radio\" %s name=\"direction\" value=\"\""
1261                         "OnChange=\"location.href='%s?sortby=reverse'\"", 
1262                         (bbs_reverse ? "checked" : ""),
1263                         oper
1264                 );
1265                 StrBufAppendPrintf(BBViewToolBar, ">");
1266                 StrBufAppendPrintf(BBViewToolBar, _("newest to oldest"));
1267                 StrBufAppendPrintf(BBViewToolBar, "\n");
1268         
1269                 StrBufAppendPrintf(BBViewToolBar, "</p></form>\n");
1270                 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
1271                 /** end bbview scroller */
1272         }
1273                         
1274         at = GetNewHashPos();
1275         while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
1276                 Msg = (message_summary*) vMsg;          
1277                 if ((Msg->msgnum >= startmsg) && (num_displayed < maxmsgs)) {
1278                                 
1279                         /** Display the message */
1280                         if (is_summary) {
1281                                 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
1282                         }
1283                         else if (is_addressbook) {
1284                                 fetch_ab_name(Msg, buf);
1285                                 ++num_ab;
1286                                 addrbook = realloc(addrbook,
1287                                                    (sizeof(addrbookent) * num_ab) );
1288                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1289                                             sizeof(addrbook[num_ab-1].ab_name));
1290                                 addrbook[num_ab-1].ab_msgnum = Msg->msgnum;
1291                         }
1292                         else if (is_calendar) {
1293                                 load_calendar_item(Msg, Msg->is_new, &calv);
1294                         }
1295                         else if (is_tasks) {
1296                                 display_task(Msg, Msg->is_new);
1297                         }
1298                         else if (is_notes) {
1299                                 display_note(Msg, Msg->is_new);
1300                         } else if (WCC->is_mobile) {
1301                                 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
1302                         }
1303                         else {
1304                                 if (displayed_msgs == NULL) {
1305                                         displayed_msgs = malloc(sizeof(long) *
1306                                                                 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
1307                                 }
1308                                 displayed_msgs[num_displayed] = Msg->msgnum;
1309                         }
1310                         
1311                         if (lowest_displayed < 0) lowest_displayed = a;
1312                         highest_displayed = a;
1313                         
1314                         ++num_displayed;
1315                 }
1316         }
1317         DeleteHashPos(&at);
1318
1319         /** Output loop */
1320         if (displayed_msgs != NULL) {
1321                 if (bbs_reverse) {
1322                         ////TODOqsort(displayed_msgs, num_displayed, sizeof(long), qlongcmp_r);
1323                 }
1324
1325                 /** if we do a split bbview in the future, begin messages div here */
1326
1327                 for (a=0; a<num_displayed; ++a) {
1328                         read_message(WC->WBuf, HKEY("view_message"), displayed_msgs[a], 0, NULL);
1329                 }
1330
1331                 /** if we do a split bbview in the future, end messages div here */
1332
1333                 free(displayed_msgs);
1334                 displayed_msgs = NULL;
1335         }
1336
1337         if (is_summary) {
1338                 wprintf("</table>"
1339                         "</div>\n");                    /**< end of 'fix_scrollbar_bug' div */
1340                 wprintf("</div>");                      /**< end of 'message_list' div */
1341                 
1342                 /** Here's the grab-it-to-resize-the-message-list widget */
1343                 wprintf("<div id=\"resize_msglist\" "
1344                         "onMouseDown=\"CtdlResizeMsgListMouseDown(event)\">"
1345                         "<div class=\"fix_scrollbar_bug\"> <hr>"
1346                         "</div></div>\n"
1347                 );
1348
1349                 wprintf("<div id=\"preview_pane\">");   /**< The preview pane will initially be empty */
1350         } else if (WCC->is_mobile) {
1351                 wprintf("</div>");
1352         }
1353
1354         /**
1355          * Bump these because although we're thinking in zero base, the user
1356          * is a drooling idiot and is thinking in one base.
1357          */
1358         ++lowest_displayed;
1359         ++highest_displayed;
1360
1361         /**
1362          * If we're not currently looking at ALL requested
1363          * messages, then display the selector bar
1364          */
1365         if (is_bbview) {
1366                 /** begin bbview scroller */
1367                 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
1368         }
1369         
1370 DONE:
1371         if (is_tasks) {
1372                 do_tasks_view();        /** Render the task list */
1373         }
1374
1375         if (is_calendar) {
1376                 render_calendar_view(&calv);
1377         }
1378
1379         if (is_addressbook) {
1380                 do_addrbook_view(addrbook, num_ab);     /** Render the address book */
1381         }
1382
1383         /** Note: wDumpContent() will output one additional </div> tag. */
1384         wprintf("</div>\n");            /** end of 'content' div */
1385         wDumpContent(1);
1386
1387         /** free the summary */
1388         if (WCC->summ != NULL) {
1389                 DeleteHash(&WCC->summ);
1390         }
1391         if (addrbook != NULL) free(addrbook);
1392 }
1393
1394
1395 /*
1396  * Back end for post_message()
1397  * ... this is where the actual message gets transmitted to the server.
1398  */
1399 void post_mime_to_server(void) {
1400         struct wcsession *WCC = WC;
1401         char top_boundary[SIZ];
1402         char alt_boundary[SIZ];
1403         int is_multipart = 0;
1404         static int seq = 0;
1405         wc_attachment *att;
1406         char *encoded;
1407         size_t encoded_length;
1408         size_t encoded_strlen;
1409         char *txtmail = NULL;
1410
1411         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
1412                 serv_info.serv_fqdn,
1413                 getpid(),
1414                 ++seq
1415         );
1416         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
1417                 serv_info.serv_fqdn,
1418                 getpid(),
1419                 ++seq
1420         );
1421
1422         /* RFC2045 requires this, and some clients look for it... */
1423         serv_puts("MIME-Version: 1.0");
1424         serv_puts("X-Mailer: " PACKAGE_STRING);
1425
1426         /* If there are attachments, we have to do multipart/mixed */
1427         if (GetCount(WCC->attachments) > 0) {
1428                 is_multipart = 1;
1429         }
1430
1431         if (is_multipart) {
1432                 /* Remember, serv_printf() appends an extra newline */
1433                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
1434                 serv_printf("This is a multipart message in MIME format.\n");
1435                 serv_printf("--%s", top_boundary);
1436         }
1437
1438         /* Remember, serv_printf() appends an extra newline */
1439         serv_printf("Content-type: multipart/alternative; "
1440                 "boundary=\"%s\"\n", alt_boundary);
1441         serv_printf("This is a multipart message in MIME format.\n");
1442         serv_printf("--%s", alt_boundary);
1443
1444         serv_puts("Content-type: text/plain; charset=utf-8");
1445         serv_puts("Content-Transfer-Encoding: quoted-printable");
1446         serv_puts("");
1447         txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
1448         text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
1449         free(txtmail);
1450
1451         serv_printf("--%s", alt_boundary);
1452
1453         serv_puts("Content-type: text/html; charset=utf-8");
1454         serv_puts("Content-Transfer-Encoding: quoted-printable");
1455         serv_puts("");
1456         serv_puts("<html><body>\r\n");
1457         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
1458         serv_puts("</body></html>\r\n");
1459
1460         serv_printf("--%s--", alt_boundary);
1461         
1462         if (is_multipart) {
1463                 long len;
1464                 const char *Key; 
1465                 void *vAtt;
1466                 HashPos  *it;
1467
1468                 /* Add in the attachments */
1469                 it = GetNewHashPos();
1470                 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
1471                         att = (wc_attachment*)vAtt;
1472                         encoded_length = ((att->length * 150) / 100);
1473                         encoded = malloc(encoded_length);
1474                         if (encoded == NULL) break;
1475                         encoded_strlen = CtdlEncodeBase64(encoded, att->data, att->length, 1);
1476
1477                         serv_printf("--%s", top_boundary);
1478                         serv_printf("Content-type: %s", ChrPtr(att->content_type));
1479                         serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->filename));
1480                         serv_puts("Content-transfer-encoding: base64");
1481                         serv_puts("");
1482                         serv_write(encoded, encoded_strlen);
1483                         serv_puts("");
1484                         serv_puts("");
1485                         free(encoded);
1486                 }
1487                 serv_printf("--%s--", top_boundary);
1488                 DeleteHashPos(&it);
1489         }
1490
1491         serv_puts("000");
1492 }
1493
1494
1495 /*
1496  * Post message (or don't post message)
1497  *
1498  * Note regarding the "dont_post" variable:
1499  * A random value (actually, it's just a timestamp) is inserted as a hidden
1500  * field called "postseq" when the display_enter page is generated.  This
1501  * value is checked when posting, using the static variable dont_post.  If a
1502  * user attempts to post twice using the same dont_post value, the message is
1503  * discarded.  This prevents the accidental double-saving of the same message
1504  * if the user happens to click the browser "back" button.
1505  */
1506 void post_message(void)
1507 {
1508         char buf[1024];
1509         StrBuf *encoded_subject = NULL;
1510         static long dont_post = (-1L);
1511         wc_attachment *att;
1512         int is_anonymous = 0;
1513         const StrBuf *display_name = NULL;
1514         struct wcsession *WCC = WC;
1515         
1516         if (havebstr("force_room")) {
1517                 gotoroom(bstr("force_room"));
1518         }
1519
1520         if (havebstr("display_name")) {
1521                 display_name = sbstr("display_name");
1522                 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1523                         display_name = NULL;
1524                         is_anonymous = 1;
1525                 }
1526         }
1527
1528         if (WCC->upload_length > 0) {
1529                 const char *pch;
1530                 int n;
1531                 char N[64];
1532
1533                 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
1534                 /** There's an attachment.  Save it to this struct... */
1535                 att = malloc(sizeof(wc_attachment));
1536                 memset(att, 0, sizeof(wc_attachment));
1537                 att->length = WCC->upload_length;
1538                 att->content_type = NewStrBufPlain(WCC->upload_content_type, -1);
1539                 att->filename = NewStrBufPlain(WCC->upload_filename, -1);
1540                 
1541                 
1542                 if (WCC->attachments == NULL)
1543                         WCC->attachments = NewHash(1, NULL);
1544                 /* And add it to the list. */
1545                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1546                 Put(WCC->attachments, N, n, att, free_attachment);
1547
1548                 /**
1549                  * Mozilla sends a simple filename, which is what we want,
1550                  * but Satan's Browser sends an entire pathname.  Reduce
1551                  * the path to just a filename if we need to.
1552                  */
1553                 pch = strrchr(ChrPtr(att->filename), '/');
1554                 if (pch != NULL) {
1555                         StrBufCutLeft(att->filename, pch - ChrPtr(att->filename));
1556                 }
1557                 pch = strrchr(ChrPtr(att->filename), '\\');
1558                 if (pch != NULL) {
1559                         StrBufCutLeft(att->filename, pch - ChrPtr(att->filename));
1560                 }
1561
1562                 /**
1563                  * Transfer control of this memory from the upload struct
1564                  * to the attachment struct.
1565                  */
1566                 att->data = WCC->upload;
1567                 WCC->upload_length = 0;
1568                 WCC->upload = NULL;
1569                 display_enter();
1570                 return;
1571         }
1572
1573         if (havebstr("cancel_button")) {
1574                 sprintf(WCC->ImportantMessage, 
1575                         _("Cancelled.  Message was not posted."));
1576         } else if (havebstr("attach_button")) {
1577                 display_enter();
1578                 return;
1579         } else if (lbstr("postseq") == dont_post) {
1580                 sprintf(WCC->ImportantMessage, 
1581                         _("Automatically cancelled because you have already "
1582                         "saved this message."));
1583         } else {
1584                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1585                 const StrBuf *Recp = NULL; 
1586                 const StrBuf *Cc = NULL;
1587                 const StrBuf *Bcc = NULL;
1588                 const StrBuf *Wikipage = NULL;
1589                 const StrBuf *my_email_addr = NULL;
1590                 StrBuf *CmdBuf = NULL;;
1591                 StrBuf *references = NULL;
1592
1593                 if (havebstr("references"))
1594                 {
1595                         const StrBuf *ref = sbstr("references");
1596                         references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
1597                         lprintf(9, "Converting: %s\n", ChrPtr(references));
1598                         StrBufReplaceChars(references, '|', '!');
1599                         lprintf(9, "Converted: %s\n", ChrPtr(references));
1600                 }
1601                 if (havebstr("subject")) {
1602                         const StrBuf *Subj;
1603                         /*
1604                          * make enough room for the encoded string; 
1605                          * plus the QP header 
1606                          */
1607                         Subj = sbstr("subject");
1608                         
1609                         StrBufRFC2047encode(&encoded_subject, Subj);
1610                 }
1611                 Recp = sbstr("recp");
1612                 Cc = sbstr("cc");
1613                 Bcc = sbstr("bcc");
1614                 Wikipage = sbstr("wikipage");
1615                 my_email_addr = sbstr("my_email_addr");
1616                 
1617                 CmdBuf = NewStrBufPlain(NULL, 
1618                                         sizeof (CMD) + 
1619                                         StrLength(Recp) + 
1620                                         StrLength(encoded_subject) +
1621                                         StrLength(Cc) +
1622                                         StrLength(Bcc) + 
1623                                         StrLength(Wikipage) +
1624                                         StrLength(my_email_addr) + 
1625                                         StrLength(references));
1626
1627                 StrBufPrintf(CmdBuf, 
1628                              CMD,
1629                              ChrPtr(Recp),
1630                              is_anonymous,
1631                              ChrPtr(encoded_subject),
1632                              ChrPtr(display_name),
1633                              ChrPtr(Cc),
1634                              ChrPtr(Bcc),
1635                              ChrPtr(Wikipage),
1636                              ChrPtr(my_email_addr),
1637                              ChrPtr(references));
1638                 FreeStrBuf(&references);
1639
1640                 lprintf(9, "%s\n", CmdBuf);
1641                 serv_puts(ChrPtr(CmdBuf));
1642                 serv_getln(buf, sizeof buf);
1643                 FreeStrBuf(&CmdBuf);
1644                 FreeStrBuf(&encoded_subject);
1645                 if (buf[0] == '4') {
1646                         post_mime_to_server();
1647                         if (  (havebstr("recp"))
1648                            || (havebstr("cc"  ))
1649                            || (havebstr("bcc" ))
1650                         ) {
1651                                 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
1652                         }
1653                         else {
1654                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
1655                         }
1656                         dont_post = lbstr("postseq");
1657                 } else {
1658                         lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
1659                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
1660                         display_enter();
1661                         return;
1662                 }
1663         }
1664
1665         DeleteHash(&WCC->attachments);
1666
1667         /**
1668          *  We may have been supplied with instructions regarding the location
1669          *  to which we must return after posting.  If found, go there.
1670          */
1671         if (havebstr("return_to")) {
1672                 http_redirect(bstr("return_to"));
1673         }
1674         /**
1675          *  If we were editing a page in a wiki room, go to that page now.
1676          */
1677         else if (havebstr("wikipage")) {
1678                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
1679                 http_redirect(buf);
1680         }
1681         /**
1682          *  Otherwise, just go to the "read messages" loop.
1683          */
1684         else {
1685                 readloop("readnew");
1686         }
1687 }
1688
1689
1690
1691
1692 /**
1693  * \brief display the message entry screen
1694  */
1695 void display_enter(void)
1696 {
1697         char buf[SIZ];
1698         long now;
1699         const StrBuf *display_name = NULL;
1700         /////wc_attachment *att;
1701         int recipient_required = 0;
1702         int subject_required = 0;
1703         int recipient_bad = 0;
1704         int is_anonymous = 0;
1705
1706         struct wcsession *WCC = WC;
1707
1708         now = time(NULL);
1709
1710         if (havebstr("force_room")) {
1711                 gotoroom(bstr("force_room"));
1712         }
1713
1714         display_name = sbstr("display_name");
1715         if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1716                 display_name = NULL;
1717                 is_anonymous = 1;
1718         }
1719
1720         /** First test to see whether this is a room that requires recipients to be entered */
1721         serv_puts("ENT0 0");
1722         serv_getln(buf, sizeof buf);
1723
1724         if (!strncmp(buf, "570", 3)) {          /** 570 means that we need a recipient here */
1725                 recipient_required = 1;
1726         }
1727         else if (buf[0] != '2') {               /** Any other error means that we cannot continue */
1728                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1729                 readloop("readnew");
1730                 return;
1731         }
1732
1733         /* Is the server strongly recommending that the user enter a message subject? */
1734         if ((buf[3] != '\0') && (buf[4] != '\0')) {
1735                 subject_required = extract_int(&buf[4], 1);
1736         }
1737
1738         /**
1739          * Are we perhaps in an address book view?  If so, then an "enter
1740          * message" command really means "add new entry."
1741          */
1742         if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
1743                 do_edit_vcard(-1, "", "", WCC->wc_roomname);
1744                 return;
1745         }
1746
1747         /*
1748          * Are we perhaps in a calendar room?  If so, then an "enter
1749          * message" command really means "add new calendar item."
1750          */
1751         if (WCC->wc_default_view == VIEW_CALENDAR) {
1752                 display_edit_event();
1753                 return;
1754         }
1755
1756         /*
1757          * Are we perhaps in a tasks view?  If so, then an "enter
1758          * message" command really means "add new task."
1759          */
1760         if (WCC->wc_default_view == VIEW_TASKS) {
1761                 display_edit_task();
1762                 return;
1763         }
1764
1765         /*
1766          * Otherwise proceed normally.
1767          * Do a custom room banner with no navbar...
1768          */
1769
1770         if (recipient_required) {
1771                 const StrBuf *Recp = NULL; 
1772                 const StrBuf *Cc = NULL;
1773                 const StrBuf *Bcc = NULL;
1774                 const StrBuf *Wikipage = NULL;
1775                 StrBuf *CmdBuf = NULL;;
1776                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1777                 
1778                 Recp = sbstr("recp");
1779                 Cc = sbstr("cc");
1780                 Bcc = sbstr("bcc");
1781                 Wikipage = sbstr("wikipage");
1782                 
1783                 CmdBuf = NewStrBufPlain(NULL, 
1784                                         sizeof (CMD) + 
1785                                         StrLength(Recp) + 
1786                                         StrLength(display_name) +
1787                                         StrLength(Cc) +
1788                                         StrLength(Bcc) + 
1789                                         StrLength(Wikipage));
1790
1791                 StrBufPrintf(CmdBuf, 
1792                              CMD,
1793                              ChrPtr(Recp), 
1794                              is_anonymous,
1795                              ChrPtr(display_name),
1796                              ChrPtr(Cc), 
1797                              ChrPtr(Bcc), 
1798                              ChrPtr(Wikipage));
1799                 serv_puts(ChrPtr(CmdBuf));
1800                 serv_getln(buf, sizeof buf);
1801                 FreeStrBuf(&CmdBuf);
1802
1803                 if (!strncmp(buf, "570", 3)) {  /** 570 means we have an invalid recipient listed */
1804                         if (havebstr("recp") && 
1805                             havebstr("cc"  ) && 
1806                             havebstr("bcc" )) {
1807                                 recipient_bad = 1;
1808                         }
1809                 }
1810                 else if (buf[0] != '2') {       /** Any other error means that we cannot continue */
1811                         wprintf("<em>%s</em><br />\n", &buf[4]);/// -> important message
1812                         return;
1813                 }
1814         }
1815         svputlong("RCPTREQUIRED", recipient_required);
1816         svputlong("SUBJREQUIRED", recipient_required || subject_required);
1817
1818         begin_burst();
1819         output_headers(1, 0, 0, 0, 1, 0);
1820         DoTemplate(HKEY("edit_message"), NULL, NULL, CTX_NONE);
1821         end_burst();
1822
1823         return;
1824 }
1825
1826 /**
1827  * \brief delete a message
1828  */
1829 void delete_msg(void)
1830 {
1831         long msgid;
1832         char buf[SIZ];
1833
1834         msgid = lbstr("msgid");
1835
1836         if (WC->wc_is_trash) {  /** Delete from Trash is a real delete */
1837                 serv_printf("DELE %ld", msgid); 
1838         }
1839         else {                  /** Otherwise move it to Trash */
1840                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1841         }
1842
1843         serv_getln(buf, sizeof buf);
1844         sprintf(WC->ImportantMessage, "%s", &buf[4]);
1845
1846         readloop("readnew");
1847 }
1848
1849
1850 /**
1851  * \brief move a message to another folder
1852  */
1853 void move_msg(void)
1854 {
1855         long msgid;
1856         char buf[SIZ];
1857
1858         msgid = lbstr("msgid");
1859
1860         if (havebstr("move_button")) {
1861                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1862                 serv_puts(buf);
1863                 serv_getln(buf, sizeof buf);
1864                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1865         } else {
1866                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1867         }
1868
1869         readloop("readnew");
1870 }
1871
1872
1873
1874
1875
1876 /*
1877  * Confirm move of a message
1878  */
1879 void confirm_move_msg(void)
1880 {
1881         long msgid;
1882         char buf[SIZ];
1883         char targ[SIZ];
1884
1885         msgid = lbstr("msgid");
1886
1887
1888         output_headers(1, 1, 2, 0, 0, 0);
1889         wprintf("<div id=\"banner\">\n");
1890         wprintf("<h1>");
1891         wprintf(_("Confirm move of message"));
1892         wprintf("</h1>");
1893         wprintf("</div>\n");
1894
1895         wprintf("<div id=\"content\" class=\"service\">\n");
1896
1897         wprintf("<CENTER>");
1898
1899         wprintf(_("Move this message to:"));
1900         wprintf("<br />\n");
1901
1902         wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1903         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1904         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1905
1906         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1907         serv_puts("LKRA");
1908         serv_getln(buf, sizeof buf);
1909         if (buf[0] == '1') {
1910                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1911                         extract_token(targ, buf, 0, '|', sizeof targ);
1912                         wprintf("<OPTION>");
1913                         escputs(targ);
1914                         wprintf("\n");
1915                 }
1916         }
1917         wprintf("</SELECT>\n");
1918         wprintf("<br />\n");
1919
1920         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1921         wprintf("&nbsp;");
1922         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1923         wprintf("</form></CENTER>\n");
1924
1925         wprintf("</CENTER>\n");
1926         wDumpContent(1);
1927 }
1928
1929 void readnew(void) { readloop("readnew");}
1930 void readold(void) { readloop("readold");}
1931 void readfwd(void) { readloop("readfwd");}
1932 void headers(void) { readloop("headers");}
1933 void do_search(void) { readloop("do_search");}
1934
1935
1936
1937
1938
1939
1940 void 
1941 InitModule_MSG
1942 (void)
1943 {
1944         WebcitAddUrlHandler(HKEY("readnew"), readnew, 0);
1945         WebcitAddUrlHandler(HKEY("readold"), readold, 0);
1946         WebcitAddUrlHandler(HKEY("readfwd"), readfwd, 0);
1947         WebcitAddUrlHandler(HKEY("headers"), headers, 0);
1948         WebcitAddUrlHandler(HKEY("do_search"), do_search, 0);
1949         WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
1950         WebcitAddUrlHandler(HKEY("post"), post_message, 0);
1951         WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
1952         WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
1953         WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
1954         WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
1955         WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
1956         WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
1957         WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
1958
1959
1960         return ;
1961 }