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