* copy daves great handler script and modify it to fit the simpler needs of webcit.
[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 #define SUBJ_COL_WIDTH_PCT              50      /**< Mailbox view column width */
13 #define SENDER_COL_WIDTH_PCT            30      /**< Mailbox view column width */
14 #define DATE_PLUS_BUTTONS_WIDTH_PCT     20      /**< Mailbox view column width */
15
16 /*
17  * Address book entry (keep it short and sweet, it's just a quickie lookup
18  * which we can use to get to the real meat and bones later)
19  */
20 struct addrbookent {
21         char ab_name[64]; /**< name string */
22         long ab_msgnum;   /**< message number of address book entry */
23 };
24
25
26
27 #ifdef HAVE_ICONV
28
29 /*
30  * Wrapper around iconv_open()
31  * Our version adds aliases for non-standard Microsoft charsets
32  * such as 'MS950', aliasing them to names like 'CP950'
33  *
34  * tocode       Target encoding
35  * fromcode     Source encoding
36  */
37 iconv_t ctdl_iconv_open(const char *tocode, const char *fromcode)
38 {
39         iconv_t ic = (iconv_t)(-1) ;
40         ic = iconv_open(tocode, fromcode);
41         if (ic == (iconv_t)(-1) ) {
42                 char alias_fromcode[64];
43                 if ( (strlen(fromcode) == 5) && (!strncasecmp(fromcode, "MS", 2)) ) {
44                         safestrncpy(alias_fromcode, fromcode, sizeof alias_fromcode);
45                         alias_fromcode[0] = 'C';
46                         alias_fromcode[1] = 'P';
47                         ic = iconv_open(tocode, alias_fromcode);
48                 }
49         }
50         return(ic);
51 }
52
53
54
55 inline char *FindNextEnd (char *bptr)
56 {
57         char * end;
58         /* Find the next ?Q? */
59         end = strchr(bptr + 2, '?');
60         if (end == NULL) return NULL;
61         if (((*(end + 1) == 'B') || (*(end + 1) == 'Q')) && 
62             (*(end + 2) == '?')) {
63                 /* skip on to the end of the cluster, the next ?= */
64                 end = strstr(end + 3, "?=");
65         }
66         else
67                 /* sort of half valid encoding, try to find an end. */
68                 end = strstr(bptr, "?=");
69         return end;
70 }
71
72 /*
73  * Handle subjects with RFC2047 encoding such as:
74  * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
75  */
76 void utf8ify_rfc822_string(char *buf) {
77         char *start, *end, *next, *nextend, *ptr;
78         char newbuf[1024];
79         char charset[128];
80         char encoding[16];
81         char istr[1024];
82         iconv_t ic = (iconv_t)(-1) ;
83         char *ibuf;                     /**< Buffer of characters to be converted */
84         char *obuf;                     /**< Buffer for converted characters */
85         size_t ibuflen;                 /**< Length of input buffer */
86         size_t obuflen;                 /**< Length of output buffer */
87         char *isav;                     /**< Saved pointer to input buffer */
88         char *osav;                     /**< Saved pointer to output buffer */
89         int passes = 0;
90         int i, len, delta;
91         int illegal_non_rfc2047_encoding = 0;
92
93         /* Sometimes, badly formed messages contain strings which were simply
94          *  written out directly in some foreign character set instead of
95          *  using RFC2047 encoding.  This is illegal but we will attempt to
96          *  handle it anyway by converting from a user-specified default
97          *  charset to UTF-8 if we see any nonprintable characters.
98          */
99         len = strlen(buf);
100         for (i=0; i<len; ++i) {
101                 if ((buf[i] < 32) || (buf[i] > 126)) {
102                         illegal_non_rfc2047_encoding = 1;
103                         i = len; ///< take a shortcut, it won't be more than one.
104                 }
105         }
106         if (illegal_non_rfc2047_encoding) {
107                 StrBuf *default_header_charset;
108                 get_preference("default_header_charset", &default_header_charset);
109                 if ( (strcasecmp(ChrPtr(default_header_charset), "UTF-8")) && 
110                      (strcasecmp(ChrPtr(default_header_charset), "us-ascii")) ) {
111                         ic = ctdl_iconv_open("UTF-8", ChrPtr(default_header_charset));
112                         if (ic != (iconv_t)(-1) ) {
113                                 ibuf = malloc(1024);
114                                 isav = ibuf;
115                                 safestrncpy(ibuf, buf, 1024);
116                                 ibuflen = strlen(ibuf);
117                                 obuflen = 1024;
118                                 obuf = (char *) malloc(obuflen);
119                                 osav = obuf;
120                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
121                                 osav[1024-obuflen] = 0;
122                                 strcpy(buf, osav);
123                                 free(osav);
124                                 iconv_close(ic);
125                                 free(isav);
126                         }
127                 }
128         }
129
130         /* pre evaluate the first pair */
131         nextend = end = NULL;
132         len = strlen(buf);
133         start = strstr(buf, "=?");
134         if (start != NULL) 
135                 end = FindNextEnd (start);
136
137         while ((start != NULL) && (end != NULL))
138         {
139                 next = strstr(end, "=?");
140                 if (next != NULL)
141                         nextend = FindNextEnd(next);
142                 if (nextend == NULL)
143                         next = NULL;
144
145                 /* did we find two partitions */
146                 if ((next != NULL) && 
147                     ((next - end) > 2))
148                 {
149                         ptr = end + 2;
150                         while ((ptr < next) && 
151                                (isspace(*ptr) ||
152                                 (*ptr == '\r') ||
153                                 (*ptr == '\n') || 
154                                 (*ptr == '\t')))
155                                 ptr ++;
156                         /* did we find a gab just filled with blanks? */
157                         if (ptr == next)
158                         {
159                                 memmove (end + 2,
160                                          next,
161                                          len - (next - start));
162
163                                 /* now terminate the gab at the end */
164                                 delta = (next - end) - 2;
165                                 len -= delta;
166                                 buf[len] = '\0';
167
168                                 /* move next to its new location. */
169                                 next -= delta;
170                                 nextend -= delta;
171                         }
172                 }
173                 /* our next-pair is our new first pair now. */
174                 start = next;
175                 end = nextend;
176         }
177
178         /* Now we handle foreign character sets properly encoded
179          * in RFC2047 format.
180          */
181         while (start=strstr(buf, "=?"), end=FindNextEnd((start != NULL)? start : buf),
182                 ((start != NULL) && (end != NULL) && (end > start)) )
183         {
184                 extract_token(charset, start, 1, '?', sizeof charset);
185                 extract_token(encoding, start, 2, '?', sizeof encoding);
186                 extract_token(istr, start, 3, '?', sizeof istr);
187
188                 ibuf = malloc(1024);
189                 isav = ibuf;
190                 if (!strcasecmp(encoding, "B")) {       /**< base64 */
191                         ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
192                 }
193                 else if (!strcasecmp(encoding, "Q")) {  /**< quoted-printable */
194                         size_t len;
195                         long pos;
196                         
197                         len = strlen(istr);
198                         pos = 0;
199                         while (pos < len)
200                         {
201                                 if (istr[pos] == '_') istr[pos] = ' ';
202                                 pos++;
203                         }
204
205                         ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, len);
206                 }
207                 else {
208                         strcpy(ibuf, istr);             /**< unknown encoding */
209                         ibuflen = strlen(istr);
210                 }
211
212                 ic = ctdl_iconv_open("UTF-8", charset);
213                 if (ic != (iconv_t)(-1) ) {
214                         obuflen = 1024;
215                         obuf = (char *) malloc(obuflen);
216                         osav = obuf;
217                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
218                         osav[1024-obuflen] = 0;
219
220                         end = start;
221                         end++;
222                         strcpy(start, "");
223                         remove_token(end, 0, '?');
224                         remove_token(end, 0, '?');
225                         remove_token(end, 0, '?');
226                         remove_token(end, 0, '?');
227                         strcpy(end, &end[1]);
228
229                         snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
230                         strcpy(buf, newbuf);
231                         free(osav);
232                         iconv_close(ic);
233                 }
234                 else {
235                         end = start;
236                         end++;
237                         strcpy(start, "");
238                         remove_token(end, 0, '?');
239                         remove_token(end, 0, '?');
240                         remove_token(end, 0, '?');
241                         remove_token(end, 0, '?');
242                         strcpy(end, &end[1]);
243
244                         snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
245                         strcpy(buf, newbuf);
246                 }
247
248                 free(isav);
249
250                 /*
251                  * Since spammers will go to all sorts of absurd lengths to get their
252                  * messages through, there are LOTS of corrupt headers out there.
253                  * So, prevent a really badly formed RFC2047 header from throwing
254                  * this function into an infinite loop.
255                  */
256                 ++passes;
257                 if (passes > 20) return;
258         }
259
260 }
261 #else
262 inline void utf8ify_rfc822_string(char *a){};
263
264 #endif
265
266
267
268
269 /**
270  * \brief       RFC2047-encode a header field if necessary.
271  *              If no non-ASCII characters are found, the string
272  *              will be copied verbatim without encoding.
273  *
274  * \param       target          Target buffer.
275  * \param       maxlen          Maximum size of target buffer.
276  * \param       source          Source string to be encoded.
277  * \param       SourceLen       Length of the source string
278  * \returns     encoded length; -1 if non success.
279  */
280 int webcit_rfc2047encode(char *target, int maxlen, char *source, long SourceLen)
281 {
282         const char headerStr[] = "=?UTF-8?Q?";
283         int need_to_encode = 0;
284         int i = 0;
285         int len;
286         unsigned char ch;
287
288         if ((source == NULL) || 
289             (target == NULL) ||
290             (SourceLen > maxlen)) return -1;
291
292         while ((!IsEmptyStr (&source[i])) && 
293                (need_to_encode == 0) &&
294                (i < SourceLen) ) {
295                 if (((unsigned char) source[i] < 32) || 
296                     ((unsigned char) source[i] > 126)) {
297                         need_to_encode = 1;
298                 }
299                 i++;
300         }
301
302         if (!need_to_encode) {
303                 memcpy (target, source, SourceLen);
304                 target[SourceLen] = '\0';
305                 return SourceLen;
306         }
307         
308         if (sizeof (headerStr + SourceLen + 2) > maxlen)
309                 return -1;
310         memcpy (target, headerStr, sizeof (headerStr));
311         len = sizeof (headerStr) - 1;
312         for (i=0; (i < SourceLen) && (len + 3< maxlen) ; ++i) {
313                 ch = (unsigned char) source[i];
314                 if ((ch < 32) || (ch > 126) || (ch == 61)) {
315                         sprintf(&target[len], "=%02X", ch);
316                         len += 3;
317                 }
318                 else {
319                         sprintf(&target[len], "%c", ch);
320                         len ++;
321                 }
322         }
323         
324         if (len + 2 < maxlen) {
325                 strcat(&target[len], "?=");
326                 len +=2;
327                 return len;
328         }
329         else
330                 return -1;
331 }
332
333
334
335
336 /*
337  * Look for URL's embedded in a buffer and make them linkable.  We use a
338  * target window in order to keep the Citadel session in its own window.
339  */
340 void url(char *buf, size_t bufsize)
341 {
342         int len, UrlLen, Offset, TrailerLen, outpos;
343         char *start, *end, *pos;
344         char urlbuf[SIZ];
345         char outbuf[SIZ];
346
347         start = NULL;
348         len = strlen(buf);
349         if (len > bufsize) {
350                 lprintf(1, "URL: content longer than buffer!");
351                 return;
352         }
353         end = buf + len;
354         for (pos = buf; (pos < end) && (start == NULL); ++pos) {
355                 if (!strncasecmp(pos, "http://", 7))
356                         start = pos;
357                 if (!strncasecmp(pos, "ftp://", 6))
358                         start = pos;
359         }
360
361         if (start == NULL)
362                 return;
363
364         for (pos = buf+len; pos > start; --pos) {
365                 if (  (!isprint(*pos))
366                    || (isspace(*pos))
367                    || (*pos == '{')
368                    || (*pos == '}')
369                    || (*pos == '|')
370                    || (*pos == '\\')
371                    || (*pos == '^')
372                    || (*pos == '[')
373                    || (*pos == ']')
374                    || (*pos == '`')
375                    || (*pos == '<')
376                    || (*pos == '>')
377                    || (*pos == '(')
378                    || (*pos == ')')
379                 ) {
380                         end = pos;
381                 }
382         }
383         
384         UrlLen = end - start;
385         if (UrlLen > sizeof(urlbuf)){
386                 lprintf(1, "URL: content longer than buffer!");
387                 return;
388         }
389         memcpy(urlbuf, start, UrlLen);
390         urlbuf[UrlLen] = '\0';
391
392         Offset = start - buf;
393         if ((Offset != 0) && (Offset < sizeof(outbuf)))
394                 memcpy(outbuf, buf, Offset);
395         outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,  
396                           "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
397                           LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
398         if (outpos >= sizeof(outbuf) - Offset) {
399                 lprintf(1, "URL: content longer than buffer!");
400                 return;
401         }
402
403         TrailerLen = len - (end - start);
404         if (TrailerLen > 0)
405                 memcpy(outbuf + Offset + outpos, end, TrailerLen);
406         if (Offset + outpos + TrailerLen > bufsize) {
407                 lprintf(1, "URL: content longer than buffer!");
408                 return;
409         }
410         memcpy (buf, outbuf, Offset + outpos + TrailerLen);
411         *(buf + Offset + outpos + TrailerLen) = '\0';
412 }
413
414
415 /**
416  * \brief Turn a vCard "n" (name) field into something displayable.
417  * \param name the name field to convert
418  */
419 void vcard_n_prettyize(char *name)
420 {
421         char *original_name;
422         int i, j, len;
423
424         original_name = strdup(name);
425         len = strlen(original_name);
426         for (i=0; i<5; ++i) {
427                 if (len > 0) {
428                         if (original_name[len-1] == ' ') {
429                                 original_name[--len] = 0;
430                         }
431                         if (original_name[len-1] == ';') {
432                                 original_name[--len] = 0;
433                         }
434                 }
435         }
436         strcpy(name, "");
437         j=0;
438         for (i=0; i<len; ++i) {
439                 if (original_name[i] == ';') {
440                         name[j++] = ',';
441                         name[j++] = ' ';                        
442                 }
443                 else {
444                         name[j++] = original_name[i];
445                 }
446         }
447         name[j] = '\0';
448         free(original_name);
449 }
450
451
452
453
454 /**
455  * \brief preparse a vcard name
456  * display_vcard() calls this after parsing the textual vCard into
457  * our 'struct vCard' data object.
458  * This gets called instead of display_parsed_vcard() if we are only looking
459  * to extract the person's name instead of displaying the card.
460  * \param v the vcard to retrieve the name from
461  * \param storename where to put the name at
462  */
463 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
464         char *name;
465
466         strcpy(storename, "");
467
468         name = vcard_get_prop(v, "n", 1, 0, 0);
469         if (name != NULL) {
470                 strcpy(storename, name);
471                 /* vcard_n_prettyize(storename); */
472         }
473
474 }
475
476
477
478 /**
479  * \brief html print a vcard
480  * display_vcard() calls this after parsing the textual vCard into
481  * our 'struct vCard' data object.
482  *
483  * Set 'full' to nonzero to display the full card, otherwise it will only
484  * show a summary line.
485  *
486  * This code is a bit ugly, so perhaps an explanation is due: we do this
487  * in two passes through the vCard fields.  On the first pass, we process
488  * fields we understand, and then render them in a pretty fashion at the
489  * end.  Then we make a second pass, outputting all the fields we don't
490  * understand in a simple two-column name/value format.
491  * \param v the vCard to display
492  * \param full display all items of the vcard?
493  * \param msgnum Citadel message pointer
494  */
495 void display_parsed_vcard(struct vCard *v, int full, long msgnum) {
496         int i, j;
497         char buf[SIZ];
498         char *name;
499         int is_qp = 0;
500         int is_b64 = 0;
501         char *thisname, *thisvalue;
502         char firsttoken[SIZ];
503         int pass;
504
505         char fullname[SIZ];
506         char title[SIZ];
507         char org[SIZ];
508         char phone[SIZ];
509         char mailto[SIZ];
510
511         strcpy(fullname, "");
512         strcpy(phone, "");
513         strcpy(mailto, "");
514         strcpy(title, "");
515         strcpy(org, "");
516
517         if (!full) {
518                 wprintf("<TD>");
519                 name = vcard_get_prop(v, "fn", 1, 0, 0);
520                 if (name != NULL) {
521                         escputs(name);
522                 }
523                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
524                         strcpy(fullname, name);
525                         vcard_n_prettyize(fullname);
526                         escputs(fullname);
527                 }
528                 else {
529                         wprintf("&nbsp;");
530                 }
531                 wprintf("</TD>");
532                 return;
533         }
534
535         wprintf("<div align=center>"
536                 "<table bgcolor=#aaaaaa width=50%%>");
537         for (pass=1; pass<=2; ++pass) {
538
539                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
540                         int len;
541                         thisname = strdup(v->prop[i].name);
542                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
543         
544                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
545                                 extract_token(buf, thisname, j, ';', sizeof buf);
546                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
547                                         is_qp = 1;
548                                         remove_token(thisname, j, ';');
549                                 }
550                                 if (!strcasecmp(buf, "encoding=base64")) {
551                                         is_b64 = 1;
552                                         remove_token(thisname, j, ';');
553                                 }
554                         }
555                         
556                         len = strlen(v->prop[i].value);
557                         /* if we have some untagged QP, detect it here. */
558                         if (!is_qp && (strstr(v->prop[i].value, "=?")!=NULL))
559                                 utf8ify_rfc822_string(v->prop[i].value);
560
561                         if (is_qp) {
562                                 // %ff can become 6 bytes in utf8 
563                                 thisvalue = malloc(len * 2 + 3); 
564                                 j = CtdlDecodeQuotedPrintable(
565                                         thisvalue, v->prop[i].value,
566                                         len);
567                                 thisvalue[j] = 0;
568                         }
569                         else if (is_b64) {
570                                 // ff will become one byte..
571                                 thisvalue = malloc(len + 50);
572                                 CtdlDecodeBase64(
573                                         thisvalue, v->prop[i].value,
574                                         strlen(v->prop[i].value) );
575                         }
576                         else {
577                                 thisvalue = strdup(v->prop[i].value);
578                         }
579         
580                         /** Various fields we may encounter ***/
581         
582                         /** N is name, but only if there's no FN already there */
583                         if (!strcasecmp(firsttoken, "n")) {
584                                 if (IsEmptyStr(fullname)) {
585                                         strcpy(fullname, thisvalue);
586                                         vcard_n_prettyize(fullname);
587                                 }
588                         }
589         
590                         /** FN (full name) is a true 'display name' field */
591                         else if (!strcasecmp(firsttoken, "fn")) {
592                                 strcpy(fullname, thisvalue);
593                         }
594
595                         /** title */
596                         else if (!strcasecmp(firsttoken, "title")) {
597                                 strcpy(title, thisvalue);
598                         }
599         
600                         /** organization */
601                         else if (!strcasecmp(firsttoken, "org")) {
602                                 strcpy(org, thisvalue);
603                         }
604         
605                         else if (!strcasecmp(firsttoken, "email")) {
606                                 size_t len;
607                                 if (!IsEmptyStr(mailto)) strcat(mailto, "<br />");
608                                 strcat(mailto,
609                                         "<a href=\"display_enter"
610                                         "?force_room=_MAIL_?recp=");
611
612                                 len = strlen(mailto);
613                                 urlesc(&mailto[len], SIZ - len, "\"");
614                                 len = strlen(mailto);
615                                 urlesc(&mailto[len], SIZ - len,  fullname);
616                                 len = strlen(mailto);
617                                 urlesc(&mailto[len], SIZ - len, "\" <");
618                                 len = strlen(mailto);
619                                 urlesc(&mailto[len], SIZ - len, thisvalue);
620                                 len = strlen(mailto);
621                                 urlesc(&mailto[len], SIZ - len, ">");
622
623                                 strcat(mailto, "\">");
624                                 len = strlen(mailto);
625                                 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
626                                 strcat(mailto, "</A>");
627                         }
628                         else if (!strcasecmp(firsttoken, "tel")) {
629                                 if (!IsEmptyStr(phone)) strcat(phone, "<br />");
630                                 strcat(phone, thisvalue);
631                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
632                                         extract_token(buf, thisname, j, ';', sizeof buf);
633                                         if (!strcasecmp(buf, "tel"))
634                                                 strcat(phone, "");
635                                         else if (!strcasecmp(buf, "work"))
636                                                 strcat(phone, _(" (work)"));
637                                         else if (!strcasecmp(buf, "home"))
638                                                 strcat(phone, _(" (home)"));
639                                         else if (!strcasecmp(buf, "cell"))
640                                                 strcat(phone, _(" (cell)"));
641                                         else {
642                                                 strcat(phone, " (");
643                                                 strcat(phone, buf);
644                                                 strcat(phone, ")");
645                                         }
646                                 }
647                         }
648                         else if (!strcasecmp(firsttoken, "adr")) {
649                                 if (pass == 2) {
650                                         wprintf("<TR><TD>");
651                                         wprintf(_("Address:"));
652                                         wprintf("</TD><TD>");
653                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
654                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
655                                                 if (!IsEmptyStr(buf)) {
656                                                         escputs(buf);
657                                                         if (j<3) wprintf("<br />");
658                                                         else wprintf(" ");
659                                                 }
660                                         }
661                                         wprintf("</TD></TR>\n");
662                                 }
663                         }
664                         /* else if (!strcasecmp(firsttoken, "photo") && full && pass == 2) { 
665                                 // Only output on second pass
666                                 wprintf("<tr><td>");
667                                 wprintf(_("Photo:"));
668                                 wprintf("</td><td>");
669                                 wprintf("<img src=\"/vcardphoto/%ld/\" alt=\"Contact photo\"/>",msgnum);
670                                 wprintf("</td></tr>\n");
671                         } */
672                         else if (!strcasecmp(firsttoken, "version")) {
673                                 /* ignore */
674                         }
675                         else if (!strcasecmp(firsttoken, "rev")) {
676                                 /* ignore */
677                         }
678                         else if (!strcasecmp(firsttoken, "label")) {
679                                 /* ignore */
680                         }
681                         else {
682
683                                 /*** Don't show extra fields.  They're ugly.
684                                 if (pass == 2) {
685                                         wprintf("<TR><TD>");
686                                         escputs(thisname);
687                                         wprintf("</TD><TD>");
688                                         escputs(thisvalue);
689                                         wprintf("</TD></TR>\n");
690                                 }
691                                 ***/
692                         }
693         
694                         free(thisname);
695                         free(thisvalue);
696                 }
697         
698                 if (pass == 1) {
699                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
700                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
701                         "<IMG ALIGN=CENTER src=\"static/viewcontacts_48x.gif\">"
702                         "<FONT SIZE=+1><B>");
703                         escputs(fullname);
704                         wprintf("</B></FONT>");
705                         if (!IsEmptyStr(title)) {
706                                 wprintf("<div align=right>");
707                                 escputs(title);
708                                 wprintf("</div>");
709                         }
710                         if (!IsEmptyStr(org)) {
711                                 wprintf("<div align=right>");
712                                 escputs(org);
713                                 wprintf("</div>");
714                         }
715                         wprintf("</TD></TR>\n");
716                 
717                         if (!IsEmptyStr(phone)) {
718                                 wprintf("<tr><td>");
719                                 wprintf(_("Telephone:"));
720                                 wprintf("</td><td>%s</td></tr>\n", phone);
721                         }
722                         if (!IsEmptyStr(mailto)) {
723                                 wprintf("<tr><td>");
724                                 wprintf(_("E-mail:"));
725                                 wprintf("</td><td>%s</td></tr>\n", mailto);
726                         }
727                 }
728
729         }
730
731         wprintf("</table></div>\n");
732 }
733
734
735
736 /**
737  * \brief  Display a textual vCard
738  * (Converts to a vCard object and then calls the actual display function)
739  * Set 'full' to nonzero to display the whole card instead of a one-liner.
740  * Or, if "storename" is non-NULL, just store the person's name in that
741  * buffer instead of displaying the card at all.
742  * \param vcard_source the buffer containing the vcard text
743  * \param alpha what???
744  * \param full should we usse all lines?
745  * \param storename where to store???
746  * \param msgnum Citadel message pointer
747  */
748 void display_vcard(char *vcard_source, char alpha, int full, char *storename, 
749         long msgnum) {
750         struct vCard *v;
751         char *name;
752         char buf[SIZ];
753         char this_alpha = 0;
754
755         v = vcard_load(vcard_source);
756         if (v == NULL) return;
757
758         name = vcard_get_prop(v, "n", 1, 0, 0);
759         if (name != NULL) {
760                 utf8ify_rfc822_string(name);
761                 strcpy(buf, name);
762                 this_alpha = buf[0];
763         }
764
765         if (storename != NULL) {
766                 fetchname_parsed_vcard(v, storename);
767         }
768         else if (       (alpha == 0)
769                         || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
770                         || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
771                 ) {
772                 display_parsed_vcard(v, full,msgnum);
773         }
774
775         vcard_free(v);
776 }
777
778
779 struct attach_link {
780         char partnum[32];
781         char html[1024];
782 };
783
784
785 /*
786  * I wanna SEE that message!
787  *
788  * msgnum               Message number to display
789  * printable_view       Nonzero to display a printable view
790  * section              Optional for encapsulated message/rfc822 submessage
791  */
792 void read_message(long msgnum, int printable_view, char *section) {
793         char buf[SIZ];
794         char mime_partnum[256] = "";
795         char mime_name[256] = "";
796         char mime_filename[256] = "";
797         char escaped_mime_filename[256] = "";
798         char mime_content_type[256] = "";
799         const char *mime_content_type_ptr;
800         char mime_charset[256] = "";
801         char mime_disposition[256] = "";
802         int mime_length;
803         struct attach_link *attach_links = NULL;
804         int num_attach_links = 0;
805         char mime_submessages[256] = "";
806         char m_subject[1024] = "";
807         char m_cc[1024] = "";
808         char from[256] = "";
809         char node[256] = "";
810         char rfca[256] = "";
811         char reply_to[512] = "";
812         char reply_all[4096] = "";
813         char reply_references[1024] = "";
814         char reply_inreplyto[256] = "";
815         char now[64] = "";
816         int format_type = 0;
817         int nhdr = 0;
818         int bq = 0;
819         int i = 0;
820         char vcard_partnum[256] = "";
821         char cal_partnum[256] = "";
822         char *part_source = NULL;
823         char msg4_partnum[32] = "";
824 #ifdef HAVE_ICONV
825         iconv_t ic = (iconv_t)(-1) ;
826         char *ibuf;                /**< Buffer of characters to be converted */
827         char *obuf;                /**< Buffer for converted characters      */
828         size_t ibuflen;    /**< Length of input buffer         */
829         size_t obuflen;    /**< Length of output buffer       */
830         char *osav;                /**< Saved pointer to output buffer       */
831 #endif
832
833         strcpy(mime_content_type, "text/plain");
834         strcpy(mime_charset, "us-ascii");
835         strcpy(mime_submessages, "");
836
837         serv_printf("MSG4 %ld|%s", msgnum, section);
838         serv_getln(buf, sizeof buf);
839         if (buf[0] != '1') {
840                 wprintf("<strong>");
841                 wprintf(_("ERROR:"));
842                 wprintf("</strong> %s<br />\n", &buf[4]);
843                 return;
844         }
845
846         /** begin everythingamundo table */
847         if (!printable_view) {
848                 wprintf("<div class=\"fix_scrollbar_bug message\" ");
849                 wprintf("onMouseOver=document.getElementById(\"msg%ld\").style.visibility=\"visible\" ", msgnum);
850                 wprintf("onMouseOut=document.getElementById(\"msg%ld\").style.visibility=\"hidden\" >", msgnum);
851         }
852
853         /** begin message header table */
854         wprintf("<div class=\"message_header\">");
855
856         strcpy(m_subject, "");
857         strcpy(m_cc, "");
858
859         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
860                 if (!strcmp(buf, "000")) {
861                         wprintf("<i>");
862                         wprintf(_("unexpected end of message"));
863                         wprintf(" (1)</i><br /><br />\n");
864                         wprintf("</div>\n");
865                         return;
866                 }
867                 if (!strncasecmp(buf, "nhdr=yes", 8))
868                         nhdr = 1;
869                 if (nhdr == 1)
870                         buf[0] = '_';
871                 if (!strncasecmp(buf, "type=", 5))
872                         format_type = atoi(&buf[5]);
873                 if (!strncasecmp(buf, "from=", 5)) {
874                         strcpy(from, &buf[5]);
875                         wprintf(_("from "));
876                         wprintf("<a href=\"showuser?who=");
877                         utf8ify_rfc822_string(from);
878                         urlescputs(from);
879                         wprintf("\">");
880                         escputs(from);
881                         wprintf("</a> ");
882                 }
883                 if (!strncasecmp(buf, "subj=", 5)) {
884                         safestrncpy(m_subject, &buf[5], sizeof m_subject);
885                 }
886                 if (!strncasecmp(buf, "msgn=", 5)) {
887                         safestrncpy(reply_inreplyto, &buf[5], sizeof reply_inreplyto);
888                 }
889                 if (!strncasecmp(buf, "wefw=", 5)) {
890                         safestrncpy(reply_references, &buf[5], sizeof reply_references);
891                 }
892                 if (!strncasecmp(buf, "cccc=", 5)) {
893                         int len;
894                         safestrncpy(m_cc, &buf[5], sizeof m_cc);
895                         if (!IsEmptyStr(reply_all)) {
896                                 strcat(reply_all, ", ");
897                         }
898                         len = strlen(reply_all);
899                         safestrncpy(&reply_all[len], &buf[5],
900                                 (sizeof reply_all - len) );
901                 }
902                 if ((!strncasecmp(buf, "hnod=", 5))
903                     && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
904                         wprintf("(%s) ", &buf[5]);
905                 }
906                 if ((!strncasecmp(buf, "room=", 5))
907                     && (strcasecmp(&buf[5], WC->wc_roomname))
908                     && (!IsEmptyStr(&buf[5])) ) {
909                         wprintf(_("in "));
910                         wprintf("%s&gt; ", &buf[5]);
911                 }
912                 if (!strncasecmp(buf, "rfca=", 5)) {
913                         strcpy(rfca, &buf[5]);
914                         wprintf("&lt;");
915                         escputs(rfca);
916                         wprintf("&gt; ");
917                 }
918
919                 if (!strncasecmp(buf, "node=", 5)) {
920                         strcpy(node, &buf[5]);
921                         if ( ((WC->room_flags & QR_NETWORK)
922                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
923                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
924                         && (IsEmptyStr(rfca))
925                         ) {
926                                 wprintf("@%s ", &buf[5]);
927                         }
928                 }
929                 if (!strncasecmp(buf, "rcpt=", 5)) {
930                         int len;
931                         wprintf(_("to "));
932                         if (!IsEmptyStr(reply_all)) {
933                                 strcat(reply_all, ", ");
934                         }
935                         len = strlen(reply_all);
936                         safestrncpy(&reply_all[len], &buf[5],
937                                 (sizeof reply_all - len) );
938                         utf8ify_rfc822_string(&buf[5]);
939                         escputs(&buf[5]);
940                         wprintf(" ");
941                 }
942                 if (!strncasecmp(buf, "time=", 5)) {
943                         webcit_fmt_date(now, atol(&buf[5]), 0);
944                         wprintf("<span>");
945                         wprintf("%s ", now);
946                         wprintf("</span>");
947                 }
948
949                 if (!strncasecmp(buf, "part=", 5)) {
950                         extract_token(mime_name, &buf[5], 0, '|', sizeof mime_filename);
951                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
952                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
953                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
954                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
955                         mime_length = extract_int(&buf[5], 5);
956
957                         striplt(mime_name);
958                         striplt(mime_filename);
959                         if ( (IsEmptyStr(mime_filename)) && (!IsEmptyStr(mime_name)) ) {
960                                 strcpy(mime_filename, mime_name);
961                         }
962
963                         if (!strcasecmp(mime_content_type, "message/rfc822")) {
964                                 if (!IsEmptyStr(mime_submessages)) {
965                                         strcat(mime_submessages, "|");
966                                 }
967                                 strcat(mime_submessages, mime_partnum);
968                         }
969                         else if ((!strcasecmp(mime_disposition, "inline"))
970                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
971                                 ++num_attach_links;
972                                 attach_links = realloc(attach_links,
973                                         (num_attach_links*sizeof(struct attach_link)));
974                                 safestrncpy(attach_links[num_attach_links-1].partnum, mime_partnum, 32);
975                                 snprintf(attach_links[num_attach_links-1].html, 1024,
976                                         "<img src=\"mimepart/%ld/%s/%s\">",
977                                         msgnum, mime_partnum, mime_filename);
978                         }
979                         else if ( ( (!strcasecmp(mime_disposition, "attachment")) 
980                              || (!strcasecmp(mime_disposition, "inline"))
981                              || (!strcasecmp(mime_disposition, ""))
982                              ) && (!IsEmptyStr(mime_content_type))
983                         ) {
984                                 ++num_attach_links;
985                                 attach_links = realloc(attach_links,
986                                         (num_attach_links*sizeof(struct attach_link)));
987                                 safestrncpy(attach_links[num_attach_links-1].partnum, mime_partnum, 32);
988                                 utf8ify_rfc822_string(mime_filename);
989
990                                 mime_content_type_ptr = mime_content_type;
991                                 if (strcasecmp(mime_content_type, "application/octet-stream") == 0) {
992                                         mime_content_type_ptr = GuessMimeByFilename(mime_filename, 
993                                                                                     strlen(mime_filename));
994                                 }
995                                 urlesc(escaped_mime_filename, 265, mime_filename);
996                                 snprintf(attach_links[num_attach_links-1].html, 1024,
997                                         "<img src=\"display_mime_icon?type=%s\" "
998                                         "border=0 align=middle>\n"
999                                         "%s (%s, %d bytes) [ "
1000                                         "<a href=\"mimepart/%ld/%s/%s\""
1001                                         "target=\"wc.%ld.%s\">%s</a>"
1002                                         " | "
1003                                         "<a href=\"mimepart_download/%ld/%s/%s\">%s</a>"
1004                                         " ]<br />\n",
1005                                         mime_content_type_ptr,
1006                                         mime_filename,
1007                                         mime_content_type, mime_length,
1008                                         msgnum, mime_partnum, escaped_mime_filename,
1009                                         msgnum, mime_partnum,
1010                                         _("View"),
1011                                         msgnum, mime_partnum, escaped_mime_filename,
1012                                         _("Download")
1013                                 );
1014                         }
1015
1016                         /** begin handler prep ***/
1017                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1018                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1019                                 strcpy(vcard_partnum, mime_partnum);
1020                         }
1021
1022                         if (  (!strcasecmp(mime_content_type, "text/calendar"))
1023                            || (!strcasecmp(mime_content_type, "application/ics")) ) {
1024                                 strcpy(cal_partnum, mime_partnum);
1025                         }
1026
1027                         /** end handler prep ***/
1028
1029                 }
1030
1031         }
1032
1033         /* Trim down excessively long lists of thread references.  We eliminate the
1034          * second one in the list so that the thread root remains intact.
1035          */
1036         int rrtok = num_tokens(reply_references, '|');
1037         int rrlen = strlen(reply_references);
1038         if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1039                 remove_token(reply_references, 1, '|');
1040         }
1041
1042         /* Generate a reply-to address */
1043         if (!IsEmptyStr(rfca)) {
1044                 if (!IsEmptyStr(from)) {
1045                         snprintf(reply_to, sizeof(reply_to), "%s <%s>", from, rfca);
1046                 }
1047                 else {
1048                         strcpy(reply_to, rfca);
1049                 }
1050         }
1051         else {
1052         if ((!IsEmptyStr(node))
1053                    && (strcasecmp(node, serv_info.serv_nodename))
1054                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
1055                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
1056                                 from, node);
1057                 }
1058                 else {
1059                         snprintf(reply_to, sizeof(reply_to), "%s", from);
1060                 }
1061         }
1062
1063         if (nhdr == 1) {
1064                 wprintf("****");
1065         }
1066
1067         utf8ify_rfc822_string(m_cc);
1068         utf8ify_rfc822_string(m_subject);
1069
1070         /** start msg buttons */
1071
1072         if (!printable_view) {
1073                 wprintf("<p id=\"msg%ld\" class=\"msgbuttons\" >\n",msgnum);
1074
1075                 /* Reply */
1076                 if ( (WC->wc_view == VIEW_MAILBOX) || (WC->wc_view == VIEW_BBS) ) {
1077                         wprintf("<a href=\"display_enter");
1078                         if (WC->is_mailbox) {
1079                                 wprintf("?replyquote=%ld", msgnum);
1080                         }
1081                         wprintf("?recp=");
1082                         urlescputs(reply_to);
1083                         if (!IsEmptyStr(m_subject)) {
1084                                 wprintf("?subject=");
1085                                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%%20");
1086                                 urlescputs(m_subject);
1087                         }
1088                         wprintf("?references=");
1089                         if (!IsEmptyStr(reply_references)) {
1090                                 urlescputs(reply_references);
1091                                 urlescputs("|");
1092                         }
1093                         urlescputs(reply_inreplyto);
1094                         wprintf("\"><span>[</span>%s<span>]</span></a> ", _("Reply"));
1095                 }
1096
1097                 /* ReplyQuoted */
1098                 if ( (WC->wc_view == VIEW_MAILBOX) || (WC->wc_view == VIEW_BBS) ) {
1099                         if (!WC->is_mailbox) {
1100                                 wprintf("<a href=\"display_enter");
1101                                 wprintf("?replyquote=%ld", msgnum);
1102                                 wprintf("?recp=");
1103                                 urlescputs(reply_to);
1104                                 if (!IsEmptyStr(m_subject)) {
1105                                         wprintf("?subject=");
1106                                         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%%20");
1107                                         urlescputs(m_subject);
1108                                 }
1109                                 wprintf("?references=");
1110                                 if (!IsEmptyStr(reply_references)) {
1111                                         urlescputs(reply_references);
1112                                         urlescputs("|");
1113                                 }
1114                                 urlescputs(reply_inreplyto);
1115                                 wprintf("\"><span>[</span>%s<span>]</span></a> ", _("ReplyQuoted"));
1116                         }
1117                 }
1118
1119                 /* ReplyAll */
1120                 if (WC->wc_view == VIEW_MAILBOX) {
1121                         wprintf("<a href=\"display_enter");
1122                         wprintf("?replyquote=%ld", msgnum);
1123                         wprintf("?recp=");
1124                         urlescputs(reply_to);
1125                         wprintf("?cc=");
1126                         urlescputs(reply_all);
1127                         if (!IsEmptyStr(m_subject)) {
1128                                 wprintf("?subject=");
1129                                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%%20");
1130                                 urlescputs(m_subject);
1131                         }
1132                         wprintf("?references=");
1133                         if (!IsEmptyStr(reply_references)) {
1134                                 urlescputs(reply_references);
1135                                 urlescputs("|");
1136                         }
1137                         urlescputs(reply_inreplyto);
1138                         wprintf("\"><span>[</span>%s<span>]</span></a> ", _("ReplyAll"));
1139                 }
1140
1141                 /* Forward */
1142                 if (WC->wc_view == VIEW_MAILBOX) {
1143                         wprintf("<a href=\"display_enter?fwdquote=%ld?subject=", msgnum);
1144                         if (strncasecmp(m_subject, "Fwd:", 4)) wprintf("Fwd:%%20");
1145                         urlescputs(m_subject);
1146                         wprintf("\"><span>[</span>%s<span>]</span></a> ", _("Forward"));
1147                 }
1148
1149                 /* If this is one of my own rooms, or if I'm an Aide or Room Aide, I can move/delete */
1150                 if ( (WC->is_room_aide) || (WC->is_mailbox) || (WC->room_flags2 & QR2_COLLABDEL) ) {
1151                         /** Move */
1152                         wprintf("<a href=\"confirm_move_msg?msgid=%ld\"><span>[</span>%s<span>]</span></a> ",
1153                                 msgnum, _("Move"));
1154         
1155                         /** Delete */
1156                         wprintf("<a href=\"delete_msg?msgid=%ld\" "
1157                                 "onClick=\"return confirm('%s');\">"
1158                                 "<span>[</span>%s<span>]</span> "
1159                                 "</a> ", msgnum, _("Delete this message?"), _("Delete")
1160                         );
1161                 }
1162
1163                 /* Headers */
1164                 wprintf("<a href=\"#\" onClick=\"window.open('msgheaders/%ld', 'headers%ld', 'toolbar=no,location=no,directories=no,copyhistory=no,status=yes,scrollbars=yes,resizable=yes,width=600,height=400'); \" >"
1165                         "<span>[</span>%s<span>]</span></a>", msgnum, msgnum, _("Headers"));
1166
1167
1168                 /* Print */
1169                 wprintf("<a href=\"#\" onClick=\"window.open('printmsg/%ld', 'print%ld', 'toolbar=no,location=no,directories=no,copyhistory=no,status=yes,scrollbars=yes,resizable=yes,width=600,height=400'); \" >"
1170                         "<span>[</span>%s<span>]</span></a>", msgnum, msgnum, _("Print"));
1171
1172                 wprintf("</p>");
1173
1174         }
1175
1176         if (!IsEmptyStr(m_cc)) {
1177                 wprintf("<p>");
1178                 wprintf(_("CC:"));
1179                 wprintf(" ");
1180                 escputs(m_cc);
1181                 wprintf("</p>");
1182         }
1183         if (!IsEmptyStr(m_subject)) {
1184                 wprintf("<p class=\"message_subject\">");
1185                 wprintf(_("Subject:"));
1186                 wprintf(" ");
1187                 escputs(m_subject);
1188                 wprintf("</p>");
1189         }
1190
1191         wprintf("</div>");
1192
1193         /* Begin body */
1194         wprintf("<div class=\"message_content\">");
1195
1196         /*
1197          * Learn the content type
1198          */
1199         strcpy(mime_content_type, "text/plain");
1200         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
1201                 if (!strcmp(buf, "000")) {
1202                         /* This is not necessarily an error condition.  See bug #226. */
1203                         goto ENDBODY;
1204                 }
1205                 if (!strncasecmp(buf, "X-Citadel-MSG4-Partnum:", 23)) {
1206                         safestrncpy(msg4_partnum, &buf[23], sizeof(msg4_partnum));
1207                         striplt(msg4_partnum);
1208                 }
1209                 if (!strncasecmp(buf, "Content-type:", 13)) {
1210                         int len;
1211                         safestrncpy(mime_content_type, &buf[13], sizeof(mime_content_type));
1212                         striplt(mime_content_type);
1213                         len = strlen(mime_content_type);
1214                         for (i=0; i<len; ++i) {
1215                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1216                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1217                                                 sizeof mime_charset);
1218                                 }
1219                         }
1220                         for (i=0; i<len; ++i) {
1221                                 if (mime_content_type[i] == ';') {
1222                                         mime_content_type[i] = 0;
1223                                         len = i - 1;
1224                                 }
1225                         }
1226                         len = strlen(mime_charset);
1227                         for (i=0; i<len; ++i) {
1228                                 if (mime_charset[i] == ';') {
1229                                         mime_charset[i] = 0;
1230                                         len = i - 1;
1231                                 }
1232                         }
1233                 }
1234         }
1235
1236         /* Set up a character set conversion if we need to (and if we can) */
1237 #ifdef HAVE_ICONV
1238         if (strchr(mime_charset, ';')) strcpy(strchr(mime_charset, ';'), "");
1239         if ( (strcasecmp(mime_charset, "us-ascii"))
1240            && (strcasecmp(mime_charset, "UTF-8"))
1241            && (strcasecmp(mime_charset, ""))
1242         ) {
1243                 ic = ctdl_iconv_open("UTF-8", mime_charset);
1244                 if (ic == (iconv_t)(-1) ) {
1245                         lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
1246                                 __FILE__, __LINE__, mime_charset, strerror(errno));
1247                 }
1248         }
1249 #endif
1250
1251         /* Messages in legacy Citadel variformat get handled thusly... */
1252         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1253                 fmout("JUSTIFY");
1254         }
1255
1256         /* Boring old 80-column fixed format text gets handled this way... */
1257         else if ( (!strcasecmp(mime_content_type, "text/plain"))
1258                 || (!strcasecmp(mime_content_type, "text")) ) {
1259                 buf [0] = '\0';
1260                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1261                         int len;
1262                         len = strlen(buf);
1263                         if ((len > 0) && buf[len-1] == '\n') buf[--len] = 0;
1264                         if ((len > 0) && buf[len-1] == '\r') buf[--len] = 0;
1265
1266 #ifdef HAVE_ICONV
1267                         if (ic != (iconv_t)(-1) ) {
1268                                 ibuf = buf;
1269                                 ibuflen = strlen(ibuf);
1270                                 obuflen = SIZ;
1271                                 obuf = (char *) malloc(obuflen);
1272                                 osav = obuf;
1273                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1274                                 osav[SIZ-obuflen] = 0;
1275                                 safestrncpy(buf, osav, sizeof buf);
1276                                 free(osav);
1277                         }
1278 #endif
1279
1280                         len = strlen(buf);
1281                         while ((!IsEmptyStr(buf)) && (isspace(buf[len-1])))
1282                                 buf[--len] = 0;
1283                         if ((bq == 0) &&
1284                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1285                                 wprintf("<blockquote>");
1286                                 bq = 1;
1287                         } else if ((bq == 1) &&
1288                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1289                                 wprintf("</blockquote>");
1290                                 bq = 0;
1291                         }
1292                         wprintf("<tt>");
1293                         url(buf, sizeof(buf));
1294                         escputs(buf);
1295                         wprintf("</tt><br />\n");
1296                 }
1297                 wprintf("</i><br />");
1298         }
1299
1300         /* HTML is fun, but we've got to strip it first */
1301         else if (!strcasecmp(mime_content_type, "text/html")) {
1302                 output_html(mime_charset, (WC->wc_view == VIEW_WIKI ? 1 : 0));
1303         }
1304
1305         /* Unknown weirdness */
1306         else {
1307                 wprintf(_("I don't know how to display %s"), mime_content_type);
1308                 wprintf("<br />\n");
1309                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1310         }
1311
1312 ENDBODY:        /* If there are attached submessages, display them now... */
1313
1314         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
1315                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
1316                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
1317                         /** use printable_view to suppress buttons */
1318                         wprintf("<blockquote>");
1319                         read_message(msgnum, 1, buf);
1320                         wprintf("</blockquote>");
1321                 }
1322         }
1323
1324
1325         /* Afterwards, offer links to download attachments 'n' such */
1326         if ( (num_attach_links > 0) && (!section[0]) ) {
1327                 for (i=0; i<num_attach_links; ++i) {
1328                         if (strcasecmp(attach_links[i].partnum, msg4_partnum)) {
1329                                 wprintf("%s", attach_links[i].html);
1330                         }
1331                 }
1332         }
1333
1334         /* Handler for vCard parts */
1335         if (!IsEmptyStr(vcard_partnum)) {
1336                 part_source = load_mimepart(msgnum, vcard_partnum);
1337                 if (part_source != NULL) {
1338
1339                         /** If it's my vCard I can edit it */
1340                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1341                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1342                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1343                         ) {
1344                                 wprintf("<a href=\"edit_vcard?msgnum=%ld?partnum=%s\">",
1345                                         msgnum, vcard_partnum);
1346                                 wprintf("[%s]</a>", _("edit"));
1347                         }
1348
1349                         /* In all cases, display the full card */
1350                         display_vcard(part_source, 0, 1, NULL,msgnum);
1351                 }
1352         }
1353
1354         /* Handler for calendar parts */
1355         if (!IsEmptyStr(cal_partnum)) {
1356                 part_source = load_mimepart(msgnum, cal_partnum);
1357                 if (part_source != NULL) {
1358                         cal_process_attachment(part_source, msgnum, cal_partnum);
1359                 }
1360         }
1361
1362         if (part_source) {
1363                 free(part_source);
1364                 part_source = NULL;
1365         }
1366
1367         wprintf("</div>\n");
1368
1369         /* end everythingamundo table */
1370         if (!printable_view) {
1371                 wprintf("</div>\n");
1372         }
1373
1374         if (num_attach_links > 0) {
1375                 free(attach_links);
1376         }
1377
1378 #ifdef HAVE_ICONV
1379         if (ic != (iconv_t)(-1) ) {
1380                 iconv_close(ic);
1381         }
1382 #endif
1383 }
1384
1385
1386
1387 /*
1388  * Unadorned HTML output of an individual message, suitable
1389  * for placing in a hidden iframe, for printing, or whatever
1390  *
1391  * msgnum_as_string == Message number, as a string instead of as a long int
1392  */
1393 void embed_message(void) {
1394         long msgnum = 0L;
1395
1396         msgnum = StrTol(WC->UrlFragment1);
1397         read_message(msgnum, 0, "");
1398 }
1399
1400
1401 /*
1402  * Printable view of a message
1403  *
1404  * msgnum_as_string == Message number, as a string instead of as a long int
1405  */
1406 void print_message(void) {
1407         long msgnum = 0L;
1408
1409         msgnum = StrTol(WC->UrlFragment1);
1410         output_headers(0, 0, 0, 0, 0, 0);
1411
1412         wprintf("Content-type: text/html\r\n"
1413                 "Server: %s\r\n"
1414                 "Connection: close\r\n",
1415                 PACKAGE_STRING);
1416         begin_burst();
1417
1418         wprintf("\r\n\r\n<html>\n<head><title>");
1419         escputs(WC->wc_fullname);
1420         wprintf("</title></head>\n"
1421                 "<body onLoad=\" window.print(); window.close(); \">\n"
1422         );
1423         
1424         read_message(msgnum, 1, "");
1425
1426         wprintf("\n</body></html>\n\n");
1427         wDumpContent(0);
1428 }
1429
1430
1431
1432 /**
1433  * \brief Display a message's headers
1434  *
1435  * \param msgnum_as_string Message number, as a string instead of as a long int
1436  */
1437 void display_headers(void) {
1438         long msgnum = 0L;
1439         char buf[1024];
1440
1441         msgnum = StrTol(WC->UrlFragment1);
1442         output_headers(0, 0, 0, 0, 0, 0);
1443
1444         wprintf("Content-type: text/plain\r\n"
1445                 "Server: %s\r\n"
1446                 "Connection: close\r\n",
1447                 PACKAGE_STRING);
1448         begin_burst();
1449
1450         serv_printf("MSG2 %ld|3", msgnum);
1451         serv_getln(buf, sizeof buf);
1452         if (buf[0] == '1') {
1453                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1454                         wprintf("%s\n", buf);
1455                 }
1456         }
1457
1458         wDumpContent(0);
1459 }
1460
1461
1462
1463 /**
1464  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
1465  *      or 'reply quoted' operations.
1466  *
1467  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
1468  *       in this function.  Doing so would throw a JavaScript error in the
1469  *       'supplied text' argument to the editor.
1470  *
1471  * \param msgnum Message number of the message we want to quote
1472  * \param forward_attachments Nonzero if we want attachments to be forwarded
1473  */
1474 void pullquote_message(long msgnum, int forward_attachments, int include_headers) {
1475         char buf[SIZ];
1476         char mime_partnum[256];
1477         char mime_filename[256];
1478         char mime_content_type[256];
1479         char mime_charset[256];
1480         char mime_disposition[256];
1481         int mime_length;
1482         char *attachments = NULL;
1483         char *ptr = NULL;
1484         int num_attachments = 0;
1485         struct wc_attachment *att, *aptr;
1486         char m_subject[1024];
1487         char from[256];
1488         char node[256];
1489         char rfca[256];
1490         char to[256];
1491         char reply_to[512];
1492         char now[256];
1493         int format_type = 0;
1494         int nhdr = 0;
1495         int bq = 0;
1496         int i = 0;
1497 #ifdef HAVE_ICONV
1498         iconv_t ic = (iconv_t)(-1) ;
1499         char *ibuf;                /**< Buffer of characters to be converted */
1500         char *obuf;                /**< Buffer for converted characters      */
1501         size_t ibuflen;    /**< Length of input buffer         */
1502         size_t obuflen;    /**< Length of output buffer       */
1503         char *osav;                /**< Saved pointer to output buffer       */
1504 #endif
1505
1506         strcpy(from, "");
1507         strcpy(node, "");
1508         strcpy(rfca, "");
1509         strcpy(reply_to, "");
1510         strcpy(mime_content_type, "text/plain");
1511         strcpy(mime_charset, "us-ascii");
1512
1513         serv_printf("MSG4 %ld", msgnum);
1514         serv_getln(buf, sizeof buf);
1515         if (buf[0] != '1') {
1516                 wprintf(_("ERROR:"));
1517                 wprintf("%s<br />", &buf[4]);
1518                 return;
1519         }
1520
1521         strcpy(m_subject, "");
1522
1523         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
1524                 if (!strcmp(buf, "000")) {
1525                         wprintf("%s (3)", _("unexpected end of message"));
1526                         return;
1527                 }
1528                 if (include_headers) {
1529                         if (!strncasecmp(buf, "nhdr=yes", 8))
1530                                 nhdr = 1;
1531                         if (nhdr == 1)
1532                                 buf[0] = '_';
1533                         if (!strncasecmp(buf, "type=", 5))
1534                                 format_type = atoi(&buf[5]);
1535                         if (!strncasecmp(buf, "from=", 5)) {
1536                                 strcpy(from, &buf[5]);
1537                                 wprintf(_("from "));
1538                                 utf8ify_rfc822_string(from);
1539                                 msgescputs(from);
1540                         }
1541                         if (!strncasecmp(buf, "subj=", 5)) {
1542                                 strcpy(m_subject, &buf[5]);
1543                         }
1544                         if ((!strncasecmp(buf, "hnod=", 5))
1545                             && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
1546                                 wprintf("(%s) ", &buf[5]);
1547                         }
1548                         if ((!strncasecmp(buf, "room=", 5))
1549                             && (strcasecmp(&buf[5], WC->wc_roomname))
1550                             && (!IsEmptyStr(&buf[5])) ) {
1551                                 wprintf(_("in "));
1552                                 wprintf("%s&gt; ", &buf[5]);
1553                         }
1554                         if (!strncasecmp(buf, "rfca=", 5)) {
1555                                 strcpy(rfca, &buf[5]);
1556                                 wprintf("&lt;");
1557                                 msgescputs(rfca);
1558                                 wprintf("&gt; ");
1559                         }
1560                         if (!strncasecmp(buf, "node=", 5)) {
1561                                 strcpy(node, &buf[5]);
1562                                 if ( ((WC->room_flags & QR_NETWORK)
1563                                 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
1564                                 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
1565                                 && (IsEmptyStr(rfca))
1566                                 ) {
1567                                         wprintf("@%s ", &buf[5]);
1568                                 }
1569                         }
1570                         if (!strncasecmp(buf, "rcpt=", 5)) {
1571                                 wprintf(_("to "));
1572                                 strcpy(to, &buf[5]);
1573                                 utf8ify_rfc822_string(to);
1574                                 wprintf("%s ", to);
1575                         }
1576                         if (!strncasecmp(buf, "time=", 5)) {
1577                                 webcit_fmt_date(now, atol(&buf[5]), 0);
1578                                 wprintf("%s ", now);
1579                         }
1580                 }
1581
1582                 /**
1583                  * Save attachment info for later.  We can't start downloading them
1584                  * yet because we're in the middle of a server transaction.
1585                  */
1586                 if (!strncasecmp(buf, "part=", 5)) {
1587                         ptr = malloc( (strlen(buf) + ((attachments != NULL) ? strlen(attachments) : 0)) ) ;
1588                         if (ptr != NULL) {
1589                                 ++num_attachments;
1590                                 sprintf(ptr, "%s%s\n",
1591                                         ((attachments != NULL) ? attachments : ""),
1592                                         &buf[5]
1593                                 );
1594                                 free(attachments);
1595                                 attachments = ptr;
1596                                 lprintf(9, "attachments=<%s>\n", attachments);
1597                         }
1598                 }
1599
1600         }
1601
1602         if (include_headers) {
1603                 wprintf("<br>");
1604
1605                 utf8ify_rfc822_string(m_subject);
1606                 if (!IsEmptyStr(m_subject)) {
1607                         wprintf(_("Subject:"));
1608                         wprintf(" ");
1609                         msgescputs(m_subject);
1610                         wprintf("<br />");
1611                 }
1612
1613                 /**
1614                  * Begin body
1615                  */
1616                 wprintf("<br />");
1617         }
1618
1619         /**
1620          * Learn the content type
1621          */
1622         strcpy(mime_content_type, "text/plain");
1623         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
1624                 if (!strcmp(buf, "000")) {
1625                         wprintf("%s (4)", _("unexpected end of message"));
1626                         goto ENDBODY;
1627                 }
1628                 if (!strncasecmp(buf, "Content-type: ", 14)) {
1629                         int len;
1630                         safestrncpy(mime_content_type, &buf[14],
1631                                 sizeof(mime_content_type));
1632                         for (i=0; i<strlen(mime_content_type); ++i) {
1633                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1634                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1635                                                 sizeof mime_charset);
1636                                 }
1637                         }
1638                         len = strlen(mime_content_type);
1639                         for (i=0; i<len; ++i) {
1640                                 if (mime_content_type[i] == ';') {
1641                                         mime_content_type[i] = 0;
1642                                         len = i - 1;
1643                                 }
1644                         }
1645                         len = strlen(mime_charset);
1646                         for (i=0; i<len; ++i) {
1647                                 if (mime_charset[i] == ';') {
1648                                         mime_charset[i] = 0;
1649                                         len = i - 1;
1650                                 }
1651                         }
1652                 }
1653         }
1654
1655         /** Set up a character set conversion if we need to (and if we can) */
1656 #ifdef HAVE_ICONV
1657         if ( (strcasecmp(mime_charset, "us-ascii"))
1658            && (strcasecmp(mime_charset, "UTF-8"))
1659            && (strcasecmp(mime_charset, ""))
1660         ) {
1661                 ic = ctdl_iconv_open("UTF-8", mime_charset);
1662                 if (ic == (iconv_t)(-1) ) {
1663                         lprintf(5, "%s:%d iconv_open(%s, %s) failed: %s\n",
1664                                 __FILE__, __LINE__, "UTF-8", mime_charset, strerror(errno));
1665                 }
1666         }
1667 #endif
1668
1669         /** Messages in legacy Citadel variformat get handled thusly... */
1670         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1671                 pullquote_fmout();
1672         }
1673
1674         /* Boring old 80-column fixed format text gets handled this way... */
1675         else if (!strcasecmp(mime_content_type, "text/plain")) {
1676                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1677                         int len;
1678                         len = strlen(buf);
1679                         if (buf[len-1] == '\n') buf[--len] = 0;
1680                         if (buf[len-1] == '\r') buf[--len] = 0;
1681
1682 #ifdef HAVE_ICONV
1683                         if (ic != (iconv_t)(-1) ) {
1684                                 ibuf = buf;
1685                                 ibuflen = len;
1686                                 obuflen = SIZ;
1687                                 obuf = (char *) malloc(obuflen);
1688                                 osav = obuf;
1689                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1690                                 osav[SIZ-obuflen] = 0;
1691                                 safestrncpy(buf, osav, sizeof buf);
1692                                 free(osav);
1693                         }
1694 #endif
1695
1696                         len = strlen(buf);
1697                         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1]))) 
1698                                 buf[--len] = 0;
1699                         if ((bq == 0) &&
1700                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1701                                 wprintf("<blockquote>");
1702                                 bq = 1;
1703                         } else if ((bq == 1) &&
1704                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1705                                 wprintf("</blockquote>");
1706                                 bq = 0;
1707                         }
1708                         wprintf("<tt>");
1709                         url(buf, sizeof(buf));
1710                         msgescputs1(buf);
1711                         wprintf("</tt><br />");
1712                 }
1713                 wprintf("</i><br />");
1714         }
1715
1716         /** HTML just gets escaped and stuffed back into the editor */
1717         else if (!strcasecmp(mime_content_type, "text/html")) {
1718                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1719                         strcat(buf, "\n");
1720                         msgescputs(buf);
1721                 }
1722         }//// TODO: charset? utf8?
1723
1724         /** Unknown weirdness ... don't know how to handle this content type */
1725         else {
1726                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1727         }
1728
1729 ENDBODY:
1730         /** end of body handler */
1731
1732         /*
1733          * If there were attachments, we have to download them and insert them
1734          * into the attachment chain for the forwarded message we are composing.
1735          */
1736         if ( (forward_attachments) && (num_attachments) ) {
1737                 for (i=0; i<num_attachments; ++i) {
1738                         extract_token(buf, attachments, i, '\n', sizeof buf);
1739                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
1740                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
1741                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
1742                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
1743                         mime_length = extract_int(buf, 5);
1744
1745                         /*
1746                          * tracing  ... uncomment if necessary
1747                          *
1748                          */
1749                         lprintf(9, "fwd filename: %s\n", mime_filename);
1750                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
1751                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
1752                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
1753                         lprintf(9, "fwd length  : %d\n", mime_length);
1754
1755                         if ( (!strcasecmp(mime_disposition, "inline"))
1756                            || (!strcasecmp(mime_disposition, "attachment")) ) {
1757                 
1758                                 /* Create an attachment struct from this mime part... */
1759                                 att = malloc(sizeof(struct wc_attachment));
1760                                 memset(att, 0, sizeof(struct wc_attachment));
1761                                 att->length = mime_length;
1762                                 strcpy(att->content_type, mime_content_type);
1763                                 strcpy(att->filename, mime_filename);
1764                                 att->next = NULL;
1765                                 att->data = load_mimepart(msgnum, mime_partnum);
1766                 
1767                                 /* And add it to the list. */
1768                                 if (WC->first_attachment == NULL) {
1769                                         WC->first_attachment = att;
1770                                 }
1771                                 else {
1772                                         aptr = WC->first_attachment;
1773                                         while (aptr->next != NULL) aptr = aptr->next;
1774                                         aptr->next = att;
1775                                 }
1776                         }
1777
1778                 }
1779         }
1780
1781 #ifdef HAVE_ICONV
1782         if (ic != (iconv_t)(-1) ) {
1783                 iconv_close(ic);
1784         }
1785 #endif
1786
1787         if (attachments != NULL) {
1788                 free(attachments);
1789         }
1790 }
1791
1792 /**
1793  * \brief Display one row in the mailbox summary view
1794  *
1795  * \param num The row number to be displayed
1796  */
1797 void display_summarized(int num) {
1798         char datebuf[64];
1799         wprintf("<tr id=\"m%ld\" style=\"font-weight:%s;\" "
1800                 "onMouseDown=\"CtdlMoveMsgMouseDown(event,%ld)\">",
1801                 WC->summ[num].msgnum,
1802                 (WC->summ[num].is_new ? "bold" : "normal"),
1803                 WC->summ[num].msgnum
1804         );
1805
1806         wprintf("<td width=%d%%>", SUBJ_COL_WIDTH_PCT);
1807
1808         escputs(WC->summ[num].subj);//////////////////////////////////TODO: QP DECODE
1809         wprintf("</td>");
1810
1811         wprintf("<td width=%d%%>", SENDER_COL_WIDTH_PCT);
1812         escputs(WC->summ[num].from);
1813         wprintf("</td>");
1814
1815         wprintf("<td width=%d%%>", DATE_PLUS_BUTTONS_WIDTH_PCT);
1816         webcit_fmt_date(datebuf, WC->summ[num].date, 1);        /* brief */
1817         escputs(datebuf);
1818         wprintf("</td>");
1819
1820         wprintf("</tr>\n");
1821 }
1822 /**
1823  * \brief Output a message row for the mobile view
1824  * \param The row number 
1825  */
1826 void display_mobile_summary(int num) {
1827         char datebuf[64];
1828         wprintf("\n<div><div id=\"m%ld\" style=\"font-weight:%s;\" "
1829                 "onClick=\"CtdlLoadMsgMouseDown(event,%ld)\">",
1830                 WC->summ[num].msgnum,
1831                 (WC->summ[num].is_new ? "bold" : "normal"),
1832                 WC->summ[num].msgnum
1833         );
1834                 wprintf("<span>%s</span>",WC->summ[num].from);
1835                 wprintf("<span style=\"float: right;\">");
1836                 webcit_fmt_date(datebuf, WC->summ[num].date, 1);        /* brief */
1837         escputs(datebuf);
1838                 wprintf("</span><br/><span class=\"subject\">");
1839                 wprintf(WC->summ[num].subj);
1840                 wprintf("</span></div><div id=\"m_%ld\" class=\"msgview\" onMouseDown=\"\"></div></div>",WC->summ[num].msgnum);
1841 }
1842
1843 /**
1844  * \brief display the adressbook overview
1845  * \param msgnum the citadel message number
1846  * \param alpha what????
1847  */
1848 void display_addressbook(long msgnum, char alpha) {
1849         char buf[SIZ];
1850         char mime_partnum[SIZ];
1851         char mime_filename[SIZ];
1852         char mime_content_type[SIZ];
1853         char mime_disposition[SIZ];
1854         int mime_length;
1855         char vcard_partnum[SIZ];
1856         char *vcard_source = NULL;
1857         struct message_summary summ;
1858
1859         memset(&summ, 0, sizeof(summ));
1860         safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
1861
1862         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1863         serv_puts(buf);
1864         serv_getln(buf, sizeof buf);
1865         if (buf[0] != '1') return;
1866
1867         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1868                 if (!strncasecmp(buf, "part=", 5)) {
1869                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1870                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1871                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1872                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1873                         mime_length = extract_int(&buf[5], 5);
1874
1875                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1876                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1877                                 strcpy(vcard_partnum, mime_partnum);
1878                         }
1879
1880                 }
1881         }
1882
1883         if (!IsEmptyStr(vcard_partnum)) {
1884                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1885                 if (vcard_source != NULL) {
1886
1887                         /** Display the summary line */
1888                         display_vcard(vcard_source, alpha, 0, NULL,msgnum);
1889
1890                         /** If it's my vCard I can edit it */
1891                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1892                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1893                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1894                         ) {
1895                                 wprintf("<a href=\"edit_vcard?"
1896                                         "msgnum=%ld?partnum=%s\">",
1897                                         msgnum, vcard_partnum);
1898                                 wprintf("[%s]</a>", _("edit"));
1899                         }
1900
1901                         free(vcard_source);
1902                 }
1903         }
1904
1905 }
1906
1907
1908
1909 /**
1910  * \brief  If it's an old "Firstname Lastname" style record, try to convert it.
1911  * \param namebuf name to analyze, reverse if nescessary
1912  */
1913 void lastfirst_firstlast(char *namebuf) {
1914         char firstname[SIZ];
1915         char lastname[SIZ];
1916         int i;
1917
1918         if (namebuf == NULL) return;
1919         if (strchr(namebuf, ';') != NULL) return;
1920
1921         i = num_tokens(namebuf, ' ');
1922         if (i < 2) return;
1923
1924         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1925         remove_token(namebuf, i-1, ' ');
1926         strcpy(firstname, namebuf);
1927         sprintf(namebuf, "%s; %s", lastname, firstname);
1928 }
1929
1930 /**
1931  * \brief fetch what??? name
1932  * \param msgnum the citadel message number
1933  * \param namebuf where to put the name in???
1934  */
1935 void fetch_ab_name(long msgnum, char *namebuf) {
1936         char buf[SIZ];
1937         char mime_partnum[SIZ];
1938         char mime_filename[SIZ];
1939         char mime_content_type[SIZ];
1940         char mime_disposition[SIZ];
1941         int mime_length;
1942         char vcard_partnum[SIZ];
1943         char *vcard_source = NULL;
1944         int i, len;
1945         struct message_summary summ;
1946
1947         if (namebuf == NULL) return;
1948         strcpy(namebuf, "");
1949
1950         memset(&summ, 0, sizeof(summ));
1951         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1952
1953         sprintf(buf, "MSG0 %ld|0", msgnum);     /** unfortunately we need the mime info now */
1954         serv_puts(buf);
1955         serv_getln(buf, sizeof buf);
1956         if (buf[0] != '1') return;
1957
1958         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1959                 if (!strncasecmp(buf, "part=", 5)) {
1960                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1961                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1962                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1963                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1964                         mime_length = extract_int(&buf[5], 5);
1965
1966                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1967                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1968                                 strcpy(vcard_partnum, mime_partnum);
1969                         }
1970
1971                 }
1972         }
1973
1974         if (!IsEmptyStr(vcard_partnum)) {
1975                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1976                 if (vcard_source != NULL) {
1977
1978                         /* Grab the name off the card */
1979                         display_vcard(vcard_source, 0, 0, namebuf,msgnum);
1980
1981                         free(vcard_source);
1982                 }
1983         }
1984
1985         lastfirst_firstlast(namebuf);
1986         striplt(namebuf);
1987         len = strlen(namebuf);
1988         for (i=0; i<len; ++i) {
1989                 if (namebuf[i] != ';') return;
1990         }
1991         strcpy(namebuf, _("(no name)"));
1992 }
1993
1994
1995
1996 /**
1997  * \brief Record compare function for sorting address book indices
1998  * \param ab1 adressbook one
1999  * \param ab2 adressbook two
2000  */
2001 int abcmp(const void *ab1, const void *ab2) {
2002         return(strcasecmp(
2003                 (((const struct addrbookent *)ab1)->ab_name),
2004                 (((const struct addrbookent *)ab2)->ab_name)
2005         ));
2006 }
2007
2008
2009 /**
2010  * \brief Helper function for do_addrbook_view()
2011  * Converts a name into a three-letter tab label
2012  * \param tabbuf the tabbuffer to add name to
2013  * \param name the name to add to the tabbuffer
2014  */
2015 void nametab(char *tabbuf, long len, char *name) {
2016         stresc(tabbuf, len, name, 0, 0);
2017         tabbuf[0] = toupper(tabbuf[0]);
2018         tabbuf[1] = tolower(tabbuf[1]);
2019         tabbuf[2] = tolower(tabbuf[2]);
2020         tabbuf[3] = 0;
2021 }
2022
2023
2024 /**
2025  * \brief Render the address book using info we gathered during the scan
2026  * \param addrbook the addressbook to render
2027  * \param num_ab the number of the addressbook
2028  */
2029 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
2030         int i = 0;
2031         int displayed = 0;
2032         int bg = 0;
2033         static int NAMESPERPAGE = 60;
2034         int num_pages = 0;
2035         int tabfirst = 0;
2036         char tabfirst_label[64];
2037         int tablast = 0;
2038         char tablast_label[64];
2039         char this_tablabel[64];
2040         int page = 0;
2041         char **tablabels;
2042
2043         if (num_ab == 0) {
2044                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
2045                 wprintf(_("This address book is empty."));
2046                 wprintf("</i></div>\n");
2047                 return;
2048         }
2049
2050         if (num_ab > 1) {
2051                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
2052         }
2053
2054         num_pages = (num_ab / NAMESPERPAGE) + 1;
2055
2056         tablabels = malloc(num_pages * sizeof (char *));
2057         if (tablabels == NULL) {
2058                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
2059                 wprintf(_("An internal error has occurred."));
2060                 wprintf("</i></div>\n");
2061                 return;
2062         }
2063
2064         for (i=0; i<num_pages; ++i) {
2065                 tabfirst = i * NAMESPERPAGE;
2066                 tablast = tabfirst + NAMESPERPAGE - 1;
2067                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
2068                 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
2069                 nametab(tablast_label, 64, addrbook[tablast].ab_name);
2070                 sprintf(this_tablabel, "%s&nbsp;-&nbsp;%s", tabfirst_label, tablast_label);
2071                 tablabels[i] = strdup(this_tablabel);
2072         }
2073
2074         tabbed_dialog(num_pages, tablabels);
2075         page = (-1);
2076
2077         for (i=0; i<num_ab; ++i) {
2078
2079                 if ((i / NAMESPERPAGE) != page) {       /* New tab */
2080                         page = (i / NAMESPERPAGE);
2081                         if (page > 0) {
2082                                 wprintf("</tr></table>\n");
2083                                 end_tab(page-1, num_pages);
2084                         }
2085                         begin_tab(page, num_pages);
2086                         wprintf("<table border=0 cellspacing=0 cellpadding=3 width=100%%>\n");
2087                         displayed = 0;
2088                 }
2089
2090                 if ((displayed % 4) == 0) {
2091                         if (displayed > 0) {
2092                                 wprintf("</tr>\n");
2093                         }
2094                         bg = 1 - bg;
2095                         wprintf("<tr bgcolor=\"#%s\">",
2096                                 (bg ? "DDDDDD" : "FFFFFF")
2097                         );
2098                 }
2099         
2100                 wprintf("<td>");
2101
2102                 wprintf("<a href=\"readfwd?startmsg=%ld&is_singlecard=1",
2103                         addrbook[i].ab_msgnum);
2104                 wprintf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
2105                 vcard_n_prettyize(addrbook[i].ab_name);
2106                 escputs(addrbook[i].ab_name);
2107                 wprintf("</a></td>\n");
2108                 ++displayed;
2109         }
2110
2111         wprintf("</tr></table>\n");
2112         end_tab((num_pages-1), num_pages);
2113
2114         for (i=0; i<num_pages; ++i) {
2115                 free(tablabels[i]);
2116         }
2117         free(tablabels);
2118 }
2119
2120
2121
2122 /**
2123  * \brief load message pointers from the server
2124  * \param servcmd the citadel command to send to the citserver
2125  * \param with_headers what headers???
2126  */
2127 int load_msg_ptrs(char *servcmd, int with_headers)
2128 {
2129         char buf[1024];
2130         time_t datestamp;
2131         char fullname[128];
2132         char nodename[128];
2133         char inetaddr[128];
2134         char subject[1024];
2135         char *ptr;
2136         int nummsgs;
2137         int sbjlen;
2138         int maxload = 0;
2139
2140         int num_summ_alloc = 0;
2141
2142         if (WC->summ != NULL) {
2143                 free(WC->summ);
2144                 WC->num_summ = 0;
2145                 WC->summ = NULL;
2146         }
2147         num_summ_alloc = 100;
2148         WC->num_summ = 0;
2149         WC->summ = malloc(num_summ_alloc * sizeof(struct message_summary));
2150
2151         nummsgs = 0;
2152         maxload = sizeof(WC->msgarr) / sizeof(long) ;
2153         serv_puts(servcmd);
2154         serv_getln(buf, sizeof buf);
2155         if (buf[0] != '1') {
2156                 return (nummsgs);
2157         }
2158         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2159                 if (nummsgs < maxload) {
2160                         WC->msgarr[nummsgs] = extract_long(buf, 0);
2161                         datestamp = extract_long(buf, 1);
2162                         extract_token(fullname, buf, 2, '|', sizeof fullname);
2163                         extract_token(nodename, buf, 3, '|', sizeof nodename);
2164                         extract_token(inetaddr, buf, 4, '|', sizeof inetaddr);
2165                         extract_token(subject, buf, 5, '|', sizeof subject);
2166                         ++nummsgs;
2167
2168                         if (with_headers) {
2169                                 if (nummsgs > num_summ_alloc) {
2170                                         num_summ_alloc *= 2;
2171                                         WC->summ = realloc(WC->summ,
2172                                                 num_summ_alloc * sizeof(struct message_summary));
2173                                 }
2174                                 ++WC->num_summ;
2175
2176                                 memset(&WC->summ[nummsgs-1], 0, sizeof(struct message_summary));
2177                                 WC->summ[nummsgs-1].msgnum = WC->msgarr[nummsgs-1];
2178                                 safestrncpy(WC->summ[nummsgs-1].subj,
2179                                         _("(no subject)"), sizeof WC->summ[nummsgs-1].subj);
2180                                 if (!IsEmptyStr(subject)) {
2181                                 /** Handle subjects with RFC2047 encoding */
2182                                         utf8ify_rfc822_string(subject);
2183                                         safestrncpy(WC->summ[nummsgs-1].subj, subject,
2184                                                     sizeof WC->summ[nummsgs-1].subj);
2185                                 }
2186                                 sbjlen = Ctdl_Utf8StrLen(WC->summ[nummsgs-1].subj);
2187                                 if (sbjlen > 75) {
2188                                         ptr = Ctdl_Utf8StrCut(WC->summ[nummsgs-1].subj, 72);
2189
2190                                         strcpy(ptr, "...");
2191                                 }
2192
2193                                 if (!IsEmptyStr(fullname)) {
2194                                 /** Handle senders with RFC2047 encoding */
2195                                         utf8ify_rfc822_string(fullname);
2196                                         safestrncpy(WC->summ[nummsgs-1].from,
2197                                                 fullname, sizeof WC->summ[nummsgs-1].from);
2198                                 }
2199                                 if ((!IsEmptyStr(nodename)) &&
2200                                     ( ((WC->room_flags & QR_NETWORK)
2201                                        || ((strcasecmp(nodename, serv_info.serv_nodename)
2202                                                  && (strcasecmp(nodename, serv_info.serv_fqdn)))))))
2203                                 {
2204                                         strcat(WC->summ[nummsgs-1].from, " @ ");
2205                                         strcat(WC->summ[nummsgs-1].from, nodename);
2206                                         
2207                                 }
2208                                 sbjlen = Ctdl_Utf8StrLen(WC->summ[nummsgs-1].from);
2209                                 if (sbjlen > 25) {
2210                                         ptr = Ctdl_Utf8StrCut(WC->summ[nummsgs-1].from, 23);
2211                                         strcpy(ptr, "...");
2212                                 }
2213
2214                                 WC->summ[nummsgs-1].date = datestamp;
2215                         }
2216                 }
2217         }
2218         return (nummsgs);
2219 }
2220
2221
2222 typedef int (*QSortFunction) (const void*, const void*);
2223 /**
2224  * \brief qsort() compatible function to compare two longs in descending order.
2225  *
2226  * \param s1 first number to compare 
2227  * \param s2 second number to compare
2228  */
2229 int longcmp_r(const void *s1, const void *s2) {
2230         long l1;
2231         long l2;
2232
2233         l1 = *(long *)s1;
2234         l2 = *(long *)s2;
2235
2236         if (l1 > l2) return(-1);
2237         if (l1 < l2) return(+1);
2238         return(0);
2239 }
2240
2241  
2242 /**
2243  * \brief qsort() compatible function to compare two message summary structs by ascending subject.
2244  *
2245  * \param s1 first item to compare 
2246  * \param s2 second item to compare
2247  */
2248 int summcmp_subj(const void *s1, const void *s2) {
2249         struct message_summary *summ1;
2250         struct message_summary *summ2;
2251         
2252         summ1 = (struct message_summary *)s1;
2253         summ2 = (struct message_summary *)s2;
2254         return strcasecmp(summ1->subj, summ2->subj);
2255 }
2256
2257 /**
2258  * \brief qsort() compatible function to compare two message summary structs by descending subject.
2259  *
2260  * \param s1 first item to compare 
2261  * \param s2 second item to compare
2262  */
2263 int summcmp_rsubj(const void *s1, const void *s2) {
2264         struct message_summary *summ1;
2265         struct message_summary *summ2;
2266         
2267         summ1 = (struct message_summary *)s1;
2268         summ2 = (struct message_summary *)s2;
2269         return strcasecmp(summ2->subj, summ1->subj);
2270 }
2271
2272 /**
2273  * \brief qsort() compatible function to compare two message summary structs by ascending sender.
2274  *
2275  * \param s1 first item to compare 
2276  * \param s2 second item to compare
2277  */
2278 int summcmp_sender(const void *s1, const void *s2) {
2279         struct message_summary *summ1;
2280         struct message_summary *summ2;
2281         
2282         summ1 = (struct message_summary *)s1;
2283         summ2 = (struct message_summary *)s2;
2284         return strcasecmp(summ1->from, summ2->from);
2285 }
2286
2287 /**
2288  * \brief qsort() compatible function to compare two message summary structs by descending sender.
2289  *
2290  * \param s1 first item to compare 
2291  * \param s2 second item to compare
2292  */
2293 int summcmp_rsender(const void *s1, const void *s2) {
2294         struct message_summary *summ1;
2295         struct message_summary *summ2;
2296         
2297         summ1 = (struct message_summary *)s1;
2298         summ2 = (struct message_summary *)s2;
2299         return strcasecmp(summ2->from, summ1->from);
2300 }
2301
2302 /**
2303  * \brief qsort() compatible function to compare two message summary structs by ascending date.
2304  *
2305  * \param s1 first item to compare 
2306  * \param s2 second item to compare
2307  */
2308 int summcmp_date(const void *s1, const void *s2) {
2309         struct message_summary *summ1;
2310         struct message_summary *summ2;
2311         
2312         summ1 = (struct message_summary *)s1;
2313         summ2 = (struct message_summary *)s2;
2314
2315         if (summ1->date < summ2->date) return -1;
2316         else if (summ1->date > summ2->date) return +1;
2317         else return 0;
2318 }
2319
2320 /**
2321  * \brief qsort() compatible function to compare two message summary structs by descending date.
2322  *
2323  * \param s1 first item to compare 
2324  * \param s2 second item to compare
2325  */
2326 int summcmp_rdate(const void *s1, const void *s2) {
2327         struct message_summary *summ1;
2328         struct message_summary *summ2;
2329         
2330         summ1 = (struct message_summary *)s1;
2331         summ2 = (struct message_summary *)s2;
2332
2333         if (summ1->date < summ2->date) return +1;
2334         else if (summ1->date > summ2->date) return -1;
2335         else return 0;
2336 }
2337
2338
2339 enum {
2340         eUp,
2341         eDown,
2342         eNone
2343 };
2344
2345 const char* SortIcons[3] = {
2346         "static/up_pointer.gif",
2347         "static/down_pointer.gif",
2348         "static/sort_none.gif"
2349 };
2350
2351  enum  {/// SortByEnum
2352         eDate,
2353         eRDate,
2354         eSubject,
2355         eRSubject,
2356         eSender,
2357         eRSender,
2358         eReverse,
2359         eUnSet
2360 }; 
2361
2362 /** SortEnum to plain string representation */
2363 static const char* SortByStrings[] = {
2364         "date",
2365         "rdate",
2366         "subject", 
2367         "rsubject", 
2368         "sender",
2369         "rsender",
2370         "reverse",
2371         "unset"
2372 };
2373
2374 /** SortEnum to sort-Function Table */
2375 const QSortFunction SortFuncs[eUnSet] = {
2376         summcmp_date,
2377         summcmp_rdate,
2378         summcmp_subj,
2379         summcmp_rsubj,
2380         summcmp_sender,
2381         summcmp_rsender,
2382         summcmp_rdate
2383 };
2384
2385 /** given a SortEnum, which icon should we choose? */
2386 const int SortDateToIcon[eUnSet] = { eUp, eDown, eNone, eNone, eNone, eNone, eNone};
2387 const int SortSubjectToIcon[eUnSet] = { eNone, eNone, eUp, eDown, eNone, eNone, eNone};
2388 const int SortSenderToIcon[eUnSet] = { eNone, eNone, eNone, eNone, eUp, eDown, eNone};
2389
2390 /** given a SortEnum, which would be the "opposite" search option? */
2391 const int DateInvertSortString[eUnSet] =  { eRDate, eDate, eDate, eDate, eDate, eDate, eDate};
2392 const int SubjectInvertSortString[eUnSet] =  { eSubject, eSubject, eRSubject, eUnSet, eSubject, eSubject, eSubject};
2393 const int SenderInvertSortString[eUnSet] =  { eSender, eSender, eSender, eSender, eRSender, eUnSet, eSender};
2394
2395
2396 /**
2397  * \Brief Translates sortoption String to its SortEnum representation 
2398  * \param SortBy string to translate
2399  * \return the enum matching the string; defaults to RDate
2400  */
2401 //SortByEnum 
2402 int StrToESort (StrBuf *sortby)
2403 {
2404         int result = eDate;
2405
2406         if (!IsEmptyStr(ChrPtr(sortby))) while (result < eUnSet){
2407                         if (!strcasecmp(ChrPtr(sortby), 
2408                                         SortByStrings[result])) 
2409                                 return result;
2410                         result ++;
2411                 }
2412         return eRDate;
2413 }
2414
2415
2416
2417
2418 /**
2419  * \brief command loop for reading messages
2420  *
2421  * \param oper Set to "readnew" or "readold" or "readfwd" or "headers"
2422  */
2423 void readloop(char *oper)
2424 {
2425         char cmd[256] = "";
2426         char buf[SIZ];
2427         char old_msgs[SIZ];
2428         int a, b;
2429         int nummsgs;
2430         long startmsg;
2431         int maxmsgs;
2432         long *displayed_msgs = NULL;
2433         int num_displayed = 0;
2434         int is_summary = 0;
2435         int is_addressbook = 0;
2436         int is_singlecard = 0;
2437         int is_calendar = 0;
2438         int is_tasks = 0;
2439         int is_notes = 0;
2440         int is_bbview = 0;
2441         int lo, hi;
2442         int lowest_displayed = (-1);
2443         int highest_displayed = 0;
2444         struct addrbookent *addrbook = NULL;
2445         int num_ab = 0;
2446         StrBuf *sortby = NULL;
2447         //SortByEnum 
2448         int SortBy = eRDate;
2449         StrBuf *sortpref_name;
2450         StrBuf *sortpref_value;
2451         int bbs_reverse = 0;
2452         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
2453
2454         if (WCC->wc_view == VIEW_WIKI) {
2455                 sprintf(buf, "wiki?room=%s?page=home", WCC->wc_roomname);
2456                 http_redirect(buf);
2457                 return;
2458         }
2459
2460         startmsg = lbstr("startmsg");
2461         maxmsgs = ibstr("maxmsgs");
2462         is_summary = (ibstr("is_summary") && !WCC->is_mobile);
2463         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
2464
2465         sortpref_name = NewStrBuf ();
2466         StrBufPrintf(sortpref_name, "sort %s", WCC->wc_roomname);
2467         get_pref(sortpref_name, &sortpref_value);
2468
2469         sortby = NewStrBufPlain(bstr("sortby"), -1);
2470         if ( (!IsEmptyStr(ChrPtr(sortby))) && 
2471              (strcasecmp(ChrPtr(sortby), ChrPtr(sortpref_value)) != 0)) {
2472                 set_pref(sortpref_name, sortby, 1);
2473                 sortpref_value = NULL;
2474                 sortpref_value = sortby;
2475         }
2476         
2477         FreeStrBuf(&sortpref_name);
2478         SortBy = StrToESort(sortpref_value);
2479         /** message board sort */
2480         if (SortBy == eReverse) {
2481                 bbs_reverse = 1;
2482         }
2483         else {
2484                 bbs_reverse = 0;
2485         }
2486
2487         output_headers(1, 1, 1, 0, 0, 0);
2488
2489         /**
2490          * When in summary mode, always show ALL messages instead of just
2491          * new or old.  Otherwise, show what the user asked for.
2492          */
2493         if (!strcmp(oper, "readnew")) {
2494                 strcpy(cmd, "MSGS NEW");
2495         }
2496         else if (!strcmp(oper, "readold")) {
2497                 strcpy(cmd, "MSGS OLD");
2498         }
2499         else if (!strcmp(oper, "do_search")) {
2500                 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2501         }
2502         else {
2503                 strcpy(cmd, "MSGS ALL");
2504         }
2505
2506         if ((WCC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1) && !WCC->is_mobile) {
2507                 is_summary = 1;
2508                 if (!strcmp(oper, "do_search")) {
2509                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2510                 }
2511                 else {
2512                         strcpy(cmd, "MSGS ALL");
2513                 }
2514         }
2515
2516         if ((WCC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
2517                 is_addressbook = 1;
2518                 if (!strcmp(oper, "do_search")) {
2519                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2520                 }
2521                 else {
2522                         strcpy(cmd, "MSGS ALL");
2523                 }
2524                 maxmsgs = 9999999;
2525         }
2526
2527         if (is_summary || WCC->is_mobile) {                     /**< fetch header summary */
2528                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
2529                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
2530                         (!strcmp(oper, "do_search") ? bstr("query") : "")
2531                 );
2532                 startmsg = 1;
2533                 maxmsgs = 9999999;
2534         } 
2535         if (WCC->is_mobile) {
2536                 maxmsgs = 20;
2537                 SortBy =  eRDate;
2538         }
2539
2540         /**
2541          * Are we doing a summary view?  If so, we need to know old messages
2542          * and new messages, so we can do that pretty boldface thing for the
2543          * new messages.
2544          */
2545         strcpy(old_msgs, "");
2546         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
2547                 serv_puts("GTSN");
2548                 serv_getln(buf, sizeof buf);
2549                 if (buf[0] == '2') {
2550                         strcpy(old_msgs, &buf[4]);
2551                 }
2552         }
2553
2554         is_singlecard = ibstr("is_singlecard");
2555
2556         if (WCC->wc_default_view == VIEW_CALENDAR) {            /**< calendar */
2557                 is_calendar = 1;
2558                 strcpy(cmd, "MSGS ALL|||1");
2559                 maxmsgs = 32767;
2560         }
2561         if (WCC->wc_default_view == VIEW_TASKS) {               /**< tasks */
2562                 is_tasks = 1;
2563                 strcpy(cmd, "MSGS ALL");
2564                 maxmsgs = 32767;
2565         }
2566         if (WCC->wc_default_view == VIEW_NOTES) {               /**< notes */
2567                 is_notes = 1;
2568                 strcpy(cmd, "MSGS ALL");
2569                 maxmsgs = 32767;
2570         }
2571
2572         if (is_notes) {
2573                 wprintf("<div id=\"new_notes_here\"></div>\n");
2574         }
2575
2576         nummsgs = load_msg_ptrs(cmd, (is_summary || WCC->is_mobile));
2577         if (nummsgs == 0) {
2578
2579                 if ((!is_tasks) && (!is_calendar) && (!is_notes) && (!is_addressbook)) {
2580                         wprintf("<div align=\"center\"><br /><em>");
2581                         if (!strcmp(oper, "readnew")) {
2582                                 wprintf(_("No new messages."));
2583                         } else if (!strcmp(oper, "readold")) {
2584                                 wprintf(_("No old messages."));
2585                         } else {
2586                                 wprintf(_("No messages here."));
2587                         }
2588                         wprintf("</em><br /></div>\n");
2589                 }
2590
2591                 goto DONE;
2592         }
2593
2594         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
2595                 for (a = 0; a < nummsgs; ++a) {
2596                         /** Are you a new message, or an old message? */
2597                         if (is_summary) {
2598                                 if (is_msg_in_mset(old_msgs, WCC->msgarr[a])) {
2599                                         WCC->summ[a].is_new = 0;
2600                                 }
2601                                 else {
2602                                         WCC->summ[a].is_new = 1;
2603                                 }
2604                         }
2605                 }
2606         }
2607
2608         if (startmsg == 0L) {
2609                 if (bbs_reverse) {
2610                         startmsg = WCC->msgarr[(nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0];
2611                 }
2612                 else {
2613                         startmsg = WCC->msgarr[0];
2614                 }
2615         }
2616
2617         if (is_summary || WCC->is_mobile) {
2618                 qsort(WCC->summ, WCC->num_summ,
2619                       sizeof(struct message_summary), SortFuncs[SortBy]);
2620         }
2621
2622
2623         if (is_summary) {
2624
2625                 wprintf("<script language=\"javascript\" type=\"text/javascript\">"
2626                         " document.onkeydown = CtdlMsgListKeyPress;     "
2627                         " if (document.layers) {                        "
2628                         "       document.captureEvents(Event.KEYPRESS); "
2629                         " }                                             "
2630                         "</script>\n"
2631                 );
2632
2633                 /** note that Date and Delete are now in the same column */
2634                 wprintf("<div id=\"message_list_hdr\">"
2635                         "<div class=\"fix_scrollbar_bug\">"
2636                         "<table cellspacing=0 style=\"width:100%%\">"
2637                         "<tr>"
2638                 );
2639                 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"
2640                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> </th>\n"
2641                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> \n"
2642                         "&nbsp;"
2643                         "<input type=\"submit\" name=\"delete_button\" id=\"delbutton\" "
2644                         " onClick=\"CtdlDeleteSelectedMessages(event)\" "
2645                         " value=\"%s\">"
2646                         "</th>"
2647                         "</tr>\n"
2648                         ,
2649                         SUBJ_COL_WIDTH_PCT,
2650                         _("Subject"),
2651                         SortByStrings[SubjectInvertSortString[SortBy]],
2652                         SortIcons[SortSubjectToIcon[SortBy]],
2653                         SENDER_COL_WIDTH_PCT,
2654                         _("Sender"),
2655                         SortByStrings[SenderInvertSortString[SortBy]],
2656                         SortIcons[SortSenderToIcon[SortBy]],
2657                         DATE_PLUS_BUTTONS_WIDTH_PCT,
2658                         _("Date"),
2659                         SortByStrings[DateInvertSortString[SortBy]],
2660                         SortIcons[SortDateToIcon[SortBy]],
2661                         _("Delete")
2662                 );
2663                 wprintf("</table></div></div>\n");
2664                 wprintf("<div id=\"message_list\">"
2665
2666                         "<div class=\"fix_scrollbar_bug\">\n"
2667                         "<table class=\"mailbox_summary\" id=\"summary_headers\" "
2668                         "cellspacing=0 style=\"width:100%%;-moz-user-select:none;\">"
2669                 );
2670         } else if (WCC->is_mobile) {
2671                 wprintf("<div id=\"message_list\">");
2672         }
2673
2674
2675         /**
2676          * Set the "is_bbview" variable if it appears that we are looking at
2677          * a classic bulletin board view.
2678          */
2679         if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
2680               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
2681                 is_bbview = 1;
2682         }
2683
2684         /**
2685          * If we're not currently looking at ALL requested
2686          * messages, then display the selector bar
2687          */
2688         if (is_bbview) {
2689                 /** begin bbview scroller */
2690                 wprintf("<form name=\"msgomatictop\" class=\"selector_top\" > \n <p>");
2691                 wprintf(_("Reading #"));//// TODO this isn't used, should it? : , lowest_displayed, highest_displayed);
2692
2693                 wprintf("<select name=\"whichones\" size=\"1\" "
2694                         "OnChange=\"location.href=msgomatictop.whichones.options"
2695                         "[selectedIndex].value\">\n");
2696
2697                 if (bbs_reverse) {
2698                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2699                                 hi = b + 1;
2700                                 lo = b - maxmsgs + 2;
2701                                 if (lo < 1) lo = 1;
2702                                 wprintf("<option %s value="
2703                                         "\"%s"
2704                                         "?startmsg=%ld"
2705                                         "?maxmsgs=%d"
2706                                         "?is_summary=%d\">"
2707                                         "%d-%d</option> \n",
2708                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2709                                         oper,
2710                                         WCC->msgarr[lo-1],
2711                                         maxmsgs,
2712                                         is_summary,
2713                                         hi, lo);
2714                         }
2715                 }
2716                 else {
2717                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2718                                 lo = b + 1;
2719                                 hi = b + maxmsgs + 1;
2720                                 if (hi > nummsgs) hi = nummsgs;
2721                                 wprintf("<option %s value="
2722                                         "\"%s"
2723                                         "?startmsg=%ld"
2724                                         "?maxmsgs=%d"
2725                                         "?is_summary=%d\">"
2726                                         "%d-%d</option> \n",
2727                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
2728                                         oper,
2729                                         WCC->msgarr[lo-1],
2730                                         maxmsgs,
2731                                         is_summary,
2732                                         lo, hi);
2733                         }
2734                 }
2735
2736                 wprintf("<option value=\"%s?startmsg=%ld"
2737                         "?maxmsgs=9999999?is_summary=%d\">",
2738                         oper,
2739                         WCC->msgarr[0], is_summary);
2740                 wprintf(_("All"));
2741                 wprintf("</option>");
2742                 wprintf("</select> ");
2743                 wprintf(_("of %d messages."), nummsgs);
2744
2745                 /** forward/reverse */
2746                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2747                         "OnChange=\"location.href='%s?sortby=forward'\"",  
2748                         (bbs_reverse ? "" : "checked"),
2749                         oper
2750                 );
2751                 wprintf(">");
2752                 wprintf(_("oldest to newest"));
2753                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
2754
2755                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2756                         "OnChange=\"location.href='%s?sortby=reverse'\"", 
2757                         (bbs_reverse ? "checked" : ""),
2758                         oper
2759                 );
2760                 wprintf(">");
2761                 wprintf(_("newest to oldest"));
2762                 wprintf("\n");
2763         
2764                 wprintf("</p></form>\n");
2765                 /** end bbview scroller */
2766         }
2767         for (a = 0; a < nummsgs; ++a) {
2768                 if ((WCC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
2769
2770                         /** Display the message */
2771                         if (is_summary) {
2772                                 display_summarized(a);
2773                         }
2774                         else if (is_addressbook) {
2775                                 fetch_ab_name(WCC->msgarr[a], buf);
2776                                 ++num_ab;
2777                                 addrbook = realloc(addrbook,
2778                                         (sizeof(struct addrbookent) * num_ab) );
2779                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
2780                                         sizeof(addrbook[num_ab-1].ab_name));
2781                                 addrbook[num_ab-1].ab_msgnum = WCC->msgarr[a];
2782                         }
2783                         else if (is_calendar) {
2784                                 display_calendar(WCC->msgarr[a], WCC->summ[a].is_new);
2785                         }
2786                         else if (is_tasks) {
2787                                 display_task(WCC->msgarr[a], WCC->summ[a].is_new);
2788                         }
2789                         else if (is_notes) {
2790                                 display_note(WCC->msgarr[a], WCC->summ[a].is_new);
2791                         } else if (WCC->is_mobile) {
2792                                 display_mobile_summary(a);
2793                         }
2794                         else {
2795                                 if (displayed_msgs == NULL) {
2796                                         displayed_msgs = malloc(sizeof(long) *
2797                                                                 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
2798                                 }
2799                                 displayed_msgs[num_displayed] = WCC->msgarr[a];
2800                         }
2801
2802                         if (lowest_displayed < 0) lowest_displayed = a;
2803                         highest_displayed = a;
2804
2805                         ++num_displayed;
2806                 }
2807         }
2808
2809         /** Output loop */
2810         if (displayed_msgs != NULL) {
2811                 if (bbs_reverse) {
2812                         qsort(displayed_msgs, num_displayed, sizeof(long), longcmp_r);
2813                 }
2814
2815                 /** if we do a split bbview in the future, begin messages div here */
2816
2817                 for (a=0; a<num_displayed; ++a) {
2818                         read_message(displayed_msgs[a], 0, "");
2819                 }
2820
2821                 /** if we do a split bbview in the future, end messages div here */
2822
2823                 free(displayed_msgs);
2824                 displayed_msgs = NULL;
2825         }
2826
2827         if (is_summary) {
2828                 wprintf("</table>"
2829                         "</div>\n");                    /**< end of 'fix_scrollbar_bug' div */
2830                 wprintf("</div>");                      /**< end of 'message_list' div */
2831                 
2832                 /** Here's the grab-it-to-resize-the-message-list widget */
2833                 wprintf("<div id=\"resize_msglist\" "
2834                         "onMouseDown=\"CtdlResizeMsgListMouseDown(event)\">"
2835                         "<div class=\"fix_scrollbar_bug\"> <hr>"
2836                         "</div></div>\n"
2837                 );
2838
2839                 wprintf("<div id=\"preview_pane\">");   /**< The preview pane will initially be empty */
2840         } else if (WCC->is_mobile) {
2841                 wprintf("</div>");
2842         }
2843
2844         /**
2845          * Bump these because although we're thinking in zero base, the user
2846          * is a drooling idiot and is thinking in one base.
2847          */
2848         ++lowest_displayed;
2849         ++highest_displayed;
2850
2851         /**
2852          * If we're not currently looking at ALL requested
2853          * messages, then display the selector bar
2854          */
2855         if (is_bbview) {
2856                 /** begin bbview scroller */
2857                 wprintf("<form name=\"msgomatic\" class=\"selector_bottom\" > \n <p>");
2858                 wprintf(_("Reading #")); /// TODO: this isn't used: , lowest_displayed, highest_displayed);
2859
2860                 wprintf("<select name=\"whichones\" size=\"1\" "
2861                         "OnChange=\"location.href=msgomatic.whichones.options"
2862                         "[selectedIndex].value\">\n");
2863
2864                 if (bbs_reverse) {
2865                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2866                                 hi = b + 1;
2867                                 lo = b - maxmsgs + 2;
2868                                 if (lo < 1) lo = 1;
2869                                 wprintf("<option %s value="
2870                                         "\"%s"
2871                                         "?startmsg=%ld"
2872                                         "?maxmsgs=%d"
2873                                         "?is_summary=%d\">"
2874                                         "%d-%d</option> \n",
2875                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2876                                         oper,
2877                                         WCC->msgarr[lo-1],
2878                                         maxmsgs,
2879                                         is_summary,
2880                                         hi, lo);
2881                         }
2882                 }
2883                 else {
2884                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2885                                 lo = b + 1;
2886                                 hi = b + maxmsgs + 1;
2887                                 if (hi > nummsgs) hi = nummsgs;
2888                                 wprintf("<option %s value="
2889                                         "\"%s"
2890                                         "?startmsg=%ld"
2891                                         "?maxmsgs=%d"
2892                                         "?is_summary=%d\">"
2893                                         "%d-%d</option> \n",
2894                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
2895                                         oper,
2896                                         WCC->msgarr[lo-1],
2897                                         maxmsgs,
2898                                         is_summary,
2899                                         lo, hi);
2900                         }
2901                 }
2902
2903                 wprintf("<option value=\"%s?startmsg=%ld"
2904                         "?maxmsgs=9999999?is_summary=%d\">",
2905                         oper,
2906                         WCC->msgarr[0], is_summary);
2907                 wprintf(_("All"));
2908                 wprintf("</option>");
2909                 wprintf("</select> ");
2910                 wprintf(_("of %d messages."), nummsgs);
2911
2912                 /** forward/reverse */
2913                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2914                         "OnChange=\"location.href='%s?sortby=forward'\"",  
2915                         (bbs_reverse ? "" : "checked"),
2916                         oper
2917                 );
2918                 wprintf(">");
2919                 wprintf(_("oldest to newest"));
2920                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
2921                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2922                         "OnChange=\"location.href='%s?sortby=reverse'\"", 
2923                         (bbs_reverse ? "checked" : ""),
2924                         oper
2925                 );
2926                 wprintf(">");
2927                 wprintf(_("newest to oldest"));
2928                 wprintf("\n");
2929
2930                 wprintf("</p></form>\n");
2931                 /** end bbview scroller */
2932         }
2933         
2934 DONE:
2935         if (is_tasks) {
2936                 do_tasks_view();        /** Render the task list */
2937         }
2938
2939         if (is_calendar) {
2940                 do_calendar_view();     /** Render the calendar */
2941         }
2942
2943         if (is_addressbook) {
2944                 do_addrbook_view(addrbook, num_ab);     /** Render the address book */
2945         }
2946
2947         /** Note: wDumpContent() will output one additional </div> tag. */
2948         wprintf("</div>\n");            /** end of 'content' div */
2949         wDumpContent(1);
2950
2951         /** free the summary */
2952         if (WCC->summ != NULL) {
2953                 free(WCC->summ);
2954                 WCC->num_summ = 0;
2955                 WCC->summ = NULL;
2956         }
2957         if (addrbook != NULL) free(addrbook);
2958 }
2959
2960
2961 /*
2962  * Back end for post_message()
2963  * ... this is where the actual message gets transmitted to the server.
2964  */
2965 void post_mime_to_server(void) {
2966         char top_boundary[SIZ];
2967         char alt_boundary[SIZ];
2968         int is_multipart = 0;
2969         static int seq = 0;
2970         struct wc_attachment *att;
2971         char *encoded;
2972         size_t encoded_length;
2973         size_t encoded_strlen;
2974         char *txtmail = NULL;
2975
2976         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
2977                 serv_info.serv_fqdn,
2978                 getpid(),
2979                 ++seq
2980         );
2981         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
2982                 serv_info.serv_fqdn,
2983                 getpid(),
2984                 ++seq
2985         );
2986
2987         /* RFC2045 requires this, and some clients look for it... */
2988         serv_puts("MIME-Version: 1.0");
2989         serv_puts("X-Mailer: " PACKAGE_STRING);
2990
2991         /* If there are attachments, we have to do multipart/mixed */
2992         if (WC->first_attachment != NULL) {
2993                 is_multipart = 1;
2994         }
2995
2996         if (is_multipart) {
2997                 /* Remember, serv_printf() appends an extra newline */
2998                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
2999                 serv_printf("This is a multipart message in MIME format.\n");
3000                 serv_printf("--%s", top_boundary);
3001         }
3002
3003         /* Remember, serv_printf() appends an extra newline */
3004         serv_printf("Content-type: multipart/alternative; "
3005                 "boundary=\"%s\"\n", alt_boundary);
3006         serv_printf("This is a multipart message in MIME format.\n");
3007         serv_printf("--%s", alt_boundary);
3008
3009         serv_puts("Content-type: text/plain; charset=utf-8");
3010         serv_puts("Content-Transfer-Encoding: quoted-printable");
3011         serv_puts("");
3012         txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
3013         text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
3014         free(txtmail);
3015
3016         serv_printf("--%s", alt_boundary);
3017
3018         serv_puts("Content-type: text/html; charset=utf-8");
3019         serv_puts("Content-Transfer-Encoding: quoted-printable");
3020         serv_puts("");
3021         serv_puts("<html><body>\r\n");
3022         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
3023         serv_puts("</body></html>\r\n");
3024
3025         serv_printf("--%s--", alt_boundary);
3026         
3027         if (is_multipart) {
3028
3029                 /* Add in the attachments */
3030                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
3031
3032                         encoded_length = ((att->length * 150) / 100);
3033                         encoded = malloc(encoded_length);
3034                         if (encoded == NULL) break;
3035                         encoded_strlen = CtdlEncodeBase64(encoded, att->data, att->length, 1);
3036
3037                         serv_printf("--%s", top_boundary);
3038                         serv_printf("Content-type: %s", att->content_type);
3039                         serv_printf("Content-disposition: attachment; filename=\"%s\"", att->filename);
3040                         serv_puts("Content-transfer-encoding: base64");
3041                         serv_puts("");
3042                         serv_write(encoded, encoded_strlen);
3043                         serv_puts("");
3044                         serv_puts("");
3045                         free(encoded);
3046                 }
3047                 serv_printf("--%s--", top_boundary);
3048         }
3049
3050         serv_puts("000");
3051 }
3052
3053
3054 /*
3055  * Post message (or don't post message)
3056  *
3057  * Note regarding the "dont_post" variable:
3058  * A random value (actually, it's just a timestamp) is inserted as a hidden
3059  * field called "postseq" when the display_enter page is generated.  This
3060  * value is checked when posting, using the static variable dont_post.  If a
3061  * user attempts to post twice using the same dont_post value, the message is
3062  * discarded.  This prevents the accidental double-saving of the same message
3063  * if the user happens to click the browser "back" button.
3064  */
3065 void post_message(void)
3066 {
3067         urlcontent *u;
3068         void *U;
3069         char buf[1024];
3070         char *encoded_subject = NULL;
3071         static long dont_post = (-1L);
3072         struct wc_attachment *att, *aptr;
3073         int is_anonymous = 0;
3074         const char *display_name;
3075         long dpLen = 0;
3076         struct wcsession *WCC = WC;
3077         char *ptr = NULL;
3078
3079         if (havebstr("force_room")) {
3080                 gotoroom(bstr("force_room"));
3081         }
3082
3083         if (GetHash(WC->urlstrings, HKEY("display_name"), &U)) {
3084                 u = (urlcontent*) U;
3085                 display_name = u->url_data;
3086                 dpLen = u->url_data_size;
3087         }
3088         else {
3089                 display_name="";
3090         }
3091         if (!strcmp(display_name, "__ANONYMOUS__")) {
3092                 display_name = "";
3093                 is_anonymous = 1;
3094         }
3095
3096         if (WCC->upload_length > 0) {
3097
3098                 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
3099                 /** There's an attachment.  Save it to this struct... */
3100                 att = malloc(sizeof(struct wc_attachment));
3101                 memset(att, 0, sizeof(struct wc_attachment));
3102                 att->length = WCC->upload_length;
3103                 strcpy(att->content_type, WCC->upload_content_type);
3104                 strcpy(att->filename, WCC->upload_filename);
3105                 att->next = NULL;
3106
3107                 /** And add it to the list. */
3108                 if (WCC->first_attachment == NULL) {
3109                         WCC->first_attachment = att;
3110                 }
3111                 else {
3112                         aptr = WCC->first_attachment;
3113                         while (aptr->next != NULL) aptr = aptr->next;
3114                         aptr->next = att;
3115                 }
3116
3117                 /**
3118                  * Mozilla sends a simple filename, which is what we want,
3119                  * but Satan's Browser sends an entire pathname.  Reduce
3120                  * the path to just a filename if we need to.
3121                  */
3122                 while (num_tokens(att->filename, '/') > 1) {
3123                         remove_token(att->filename, 0, '/');
3124                 }
3125                 while (num_tokens(att->filename, '\\') > 1) {
3126                         remove_token(att->filename, 0, '\\');
3127                 }
3128
3129                 /**
3130                  * Transfer control of this memory from the upload struct
3131                  * to the attachment struct.
3132                  */
3133                 att->data = WCC->upload;
3134                 WCC->upload_length = 0;
3135                 WCC->upload = NULL;
3136                 display_enter();
3137                 return;
3138         }
3139
3140         if (havebstr("cancel_button")) {
3141                 sprintf(WCC->ImportantMessage, 
3142                         _("Cancelled.  Message was not posted."));
3143         } else if (havebstr("attach_button")) {
3144                 display_enter();
3145                 return;
3146         } else if (lbstr("postseq") == dont_post) {
3147                 sprintf(WCC->ImportantMessage, 
3148                         _("Automatically cancelled because you have already "
3149                         "saved this message."));
3150         } else {
3151                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
3152                 const char *Recp = ""; 
3153                 const char *Cc = "";
3154                 const char *Bcc = "";
3155                 const char *Wikipage = "";
3156                 const char *my_email_addr = "";
3157                 char *CmdBuf = NULL;;
3158                 long len = 0;
3159                 size_t nLen;
3160                 char references[SIZ] = "";
3161                 size_t references_len = 0;
3162
3163                 safestrncpy(references, bstr("references"), sizeof references);
3164                 lprintf(9, "Converting: %s\n", references);
3165                 for (ptr=references; *ptr != 0; ++ptr) {
3166                         if (*ptr == '|') *ptr = '!';
3167                         ++references_len;
3168                 } 
3169                 lprintf(9, "Converted: %s\n", references);
3170
3171                 if (havebstr("subject")) {
3172                         char *Subj;
3173                         size_t SLen;
3174                         /*
3175                          * make enough room for the encoded string; 
3176                          * plus the QP header 
3177                          */
3178                         Subj = xbstr("subject", &SLen);
3179                         len = SLen * 3 + 32;
3180                         encoded_subject = malloc (len);
3181                         len = webcit_rfc2047encode(encoded_subject, len, Subj, SLen);
3182                         if (len < 0) {
3183                                 free (encoded_subject);
3184                                 return;
3185                         }
3186                 }
3187                 len += sizeof (CMD) + dpLen;
3188                 Recp = xbstr("recp", &nLen);
3189                 len += nLen;
3190                 Cc = xbstr("cc", &nLen);
3191                 len += nLen;
3192                 Bcc = xbstr("bcc", &nLen);
3193                 len += nLen;
3194                 Wikipage = xbstr("wikipage", &nLen);
3195                 len += nLen;
3196                 my_email_addr = xbstr("my_email_addr", &nLen);
3197                 len += nLen;
3198                 len += references_len;
3199
3200                 CmdBuf = (char*) malloc (len + 11);
3201
3202                 snprintf(CmdBuf, len + 1, CMD,
3203                         Recp,
3204                         is_anonymous,
3205                         (encoded_subject ? encoded_subject : ""),
3206                         display_name,
3207                         Cc,
3208                         Bcc,
3209                         Wikipage,
3210                         my_email_addr,
3211                         references);
3212                 lprintf(9, "%s\n", CmdBuf);
3213                 serv_puts(CmdBuf);
3214                 serv_getln(buf, sizeof buf);
3215                 free (CmdBuf);
3216                 if (encoded_subject) free(encoded_subject);
3217                 if (buf[0] == '4') {
3218                         post_mime_to_server();
3219                         if (  (havebstr("recp"))
3220                            || (havebstr("cc"  ))
3221                            || (havebstr("bcc" ))
3222                         ) {
3223                                 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
3224                         }
3225                         else {
3226                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
3227                         }
3228                         dont_post = lbstr("postseq");
3229                 } else {
3230                         lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
3231                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3232                         display_enter();
3233                         return;
3234                 }
3235         }
3236
3237         free_attachments(WCC);
3238
3239         /**
3240          *  We may have been supplied with instructions regarding the location
3241          *  to which we must return after posting.  If found, go there.
3242          */
3243         if (havebstr("return_to")) {
3244                 http_redirect(bstr("return_to"));
3245         }
3246         /**
3247          *  If we were editing a page in a wiki room, go to that page now.
3248          */
3249         else if (havebstr("wikipage")) {
3250                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
3251                 http_redirect(buf);
3252         }
3253         /**
3254          *  Otherwise, just go to the "read messages" loop.
3255          */
3256         else {
3257                 readloop("readnew");
3258         }
3259 }
3260
3261
3262
3263
3264 /**
3265  * \brief display the message entry screen
3266  */
3267 void display_enter(void)
3268 {
3269         char buf[SIZ];
3270         StrBuf *ebuf;
3271         long now;
3272         char *display_name;
3273         struct wc_attachment *att;
3274         int recipient_required = 0;
3275         int subject_required = 0;
3276         int recipient_bad = 0;
3277         int is_anonymous = 0;
3278         long existing_page = (-1L);
3279         size_t dplen;
3280         struct wcsession *WCC = WC;
3281
3282         now = time(NULL);
3283
3284         if (havebstr("force_room")) {
3285                 gotoroom(bstr("force_room"));
3286         }
3287
3288         display_name = xbstr("display_name", &dplen);
3289         if (!strcmp(display_name, "__ANONYMOUS__")) {
3290                 display_name = "";
3291                 dplen = 0;
3292                 is_anonymous = 1;
3293         }
3294
3295         /** First test to see whether this is a room that requires recipients to be entered */
3296         serv_puts("ENT0 0");
3297         serv_getln(buf, sizeof buf);
3298
3299         if (!strncmp(buf, "570", 3)) {          /** 570 means that we need a recipient here */
3300                 recipient_required = 1;
3301         }
3302         else if (buf[0] != '2') {               /** Any other error means that we cannot continue */
3303                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
3304                 readloop("readnew");
3305                 return;
3306         }
3307
3308         /* Is the server strongly recommending that the user enter a message subject? */
3309         if ((buf[3] != '\0') && (buf[4] != '\0')) {
3310                 subject_required = extract_int(&buf[4], 1);
3311         }
3312
3313         /**
3314          * Are we perhaps in an address book view?  If so, then an "enter
3315          * message" command really means "add new entry."
3316          */
3317         if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
3318                 do_edit_vcard(-1, "", "", WCC->wc_roomname);
3319                 return;
3320         }
3321
3322         /*
3323          * Are we perhaps in a calendar room?  If so, then an "enter
3324          * message" command really means "add new calendar item."
3325          */
3326         if (WCC->wc_default_view == VIEW_CALENDAR) {
3327                 display_edit_event();
3328                 return;
3329         }
3330
3331         /*
3332          * Are we perhaps in a tasks view?  If so, then an "enter
3333          * message" command really means "add new task."
3334          */
3335         if (WCC->wc_default_view == VIEW_TASKS) {
3336                 display_edit_task();
3337                 return;
3338         }
3339
3340         /*
3341          * Otherwise proceed normally.
3342          * Do a custom room banner with no navbar...
3343          */
3344         output_headers(1, 1, 2, 0, 0, 0);
3345         wprintf("<div id=\"banner\">\n");
3346         embed_room_banner(NULL, navbar_none);
3347         wprintf("</div>\n");
3348         wprintf("<div id=\"content\">\n"
3349                 "<div class=\"fix_scrollbar_bug message \">");
3350
3351         /* Now check our actual recipients if there are any */
3352         if (recipient_required) {
3353                 const char *Recp = ""; 
3354                 const char *Cc = "";
3355                 const char *Bcc = "";
3356                 const char *Wikipage = "";
3357                 char *CmdBuf = NULL;;
3358                 size_t len = 0;
3359                 size_t nLen;
3360                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
3361                 
3362                 len = sizeof(CMD) + dplen;
3363                 Recp = xbstr("recp", &nLen);
3364                 len += nLen;
3365                 Cc = xbstr("cc", &nLen);
3366                 len += nLen;
3367                 Bcc = xbstr("bcc", &nLen);
3368                 len += nLen;
3369                 Wikipage = xbstr("wikipage", &nLen);
3370                 len += nLen;
3371                 
3372
3373                 CmdBuf = (char*) malloc (len + 1);
3374
3375                 snprintf(CmdBuf, len, CMD,
3376                          Recp, is_anonymous,
3377                          display_name,
3378                          Cc, Bcc, Wikipage);
3379                 serv_puts(CmdBuf);
3380                 serv_getln(buf, sizeof buf);
3381
3382                 if (!strncmp(buf, "570", 3)) {  /** 570 means we have an invalid recipient listed */
3383                         if (havebstr("recp") && 
3384                             havebstr("cc"  ) && 
3385                             havebstr("bcc" )) {
3386                                 recipient_bad = 1;
3387                         }
3388                 }
3389                 else if (buf[0] != '2') {       /** Any other error means that we cannot continue */
3390                         wprintf("<em>%s</em><br />\n", &buf[4]);
3391                         goto DONE;
3392                 }
3393         }
3394
3395         /** If we got this far, we can display the message entry screen. */
3396
3397         /* begin message entry screen */
3398         wprintf("<form "
3399                 "enctype=\"multipart/form-data\" "
3400                 "method=\"POST\" "
3401                 "accept-charset=\"UTF-8\" "
3402                 "action=\"post\" "
3403                 "name=\"enterform\""
3404                 ">\n");
3405         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n", now);
3406         if (WCC->wc_view == VIEW_WIKI) {
3407                 wprintf("<input type=\"hidden\" name=\"wikipage\" value=\"%s\">\n", bstr("wikipage"));
3408         }
3409         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"%s\">\n", bstr("return_to"));
3410         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
3411         wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
3412         escputs(WCC->wc_roomname);
3413         wprintf("\">\n");
3414         wprintf("<input type=\"hidden\" name=\"references\" value=\"");
3415         escputs(bstr("references"));
3416         wprintf("\">\n");
3417
3418         /** submit or cancel buttons */
3419         wprintf("<p class=\"send_edit_msg\">");
3420         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
3421         if (recipient_required) {
3422                 wprintf(_("Send message"));
3423         } else {
3424                 wprintf(_("Post message"));
3425         }
3426         wprintf("\">&nbsp;"
3427                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
3428         wprintf("</p>");
3429
3430         /** header bar */
3431
3432         wprintf("<img src=\"static/newmess3_24x.gif\" class=\"imgedit\">");
3433         wprintf("  ");  /** header bar */
3434         webcit_fmt_date(buf, now, 0);
3435         wprintf("%s", buf);
3436         wprintf("\n");  /** header bar */
3437
3438         wprintf("<table width=\"100%%\" class=\"edit_msg_table\">");
3439         wprintf("<tr>");
3440         wprintf("<th><label for=\"from_id\" > ");
3441         wprintf(_(" <I>from</I> "));
3442         wprintf("</label></th>");
3443
3444         wprintf("<td colspan=\"2\">");
3445
3446         /* Allow the user to select any of his valid screen names */
3447
3448         wprintf("<select name=\"display_name\" size=1 id=\"from_id\">\n");
3449
3450         serv_puts("GVSN");
3451         serv_getln(buf, sizeof buf);
3452         if (buf[0] == '1') {
3453                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3454                         wprintf("<option %s value=\"",
3455                                 ((!strcasecmp(bstr("display_name"), buf)) ? "selected" : "")
3456                         );
3457                         escputs(buf);
3458                         wprintf("\">");
3459                         escputs(buf);
3460                         wprintf("</option>\n");
3461                 }
3462         }
3463
3464         if (WCC->room_flags & QR_ANONOPT) {
3465                 wprintf("<option %s value=\"__ANONYMOUS__\">%s</option>\n",
3466                         ((!strcasecmp(bstr("__ANONYMOUS__"), WCC->wc_fullname)) ? "selected" : ""),
3467                         _("Anonymous")
3468                 );
3469         }
3470
3471         wprintf("</select>\n");
3472
3473         /* If this is an email (not a post), allow the user to select any of his
3474          * valid email addresses.
3475          */
3476         if (recipient_required) {
3477                 serv_puts("GVEA");
3478                 serv_getln(buf, sizeof buf);
3479                 if (buf[0] == '1') {
3480                         wprintf("<select name=\"my_email_addr\" size=1>\n");
3481                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3482                                 wprintf("<option value=\"");
3483                                 escputs(buf);
3484                                 wprintf("\">&lt;");
3485                                 escputs(buf);
3486                                 wprintf("&gt;</option>\n");
3487                         }
3488                         wprintf("</select>\n");
3489                 }
3490         }
3491
3492         wprintf(_(" <I>in</I> "));
3493         escputs(WCC->wc_roomname);
3494
3495         wprintf("</td></tr>");
3496
3497         if (recipient_required) {
3498                 char *ccraw;
3499                 char *copy;
3500                 size_t len;
3501                 wprintf("<tr><th><label for=\"recp_id\"> ");
3502                 wprintf(_("To:"));
3503                 wprintf("</label></th>"
3504                         "<td><input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_id\" value=\"");
3505                 ccraw = xbstr("recp", &len);
3506                 copy = (char*) malloc(len * 2 + 1);
3507                 memcpy(copy, ccraw, len + 1); 
3508                 utf8ify_rfc822_string(copy);
3509                 escputs(copy);
3510                 free(copy);
3511                 wprintf("\" size=45 maxlength=1000 />");
3512                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
3513                 wprintf("</td><td rowspan=\"3\" align=\"left\" valign=\"top\">");
3514
3515                 /** Pop open an address book -- begin **/
3516                 wprintf(
3517                         "<a href=\"javascript:PopOpenAddressBook('recp_id|%s|cc_id|%s|bcc_id|%s');\" "
3518                         "title=\"%s\">"
3519                         "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
3520                         "&nbsp;%s</a>",
3521                         _("To:"), _("CC:"), _("BCC:"),
3522                         _("Contacts"), _("Contacts")
3523                 );
3524                 /** Pop open an address book -- end **/
3525
3526                 wprintf("</td></tr>");
3527
3528                 wprintf("<tr><th><label for=\"cc_id\"> ");
3529                 wprintf(_("CC:"));
3530                 wprintf("</label></th>"
3531                         "<td><input autocomplete=\"off\" type=\"text\" name=\"cc\" id=\"cc_id\" value=\"");
3532                 ccraw = xbstr("cc", &len);
3533                 copy = (char*) malloc(len * 2 + 1);
3534                 memcpy(copy, ccraw, len + 1); 
3535                 utf8ify_rfc822_string(copy);
3536                 escputs(copy);
3537                 free(copy);
3538                 wprintf("\" size=45 maxlength=1000 />");
3539                 wprintf("<div class=\"auto_complete\" id=\"cc_name_choices\"></div>");
3540                 wprintf("</td></tr>");
3541
3542                 wprintf("<tr><th><label for=\"bcc_id\"> ");
3543                 wprintf(_("BCC:"));
3544                 wprintf("</label></th>"
3545                         "<td><input autocomplete=\"off\" type=\"text\" name=\"bcc\" id=\"bcc_id\" value=\"");
3546                 ccraw = xbstr("bcc", &len);
3547                 copy = (char*) malloc(len * 2 + 1);
3548                 memcpy(copy, ccraw, len + 1); 
3549                 utf8ify_rfc822_string(copy);
3550                 escputs(copy);
3551                 free(copy);
3552                 wprintf("\" size=45 maxlength=1000 />");
3553                 wprintf("<div class=\"auto_complete\" id=\"bcc_name_choices\"></div>");
3554                 wprintf("</td></tr>");
3555
3556                 /** Initialize the autocomplete ajax helpers (found in wclib.js) */
3557                 wprintf("<script type=\"text/javascript\">      \n"
3558                         " activate_entmsg_autocompleters();     \n"
3559                         "</script>                              \n"
3560                 );
3561
3562         }
3563
3564         wprintf("<tr><th><label for=\"subject_id\" > ");
3565         if (recipient_required || subject_required) {
3566                 wprintf(_("Subject:"));
3567         }
3568         else {
3569                 wprintf(_("Subject (optional):"));
3570         }
3571         wprintf("</label></th>"
3572                 "<td colspan=\"2\">"
3573                 "<input type=\"text\" name=\"subject\" id=\"subject_id\" value=\"");
3574         escputs(bstr("subject"));
3575         wprintf("\" size=45 maxlength=70>\n");
3576         wprintf("</td></tr>");
3577
3578         wprintf("<tr><td colspan=\"3\">\n");
3579
3580         wprintf("<textarea name=\"msgtext\" cols=\"80\" rows=\"15\">");
3581
3582         /** If we're continuing from a previous edit, put our partially-composed message back... */
3583         msgescputs(bstr("msgtext"));
3584
3585         /* If we're forwarding a message, insert it here... */
3586         if (lbstr("fwdquote") > 0L) {
3587                 wprintf("<br><div align=center><i>");
3588                 wprintf(_("--- forwarded message ---"));
3589                 wprintf("</i></div><br>");
3590                 pullquote_message(lbstr("fwdquote"), 1, 1);
3591         }
3592
3593         /** If we're replying quoted, insert the quote here... */
3594         else if (lbstr("replyquote") > 0L) {
3595                 wprintf("<br>"
3596                         "<blockquote>");
3597                 pullquote_message(lbstr("replyquote"), 0, 1);
3598                 wprintf("</blockquote><br>");
3599         }
3600
3601         /** If we're editing a wiki page, insert the existing page here... */
3602         else if (WCC->wc_view == VIEW_WIKI) {
3603                 safestrncpy(buf, bstr("wikipage"), sizeof buf);
3604                 str_wiki_index(buf);
3605                 existing_page = locate_message_by_uid(buf);
3606                 if (existing_page >= 0L) {
3607                         pullquote_message(existing_page, 1, 0);
3608                 }
3609         }
3610
3611         /** Insert our signature if appropriate... */
3612         if ( (WCC->is_mailbox) && !yesbstr("sig_inserted") ) {
3613                 int UseSig;
3614                 get_pref_yesno("use_sig", &UseSig, 0);
3615                 if (UseSig) {
3616                         StrBuf *Sig;
3617                         const char *sig, *esig;
3618
3619                         get_preference("signature", &ebuf);
3620                         Sig = NewStrBuf();
3621                         StrBufEUid_unescapize(Sig, ebuf);
3622                         sig = ChrPtr(Sig);
3623                         esig = sig + StrLength(Sig);
3624                         wprintf("<br>--<br>");
3625                         while (sig <= esig) {
3626                                 if (*sig == '\n') {
3627                                         wprintf("<br>");
3628                                 }
3629                                 else if (*sig == '<') {
3630                                         wprintf("&lt;");
3631                                 }
3632                                 else if (*sig == '>') {
3633                                         wprintf("&gt;");
3634                                 }
3635                                 else if (*sig == '&') {
3636                                         wprintf("&amp;");
3637                                 }
3638                                 else if (*sig == '\"') {
3639                                         wprintf("&quot;");
3640                                 }
3641                                 else if (*sig == '\'') {
3642                                         wprintf("&#39;");
3643                                 }
3644                                 else /* since we're utf 8, is this a good idea? if (isprint(*sig))*/ {
3645                                         wprintf("%c", *sig);
3646                                 } 
3647                                 sig ++;
3648                         }
3649                 }
3650         }
3651
3652         wprintf("</textarea>\n");
3653
3654         /** Make sure we only insert our signature once */
3655         /** We don't care if it was there or not before, it needs to be there now. */
3656         wprintf("<input type=\"hidden\" name=\"sig_inserted\" value=\"yes\">\n");
3657         
3658         /**
3659          * The following template embeds the TinyMCE richedit control, and automatically
3660          * transforms the textarea into a richedit textarea.
3661          */
3662         do_template("richedit");
3663
3664         /** Enumerate any attachments which are already in place... */
3665         wprintf("<div class=\"attachment buttons\"><img src=\"static/diskette_24x.gif\" class=\"imgedit\" > ");
3666         wprintf(_("Attachments:"));
3667         wprintf(" ");
3668         wprintf("<select name=\"which_attachment\" size=1>");
3669         for (att = WCC->first_attachment; att != NULL; att = att->next) {
3670                 wprintf("<option value=\"");
3671                 urlescputs(att->filename);
3672                 wprintf("\">");
3673                 escputs(att->filename);
3674                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
3675                 wprintf("</option>\n");
3676         }
3677         wprintf("</select>");
3678
3679         /** Now offer the ability to attach additional files... */
3680         wprintf("&nbsp;&nbsp;&nbsp;");
3681         wprintf(_("Attach file:"));
3682         wprintf(" <input name=\"attachfile\" class=\"attachfile\" "
3683                 "size=16 type=\"file\">\n&nbsp;&nbsp;"
3684                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
3685         wprintf("</div>");      /* End of "attachment buttons" div */
3686
3687
3688         wprintf("</td></tr></table>");
3689         
3690         wprintf("</form>\n");
3691         wprintf("</div>\n");    /* end of "fix_scrollbar_bug" div */
3692
3693         /* NOTE: address_book_popup() will close the "content" div.  Don't close it here. */
3694 DONE:   address_book_popup();
3695         wDumpContent(1);
3696 }
3697
3698
3699 /**
3700  * \brief delete a message
3701  */
3702 void delete_msg(void)
3703 {
3704         long msgid;
3705         char buf[SIZ];
3706
3707         msgid = lbstr("msgid");
3708
3709         if (WC->wc_is_trash) {  /** Delete from Trash is a real delete */
3710                 serv_printf("DELE %ld", msgid); 
3711         }
3712         else {                  /** Otherwise move it to Trash */
3713                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
3714         }
3715
3716         serv_getln(buf, sizeof buf);
3717         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3718
3719         readloop("readnew");
3720 }
3721
3722
3723 /**
3724  * \brief move a message to another folder
3725  */
3726 void move_msg(void)
3727 {
3728         long msgid;
3729         char buf[SIZ];
3730
3731         msgid = lbstr("msgid");
3732
3733         if (havebstr("move_button")) {
3734                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
3735                 serv_puts(buf);
3736                 serv_getln(buf, sizeof buf);
3737                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
3738         } else {
3739                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
3740         }
3741
3742         readloop("readnew");
3743 }
3744
3745
3746
3747
3748
3749 /*
3750  * Confirm move of a message
3751  */
3752 void confirm_move_msg(void)
3753 {
3754         long msgid;
3755         char buf[SIZ];
3756         char targ[SIZ];
3757
3758         msgid = lbstr("msgid");
3759
3760
3761         output_headers(1, 1, 2, 0, 0, 0);
3762         wprintf("<div id=\"banner\">\n");
3763         wprintf("<h1>");
3764         wprintf(_("Confirm move of message"));
3765         wprintf("</h1>");
3766         wprintf("</div>\n");
3767
3768         wprintf("<div id=\"content\" class=\"service\">\n");
3769
3770         wprintf("<CENTER>");
3771
3772         wprintf(_("Move this message to:"));
3773         wprintf("<br />\n");
3774
3775         wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
3776         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
3777         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
3778
3779         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
3780         serv_puts("LKRA");
3781         serv_getln(buf, sizeof buf);
3782         if (buf[0] == '1') {
3783                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3784                         extract_token(targ, buf, 0, '|', sizeof targ);
3785                         wprintf("<OPTION>");
3786                         escputs(targ);
3787                         wprintf("\n");
3788                 }
3789         }
3790         wprintf("</SELECT>\n");
3791         wprintf("<br />\n");
3792
3793         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
3794         wprintf("&nbsp;");
3795         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
3796         wprintf("</form></CENTER>\n");
3797
3798         wprintf("</CENTER>\n");
3799         wDumpContent(1);
3800 }
3801
3802 void readnew(void) { readloop("readnew");}
3803 void readold(void) { readloop("readold");}
3804 void readfwd(void) { readloop("readfwd");}
3805 void headers(void) { readloop("headers");}
3806 void do_search(void) { readloop("do_search");}
3807
3808 void 
3809 InitModule_MSG
3810 (void)
3811 {
3812         WebcitAddUrlHandler(HKEY("readnew"), readnew, 0);
3813         WebcitAddUrlHandler(HKEY("readold"), readold, 0);
3814         WebcitAddUrlHandler(HKEY("readfwd"), readfwd, 0);
3815         WebcitAddUrlHandler(HKEY("headers"), headers, 0);
3816         WebcitAddUrlHandler(HKEY("do_search"), do_search, 0);
3817         WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
3818         WebcitAddUrlHandler(HKEY("post"), post_message, 0);
3819         WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
3820         WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
3821         WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
3822         WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
3823         WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
3824         WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
3825         return ;
3826 }