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