Cleaned up some bad html
[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         char Urlsep = '?';
1075
1076         if (!printable_view) {
1077                 wprintf("<p id=\"msg%ld\" class=\"msgbuttons\" >\n",msgnum);
1078
1079                 /* Reply */
1080                 if ( (WC->wc_view == VIEW_MAILBOX) || (WC->wc_view == VIEW_BBS) ) {
1081                         wprintf("<a href=\"display_enter");
1082                         if (WC->is_mailbox) {
1083                                 wprintf("%creplyquote=%ld", Urlsep, msgnum);
1084                                 Urlsep = '&';
1085                         }
1086                         wprintf("%crecp=", Urlsep);
1087                         Urlsep = '&';
1088                         urlescputs(reply_to);
1089                         if (!IsEmptyStr(m_subject)) {
1090                                 wprintf("%csubject=", Urlsep);
1091                                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%%20");
1092                                 urlescputs(m_subject);
1093                         }
1094                         wprintf("%creferences=", Urlsep);
1095                         if (!IsEmptyStr(reply_references)) {
1096                                 urlescputs(reply_references);
1097                                 urlescputs("|");
1098                         }
1099                         urlescputs(reply_inreplyto);
1100                         wprintf("\"><span>[</span>%s<span>]</span></a> ", _("Reply"));
1101                 }
1102
1103                 /* ReplyQuoted */
1104                 if ( (WC->wc_view == VIEW_MAILBOX) || (WC->wc_view == VIEW_BBS) ) {
1105                         if (!WC->is_mailbox) {
1106                                 wprintf("<a href=\"display_enter");
1107                                 wprintf("?replyquote=%ld", msgnum);
1108                                 wprintf("&recp=");
1109                                 urlescputs(reply_to);
1110                                 if (!IsEmptyStr(m_subject)) {
1111                                         wprintf("&subject=");
1112                                         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%%20");
1113                                         urlescputs(m_subject);
1114                                 }
1115                                 wprintf("&references=");
1116                                 if (!IsEmptyStr(reply_references)) {
1117                                         urlescputs(reply_references);
1118                                         urlescputs("|");
1119                                 }
1120                                 urlescputs(reply_inreplyto);
1121                                 wprintf("\"><span>[</span>%s<span>]</span></a> ", _("ReplyQuoted"));
1122                         }
1123                 }
1124
1125                 /* ReplyAll */
1126                 if (WC->wc_view == VIEW_MAILBOX) {
1127                         wprintf("<a href=\"display_enter");
1128                         wprintf("?replyquote=%ld", msgnum);
1129                         wprintf("&recp=");
1130                         urlescputs(reply_to);
1131                         wprintf("&cc=");
1132                         urlescputs(reply_all);
1133                         if (!IsEmptyStr(m_subject)) {
1134                                 wprintf("&subject=");
1135                                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%%20");
1136                                 urlescputs(m_subject);
1137                         }
1138                         wprintf("&references=");
1139                         if (!IsEmptyStr(reply_references)) {
1140                                 urlescputs(reply_references);
1141                                 urlescputs("|");
1142                         }
1143                         urlescputs(reply_inreplyto);
1144                         wprintf("\"><span>[</span>%s<span>]</span></a> ", _("ReplyAll"));
1145                 }
1146
1147                 /* Forward */
1148                 if (WC->wc_view == VIEW_MAILBOX) {
1149                         wprintf("<a href=\"display_enter?fwdquote=%ld&subject=", msgnum);
1150                         if (strncasecmp(m_subject, "Fwd:", 4)) wprintf("Fwd:%%20");
1151                         urlescputs(m_subject);
1152                         wprintf("\"><span>[</span>%s<span>]</span></a> ", _("Forward"));
1153                 }
1154
1155                 /* If this is one of my own rooms, or if I'm an Aide or Room Aide, I can move/delete */
1156                 if ( (WC->is_room_aide) || (WC->is_mailbox) || (WC->room_flags2 & QR2_COLLABDEL) ) {
1157                         /** Move */
1158                         wprintf("<a href=\"confirm_move_msg?msgid=%ld\"><span>[</span>%s<span>]</span></a> ",
1159                                 msgnum, _("Move"));
1160         
1161                         /** Delete */
1162                         wprintf("<a href=\"delete_msg?msgid=%ld\" "
1163                                 "onClick=\"return confirm('%s');\">"
1164                                 "<span>[</span>%s<span>]</span> "
1165                                 "</a> ", msgnum, _("Delete this message?"), _("Delete")
1166                         );
1167                 }
1168
1169                 /* Headers */
1170                 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'); \" >"
1171                         "<span>[</span>%s<span>]</span></a>", msgnum, msgnum, _("Headers"));
1172
1173
1174                 /* Print */
1175                 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'); \" >"
1176                         "<span>[</span>%s<span>]</span></a>", msgnum, msgnum, _("Print"));
1177
1178                 wprintf("</p>");
1179
1180         }
1181
1182         if (!IsEmptyStr(m_cc)) {
1183                 wprintf("<p>");
1184                 wprintf(_("CC:"));
1185                 wprintf(" ");
1186                 escputs(m_cc);
1187                 wprintf("</p>");
1188         }
1189         if (!IsEmptyStr(m_subject)) {
1190                 wprintf("<p class=\"message_subject\">");
1191                 wprintf(_("Subject:"));
1192                 wprintf(" ");
1193                 escputs(m_subject);
1194                 wprintf("</p>");
1195         }
1196
1197         wprintf("</div>");
1198
1199         /* Begin body */
1200         wprintf("<div class=\"message_content\">");
1201
1202         /*
1203          * Learn the content type
1204          */
1205         strcpy(mime_content_type, "text/plain");
1206         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
1207                 if (!strcmp(buf, "000")) {
1208                         /* This is not necessarily an error condition.  See bug #226. */
1209                         goto ENDBODY;
1210                 }
1211                 if (!strncasecmp(buf, "X-Citadel-MSG4-Partnum:", 23)) {
1212                         safestrncpy(msg4_partnum, &buf[23], sizeof(msg4_partnum));
1213                         striplt(msg4_partnum);
1214                 }
1215                 if (!strncasecmp(buf, "Content-type:", 13)) {
1216                         int len;
1217                         safestrncpy(mime_content_type, &buf[13], sizeof(mime_content_type));
1218                         striplt(mime_content_type);
1219                         len = strlen(mime_content_type);
1220                         for (i=0; i<len; ++i) {
1221                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1222                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1223                                                 sizeof mime_charset);
1224                                 }
1225                         }
1226                         for (i=0; i<len; ++i) {
1227                                 if (mime_content_type[i] == ';') {
1228                                         mime_content_type[i] = 0;
1229                                         len = i - 1;
1230                                 }
1231                         }
1232                         len = strlen(mime_charset);
1233                         for (i=0; i<len; ++i) {
1234                                 if (mime_charset[i] == ';') {
1235                                         mime_charset[i] = 0;
1236                                         len = i - 1;
1237                                 }
1238                         }
1239                 }
1240         }
1241
1242         /* Set up a character set conversion if we need to (and if we can) */
1243 #ifdef HAVE_ICONV
1244         if (strchr(mime_charset, ';')) strcpy(strchr(mime_charset, ';'), "");
1245         if ( (strcasecmp(mime_charset, "us-ascii"))
1246            && (strcasecmp(mime_charset, "UTF-8"))
1247            && (strcasecmp(mime_charset, ""))
1248         ) {
1249                 ic = ctdl_iconv_open("UTF-8", mime_charset);
1250                 if (ic == (iconv_t)(-1) ) {
1251                         lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
1252                                 __FILE__, __LINE__, mime_charset, strerror(errno));
1253                 }
1254         }
1255 #endif
1256
1257         /* Messages in legacy Citadel variformat get handled thusly... */
1258         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1259                 fmout("JUSTIFY");
1260         }
1261
1262         /* Boring old 80-column fixed format text gets handled this way... */
1263         else if ( (!strcasecmp(mime_content_type, "text/plain"))
1264                 || (!strcasecmp(mime_content_type, "text")) ) {
1265                 buf [0] = '\0';
1266                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1267                         int len;
1268                         len = strlen(buf);
1269                         if ((len > 0) && buf[len-1] == '\n') buf[--len] = 0;
1270                         if ((len > 0) && buf[len-1] == '\r') buf[--len] = 0;
1271
1272 #ifdef HAVE_ICONV
1273                         if (ic != (iconv_t)(-1) ) {
1274                                 ibuf = buf;
1275                                 ibuflen = strlen(ibuf);
1276                                 obuflen = SIZ;
1277                                 obuf = (char *) malloc(obuflen);
1278                                 osav = obuf;
1279                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1280                                 osav[SIZ-obuflen] = 0;
1281                                 safestrncpy(buf, osav, sizeof buf);
1282                                 free(osav);
1283                         }
1284 #endif
1285
1286                         len = strlen(buf);
1287                         while ((!IsEmptyStr(buf)) && (isspace(buf[len-1])))
1288                                 buf[--len] = 0;
1289                         if ((bq == 0) &&
1290                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1291                                 wprintf("<blockquote>");
1292                                 bq = 1;
1293                         } else if ((bq == 1) &&
1294                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1295                                 wprintf("</blockquote>");
1296                                 bq = 0;
1297                         }
1298                         wprintf("<tt>");
1299                         url(buf, sizeof(buf));
1300                         escputs(buf);
1301                         wprintf("</tt><br />\n");
1302                 }
1303                 wprintf("</i><br />");
1304         }
1305
1306         /* HTML is fun, but we've got to strip it first */
1307         else if (!strcasecmp(mime_content_type, "text/html")) {
1308                 output_html(mime_charset, (WC->wc_view == VIEW_WIKI ? 1 : 0), msgnum);
1309         }
1310
1311         /* Unknown weirdness */
1312         else {
1313                 wprintf(_("I don't know how to display %s"), mime_content_type);
1314                 wprintf("<br />\n");
1315                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1316         }
1317
1318 ENDBODY:        /* If there are attached submessages, display them now... */
1319
1320         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
1321                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
1322                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
1323                         /** use printable_view to suppress buttons */
1324                         wprintf("<blockquote>");
1325                         read_message(msgnum, 1, buf);
1326                         wprintf("</blockquote>");
1327                 }
1328         }
1329
1330
1331         /* Afterwards, offer links to download attachments 'n' such */
1332         if ( (num_attach_links > 0) && (!section[0]) ) {
1333                 for (i=0; i<num_attach_links; ++i) {
1334                         if (strcasecmp(attach_links[i].partnum, msg4_partnum)) {
1335                                 wprintf("%s", attach_links[i].html);
1336                         }
1337                 }
1338         }
1339
1340         /* Handler for vCard parts */
1341         if (!IsEmptyStr(vcard_partnum)) {
1342                 part_source = load_mimepart(msgnum, vcard_partnum);
1343                 if (part_source != NULL) {
1344
1345                         /** If it's my vCard I can edit it */
1346                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1347                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1348                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1349                         ) {
1350                                 wprintf("<a href=\"edit_vcard?msgnum=%ld&partnum=%s\">",
1351                                         msgnum, vcard_partnum);
1352                                 wprintf("[%s]</a>", _("edit"));
1353                         }
1354
1355                         /* In all cases, display the full card */
1356                         display_vcard(part_source, 0, 1, NULL,msgnum);
1357                 }
1358         }
1359
1360         /* Handler for calendar parts */
1361         if (!IsEmptyStr(cal_partnum)) {
1362                 part_source = load_mimepart(msgnum, cal_partnum);
1363                 if (part_source != NULL) {
1364                         cal_process_attachment(part_source, msgnum, cal_partnum);
1365                 }
1366         }
1367
1368         if (part_source) {
1369                 free(part_source);
1370                 part_source = NULL;
1371         }
1372
1373         wprintf("</div>\n");
1374
1375         /* end everythingamundo table */
1376         if (!printable_view) {
1377                 wprintf("</div>\n");
1378         }
1379
1380         if (num_attach_links > 0) {
1381                 free(attach_links);
1382         }
1383
1384 #ifdef HAVE_ICONV
1385         if (ic != (iconv_t)(-1) ) {
1386                 iconv_close(ic);
1387         }
1388 #endif
1389 }
1390
1391
1392
1393 /*
1394  * Unadorned HTML output of an individual message, suitable
1395  * for placing in a hidden iframe, for printing, or whatever
1396  *
1397  * msgnum_as_string == Message number, as a string instead of as a long int
1398  */
1399 void embed_message(void) {
1400         long msgnum = 0L;
1401
1402         msgnum = StrTol(WC->UrlFragment1);
1403         read_message(msgnum, 0, "");
1404 }
1405
1406
1407 /*
1408  * Printable view of a message
1409  *
1410  * msgnum_as_string == Message number, as a string instead of as a long int
1411  */
1412 void print_message(void) {
1413         long msgnum = 0L;
1414
1415         msgnum = StrTol(WC->UrlFragment1);
1416         output_headers(0, 0, 0, 0, 0, 0);
1417
1418         hprintf("Content-type: text/html\r\n"
1419                 "Server: %s\r\n"
1420                 "Connection: close\r\n",
1421                 PACKAGE_STRING);
1422         begin_burst();
1423
1424         wprintf("\r\n<html>\n<head><title>");
1425         escputs(WC->wc_fullname);
1426         wprintf("</title></head>\n"
1427                 "<body onLoad=\" window.print(); window.close(); \">\n"
1428         );
1429         
1430         read_message(msgnum, 1, "");
1431
1432         wprintf("\n</body></html>\n\n");
1433         wDumpContent(0);
1434 }
1435
1436 /* 
1437  * Mobile browser view of message
1438  *
1439  * @param msg_num_as_string Message number as a string instead of as a long int 
1440  */
1441 void mobile_message_view(void) {
1442   long msgnum = 0L;
1443   msgnum = StrTol(WC->UrlFragment1);
1444   output_headers(1, 0, 0, 0, 0, 1);
1445   begin_burst();
1446   do_template("msgcontrols", NULL);
1447   read_message(msgnum,1, "");
1448   wDumpContent(0);
1449 }
1450
1451 /**
1452  * \brief Display a message's headers
1453  *
1454  * \param msgnum_as_string Message number, as a string instead of as a long int
1455  */
1456 void display_headers(void) {
1457         long msgnum = 0L;
1458         char buf[1024];
1459
1460         msgnum = StrTol(WC->UrlFragment1);
1461         output_headers(0, 0, 0, 0, 0, 0);
1462
1463         hprintf("Content-type: text/plain\r\n"
1464                 "Server: %s\r\n"
1465                 "Connection: close\r\n",
1466                 PACKAGE_STRING);
1467         begin_burst();
1468
1469         serv_printf("MSG2 %ld|3", msgnum);
1470         serv_getln(buf, sizeof buf);
1471         if (buf[0] == '1') {
1472                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1473                         wprintf("%s\n", buf);
1474                 }
1475         }
1476
1477         wDumpContent(0);
1478 }
1479
1480
1481
1482 /**
1483  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
1484  *      or 'reply quoted' operations.
1485  *
1486  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
1487  *       in this function.  Doing so would throw a JavaScript error in the
1488  *       'supplied text' argument to the editor.
1489  *
1490  * \param msgnum Message number of the message we want to quote
1491  * \param forward_attachments Nonzero if we want attachments to be forwarded
1492  */
1493 void pullquote_message(long msgnum, int forward_attachments, int include_headers) {
1494         char buf[SIZ];
1495         char mime_partnum[256];
1496         char mime_filename[256];
1497         char mime_content_type[256];
1498         char mime_charset[256];
1499         char mime_disposition[256];
1500         int mime_length;
1501         char *attachments = NULL;
1502         char *ptr = NULL;
1503         int num_attachments = 0;
1504         struct wc_attachment *att, *aptr;
1505         char m_subject[1024];
1506         char from[256];
1507         char node[256];
1508         char rfca[256];
1509         char to[256];
1510         char reply_to[512];
1511         char now[256];
1512         int format_type = 0;
1513         int nhdr = 0;
1514         int bq = 0;
1515         int i = 0;
1516 #ifdef HAVE_ICONV
1517         iconv_t ic = (iconv_t)(-1) ;
1518         char *ibuf;                /**< Buffer of characters to be converted */
1519         char *obuf;                /**< Buffer for converted characters      */
1520         size_t ibuflen;    /**< Length of input buffer         */
1521         size_t obuflen;    /**< Length of output buffer       */
1522         char *osav;                /**< Saved pointer to output buffer       */
1523 #endif
1524
1525         strcpy(from, "");
1526         strcpy(node, "");
1527         strcpy(rfca, "");
1528         strcpy(reply_to, "");
1529         strcpy(mime_content_type, "text/plain");
1530         strcpy(mime_charset, "us-ascii");
1531
1532         serv_printf("MSG4 %ld", msgnum);
1533         serv_getln(buf, sizeof buf);
1534         if (buf[0] != '1') {
1535                 wprintf(_("ERROR:"));
1536                 wprintf("%s<br />", &buf[4]);
1537                 return;
1538         }
1539
1540         strcpy(m_subject, "");
1541
1542         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
1543                 if (!strcmp(buf, "000")) {
1544                         wprintf("%s (3)", _("unexpected end of message"));
1545                         return;
1546                 }
1547                 if (include_headers) {
1548                         if (!strncasecmp(buf, "nhdr=yes", 8))
1549                                 nhdr = 1;
1550                         if (nhdr == 1)
1551                                 buf[0] = '_';
1552                         if (!strncasecmp(buf, "type=", 5))
1553                                 format_type = atoi(&buf[5]);
1554                         if (!strncasecmp(buf, "from=", 5)) {
1555                                 strcpy(from, &buf[5]);
1556                                 wprintf(_("from "));
1557                                 utf8ify_rfc822_string(from);
1558                                 msgescputs(from);
1559                         }
1560                         if (!strncasecmp(buf, "subj=", 5)) {
1561                                 strcpy(m_subject, &buf[5]);
1562                         }
1563                         if ((!strncasecmp(buf, "hnod=", 5))
1564                             && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
1565                                 wprintf("(%s) ", &buf[5]);
1566                         }
1567                         if ((!strncasecmp(buf, "room=", 5))
1568                             && (strcasecmp(&buf[5], WC->wc_roomname))
1569                             && (!IsEmptyStr(&buf[5])) ) {
1570                                 wprintf(_("in "));
1571                                 wprintf("%s&gt; ", &buf[5]);
1572                         }
1573                         if (!strncasecmp(buf, "rfca=", 5)) {
1574                                 strcpy(rfca, &buf[5]);
1575                                 wprintf("&lt;");
1576                                 msgescputs(rfca);
1577                                 wprintf("&gt; ");
1578                         }
1579                         if (!strncasecmp(buf, "node=", 5)) {
1580                                 strcpy(node, &buf[5]);
1581                                 if ( ((WC->room_flags & QR_NETWORK)
1582                                 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
1583                                 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
1584                                 && (IsEmptyStr(rfca))
1585                                 ) {
1586                                         wprintf("@%s ", &buf[5]);
1587                                 }
1588                         }
1589                         if (!strncasecmp(buf, "rcpt=", 5)) {
1590                                 wprintf(_("to "));
1591                                 strcpy(to, &buf[5]);
1592                                 utf8ify_rfc822_string(to);
1593                                 wprintf("%s ", to);
1594                         }
1595                         if (!strncasecmp(buf, "time=", 5)) {
1596                                 webcit_fmt_date(now, atol(&buf[5]), 0);
1597                                 wprintf("%s ", now);
1598                         }
1599                 }
1600
1601                 /**
1602                  * Save attachment info for later.  We can't start downloading them
1603                  * yet because we're in the middle of a server transaction.
1604                  */
1605                 if (!strncasecmp(buf, "part=", 5)) {
1606                         ptr = malloc( (strlen(buf) + ((attachments != NULL) ? strlen(attachments) : 0)) ) ;
1607                         if (ptr != NULL) {
1608                                 ++num_attachments;
1609                                 sprintf(ptr, "%s%s\n",
1610                                         ((attachments != NULL) ? attachments : ""),
1611                                         &buf[5]
1612                                 );
1613                                 free(attachments);
1614                                 attachments = ptr;
1615                                 lprintf(9, "attachments=<%s>\n", attachments);
1616                         }
1617                 }
1618
1619         }
1620
1621         if (include_headers) {
1622                 wprintf("<br>");
1623
1624                 utf8ify_rfc822_string(m_subject);
1625                 if (!IsEmptyStr(m_subject)) {
1626                         wprintf(_("Subject:"));
1627                         wprintf(" ");
1628                         msgescputs(m_subject);
1629                         wprintf("<br />");
1630                 }
1631
1632                 /**
1633                  * Begin body
1634                  */
1635                 wprintf("<br />");
1636         }
1637
1638         /**
1639          * Learn the content type
1640          */
1641         strcpy(mime_content_type, "text/plain");
1642         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
1643                 if (!strcmp(buf, "000")) {
1644                         wprintf("%s (4)", _("unexpected end of message"));
1645                         goto ENDBODY;
1646                 }
1647                 if (!strncasecmp(buf, "Content-type: ", 14)) {
1648                         int len;
1649                         safestrncpy(mime_content_type, &buf[14],
1650                                 sizeof(mime_content_type));
1651                         for (i=0; i<strlen(mime_content_type); ++i) {
1652                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1653                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1654                                                 sizeof mime_charset);
1655                                 }
1656                         }
1657                         len = strlen(mime_content_type);
1658                         for (i=0; i<len; ++i) {
1659                                 if (mime_content_type[i] == ';') {
1660                                         mime_content_type[i] = 0;
1661                                         len = i - 1;
1662                                 }
1663                         }
1664                         len = strlen(mime_charset);
1665                         for (i=0; i<len; ++i) {
1666                                 if (mime_charset[i] == ';') {
1667                                         mime_charset[i] = 0;
1668                                         len = i - 1;
1669                                 }
1670                         }
1671                 }
1672         }
1673
1674         /** Set up a character set conversion if we need to (and if we can) */
1675 #ifdef HAVE_ICONV
1676         if ( (strcasecmp(mime_charset, "us-ascii"))
1677            && (strcasecmp(mime_charset, "UTF-8"))
1678            && (strcasecmp(mime_charset, ""))
1679         ) {
1680                 ic = ctdl_iconv_open("UTF-8", mime_charset);
1681                 if (ic == (iconv_t)(-1) ) {
1682                         lprintf(5, "%s:%d iconv_open(%s, %s) failed: %s\n",
1683                                 __FILE__, __LINE__, "UTF-8", mime_charset, strerror(errno));
1684                 }
1685         }
1686 #endif
1687
1688         /** Messages in legacy Citadel variformat get handled thusly... */
1689         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1690                 pullquote_fmout();
1691         }
1692
1693         /* Boring old 80-column fixed format text gets handled this way... */
1694         else if (!strcasecmp(mime_content_type, "text/plain")) {
1695                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1696                         int len;
1697                         len = strlen(buf);
1698                         if ((len > 0) && (buf[len-1] == '\n')) buf[--len] = 0;
1699                         if ((len > 0) && (buf[len-1] == '\r')) buf[--len] = 0;
1700
1701 #ifdef HAVE_ICONV
1702                         if (ic != (iconv_t)(-1) ) {
1703                                 ibuf = buf;
1704                                 ibuflen = len;
1705                                 obuflen = SIZ;
1706                                 obuf = (char *) malloc(obuflen);
1707                                 osav = obuf;
1708                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1709                                 osav[SIZ-obuflen] = 0;
1710                                 safestrncpy(buf, osav, sizeof buf);
1711                                 free(osav);
1712                         }
1713 #endif
1714
1715                         len = strlen(buf);
1716                         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1]))) 
1717                                 buf[--len] = 0;
1718                         if ((bq == 0) &&
1719                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1720                                 wprintf("<blockquote>");
1721                                 bq = 1;
1722                         } else if ((bq == 1) &&
1723                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1724                                 wprintf("</blockquote>");
1725                                 bq = 0;
1726                         }
1727                         wprintf("<tt>");
1728                         url(buf, sizeof(buf));
1729                         msgescputs1(buf);
1730                         wprintf("</tt><br />");
1731                 }
1732                 wprintf("</i><br />");
1733         }
1734
1735         /** HTML just gets escaped and stuffed back into the editor */
1736         else if (!strcasecmp(mime_content_type, "text/html")) {
1737                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1738                         strcat(buf, "\n");
1739                         msgescputs(buf);
1740                 }
1741         }//// TODO: charset? utf8?
1742
1743         /** Unknown weirdness ... don't know how to handle this content type */
1744         else {
1745                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1746         }
1747
1748 ENDBODY:
1749         /** end of body handler */
1750
1751         /*
1752          * If there were attachments, we have to download them and insert them
1753          * into the attachment chain for the forwarded message we are composing.
1754          */
1755         if ( (forward_attachments) && (num_attachments) ) {
1756                 for (i=0; i<num_attachments; ++i) {
1757                         extract_token(buf, attachments, i, '\n', sizeof buf);
1758                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
1759                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
1760                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
1761                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
1762                         mime_length = extract_int(buf, 5);
1763
1764                         /*
1765                          * tracing  ... uncomment if necessary
1766                          *
1767                          */
1768                         lprintf(9, "fwd filename: %s\n", mime_filename);
1769                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
1770                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
1771                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
1772                         lprintf(9, "fwd length  : %d\n", mime_length);
1773
1774                         if ( (!strcasecmp(mime_disposition, "inline"))
1775                            || (!strcasecmp(mime_disposition, "attachment")) ) {
1776                 
1777                                 /* Create an attachment struct from this mime part... */
1778                                 att = malloc(sizeof(struct wc_attachment));
1779                                 memset(att, 0, sizeof(struct wc_attachment));
1780                                 att->length = mime_length;
1781                                 strcpy(att->content_type, mime_content_type);
1782                                 strcpy(att->filename, mime_filename);
1783                                 att->next = NULL;
1784                                 att->data = load_mimepart(msgnum, mime_partnum);
1785                 
1786                                 /* And add it to the list. */
1787                                 if (WC->first_attachment == NULL) {
1788                                         WC->first_attachment = att;
1789                                 }
1790                                 else {
1791                                         aptr = WC->first_attachment;
1792                                         while (aptr->next != NULL) aptr = aptr->next;
1793                                         aptr->next = att;
1794                                 }
1795                         }
1796
1797                 }
1798         }
1799
1800 #ifdef HAVE_ICONV
1801         if (ic != (iconv_t)(-1) ) {
1802                 iconv_close(ic);
1803         }
1804 #endif
1805
1806         if (attachments != NULL) {
1807                 free(attachments);
1808         }
1809 }
1810
1811 /**
1812  * \brief Display one row in the mailbox summary view
1813  *
1814  * \param num The row number to be displayed
1815  */
1816 void display_summarized(int num) {
1817         char datebuf[64];
1818         wprintf("<tr id=\"m%ld\" style=\"font-weight:%s;\" "
1819                 "onMouseDown=\"CtdlMoveMsgMouseDown(event,%ld)\">",
1820                 WC->summ[num].msgnum,
1821                 ((WC->summ[num].is_new) ? "bold" : "normal"),
1822                 WC->summ[num].msgnum
1823         );
1824
1825         wprintf("<td width=%d%%>", SUBJ_COL_WIDTH_PCT);
1826
1827         escputs(WC->summ[num].subj);//////////////////////////////////TODO: QP DECODE
1828         wprintf("</td>");
1829
1830         wprintf("<td width=%d%%>", SENDER_COL_WIDTH_PCT);
1831         escputs(WC->summ[num].from);
1832         wprintf("</td>");
1833
1834         wprintf("<td width=%d%%>", DATE_PLUS_BUTTONS_WIDTH_PCT);
1835         webcit_fmt_date(datebuf, WC->summ[num].date, 1);        /* brief */
1836         escputs(datebuf);
1837         wprintf("</td>");
1838
1839         wprintf("</tr>\n");
1840 }
1841 /**
1842  * \brief Output a message row for the mobile view
1843  * \param The row number 
1844  */
1845 void display_mobile_summary(int num) {
1846         char datebuf[64];
1847         wprintf("\n<div><div id=\"m%ld\" style=\"font-weight:%s;\" "
1848                 "onClick=\"CtdlLoadMsgMouseDown(event,%ld)\">",
1849                 WC->summ[num].msgnum,
1850                 (WC->summ[num].is_new ? "bold" : "normal"),
1851                 WC->summ[num].msgnum
1852         );
1853                 wprintf("<span class=\"from\">%s</span>",WC->summ[num].from);
1854                 wprintf("<span style=\"float: right;\">");
1855                 webcit_fmt_date(datebuf, WC->summ[num].date, 1);        /* brief */
1856                 escputs(datebuf);
1857                 wprintf("</span><br/><span class=\"subject\">");
1858                 escputs(WC->summ[num].subj);
1859                 wprintf("</span></div><div id=\"m_%ld\" class=\"msgview\" onMouseDown=\"\"></div></div>",WC->summ[num].msgnum);
1860 }
1861
1862 /**
1863  * \brief display the adressbook overview
1864  * \param msgnum the citadel message number
1865  * \param alpha what????
1866  */
1867 void display_addressbook(long msgnum, char alpha) {
1868         char buf[SIZ];
1869         char mime_partnum[SIZ];
1870         char mime_filename[SIZ];
1871         char mime_content_type[SIZ];
1872         char mime_disposition[SIZ];
1873         int mime_length;
1874         char vcard_partnum[SIZ];
1875         char *vcard_source = NULL;
1876         struct message_summary summ;
1877
1878         memset(&summ, 0, sizeof(summ));
1879         safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
1880
1881         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1882         serv_puts(buf);
1883         serv_getln(buf, sizeof buf);
1884         if (buf[0] != '1') return;
1885
1886         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1887                 if (!strncasecmp(buf, "part=", 5)) {
1888                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1889                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1890                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1891                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1892                         mime_length = extract_int(&buf[5], 5);
1893
1894                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1895                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1896                                 strcpy(vcard_partnum, mime_partnum);
1897                         }
1898
1899                 }
1900         }
1901
1902         if (!IsEmptyStr(vcard_partnum)) {
1903                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1904                 if (vcard_source != NULL) {
1905
1906                         /** Display the summary line */
1907                         display_vcard(vcard_source, alpha, 0, NULL,msgnum);
1908
1909                         /** If it's my vCard I can edit it */
1910                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1911                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1912                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1913                         ) {
1914                                 wprintf("<a href=\"edit_vcard?"
1915                                         "msgnum=%ld&partnum=%s\">",
1916                                         msgnum, vcard_partnum);
1917                                 wprintf("[%s]</a>", _("edit"));
1918                         }
1919
1920                         free(vcard_source);
1921                 }
1922         }
1923
1924 }
1925
1926
1927
1928 /**
1929  * \brief  If it's an old "Firstname Lastname" style record, try to convert it.
1930  * \param namebuf name to analyze, reverse if nescessary
1931  */
1932 void lastfirst_firstlast(char *namebuf) {
1933         char firstname[SIZ];
1934         char lastname[SIZ];
1935         int i;
1936
1937         if (namebuf == NULL) return;
1938         if (strchr(namebuf, ';') != NULL) return;
1939
1940         i = num_tokens(namebuf, ' ');
1941         if (i < 2) return;
1942
1943         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1944         remove_token(namebuf, i-1, ' ');
1945         strcpy(firstname, namebuf);
1946         sprintf(namebuf, "%s; %s", lastname, firstname);
1947 }
1948
1949 /**
1950  * \brief fetch what??? name
1951  * \param msgnum the citadel message number
1952  * \param namebuf where to put the name in???
1953  */
1954 void fetch_ab_name(long msgnum, char *namebuf) {
1955         char buf[SIZ];
1956         char mime_partnum[SIZ];
1957         char mime_filename[SIZ];
1958         char mime_content_type[SIZ];
1959         char mime_disposition[SIZ];
1960         int mime_length;
1961         char vcard_partnum[SIZ];
1962         char *vcard_source = NULL;
1963         int i, len;
1964         struct message_summary summ;
1965
1966         if (namebuf == NULL) return;
1967         strcpy(namebuf, "");
1968
1969         memset(&summ, 0, sizeof(summ));
1970         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1971
1972         sprintf(buf, "MSG0 %ld|0", msgnum);     /** unfortunately we need the mime info now */
1973         serv_puts(buf);
1974         serv_getln(buf, sizeof buf);
1975         if (buf[0] != '1') return;
1976
1977         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1978                 if (!strncasecmp(buf, "part=", 5)) {
1979                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1980                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1981                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1982                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1983                         mime_length = extract_int(&buf[5], 5);
1984
1985                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1986                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1987                                 strcpy(vcard_partnum, mime_partnum);
1988                         }
1989
1990                 }
1991         }
1992
1993         if (!IsEmptyStr(vcard_partnum)) {
1994                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1995                 if (vcard_source != NULL) {
1996
1997                         /* Grab the name off the card */
1998                         display_vcard(vcard_source, 0, 0, namebuf,msgnum);
1999
2000                         free(vcard_source);
2001                 }
2002         }
2003
2004         lastfirst_firstlast(namebuf);
2005         striplt(namebuf);
2006         len = strlen(namebuf);
2007         for (i=0; i<len; ++i) {
2008                 if (namebuf[i] != ';') return;
2009         }
2010         strcpy(namebuf, _("(no name)"));
2011 }
2012
2013
2014
2015 /**
2016  * \brief Record compare function for sorting address book indices
2017  * \param ab1 adressbook one
2018  * \param ab2 adressbook two
2019  */
2020 int abcmp(const void *ab1, const void *ab2) {
2021         return(strcasecmp(
2022                 (((const struct addrbookent *)ab1)->ab_name),
2023                 (((const struct addrbookent *)ab2)->ab_name)
2024         ));
2025 }
2026
2027
2028 /**
2029  * \brief Helper function for do_addrbook_view()
2030  * Converts a name into a three-letter tab label
2031  * \param tabbuf the tabbuffer to add name to
2032  * \param name the name to add to the tabbuffer
2033  */
2034 void nametab(char *tabbuf, long len, char *name) {
2035         stresc(tabbuf, len, name, 0, 0);
2036         tabbuf[0] = toupper(tabbuf[0]);
2037         tabbuf[1] = tolower(tabbuf[1]);
2038         tabbuf[2] = tolower(tabbuf[2]);
2039         tabbuf[3] = 0;
2040 }
2041
2042
2043 /**
2044  * \brief Render the address book using info we gathered during the scan
2045  * \param addrbook the addressbook to render
2046  * \param num_ab the number of the addressbook
2047  */
2048 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
2049         int i = 0;
2050         int displayed = 0;
2051         int bg = 0;
2052         static int NAMESPERPAGE = 60;
2053         int num_pages = 0;
2054         int tabfirst = 0;
2055         char tabfirst_label[64];
2056         int tablast = 0;
2057         char tablast_label[64];
2058         char this_tablabel[64];
2059         int page = 0;
2060         char **tablabels;
2061
2062         if (num_ab == 0) {
2063                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
2064                 wprintf(_("This address book is empty."));
2065                 wprintf("</i></div>\n");
2066                 return;
2067         }
2068
2069         if (num_ab > 1) {
2070                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
2071         }
2072
2073         num_pages = (num_ab / NAMESPERPAGE) + 1;
2074
2075         tablabels = malloc(num_pages * sizeof (char *));
2076         if (tablabels == NULL) {
2077                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
2078                 wprintf(_("An internal error has occurred."));
2079                 wprintf("</i></div>\n");
2080                 return;
2081         }
2082
2083         for (i=0; i<num_pages; ++i) {
2084                 tabfirst = i * NAMESPERPAGE;
2085                 tablast = tabfirst + NAMESPERPAGE - 1;
2086                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
2087                 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
2088                 nametab(tablast_label, 64, addrbook[tablast].ab_name);
2089                 sprintf(this_tablabel, "%s&nbsp;-&nbsp;%s", tabfirst_label, tablast_label);
2090                 tablabels[i] = strdup(this_tablabel);
2091         }
2092
2093         tabbed_dialog(num_pages, tablabels);
2094         page = (-1);
2095
2096         for (i=0; i<num_ab; ++i) {
2097
2098                 if ((i / NAMESPERPAGE) != page) {       /* New tab */
2099                         page = (i / NAMESPERPAGE);
2100                         if (page > 0) {
2101                                 wprintf("</tr></table>\n");
2102                                 end_tab(page-1, num_pages);
2103                         }
2104                         begin_tab(page, num_pages);
2105                         wprintf("<table border=0 cellspacing=0 cellpadding=3 width=100%%>\n");
2106                         displayed = 0;
2107                 }
2108
2109                 if ((displayed % 4) == 0) {
2110                         if (displayed > 0) {
2111                                 wprintf("</tr>\n");
2112                         }
2113                         bg = 1 - bg;
2114                         wprintf("<tr bgcolor=\"#%s\">",
2115                                 (bg ? "DDDDDD" : "FFFFFF")
2116                         );
2117                 }
2118         
2119                 wprintf("<td>");
2120
2121                 wprintf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
2122                         addrbook[i].ab_msgnum);
2123                 wprintf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
2124                 vcard_n_prettyize(addrbook[i].ab_name);
2125                 escputs(addrbook[i].ab_name);
2126                 wprintf("</a></td>\n");
2127                 ++displayed;
2128         }
2129
2130         /* Placeholders for empty columns at end */
2131         if ((num_ab % 4) != 0) {
2132                 for (i=0; i<(4-(num_ab % 4)); ++i) {
2133                         wprintf("<td>&nbsp;</td>");
2134                 }
2135         }
2136
2137         wprintf("</tr></table>\n");
2138         end_tab((num_pages-1), num_pages);
2139
2140         begin_tab(num_pages, num_pages);
2141         /* FIXME there ought to be something here */
2142         end_tab(num_pages, num_pages);
2143
2144         for (i=0; i<num_pages; ++i) {
2145                 free(tablabels[i]);
2146         }
2147         free(tablabels);
2148 }
2149
2150
2151
2152 /*
2153  * load message pointers from the server for a "read messages" operation
2154  *
2155  * servcmd:             the citadel command to send to the citserver
2156  * with_headers:        also include some of the headers with the message numbers (more expensive)
2157  */
2158 int load_msg_ptrs(char *servcmd, int with_headers)
2159 {
2160         char buf[1024];
2161         time_t datestamp;
2162         char fullname[128];
2163         char nodename[128];
2164         char inetaddr[128];
2165         char subject[1024];
2166         char *ptr;
2167         int nummsgs;
2168         int sbjlen;
2169         int maxload = 0;
2170
2171         int num_summ_alloc = 0;
2172
2173         if (WC->summ != NULL) {
2174                 free(WC->summ);
2175                 WC->num_summ = 0;
2176                 WC->summ = NULL;
2177         }
2178         num_summ_alloc = 100;
2179         WC->num_summ = 0;
2180         WC->summ = malloc(num_summ_alloc * sizeof(struct message_summary));
2181
2182         nummsgs = 0;
2183         maxload = sizeof(WC->msgarr) / sizeof(long) ;
2184         serv_puts(servcmd);
2185         serv_getln(buf, sizeof buf);
2186         if (buf[0] != '1') {
2187                 return (nummsgs);
2188         }
2189         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2190                 if (nummsgs < maxload) {
2191                         WC->msgarr[nummsgs] = extract_long(buf, 0);
2192                         datestamp = extract_long(buf, 1);
2193                         extract_token(fullname, buf, 2, '|', sizeof fullname);
2194                         extract_token(nodename, buf, 3, '|', sizeof nodename);
2195                         extract_token(inetaddr, buf, 4, '|', sizeof inetaddr);
2196                         extract_token(subject, buf, 5, '|', sizeof subject);
2197                         ++nummsgs;
2198
2199                         if (with_headers) {
2200                                 if (nummsgs > num_summ_alloc) {
2201                                         num_summ_alloc *= 2;
2202                                         WC->summ = realloc(WC->summ,
2203                                                 num_summ_alloc * sizeof(struct message_summary));
2204                                 }
2205                                 ++WC->num_summ;
2206
2207                                 memset(&WC->summ[nummsgs-1], 0, sizeof(struct message_summary));
2208                                 WC->summ[nummsgs-1].msgnum = WC->msgarr[nummsgs-1];
2209                                 safestrncpy(WC->summ[nummsgs-1].subj,
2210                                         _("(no subject)"), sizeof WC->summ[nummsgs-1].subj);
2211                                 if (!IsEmptyStr(subject)) {
2212                                 /** Handle subjects with RFC2047 encoding */
2213                                         utf8ify_rfc822_string(subject);
2214                                         safestrncpy(WC->summ[nummsgs-1].subj, subject,
2215                                                     sizeof WC->summ[nummsgs-1].subj);
2216                                 }
2217                                 sbjlen = Ctdl_Utf8StrLen(WC->summ[nummsgs-1].subj);
2218                                 if (sbjlen > 75) {
2219                                         ptr = Ctdl_Utf8StrCut(WC->summ[nummsgs-1].subj, 72);
2220
2221                                         strcpy(ptr, "...");
2222                                 }
2223
2224                                 if (!IsEmptyStr(fullname)) {
2225                                 /** Handle senders with RFC2047 encoding */
2226                                         utf8ify_rfc822_string(fullname);
2227                                         safestrncpy(WC->summ[nummsgs-1].from,
2228                                                 fullname, sizeof WC->summ[nummsgs-1].from);
2229                                 }
2230                                 if ((!IsEmptyStr(nodename)) &&
2231                                     ( ((WC->room_flags & QR_NETWORK)
2232                                        || ((strcasecmp(nodename, serv_info.serv_nodename)
2233                                                  && (strcasecmp(nodename, serv_info.serv_fqdn)))))))
2234                                 {
2235                                         strcat(WC->summ[nummsgs-1].from, " @ ");
2236                                         strcat(WC->summ[nummsgs-1].from, nodename);
2237                                         
2238                                 }
2239                                 sbjlen = Ctdl_Utf8StrLen(WC->summ[nummsgs-1].from);
2240                                 if (sbjlen > 25) {
2241                                         ptr = Ctdl_Utf8StrCut(WC->summ[nummsgs-1].from, 23);
2242                                         strcpy(ptr, "...");
2243                                 }
2244
2245                                 WC->summ[nummsgs-1].date = datestamp;
2246                         }
2247                 }
2248         }
2249         return (nummsgs);
2250 }
2251
2252
2253 typedef int (*QSortFunction) (const void*, const void*);
2254
2255 /*
2256  * qsort() compatible function to compare two longs in descending order.
2257  */
2258 int longcmp_r(const void *s1, const void *s2) {
2259         long l1;
2260         long l2;
2261
2262         l1 = *(long *)s1;
2263         l2 = *(long *)s2;
2264
2265         if (l1 > l2) return(-1);
2266         if (l1 < l2) return(+1);
2267         return(0);
2268 }
2269
2270  
2271 /*
2272  * qsort() compatible function to compare two message summary structs by ascending subject.
2273  */
2274 int summcmp_subj(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(summ1->subj, summ2->subj);
2281 }
2282
2283 /*
2284  * qsort() compatible function to compare two message summary structs by descending subject.
2285  */
2286 int summcmp_rsubj(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(summ2->subj, summ1->subj);
2293 }
2294
2295 /*
2296  * qsort() compatible function to compare two message summary structs by ascending sender.
2297  */
2298 int summcmp_sender(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(summ1->from, summ2->from);
2305 }
2306
2307 /*
2308  * qsort() compatible function to compare two message summary structs by descending sender.
2309  */
2310 int summcmp_rsender(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         return strcasecmp(summ2->from, summ1->from);
2317 }
2318
2319 /*
2320  * qsort() compatible function to compare two message summary structs by ascending date.
2321  */
2322 int summcmp_date(const void *s1, const void *s2) {
2323         struct message_summary *summ1;
2324         struct message_summary *summ2;
2325         
2326         summ1 = (struct message_summary *)s1;
2327         summ2 = (struct message_summary *)s2;
2328
2329         if (summ1->date < summ2->date) return -1;
2330         else if (summ1->date > summ2->date) return +1;
2331         else return 0;
2332 }
2333
2334 /*
2335  * qsort() compatible function to compare two message summary structs by descending date.
2336  */
2337 int summcmp_rdate(const void *s1, const void *s2) {
2338         struct message_summary *summ1;
2339         struct message_summary *summ2;
2340         
2341         summ1 = (struct message_summary *)s1;
2342         summ2 = (struct message_summary *)s2;
2343
2344         if (summ1->date < summ2->date) return +1;
2345         else if (summ1->date > summ2->date) return -1;
2346         else return 0;
2347 }
2348
2349
2350 enum {
2351         eUp,
2352         eDown,
2353         eNone
2354 };
2355
2356 const char* SortIcons[3] = {
2357         "static/up_pointer.gif",
2358         "static/down_pointer.gif",
2359         "static/sort_none.gif"
2360 };
2361
2362  enum  {/// SortByEnum
2363         eDate,
2364         eRDate,
2365         eSubject,
2366         eRSubject,
2367         eSender,
2368         eRSender,
2369         eReverse,
2370         eUnSet
2371 }; 
2372
2373 /* SortEnum to plain string representation */
2374 static const char* SortByStrings[] = {
2375         "date",
2376         "rdate",
2377         "subject", 
2378         "rsubject", 
2379         "sender",
2380         "rsender",
2381         "reverse",
2382         "unset"
2383 };
2384
2385 /* SortEnum to sort-Function Table */
2386 const QSortFunction SortFuncs[eUnSet] = {
2387         summcmp_date,
2388         summcmp_rdate,
2389         summcmp_subj,
2390         summcmp_rsubj,
2391         summcmp_sender,
2392         summcmp_rsender,
2393         summcmp_rdate
2394 };
2395
2396 /* given a SortEnum, which icon should we choose? */
2397 const int SortDateToIcon[eUnSet] = { eUp, eDown, eNone, eNone, eNone, eNone, eNone};
2398 const int SortSubjectToIcon[eUnSet] = { eNone, eNone, eUp, eDown, eNone, eNone, eNone};
2399 const int SortSenderToIcon[eUnSet] = { eNone, eNone, eNone, eNone, eUp, eDown, eNone};
2400
2401 /* given a SortEnum, which would be the "opposite" search option? */
2402 const int DateInvertSortString[eUnSet] =  { eRDate, eDate, eDate, eDate, eDate, eDate, eDate};
2403 const int SubjectInvertSortString[eUnSet] =  { eSubject, eSubject, eRSubject, eUnSet, eSubject, eSubject, eSubject};
2404 const int SenderInvertSortString[eUnSet] =  { eSender, eSender, eSender, eSender, eRSender, eUnSet, eSender};
2405
2406
2407 /*
2408  * Translates sortoption String to its SortEnum representation 
2409  * returns the enum matching the string; defaults to RDate
2410  */
2411 //SortByEnum 
2412 int StrToESort (const StrBuf *sortby)
2413 {
2414         int result = eDate;
2415
2416         if (!IsEmptyStr(ChrPtr(sortby))) while (result < eUnSet){
2417                         if (!strcasecmp(ChrPtr(sortby), 
2418                                         SortByStrings[result])) 
2419                                 return result;
2420                         result ++;
2421                 }
2422         return eRDate;
2423 }
2424
2425
2426
2427
2428 /*
2429  * command loop for reading messages
2430  *
2431  * Set oper to "readnew" or "readold" or "readfwd" or "headers"
2432  */
2433 void readloop(char *oper)
2434 {
2435         char cmd[256] = "";
2436         char buf[SIZ];
2437         char old_msgs[SIZ];
2438         int a, b;
2439         int nummsgs;
2440         long startmsg;
2441         int maxmsgs;
2442         long *displayed_msgs = NULL;
2443         int num_displayed = 0;
2444         int is_summary = 0;
2445         int is_addressbook = 0;
2446         int is_singlecard = 0;
2447         int is_calendar = 0;
2448         int is_tasks = 0;
2449         int is_notes = 0;
2450         int is_bbview = 0;
2451         int lo, hi;
2452         int lowest_displayed = (-1);
2453         int highest_displayed = 0;
2454         struct addrbookent *addrbook = NULL;
2455         int num_ab = 0;
2456         const StrBuf *sortby = NULL;
2457         //SortByEnum 
2458         int SortBy = eRDate;
2459         const StrBuf *sortpref_value;
2460         int bbs_reverse = 0;
2461         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
2462
2463         if (WCC->wc_view == VIEW_WIKI) {
2464                 sprintf(buf, "wiki?room=%s&page=home", WCC->wc_roomname);
2465                 http_redirect(buf);
2466                 return;
2467         }
2468
2469         startmsg = lbstr("startmsg");
2470         maxmsgs = ibstr("maxmsgs");
2471         is_summary = (ibstr("is_summary") && !WCC->is_mobile);
2472         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
2473
2474         sortpref_value = get_room_pref("sort");
2475
2476         sortby = sbstr("sortby");
2477         if ( (!IsEmptyStr(ChrPtr(sortby))) && 
2478              (strcasecmp(ChrPtr(sortby), ChrPtr(sortpref_value)) != 0)) {
2479                 set_room_pref("sort", NewStrBufDup(sortby), 1);
2480                 sortpref_value = NULL;
2481                 sortpref_value = sortby;
2482         }
2483
2484         SortBy = StrToESort(sortpref_value);
2485         /* message board sort */
2486         if (SortBy == eReverse) {
2487                 bbs_reverse = 1;
2488         }
2489         else {
2490                 bbs_reverse = 0;
2491         }
2492
2493         output_headers(1, 1, 1, 0, 0, 0);
2494
2495         /*
2496          * When in summary mode, always show ALL messages instead of just
2497          * new or old.  Otherwise, show what the user asked for.
2498          */
2499         if (!strcmp(oper, "readnew")) {
2500                 strcpy(cmd, "MSGS NEW");
2501         }
2502         else if (!strcmp(oper, "readold")) {
2503                 strcpy(cmd, "MSGS OLD");
2504         }
2505         else if (!strcmp(oper, "do_search")) {
2506                 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2507         }
2508         else {
2509                 strcpy(cmd, "MSGS ALL");
2510         }
2511
2512         if ((WCC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1) && !WCC->is_mobile) {
2513                 is_summary = 1;
2514                 if (!strcmp(oper, "do_search")) {
2515                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2516                 }
2517                 else {
2518                         strcpy(cmd, "MSGS ALL");
2519                 }
2520         }
2521
2522         if ((WCC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
2523                 is_addressbook = 1;
2524                 if (!strcmp(oper, "do_search")) {
2525                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2526                 }
2527                 else {
2528                         strcpy(cmd, "MSGS ALL");
2529                 }
2530                 maxmsgs = 9999999;
2531         }
2532
2533         if (is_summary) {                       /**< fetch header summary */
2534                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
2535                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
2536                         (!strcmp(oper, "do_search") ? bstr("query") : "")
2537                 );
2538                 startmsg = 1;
2539                 maxmsgs = 9999999;
2540         } 
2541         if (WCC->is_mobile) {
2542                 maxmsgs = 20;
2543                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
2544                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
2545                         (!strcmp(oper, "do_search") ? bstr("query") : "")
2546                 );
2547                 SortBy =  eRDate;
2548         }
2549
2550         /*
2551          * Are we doing a summary view?  If so, we need to know old messages
2552          * and new messages, so we can do that pretty boldface thing for the
2553          * new messages.
2554          */
2555         strcpy(old_msgs, "");
2556         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
2557                 serv_puts("GTSN");
2558                 serv_getln(buf, sizeof buf);
2559                 if (buf[0] == '2') {
2560                         strcpy(old_msgs, &buf[4]);
2561                 }
2562         }
2563
2564         is_singlecard = ibstr("is_singlecard");
2565
2566         if (WCC->wc_default_view == VIEW_CALENDAR) {            /**< calendar */
2567                 is_calendar = 1;
2568                 strcpy(cmd, "MSGS ALL|||1");
2569                 maxmsgs = 32767;
2570         }
2571         if (WCC->wc_default_view == VIEW_TASKS) {               /**< tasks */
2572                 is_tasks = 1;
2573                 strcpy(cmd, "MSGS ALL");
2574                 maxmsgs = 32767;
2575         }
2576         if (WCC->wc_default_view == VIEW_NOTES) {               /**< notes */
2577                 is_notes = 1;
2578                 strcpy(cmd, "MSGS ALL");
2579                 maxmsgs = 32767;
2580         }
2581
2582         if (is_notes) {
2583                 wprintf("<div id=\"new_notes_here\"></div>\n");
2584         }
2585
2586         nummsgs = load_msg_ptrs(cmd, (is_summary || WCC->is_mobile));
2587         if (nummsgs == 0) {
2588
2589                 if ((!is_tasks) && (!is_calendar) && (!is_notes) && (!is_addressbook)) {
2590                         wprintf("<div align=\"center\"><br /><em>");
2591                         if (!strcmp(oper, "readnew")) {
2592                                 wprintf(_("No new messages."));
2593                         } else if (!strcmp(oper, "readold")) {
2594                                 wprintf(_("No old messages."));
2595                         } else {
2596                                 wprintf(_("No messages here."));
2597                         }
2598                         wprintf("</em><br /></div>\n");
2599                 }
2600
2601                 goto DONE;
2602         }
2603
2604         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
2605                 for (a = 0; a < nummsgs; ++a) {
2606                         /** Are you a new message, or an old message? */
2607                         if (is_summary) {
2608                                 if (is_msg_in_mset(old_msgs, WCC->msgarr[a])) {
2609                                         WCC->summ[a].is_new = 0;
2610                                 }
2611                                 else {
2612                                         WCC->summ[a].is_new = 1;
2613                                 }
2614                         }
2615                 }
2616         }
2617
2618         if (startmsg == 0L) {
2619                 if (bbs_reverse) {
2620                         startmsg = WCC->msgarr[(nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0];
2621                 }
2622                 else {
2623                         startmsg = WCC->msgarr[0];
2624                 }
2625         }
2626
2627         if (is_summary || WCC->is_mobile) {
2628                 qsort(WCC->summ, WCC->num_summ,
2629                       sizeof(struct message_summary), SortFuncs[SortBy]);
2630         }
2631
2632
2633         if (is_summary) {
2634
2635                 wprintf("<script language=\"javascript\" type=\"text/javascript\">"
2636                         " document.onkeydown = CtdlMsgListKeyPress;     "
2637                         " if (document.layers) {                        "
2638                         "       document.captureEvents(Event.KEYPRESS); "
2639                         " }                                             "
2640                         "</script>\n"
2641                 );
2642
2643                 /** note that Date and Delete are now in the same column */
2644                 wprintf("<div id=\"message_list_hdr\">"
2645                         "<div class=\"fix_scrollbar_bug\">"
2646                         "<table cellspacing=0 style=\"width:100%%\">"
2647                         "<tr>"
2648                 );
2649                 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"
2650                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> </th>\n"
2651                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> \n"
2652                         "&nbsp;"
2653                         "<input type=\"submit\" name=\"delete_button\" id=\"delbutton\" "
2654                         " onClick=\"CtdlDeleteSelectedMessages(event)\" "
2655                         " value=\"%s\">"
2656                         "</th>"
2657                         "</tr>\n"
2658                         ,
2659                         SUBJ_COL_WIDTH_PCT,
2660                         _("Subject"),
2661                         SortByStrings[SubjectInvertSortString[SortBy]],
2662                         SortIcons[SortSubjectToIcon[SortBy]],
2663                         SENDER_COL_WIDTH_PCT,
2664                         _("Sender"),
2665                         SortByStrings[SenderInvertSortString[SortBy]],
2666                         SortIcons[SortSenderToIcon[SortBy]],
2667                         DATE_PLUS_BUTTONS_WIDTH_PCT,
2668                         _("Date"),
2669                         SortByStrings[DateInvertSortString[SortBy]],
2670                         SortIcons[SortDateToIcon[SortBy]],
2671                         _("Delete")
2672                 );
2673                 wprintf("</table></div></div>\n");
2674                 wprintf("<div id=\"message_list\">"
2675
2676                         "<div class=\"fix_scrollbar_bug\">\n"
2677                         "<table class=\"mailbox_summary\" id=\"summary_headers\" "
2678                         "cellspacing=0 style=\"width:100%%;-moz-user-select:none;\">"
2679                 );
2680         } else if (WCC->is_mobile) {
2681                 wprintf("<div id=\"message_list\">");
2682         }
2683
2684
2685         /**
2686          * Set the "is_bbview" variable if it appears that we are looking at
2687          * a classic bulletin board view.
2688          */
2689         if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
2690               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
2691                 is_bbview = 1;
2692         }
2693
2694         /**
2695          * If we're not currently looking at ALL requested
2696          * messages, then display the selector bar
2697          */
2698         if (is_bbview) {
2699                 /** begin bbview scroller */
2700                 wprintf("<form name=\"msgomatictop\" class=\"selector_top\" > \n <p>");
2701                 wprintf(_("Reading #"));//// TODO this isn't used, should it? : , lowest_displayed, highest_displayed);
2702
2703                 wprintf("<select name=\"whichones\" size=\"1\" "
2704                         "OnChange=\"location.href=msgomatictop.whichones.options"
2705                         "[selectedIndex].value\">\n");
2706
2707                 if (bbs_reverse) {
2708                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2709                                 hi = b + 1;
2710                                 lo = b - maxmsgs + 2;
2711                                 if (lo < 1) lo = 1;
2712                                 wprintf("<option %s value="
2713                                         "\"%s"
2714                                         "&startmsg=%ld"
2715                                         "&maxmsgs=%d"
2716                                         "&is_summary=%d\">"
2717                                         "%d-%d</option> \n",
2718                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2719                                         oper,
2720                                         WCC->msgarr[lo-1],
2721                                         maxmsgs,
2722                                         is_summary,
2723                                         hi, lo);
2724                         }
2725                 }
2726                 else {
2727                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2728                                 lo = b + 1;
2729                                 hi = b + maxmsgs + 1;
2730                                 if (hi > nummsgs) hi = nummsgs;
2731                                 wprintf("<option %s value="
2732                                         "\"%s"
2733                                         "&startmsg=%ld"
2734                                         "&maxmsgs=%d"
2735                                         "&is_summary=%d\">"
2736                                         "%d-%d</option> \n",
2737                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
2738                                         oper,
2739                                         WCC->msgarr[lo-1],
2740                                         maxmsgs,
2741                                         is_summary,
2742                                         lo, hi);
2743                         }
2744                 }
2745
2746                 wprintf("<option value=\"%s?startmsg=%ld"
2747                         "&maxmsgs=9999999&is_summary=%d\">",
2748                         oper,
2749                         WCC->msgarr[0], is_summary);
2750                 wprintf(_("All"));
2751                 wprintf("</option>");
2752                 wprintf("</select> ");
2753                 wprintf(_("of %d messages."), nummsgs);
2754
2755                 /** forward/reverse */
2756                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2757                         "OnChange=\"location.href='%s?sortby=forward'\"",  
2758                         (bbs_reverse ? "" : "checked"),
2759                         oper
2760                 );
2761                 wprintf(">");
2762                 wprintf(_("oldest to newest"));
2763                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
2764
2765                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2766                         "OnChange=\"location.href='%s?sortby=reverse'\"", 
2767                         (bbs_reverse ? "checked" : ""),
2768                         oper
2769                 );
2770                 wprintf(">");
2771                 wprintf(_("newest to oldest"));
2772                 wprintf("\n");
2773         
2774                 wprintf("</p></form>\n");
2775                 /** end bbview scroller */
2776         }
2777         for (a = 0; a < nummsgs; ++a) {
2778                 if ((WCC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
2779
2780                         /** Display the message */
2781                         if (is_summary) {
2782                                 display_summarized(a);
2783                         }
2784                         else if (is_addressbook) {
2785                                 fetch_ab_name(WCC->msgarr[a], buf);
2786                                 ++num_ab;
2787                                 addrbook = realloc(addrbook,
2788                                         (sizeof(struct addrbookent) * num_ab) );
2789                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
2790                                         sizeof(addrbook[num_ab-1].ab_name));
2791                                 addrbook[num_ab-1].ab_msgnum = WCC->msgarr[a];
2792                         }
2793                         else if (is_calendar) {
2794                                 display_calendar(WCC->msgarr[a], WCC->summ[a].is_new);
2795                         }
2796                         else if (is_tasks) {
2797                                 display_task(WCC->msgarr[a], WCC->summ[a].is_new);
2798                         }
2799                         else if (is_notes) {
2800                                 display_note(WCC->msgarr[a], WCC->summ[a].is_new);
2801                         } else if (WCC->is_mobile) {
2802                                 display_mobile_summary(a);
2803                         }
2804                         else {
2805                                 if (displayed_msgs == NULL) {
2806                                         displayed_msgs = malloc(sizeof(long) *
2807                                                                 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
2808                                 }
2809                                 displayed_msgs[num_displayed] = WCC->msgarr[a];
2810                         }
2811
2812                         if (lowest_displayed < 0) lowest_displayed = a;
2813                         highest_displayed = a;
2814
2815                         ++num_displayed;
2816                 }
2817         }
2818
2819         /** Output loop */
2820         if (displayed_msgs != NULL) {
2821                 if (bbs_reverse) {
2822                         qsort(displayed_msgs, num_displayed, sizeof(long), longcmp_r);
2823                 }
2824
2825                 /** if we do a split bbview in the future, begin messages div here */
2826
2827                 for (a=0; a<num_displayed; ++a) {
2828                         read_message(displayed_msgs[a], 0, "");
2829                 }
2830
2831                 /** if we do a split bbview in the future, end messages div here */
2832
2833                 free(displayed_msgs);
2834                 displayed_msgs = NULL;
2835         }
2836
2837         if (is_summary) {
2838                 wprintf("</table>"
2839                         "</div>\n");                    /**< end of 'fix_scrollbar_bug' div */
2840                 wprintf("</div>");                      /**< end of 'message_list' div */
2841                 
2842                 /** Here's the grab-it-to-resize-the-message-list widget */
2843                 wprintf("<div id=\"resize_msglist\" "
2844                         "onMouseDown=\"CtdlResizeMsgListMouseDown(event)\">"
2845                         "<div class=\"fix_scrollbar_bug\"> <hr>"
2846                         "</div></div>\n"
2847                 );
2848
2849                 wprintf("<div id=\"preview_pane\">");   /**< The preview pane will initially be empty */
2850         } else if (WCC->is_mobile) {
2851                 wprintf("</div>");
2852         }
2853
2854         /**
2855          * Bump these because although we're thinking in zero base, the user
2856          * is a drooling idiot and is thinking in one base.
2857          */
2858         ++lowest_displayed;
2859         ++highest_displayed;
2860
2861         /**
2862          * If we're not currently looking at ALL requested
2863          * messages, then display the selector bar
2864          */
2865         if (is_bbview) {
2866                 /** begin bbview scroller */
2867                 wprintf("<form name=\"msgomatic\" class=\"selector_bottom\" > \n <p>");
2868                 wprintf(_("Reading #")); /// TODO: this isn't used: , lowest_displayed, highest_displayed);
2869
2870                 wprintf("<select name=\"whichones\" size=\"1\" "
2871                         "OnChange=\"location.href=msgomatic.whichones.options"
2872                         "[selectedIndex].value\">\n");
2873
2874                 if (bbs_reverse) {
2875                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2876                                 hi = b + 1;
2877                                 lo = b - maxmsgs + 2;
2878                                 if (lo < 1) lo = 1;
2879                                 wprintf("<option %s value="
2880                                         "\"%s"
2881                                         "&startmsg=%ld"
2882                                         "&maxmsgs=%d"
2883                                         "&is_summary=%d\">"
2884                                         "%d-%d</option> \n",
2885                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2886                                         oper,
2887                                         WCC->msgarr[lo-1],
2888                                         maxmsgs,
2889                                         is_summary,
2890                                         hi, lo);
2891                         }
2892                 }
2893                 else {
2894                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2895                                 lo = b + 1;
2896                                 hi = b + maxmsgs + 1;
2897                                 if (hi > nummsgs) hi = nummsgs;
2898                                 wprintf("<option %s value="
2899                                         "\"%s"
2900                                         "&startmsg=%ld"
2901                                         "&maxmsgs=%d"
2902                                         "&is_summary=%d\">"
2903                                         "%d-%d</option> \n",
2904                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
2905                                         oper,
2906                                         WCC->msgarr[lo-1],
2907                                         maxmsgs,
2908                                         is_summary,
2909                                         lo, hi);
2910                         }
2911                 }
2912
2913                 wprintf("<option value=\"%s&startmsg=%ld"
2914                         "&maxmsgs=9999999&is_summary=%d\">",
2915                         oper,
2916                         WCC->msgarr[0], is_summary);
2917                 wprintf(_("All"));
2918                 wprintf("</option>");
2919                 wprintf("</select> ");
2920                 wprintf(_("of %d messages."), nummsgs);
2921
2922                 /** forward/reverse */
2923                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2924                         "OnChange=\"location.href='%s&sortby=forward'\"",  
2925                         (bbs_reverse ? "" : "checked"),
2926                         oper
2927                 );
2928                 wprintf(">");
2929                 wprintf(_("oldest to newest"));
2930                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
2931                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2932                         "OnChange=\"location.href='%s&sortby=reverse'\"", 
2933                         (bbs_reverse ? "checked" : ""),
2934                         oper
2935                 );
2936                 wprintf(">");
2937                 wprintf(_("newest to oldest"));
2938                 wprintf("\n");
2939
2940                 wprintf("</p></form>\n");
2941                 /** end bbview scroller */
2942         }
2943         
2944 DONE:
2945         if (is_tasks) {
2946                 do_tasks_view();        /** Render the task list */
2947         }
2948
2949         if (is_calendar) {
2950                 do_calendar_view();     /** Render the calendar */
2951         }
2952
2953         if (is_addressbook) {
2954                 do_addrbook_view(addrbook, num_ab);     /** Render the address book */
2955         }
2956
2957         /** Note: wDumpContent() will output one additional </div> tag. */
2958         wprintf("</div>\n");            /** end of 'content' div */
2959         wDumpContent(1);
2960
2961         /** free the summary */
2962         if (WCC->summ != NULL) {
2963                 free(WCC->summ);
2964                 WCC->num_summ = 0;
2965                 WCC->summ = NULL;
2966         }
2967         if (addrbook != NULL) free(addrbook);
2968 }
2969
2970
2971 /*
2972  * Back end for post_message()
2973  * ... this is where the actual message gets transmitted to the server.
2974  */
2975 void post_mime_to_server(void) {
2976         char top_boundary[SIZ];
2977         char alt_boundary[SIZ];
2978         int is_multipart = 0;
2979         static int seq = 0;
2980         struct wc_attachment *att;
2981         char *encoded;
2982         size_t encoded_length;
2983         size_t encoded_strlen;
2984         char *txtmail = NULL;
2985
2986         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
2987                 serv_info.serv_fqdn,
2988                 getpid(),
2989                 ++seq
2990         );
2991         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
2992                 serv_info.serv_fqdn,
2993                 getpid(),
2994                 ++seq
2995         );
2996
2997         /* RFC2045 requires this, and some clients look for it... */
2998         serv_puts("MIME-Version: 1.0");
2999         serv_puts("X-Mailer: " PACKAGE_STRING);
3000
3001         /* If there are attachments, we have to do multipart/mixed */
3002         if (WC->first_attachment != NULL) {
3003                 is_multipart = 1;
3004         }
3005
3006         if (is_multipart) {
3007                 /* Remember, serv_printf() appends an extra newline */
3008                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
3009                 serv_printf("This is a multipart message in MIME format.\n");
3010                 serv_printf("--%s", top_boundary);
3011         }
3012
3013         /* Remember, serv_printf() appends an extra newline */
3014         serv_printf("Content-type: multipart/alternative; "
3015                 "boundary=\"%s\"\n", alt_boundary);
3016         serv_printf("This is a multipart message in MIME format.\n");
3017         serv_printf("--%s", alt_boundary);
3018
3019         serv_puts("Content-type: text/plain; charset=utf-8");
3020         serv_puts("Content-Transfer-Encoding: quoted-printable");
3021         serv_puts("");
3022         txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
3023         text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
3024         free(txtmail);
3025
3026         serv_printf("--%s", alt_boundary);
3027
3028         serv_puts("Content-type: text/html; charset=utf-8");
3029         serv_puts("Content-Transfer-Encoding: quoted-printable");
3030         serv_puts("");
3031         serv_puts("<html><body>\r\n");
3032         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
3033         serv_puts("</body></html>\r\n");
3034
3035         serv_printf("--%s--", alt_boundary);
3036         
3037         if (is_multipart) {
3038
3039                 /* Add in the attachments */
3040                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
3041
3042                         encoded_length = ((att->length * 150) / 100);
3043                         encoded = malloc(encoded_length);
3044                         if (encoded == NULL) break;
3045                         encoded_strlen = CtdlEncodeBase64(encoded, att->data, att->length, 1);
3046
3047                         serv_printf("--%s", top_boundary);
3048                         serv_printf("Content-type: %s", att->content_type);
3049                         serv_printf("Content-disposition: attachment; filename=\"%s\"", att->filename);
3050                         serv_puts("Content-transfer-encoding: base64");
3051                         serv_puts("");
3052                         serv_write(encoded, encoded_strlen);
3053                         serv_puts("");
3054                         serv_puts("");
3055                         free(encoded);
3056                 }
3057                 serv_printf("--%s--", top_boundary);
3058         }
3059
3060         serv_puts("000");
3061 }
3062
3063
3064 /*
3065  * Post message (or don't post message)
3066  *
3067  * Note regarding the "dont_post" variable:
3068  * A random value (actually, it's just a timestamp) is inserted as a hidden
3069  * field called "postseq" when the display_enter page is generated.  This
3070  * value is checked when posting, using the static variable dont_post.  If a
3071  * user attempts to post twice using the same dont_post value, the message is
3072  * discarded.  This prevents the accidental double-saving of the same message
3073  * if the user happens to click the browser "back" button.
3074  */
3075 void post_message(void)
3076 {
3077         char buf[1024];
3078         StrBuf *encoded_subject = NULL;
3079         static long dont_post = (-1L);
3080         struct wc_attachment *att, *aptr;
3081         int is_anonymous = 0;
3082         const StrBuf *display_name = NULL;
3083         struct wcsession *WCC = WC;
3084         
3085         if (havebstr("force_room")) {
3086                 gotoroom(bstr("force_room"));
3087         }
3088
3089         if (havebstr("display_name")) {
3090                 display_name = sbstr("display_name");
3091                 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
3092                         display_name = NULL;
3093                         is_anonymous = 1;
3094                 }
3095         }
3096
3097         if (WCC->upload_length > 0) {
3098
3099                 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
3100                 /** There's an attachment.  Save it to this struct... */
3101                 att = malloc(sizeof(struct wc_attachment));
3102                 memset(att, 0, sizeof(struct wc_attachment));
3103                 att->length = WCC->upload_length;
3104                 strcpy(att->content_type, WCC->upload_content_type);
3105                 strcpy(att->filename, WCC->upload_filename);
3106                 att->next = NULL;
3107
3108                 /** And add it to the list. */
3109                 if (WCC->first_attachment == NULL) {
3110                         WCC->first_attachment = att;
3111                 }
3112                 else {
3113                         aptr = WCC->first_attachment;
3114                         while (aptr->next != NULL) aptr = aptr->next;
3115                         aptr->next = att;
3116                 }
3117
3118                 /**
3119                  * Mozilla sends a simple filename, which is what we want,
3120                  * but Satan's Browser sends an entire pathname.  Reduce
3121                  * the path to just a filename if we need to.
3122                  */
3123                 while (num_tokens(att->filename, '/') > 1) {
3124                         remove_token(att->filename, 0, '/');
3125                 }
3126                 while (num_tokens(att->filename, '\\') > 1) {
3127                         remove_token(att->filename, 0, '\\');
3128                 }
3129
3130                 /**
3131                  * Transfer control of this memory from the upload struct
3132                  * to the attachment struct.
3133                  */
3134                 att->data = WCC->upload;
3135                 WCC->upload_length = 0;
3136                 WCC->upload = NULL;
3137                 display_enter();
3138                 return;
3139         }
3140
3141         if (havebstr("cancel_button")) {
3142                 sprintf(WCC->ImportantMessage, 
3143                         _("Cancelled.  Message was not posted."));
3144         } else if (havebstr("attach_button")) {
3145                 display_enter();
3146                 return;
3147         } else if (lbstr("postseq") == dont_post) {
3148                 sprintf(WCC->ImportantMessage, 
3149                         _("Automatically cancelled because you have already "
3150                         "saved this message."));
3151         } else {
3152                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
3153                 const StrBuf *Recp = NULL; 
3154                 const StrBuf *Cc = NULL;
3155                 const StrBuf *Bcc = NULL;
3156                 const StrBuf *Wikipage = NULL;
3157                 const StrBuf *my_email_addr = NULL;
3158                 StrBuf *CmdBuf = NULL;;
3159                 StrBuf *references = NULL;
3160
3161                 if (havebstr("references"))
3162                 {
3163                         const StrBuf *ref = sbstr("references");
3164                         references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
3165                         lprintf(9, "Converting: %s\n", ChrPtr(references));
3166                         StrBufReplaceChars(references, '|', '!');
3167                         lprintf(9, "Converted: %s\n", ChrPtr(references));
3168                 }
3169                 if (havebstr("subject")) {
3170                         const StrBuf *Subj;
3171                         /*
3172                          * make enough room for the encoded string; 
3173                          * plus the QP header 
3174                          */
3175                         Subj = sbstr("subject");
3176                         
3177                         StrBufRFC2047encode(&encoded_subject, Subj);
3178                 }
3179                 Recp = sbstr("recp");
3180                 Cc = sbstr("cc");
3181                 Bcc = sbstr("bcc");
3182                 Wikipage = sbstr("wikipage");
3183                 my_email_addr = sbstr("my_email_addr");
3184                 
3185                 CmdBuf = NewStrBufPlain(NULL, 
3186                                         sizeof (CMD) + 
3187                                         StrLength(Recp) + 
3188                                         StrLength(encoded_subject) +
3189                                         StrLength(Cc) +
3190                                         StrLength(Bcc) + 
3191                                         StrLength(Wikipage) +
3192                                         StrLength(my_email_addr) + 
3193                                         StrLength(references));
3194
3195                 StrBufPrintf(CmdBuf, 
3196                              CMD,
3197                              ChrPtr(Recp),
3198                              is_anonymous,
3199                              ChrPtr(encoded_subject),
3200                              ChrPtr(display_name),
3201                              ChrPtr(Cc),
3202                              ChrPtr(Bcc),
3203                              ChrPtr(Wikipage),
3204                              ChrPtr(my_email_addr),
3205                              ChrPtr(references));
3206                 FreeStrBuf(&references);
3207
3208                 lprintf(9, "%s\n", CmdBuf);
3209                 serv_puts(ChrPtr(CmdBuf));
3210                 serv_getln(buf, sizeof buf);
3211                 FreeStrBuf(&CmdBuf);
3212                 FreeStrBuf(&encoded_subject);
3213                 if (buf[0] == '4') {
3214                         post_mime_to_server();
3215                         if (  (havebstr("recp"))
3216                            || (havebstr("cc"  ))
3217                            || (havebstr("bcc" ))
3218                         ) {
3219                                 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
3220                         }
3221                         else {
3222                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
3223                         }
3224                         dont_post = lbstr("postseq");
3225                 } else {
3226                         lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
3227                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3228                         display_enter();
3229                         return;
3230                 }
3231         }
3232
3233         free_attachments(WCC);
3234
3235         /**
3236          *  We may have been supplied with instructions regarding the location
3237          *  to which we must return after posting.  If found, go there.
3238          */
3239         if (havebstr("return_to")) {
3240                 http_redirect(bstr("return_to"));
3241         }
3242         /**
3243          *  If we were editing a page in a wiki room, go to that page now.
3244          */
3245         else if (havebstr("wikipage")) {
3246                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
3247                 http_redirect(buf);
3248         }
3249         /**
3250          *  Otherwise, just go to the "read messages" loop.
3251          */
3252         else {
3253                 readloop("readnew");
3254         }
3255 }
3256
3257
3258
3259
3260 /**
3261  * \brief display the message entry screen
3262  */
3263 void display_enter(void)
3264 {
3265         char buf[SIZ];
3266         StrBuf *ebuf;
3267         long now;
3268         const StrBuf *display_name = NULL;
3269         struct wc_attachment *att;
3270         int recipient_required = 0;
3271         int subject_required = 0;
3272         int recipient_bad = 0;
3273         int is_anonymous = 0;
3274         long existing_page = (-1L);
3275         struct wcsession *WCC = WC;
3276
3277         now = time(NULL);
3278
3279         if (havebstr("force_room")) {
3280                 gotoroom(bstr("force_room"));
3281         }
3282
3283         display_name = sbstr("display_name");
3284         if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
3285                 display_name = NULL;
3286                 is_anonymous = 1;
3287         }
3288
3289         /** First test to see whether this is a room that requires recipients to be entered */
3290         serv_puts("ENT0 0");
3291         serv_getln(buf, sizeof buf);
3292
3293         if (!strncmp(buf, "570", 3)) {          /** 570 means that we need a recipient here */
3294                 recipient_required = 1;
3295         }
3296         else if (buf[0] != '2') {               /** Any other error means that we cannot continue */
3297                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
3298                 readloop("readnew");
3299                 return;
3300         }
3301
3302         /* Is the server strongly recommending that the user enter a message subject? */
3303         if ((buf[3] != '\0') && (buf[4] != '\0')) {
3304                 subject_required = extract_int(&buf[4], 1);
3305         }
3306
3307         /**
3308          * Are we perhaps in an address book view?  If so, then an "enter
3309          * message" command really means "add new entry."
3310          */
3311         if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
3312                 do_edit_vcard(-1, "", "", WCC->wc_roomname);
3313                 return;
3314         }
3315
3316         /*
3317          * Are we perhaps in a calendar room?  If so, then an "enter
3318          * message" command really means "add new calendar item."
3319          */
3320         if (WCC->wc_default_view == VIEW_CALENDAR) {
3321                 display_edit_event();
3322                 return;
3323         }
3324
3325         /*
3326          * Are we perhaps in a tasks view?  If so, then an "enter
3327          * message" command really means "add new task."
3328          */
3329         if (WCC->wc_default_view == VIEW_TASKS) {
3330                 display_edit_task();
3331                 return;
3332         }
3333
3334         /*
3335          * Otherwise proceed normally.
3336          * Do a custom room banner with no navbar...
3337          */
3338         output_headers(1, 1, 2, 0, 0, 0);
3339         wprintf("<div id=\"banner\">\n");
3340         embed_room_banner(NULL, navbar_none);
3341         wprintf("</div>\n");
3342         wprintf("<div id=\"content\">\n"
3343                 "<div class=\"fix_scrollbar_bug message \">");
3344
3345         /* Now check our actual recipients if there are any */
3346         if (recipient_required) {
3347                 const StrBuf *Recp = NULL; 
3348                 const StrBuf *Cc = NULL;
3349                 const StrBuf *Bcc = NULL;
3350                 const StrBuf *Wikipage = NULL;
3351                 StrBuf *CmdBuf = NULL;;
3352                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
3353                 
3354                 Recp = sbstr("recp");
3355                 Cc = sbstr("cc");
3356                 Bcc = sbstr("bcc");
3357                 Wikipage = sbstr("wikipage");
3358                 
3359                 CmdBuf = NewStrBufPlain(NULL, 
3360                                         sizeof (CMD) + 
3361                                         StrLength(Recp) + 
3362                                         StrLength(display_name) +
3363                                         StrLength(Cc) +
3364                                         StrLength(Bcc) + 
3365                                         StrLength(Wikipage));
3366
3367                 StrBufPrintf(CmdBuf, 
3368                              CMD,
3369                              ChrPtr(Recp), 
3370                              is_anonymous,
3371                              ChrPtr(display_name),
3372                              ChrPtr(Cc), 
3373                              ChrPtr(Bcc), 
3374                              ChrPtr(Wikipage));
3375                 serv_puts(ChrPtr(CmdBuf));
3376                 serv_getln(buf, sizeof buf);
3377                 FreeStrBuf(&CmdBuf);
3378
3379                 if (!strncmp(buf, "570", 3)) {  /** 570 means we have an invalid recipient listed */
3380                         if (havebstr("recp") && 
3381                             havebstr("cc"  ) && 
3382                             havebstr("bcc" )) {
3383                                 recipient_bad = 1;
3384                         }
3385                 }
3386                 else if (buf[0] != '2') {       /** Any other error means that we cannot continue */
3387                         wprintf("<em>%s</em><br />\n", &buf[4]);
3388                         goto DONE;
3389                 }
3390         }
3391
3392         /** If we got this far, we can display the message entry screen. */
3393
3394         /* begin message entry screen */
3395         wprintf("<form "
3396                 "enctype=\"multipart/form-data\" "
3397                 "method=\"POST\" "
3398                 "accept-charset=\"UTF-8\" "
3399                 "action=\"post\" "
3400                 "name=\"enterform\""
3401                 ">\n");
3402         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n", now);
3403         if (WCC->wc_view == VIEW_WIKI) {
3404                 wprintf("<input type=\"hidden\" name=\"wikipage\" value=\"%s\">\n", bstr("wikipage"));
3405         }
3406         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"%s\">\n", bstr("return_to"));
3407         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
3408         wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
3409         escputs(WCC->wc_roomname);
3410         wprintf("\">\n");
3411         wprintf("<input type=\"hidden\" name=\"references\" value=\"");
3412         escputs(bstr("references"));
3413         wprintf("\">\n");
3414
3415         /** submit or cancel buttons */
3416         wprintf("<p class=\"send_edit_msg\">");
3417         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
3418         if (recipient_required) {
3419                 wprintf(_("Send message"));
3420         } else {
3421                 wprintf(_("Post message"));
3422         }
3423         wprintf("\">&nbsp;"
3424                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
3425         wprintf("</p>");
3426
3427         /** header bar */
3428
3429         wprintf("<img src=\"static/newmess3_24x.gif\" class=\"imgedit\">");
3430         wprintf("  ");  /** header bar */
3431         webcit_fmt_date(buf, now, 0);
3432         wprintf("%s", buf);
3433         wprintf("\n");  /** header bar */
3434
3435         wprintf("<table width=\"100%%\" class=\"edit_msg_table\">");
3436         wprintf("<tr>");
3437         wprintf("<th><label for=\"from_id\" > ");
3438         wprintf(_(" <I>from</I> "));
3439         wprintf("</label></th>");
3440
3441         wprintf("<td colspan=\"2\">");
3442
3443         /* Allow the user to select any of his valid screen names */
3444
3445         wprintf("<select name=\"display_name\" size=1 id=\"from_id\">\n");
3446
3447         serv_puts("GVSN");
3448         serv_getln(buf, sizeof buf);
3449         if (buf[0] == '1') {
3450                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3451                         wprintf("<option %s value=\"",
3452                                 ((!strcasecmp(bstr("display_name"), buf)) ? "selected" : "")
3453                         );
3454                         escputs(buf);
3455                         wprintf("\">");
3456                         escputs(buf);
3457                         wprintf("</option>\n");
3458                 }
3459         }
3460
3461         if (WCC->room_flags & QR_ANONOPT) {
3462                 wprintf("<option %s value=\"__ANONYMOUS__\">%s</option>\n",
3463                         ((!strcasecmp(bstr("__ANONYMOUS__"), WCC->wc_fullname)) ? "selected" : ""),
3464                         _("Anonymous")
3465                 );
3466         }
3467
3468         wprintf("</select>\n");
3469
3470         /* If this is an email (not a post), allow the user to select any of his
3471          * valid email addresses.
3472          */
3473         if (recipient_required) {
3474                 serv_puts("GVEA");
3475                 serv_getln(buf, sizeof buf);
3476                 if (buf[0] == '1') {
3477                         wprintf("<select name=\"my_email_addr\" size=1>\n");
3478                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3479                                 wprintf("<option value=\"");
3480                                 escputs(buf);
3481                                 wprintf("\">&lt;");
3482                                 escputs(buf);
3483                                 wprintf("&gt;</option>\n");
3484                         }
3485                         wprintf("</select>\n");
3486                 }
3487         }
3488
3489         wprintf(_(" <I>in</I> "));
3490         escputs(WCC->wc_roomname);
3491
3492         wprintf("</td></tr>");
3493
3494         if (recipient_required) {
3495                 char *ccraw;
3496                 char *copy;
3497                 size_t len;
3498                 wprintf("<tr><th><label for=\"recp_id\"> ");
3499                 wprintf(_("To:"));
3500                 wprintf("</label></th>"
3501                         "<td><input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_id\" value=\"");
3502                 ccraw = xbstr("recp", &len);
3503                 copy = (char*) malloc(len * 2 + 1);
3504                 memcpy(copy, ccraw, len + 1); 
3505                 utf8ify_rfc822_string(copy);
3506                 escputs(copy);
3507                 free(copy);
3508                 wprintf("\" size=45 maxlength=1000 />");
3509                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
3510                 wprintf("</td><td rowspan=\"3\" align=\"left\" valign=\"top\">");
3511
3512                 /** Pop open an address book -- begin **/
3513                 wprintf(
3514                         "<a href=\"javascript:PopOpenAddressBook('recp_id|%s|cc_id|%s|bcc_id|%s');\" "
3515                         "title=\"%s\">"
3516                         "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
3517                         "&nbsp;%s</a>",
3518                         _("To:"), _("CC:"), _("BCC:"),
3519                         _("Contacts"), _("Contacts")
3520                 );
3521                 /** Pop open an address book -- end **/
3522
3523                 wprintf("</td></tr>");
3524
3525                 wprintf("<tr><th><label for=\"cc_id\"> ");
3526                 wprintf(_("CC:"));
3527                 wprintf("</label></th>"
3528                         "<td><input autocomplete=\"off\" type=\"text\" name=\"cc\" id=\"cc_id\" value=\"");
3529                 ccraw = xbstr("cc", &len);
3530                 copy = (char*) malloc(len * 2 + 1);
3531                 memcpy(copy, ccraw, len + 1); 
3532                 utf8ify_rfc822_string(copy);
3533                 escputs(copy);
3534                 free(copy);
3535                 wprintf("\" size=45 maxlength=1000 />");
3536                 wprintf("<div class=\"auto_complete\" id=\"cc_name_choices\"></div>");
3537                 wprintf("</td></tr>");
3538
3539                 wprintf("<tr><th><label for=\"bcc_id\"> ");
3540                 wprintf(_("BCC:"));
3541                 wprintf("</label></th>"
3542                         "<td><input autocomplete=\"off\" type=\"text\" name=\"bcc\" id=\"bcc_id\" value=\"");
3543                 ccraw = xbstr("bcc", &len);
3544                 copy = (char*) malloc(len * 2 + 1);
3545                 memcpy(copy, ccraw, len + 1); 
3546                 utf8ify_rfc822_string(copy);
3547                 escputs(copy);
3548                 free(copy);
3549                 wprintf("\" size=45 maxlength=1000 />");
3550                 wprintf("<div class=\"auto_complete\" id=\"bcc_name_choices\"></div>");
3551                 wprintf("</td></tr>");
3552
3553                 /** Initialize the autocomplete ajax helpers (found in wclib.js) */
3554                 wprintf("<script type=\"text/javascript\">      \n"
3555                         " activate_entmsg_autocompleters();     \n"
3556                         "</script>                              \n"
3557                 );
3558
3559         }
3560
3561         wprintf("<tr><th><label for=\"subject_id\" > ");
3562         if (recipient_required || subject_required) {
3563                 wprintf(_("Subject:"));
3564         }
3565         else {
3566                 wprintf(_("Subject (optional):"));
3567         }
3568         wprintf("</label></th>"
3569                 "<td colspan=\"2\">"
3570                 "<input type=\"text\" name=\"subject\" id=\"subject_id\" value=\"");
3571         escputs(bstr("subject"));
3572         wprintf("\" size=45 maxlength=70>\n");
3573         wprintf("</td></tr>");
3574
3575         wprintf("<tr><td colspan=\"3\">\n");
3576
3577         wprintf("<textarea name=\"msgtext\" cols=\"80\" rows=\"15\">");
3578
3579         /** If we're continuing from a previous edit, put our partially-composed message back... */
3580         msgescputs(bstr("msgtext"));
3581
3582         /* If we're forwarding a message, insert it here... */
3583         if (lbstr("fwdquote") > 0L) {
3584                 wprintf("<br><div align=center><i>");
3585                 wprintf(_("--- forwarded message ---"));
3586                 wprintf("</i></div><br>");
3587                 pullquote_message(lbstr("fwdquote"), 1, 1);
3588         }
3589
3590         /** If we're replying quoted, insert the quote here... */
3591         else if (lbstr("replyquote") > 0L) {
3592                 wprintf("<br>"
3593                         "<blockquote>");
3594                 pullquote_message(lbstr("replyquote"), 0, 1);
3595                 wprintf("</blockquote><br>");
3596         }
3597
3598         /** If we're editing a wiki page, insert the existing page here... */
3599         else if (WCC->wc_view == VIEW_WIKI) {
3600                 safestrncpy(buf, bstr("wikipage"), sizeof buf);
3601                 str_wiki_index(buf);
3602                 existing_page = locate_message_by_uid(buf);
3603                 if (existing_page >= 0L) {
3604                         pullquote_message(existing_page, 1, 0);
3605                 }
3606         }
3607
3608         /** Insert our signature if appropriate... */
3609         if ( (WCC->is_mailbox) && !yesbstr("sig_inserted") ) {
3610                 int UseSig;
3611                 get_pref_yesno("use_sig", &UseSig, 0);
3612                 if (UseSig) {
3613                         StrBuf *Sig;
3614                         const char *sig, *esig;
3615
3616                         get_preference("signature", &ebuf);
3617                         Sig = NewStrBuf();
3618                         StrBufEUid_unescapize(Sig, ebuf);
3619                         sig = ChrPtr(Sig);
3620                         esig = sig + StrLength(Sig);
3621                         wprintf("<br>--<br>");
3622                         while (sig <= esig) {
3623                                 if (*sig == '\n') {
3624                                         wprintf("<br>");
3625                                 }
3626                                 else if (*sig == '<') {
3627                                         wprintf("&lt;");
3628                                 }
3629                                 else if (*sig == '>') {
3630                                         wprintf("&gt;");
3631                                 }
3632                                 else if (*sig == '&') {
3633                                         wprintf("&amp;");
3634                                 }
3635                                 else if (*sig == '\"') {
3636                                         wprintf("&quot;");
3637                                 }
3638                                 else if (*sig == '\'') {
3639                                         wprintf("&#39;");
3640                                 }
3641                                 else /* since we're utf 8, is this a good idea? if (isprint(*sig))*/ {
3642                                         wprintf("%c", *sig);
3643                                 } 
3644                                 sig ++;
3645                         }
3646                         FreeStrBuf(&Sig);
3647                 }
3648         }
3649
3650         wprintf("</textarea>\n");
3651
3652         /** Make sure we only insert our signature once */
3653         /** We don't care if it was there or not before, it needs to be there now. */
3654         wprintf("<input type=\"hidden\" name=\"sig_inserted\" value=\"yes\">\n");
3655         
3656         /**
3657          * The following template embeds the TinyMCE richedit control, and automatically
3658          * transforms the textarea into a richedit textarea.
3659          */
3660         do_template("richedit", NULL);
3661
3662         /** Enumerate any attachments which are already in place... */
3663         wprintf("<div class=\"attachment buttons\"><img src=\"static/diskette_24x.gif\" class=\"imgedit\" > ");
3664         wprintf(_("Attachments:"));
3665         wprintf(" ");
3666         wprintf("<select name=\"which_attachment\" size=1>");
3667         for (att = WCC->first_attachment; att != NULL; att = att->next) {
3668                 wprintf("<option value=\"");
3669                 urlescputs(att->filename);
3670                 wprintf("\">");
3671                 escputs(att->filename);
3672                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
3673                 wprintf("</option>\n");
3674         }
3675         wprintf("</select>");
3676
3677         /** Now offer the ability to attach additional files... */
3678         wprintf("&nbsp;&nbsp;&nbsp;");
3679         wprintf(_("Attach file:"));
3680         wprintf(" <input name=\"attachfile\" class=\"attachfile\" "
3681                 "size=16 type=\"file\">\n&nbsp;&nbsp;"
3682                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
3683         wprintf("</div>");      /* End of "attachment buttons" div */
3684
3685
3686         wprintf("</td></tr></table>");
3687         
3688         wprintf("</form>\n");
3689         wprintf("</div>\n");    /* end of "fix_scrollbar_bug" div */
3690
3691         /* NOTE: address_book_popup() will close the "content" div.  Don't close it here. */
3692 DONE:   address_book_popup();
3693         wDumpContent(1);
3694 }
3695
3696
3697 /**
3698  * \brief delete a message
3699  */
3700 void delete_msg(void)
3701 {
3702         long msgid;
3703         char buf[SIZ];
3704
3705         msgid = lbstr("msgid");
3706
3707         if (WC->wc_is_trash) {  /** Delete from Trash is a real delete */
3708                 serv_printf("DELE %ld", msgid); 
3709         }
3710         else {                  /** Otherwise move it to Trash */
3711                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
3712         }
3713
3714         serv_getln(buf, sizeof buf);
3715         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3716
3717         readloop("readnew");
3718 }
3719
3720
3721 /**
3722  * \brief move a message to another folder
3723  */
3724 void move_msg(void)
3725 {
3726         long msgid;
3727         char buf[SIZ];
3728
3729         msgid = lbstr("msgid");
3730
3731         if (havebstr("move_button")) {
3732                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
3733                 serv_puts(buf);
3734                 serv_getln(buf, sizeof buf);
3735                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
3736         } else {
3737                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
3738         }
3739
3740         readloop("readnew");
3741 }
3742
3743
3744
3745
3746
3747 /*
3748  * Confirm move of a message
3749  */
3750 void confirm_move_msg(void)
3751 {
3752         long msgid;
3753         char buf[SIZ];
3754         char targ[SIZ];
3755
3756         msgid = lbstr("msgid");
3757
3758
3759         output_headers(1, 1, 2, 0, 0, 0);
3760         wprintf("<div id=\"banner\">\n");
3761         wprintf("<h1>");
3762         wprintf(_("Confirm move of message"));
3763         wprintf("</h1>");
3764         wprintf("</div>\n");
3765
3766         wprintf("<div id=\"content\" class=\"service\">\n");
3767
3768         wprintf("<CENTER>");
3769
3770         wprintf(_("Move this message to:"));
3771         wprintf("<br />\n");
3772
3773         wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
3774         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
3775         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
3776
3777         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
3778         serv_puts("LKRA");
3779         serv_getln(buf, sizeof buf);
3780         if (buf[0] == '1') {
3781                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3782                         extract_token(targ, buf, 0, '|', sizeof targ);
3783                         wprintf("<OPTION>");
3784                         escputs(targ);
3785                         wprintf("\n");
3786                 }
3787         }
3788         wprintf("</SELECT>\n");
3789         wprintf("<br />\n");
3790
3791         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
3792         wprintf("&nbsp;");
3793         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
3794         wprintf("</form></CENTER>\n");
3795
3796         wprintf("</CENTER>\n");
3797         wDumpContent(1);
3798 }
3799
3800 void readnew(void) { readloop("readnew");}
3801 void readold(void) { readloop("readold");}
3802 void readfwd(void) { readloop("readfwd");}
3803 void headers(void) { readloop("headers");}
3804 void do_search(void) { readloop("do_search");}
3805
3806 void 
3807 InitModule_MSG
3808 (void)
3809 {
3810         WebcitAddUrlHandler(HKEY("readnew"), readnew, 0);
3811         WebcitAddUrlHandler(HKEY("readold"), readold, 0);
3812         WebcitAddUrlHandler(HKEY("readfwd"), readfwd, 0);
3813         WebcitAddUrlHandler(HKEY("headers"), headers, 0);
3814         WebcitAddUrlHandler(HKEY("do_search"), do_search, 0);
3815         WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
3816         WebcitAddUrlHandler(HKEY("post"), post_message, 0);
3817         WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
3818         WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
3819         WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
3820         WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
3821         WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
3822         WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
3823         WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
3824         return ;
3825 }