Tests and preparation for another mime parser update
[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                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
837                                 strcpy(vcard_partnum, mime_partnum);
838                         }
839
840                         if (!strcasecmp(mime_content_type, "text/calendar")) {
841                                 strcpy(cal_partnum, mime_partnum);
842                         }
843
844                         /** end handler prep ***/
845
846                 }
847
848         }
849
850         /** Generate a reply-to address */
851         if (strlen(rfca) > 0) {
852                 strcpy(reply_to, rfca);
853         }
854         else {
855                 if ( (strlen(node) > 0)
856                    && (strcasecmp(node, serv_info.serv_nodename))
857                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
858                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
859                                 from, node);
860                 }
861                 else {
862                         snprintf(reply_to, sizeof(reply_to), "%s", from);
863                 }
864         }
865
866         if (nhdr == 1) {
867                 wprintf("****");
868         }
869
870         wprintf("</span>");
871 #ifdef HAVE_ICONV
872         utf8ify_rfc822_string(m_cc);
873         utf8ify_rfc822_string(m_subject);
874 #endif
875         if (strlen(m_cc) > 0) {
876                 wprintf("<br />"
877                         "<span class=\"message_subject\">");
878                 wprintf(_("CC:"));
879                 wprintf(" ");
880                 escputs(m_cc);
881                 wprintf("</span>");
882         }
883         if (strlen(m_subject) > 0) {
884                 wprintf("<br />"
885                         "<span class=\"message_subject\">");
886                 wprintf(_("Subject:"));
887                 wprintf(" ");
888                 escputs(m_subject);
889                 wprintf("</span>");
890         }
891         wprintf("</td>\n");
892
893         /** start msg buttons */
894         if (!printable_view) {
895                 wprintf("<td align=right><span class=\"msgbuttons\">\n");
896
897                 /** Reply */
898                 if ( (WC->wc_view == VIEW_MAILBOX) || (WC->wc_view == VIEW_BBS) ) {
899                         wprintf("<a href=\"display_enter");
900                         if (WC->is_mailbox) {
901                                 wprintf("?replyquote=%ld", msgnum);
902                         }
903                         wprintf("?recp=");
904                         urlescputs(reply_to);
905                         if (strlen(m_subject) > 0) {
906                                 wprintf("?subject=");
907                                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
908                                 urlescputs(m_subject);
909                         }
910                         wprintf("\">[%s]</a> ", _("Reply"));
911                 }
912
913                 /** ReplyQuoted */
914                 if ( (WC->wc_view == VIEW_MAILBOX) || (WC->wc_view == VIEW_BBS) ) {
915                         if (!WC->is_mailbox) {
916                                 wprintf("<a href=\"display_enter");
917                                 wprintf("?replyquote=%ld", msgnum);
918                                 wprintf("?recp=");
919                                 urlescputs(reply_to);
920                                 if (strlen(m_subject) > 0) {
921                                         wprintf("?subject=");
922                                         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
923                                         urlescputs(m_subject);
924                                 }
925                                 wprintf("\">[%s]</a> ", _("ReplyQuoted"));
926                         }
927                 }
928
929                 /** ReplyAll */
930                 if (WC->wc_view == VIEW_MAILBOX) {
931                         wprintf("<a href=\"display_enter");
932                         wprintf("?replyquote=%ld", msgnum);
933                         wprintf("?recp=");
934                         urlescputs(reply_to);
935                         wprintf("?cc=");
936                         urlescputs(reply_all);
937                         if (strlen(m_subject) > 0) {
938                                 wprintf("?subject=");
939                                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
940                                 urlescputs(m_subject);
941                         }
942                         wprintf("\">[%s]</a> ", _("ReplyAll"));
943                 }
944
945                 /** Forward */
946                 if (WC->wc_view == VIEW_MAILBOX) {
947                         wprintf("<a href=\"display_enter?fwdquote=%ld?subject=", msgnum);
948                         if (strncasecmp(m_subject, "Fwd:", 4)) wprintf("Fwd:%20");
949                         urlescputs(m_subject);
950                         wprintf("\">[%s]</a> ", _("Forward"));
951                 }
952
953                 /** If this is one of my own rooms, or if I'm an Aide or Room Aide, I can move/delete */
954                 if ( (WC->is_room_aide) || (WC->is_mailbox) ) {
955                         /** Move */
956                         wprintf("<a href=\"confirm_move_msg?msgid=%ld\">[%s]</a> ",
957                                 msgnum, _("Move"));
958         
959                         /** Delete */
960                         wprintf("<a href=\"delete_msg?msgid=%ld\" "
961                                 "onClick=\"return confirm('%s');\">"
962                                 "[%s]</a> ", msgnum, _("Delete this message?"), _("Delete")
963                         );
964                 }
965
966                 /** Headers */
967                 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'); \" >"
968                         "[%s]</a>", msgnum, msgnum, _("Headers"));
969
970
971                 /** Print */
972                 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'); \" >"
973                         "[%s]</a>", msgnum, msgnum, _("Print"));
974
975                 wprintf("</span></td>");
976         }
977
978         wprintf("</tr></table>\n");
979
980         /** Begin body */
981         wprintf("<table class=\"messages_background\" "
982                 "cellpadding=1 cellspacing=0><tr><td>");
983
984         /**
985          * Learn the content type
986          */
987         strcpy(mime_content_type, "text/plain");
988         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
989                 if (!strcmp(buf, "000")) {
990                         wprintf("<i>");
991                         wprintf(_("unexpected end of message"));
992                         wprintf("</i><br /><br />\n");
993                         goto ENDBODY;
994                 }
995                 if (!strncasecmp(buf, "Content-type: ", 14)) {
996                         safestrncpy(mime_content_type, &buf[14],
997                                 sizeof(mime_content_type));
998                         for (i=0; i<strlen(mime_content_type); ++i) {
999                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1000                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1001                                                 sizeof mime_charset);
1002                                 }
1003                         }
1004                         for (i=0; i<strlen(mime_content_type); ++i) {
1005                                 if (mime_content_type[i] == ';') {
1006                                         mime_content_type[i] = 0;
1007                                 }
1008                         }
1009                         for (i=0; i<strlen(mime_charset); ++i) {
1010                                 if (mime_charset[i] == ';') {
1011                                         mime_charset[i] = 0;
1012                                 }
1013                         }
1014                 }
1015         }
1016
1017         /** Set up a character set conversion if we need to (and if we can) */
1018 #ifdef HAVE_ICONV
1019         if (strchr(mime_charset, ';')) strcpy(strchr(mime_charset, ';'), "");
1020         if ( (strcasecmp(mime_charset, "us-ascii"))
1021            && (strcasecmp(mime_charset, "UTF-8"))
1022            && (strcasecmp(mime_charset, ""))
1023         ) {
1024                 ic = ctdl_iconv_open("UTF-8", mime_charset);
1025                 if (ic == (iconv_t)(-1) ) {
1026                         lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
1027                                 __FILE__, __LINE__, mime_charset, strerror(errno));
1028                 }
1029         }
1030 #endif
1031
1032         /** Messages in legacy Citadel variformat get handled thusly... */
1033         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1034                 fmout("JUSTIFY");
1035         }
1036
1037         /** Boring old 80-column fixed format text gets handled this way... */
1038         else if ( (!strcasecmp(mime_content_type, "text/plain"))
1039                 || (!strcasecmp(mime_content_type, "text")) ) {
1040                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1041                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
1042                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
1043
1044 #ifdef HAVE_ICONV
1045                         if (ic != (iconv_t)(-1) ) {
1046                                 ibuf = buf;
1047                                 ibuflen = strlen(ibuf);
1048                                 obuflen = SIZ;
1049                                 obuf = (char *) malloc(obuflen);
1050                                 osav = obuf;
1051                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1052                                 osav[SIZ-obuflen] = 0;
1053                                 safestrncpy(buf, osav, sizeof buf);
1054                                 free(osav);
1055                         }
1056 #endif
1057
1058                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
1059                                 buf[strlen(buf) - 1] = 0;
1060                         if ((bq == 0) &&
1061                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1062                                 wprintf("<blockquote>");
1063                                 bq = 1;
1064                         } else if ((bq == 1) &&
1065                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1066                                 wprintf("</blockquote>");
1067                                 bq = 0;
1068                         }
1069                         wprintf("<tt>");
1070                         url(buf);
1071                         escputs(buf);
1072                         wprintf("</tt><br />\n");
1073                 }
1074                 wprintf("</i><br />");
1075         }
1076
1077         else /** HTML is fun, but we've got to strip it first */
1078         if (!strcasecmp(mime_content_type, "text/html")) {
1079                 output_html(mime_charset, (WC->wc_view == VIEW_WIKI ? 1 : 0));
1080         }
1081
1082         /** Unknown weirdness */
1083         else {
1084                 wprintf(_("I don't know how to display %s"), mime_content_type);
1085                 wprintf("<br />\n", mime_content_type);
1086                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1087         }
1088
1089         /** If there are attached submessages, display them now... */
1090         if ( (strlen(mime_submessages) > 0) && (!section[0]) ) {
1091                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
1092                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
1093                         /** use printable_view to suppress buttons */
1094                         wprintf("<blockquote>");
1095                         read_message(msgnum, 1, buf);
1096                         wprintf("</blockquote>");
1097                 }
1098         }
1099
1100
1101         /** Afterwards, offer links to download attachments 'n' such */
1102         if ( (strlen(mime_http) > 0) && (!section[0]) ) {
1103                 wprintf("%s", mime_http);
1104         }
1105
1106         /** Handler for vCard parts */
1107         if (strlen(vcard_partnum) > 0) {
1108                 part_source = load_mimepart(msgnum, vcard_partnum);
1109                 if (part_source != NULL) {
1110
1111                         /** If it's my vCard I can edit it */
1112                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1113                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1114                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1115                         ) {
1116                                 wprintf("<a href=\"edit_vcard?"
1117                                         "msgnum=%ld?partnum=%s\">",
1118                                         msgnum, vcard_partnum);
1119                                 wprintf("[%s]</a>", _("edit"));
1120                         }
1121
1122                         /** In all cases, display the full card */
1123                         display_vcard(part_source, 0, 1, NULL);
1124                 }
1125         }
1126
1127         /** Handler for calendar parts */
1128         if (strlen(cal_partnum) > 0) {
1129                 part_source = load_mimepart(msgnum, cal_partnum);
1130                 if (part_source != NULL) {
1131                         cal_process_attachment(part_source,
1132                                                 msgnum, cal_partnum);
1133                 }
1134         }
1135
1136         if (part_source) {
1137                 free(part_source);
1138                 part_source = NULL;
1139         }
1140
1141 ENDBODY:
1142         wprintf("</td></tr></table>\n");
1143
1144         /** end everythingamundo table */
1145         if (!printable_view) {
1146                 wprintf("</td></tr></table>\n");
1147                 wprintf("</div><br />\n");
1148         }
1149
1150 #ifdef HAVE_ICONV
1151         if (ic != (iconv_t)(-1) ) {
1152                 iconv_close(ic);
1153         }
1154 #endif
1155 }
1156
1157
1158
1159 /**
1160  * \brief Unadorned HTML output of an individual message, suitable
1161  * for placing in a hidden iframe, for printing, or whatever
1162  *
1163  * \param msgnum_as_string Message number, as a string instead of as a long int
1164  */
1165 void embed_message(char *msgnum_as_string) {
1166         long msgnum = 0L;
1167
1168         msgnum = atol(msgnum_as_string);
1169         begin_ajax_response();
1170         read_message(msgnum, 0, "");
1171         end_ajax_response();
1172 }
1173
1174
1175 /**
1176  * \brief Printable view of a message
1177  *
1178  * \param msgnum_as_string Message number, as a string instead of as a long int
1179  */
1180 void print_message(char *msgnum_as_string) {
1181         long msgnum = 0L;
1182
1183         msgnum = atol(msgnum_as_string);
1184         output_headers(0, 0, 0, 0, 0, 0);
1185
1186         wprintf("Content-type: text/html\r\n"
1187                 "Server: %s\r\n"
1188                 "Connection: close\r\n",
1189                 SERVER);
1190         begin_burst();
1191
1192         wprintf("\r\n\r\n<html>\n"
1193                 "<head><title>Printable view</title></head>\n"
1194                 "<body onLoad=\" window.print(); window.close(); \">\n"
1195         );
1196         
1197         read_message(msgnum, 1, "");
1198
1199         wprintf("\n</body></html>\n\n");
1200         wDumpContent(0);
1201 }
1202
1203
1204
1205 /**
1206  * \brief Display a message's headers
1207  *
1208  * \param msgnum_as_string Message number, as a string instead of as a long int
1209  */
1210 void display_headers(char *msgnum_as_string) {
1211         long msgnum = 0L;
1212         char buf[1024];
1213
1214         msgnum = atol(msgnum_as_string);
1215         output_headers(0, 0, 0, 0, 0, 0);
1216
1217         wprintf("Content-type: text/plain\r\n"
1218                 "Server: %s\r\n"
1219                 "Connection: close\r\n",
1220                 SERVER);
1221         begin_burst();
1222
1223         serv_printf("MSG2 %ld|3", msgnum);
1224         serv_getln(buf, sizeof buf);
1225         if (buf[0] == '1') {
1226                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1227                         wprintf("%s\n", buf);
1228                 }
1229         }
1230
1231         wDumpContent(0);
1232 }
1233
1234
1235
1236 /**
1237  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
1238  *        or 'reply quoted' operations.
1239  *
1240  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
1241  *       in this function.  Doing so would throw a JavaScript error in the
1242  *       'supplied text' argument to the editor.
1243  *
1244  * \param msgnum Message number of the message we want to quote
1245  * \param forward_attachments Nonzero if we want attachments to be forwarded
1246  */
1247 void pullquote_message(long msgnum, int forward_attachments, int include_headers) {
1248         char buf[SIZ];
1249         char mime_partnum[256];
1250         char mime_filename[256];
1251         char mime_content_type[256];
1252         char mime_charset[256];
1253         char mime_disposition[256];
1254         int mime_length;
1255         char *attachments = NULL;
1256         char *ptr = NULL;
1257         int num_attachments = 0;
1258         struct wc_attachment *att, *aptr;
1259         char m_subject[256];
1260         char from[256];
1261         char node[256];
1262         char rfca[256];
1263         char reply_to[512];
1264         char now[256];
1265         int format_type = 0;
1266         int nhdr = 0;
1267         int bq = 0;
1268         int i = 0;
1269 #ifdef HAVE_ICONV
1270         iconv_t ic = (iconv_t)(-1) ;
1271         char *ibuf;                /**< Buffer of characters to be converted */
1272         char *obuf;                /**< Buffer for converted characters      */
1273         size_t ibuflen;    /**< Length of input buffer         */
1274         size_t obuflen;    /**< Length of output buffer       */
1275         char *osav;                /**< Saved pointer to output buffer       */
1276 #endif
1277
1278         strcpy(from, "");
1279         strcpy(node, "");
1280         strcpy(rfca, "");
1281         strcpy(reply_to, "");
1282         strcpy(mime_content_type, "text/plain");
1283         strcpy(mime_charset, "us-ascii");
1284
1285         serv_printf("MSG4 %ld", msgnum);
1286         serv_getln(buf, sizeof buf);
1287         if (buf[0] != '1') {
1288                 wprintf(_("ERROR:"));
1289                 wprintf("%s<br />", &buf[4]);
1290                 return;
1291         }
1292
1293         strcpy(m_subject, "");
1294
1295         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
1296                 if (!strcmp(buf, "000")) {
1297                         wprintf(_("unexpected end of message"));
1298                         return;
1299                 }
1300                 if (include_headers) {
1301                         if (!strncasecmp(buf, "nhdr=yes", 8))
1302                                 nhdr = 1;
1303                         if (nhdr == 1)
1304                                 buf[0] = '_';
1305                         if (!strncasecmp(buf, "type=", 5))
1306                                 format_type = atoi(&buf[5]);
1307                         if (!strncasecmp(buf, "from=", 5)) {
1308                                 strcpy(from, &buf[5]);
1309                                 wprintf(_("from "));
1310 #ifdef HAVE_ICONV
1311                                 utf8ify_rfc822_string(from);
1312 #endif
1313                                 msgescputs(from);
1314                         }
1315                         if (!strncasecmp(buf, "subj=", 5)) {
1316                                 strcpy(m_subject, &buf[5]);
1317                         }
1318                         if ((!strncasecmp(buf, "hnod=", 5))
1319                             && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
1320                                 wprintf("(%s) ", &buf[5]);
1321                         }
1322                         if ((!strncasecmp(buf, "room=", 5))
1323                             && (strcasecmp(&buf[5], WC->wc_roomname))
1324                             && (strlen(&buf[5])>0) ) {
1325                                 wprintf(_("in "));
1326                                 wprintf("%s&gt; ", &buf[5]);
1327                         }
1328                         if (!strncasecmp(buf, "rfca=", 5)) {
1329                                 strcpy(rfca, &buf[5]);
1330                                 wprintf("&lt;");
1331                                 msgescputs(rfca);
1332                                 wprintf("&gt; ");
1333                         }
1334         
1335                         if (!strncasecmp(buf, "node=", 5)) {
1336                                 strcpy(node, &buf[5]);
1337                                 if ( ((WC->room_flags & QR_NETWORK)
1338                                 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
1339                                 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
1340                                 && (strlen(rfca)==0)
1341                                 ) {
1342                                         wprintf("@%s ", &buf[5]);
1343                                 }
1344                         }
1345                         if (!strncasecmp(buf, "rcpt=", 5)) {
1346                                 wprintf(_("to "));
1347                                 wprintf("%s ", &buf[5]);
1348                         }
1349                         if (!strncasecmp(buf, "time=", 5)) {
1350                                 fmt_date(now, atol(&buf[5]), 0);
1351                                 wprintf("%s ", now);
1352                         }
1353                 }
1354
1355                 /**
1356                  * Save attachment info for later.  We can't start downloading them
1357                  * yet because we're in the middle of a server transaction.
1358                  */
1359                 if (!strncasecmp(buf, "part=", 5)) {
1360                         ptr = malloc( (strlen(buf) + ((attachments != NULL) ? strlen(attachments) : 0)) ) ;
1361                         if (ptr != NULL) {
1362                                 ++num_attachments;
1363                                 sprintf(ptr, "%s%s\n",
1364                                         ((attachments != NULL) ? attachments : ""),
1365                                         &buf[5]
1366                                 );
1367                                 free(attachments);
1368                                 attachments = ptr;
1369                                 lprintf(9, "attachments=<%s>\n", attachments);
1370                         }
1371                 }
1372
1373         }
1374
1375         if (include_headers) {
1376                 wprintf("<br>");
1377
1378 #ifdef HAVE_ICONV
1379                 utf8ify_rfc822_string(m_subject);
1380 #endif
1381                 if (strlen(m_subject) > 0) {
1382                         wprintf(_("Subject:"));
1383                         wprintf(" ");
1384                         msgescputs(m_subject);
1385                         wprintf("<br />");
1386                 }
1387
1388                 /**
1389                  * Begin body
1390                  */
1391                 wprintf("<br />");
1392         }
1393
1394         /**
1395          * Learn the content type
1396          */
1397         strcpy(mime_content_type, "text/plain");
1398         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
1399                 if (!strcmp(buf, "000")) {
1400                         wprintf(_("unexpected end of message"));
1401                         goto ENDBODY;
1402                 }
1403                 if (!strncasecmp(buf, "Content-type: ", 14)) {
1404                         safestrncpy(mime_content_type, &buf[14],
1405                                 sizeof(mime_content_type));
1406                         for (i=0; i<strlen(mime_content_type); ++i) {
1407                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1408                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1409                                                 sizeof mime_charset);
1410                                 }
1411                         }
1412                         for (i=0; i<strlen(mime_content_type); ++i) {
1413                                 if (mime_content_type[i] == ';') {
1414                                         mime_content_type[i] = 0;
1415                                 }
1416                         }
1417                         for (i=0; i<strlen(mime_charset); ++i) {
1418                                 if (mime_charset[i] == ';') {
1419                                         mime_charset[i] = 0;
1420                                 }
1421                         }
1422                 }
1423         }
1424
1425         /** Set up a character set conversion if we need to (and if we can) */
1426 #ifdef HAVE_ICONV
1427         if ( (strcasecmp(mime_charset, "us-ascii"))
1428            && (strcasecmp(mime_charset, "UTF-8"))
1429            && (strcasecmp(mime_charset, ""))
1430         ) {
1431                 ic = ctdl_iconv_open("UTF-8", mime_charset);
1432                 if (ic == (iconv_t)(-1) ) {
1433                         lprintf(5, "%s:%d iconv_open(%s, %s) failed: %s\n",
1434                                 __FILE__, __LINE__, "UTF-8", mime_charset, strerror(errno));
1435                 }
1436         }
1437 #endif
1438
1439         /** Messages in legacy Citadel variformat get handled thusly... */
1440         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1441                 pullquote_fmout();
1442         }
1443
1444         /* Boring old 80-column fixed format text gets handled this way... */
1445         else if (!strcasecmp(mime_content_type, "text/plain")) {
1446                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1447                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
1448                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
1449
1450 #ifdef HAVE_ICONV
1451                         if (ic != (iconv_t)(-1) ) {
1452                                 ibuf = buf;
1453                                 ibuflen = strlen(ibuf);
1454                                 obuflen = SIZ;
1455                                 obuf = (char *) malloc(obuflen);
1456                                 osav = obuf;
1457                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1458                                 osav[SIZ-obuflen] = 0;
1459                                 safestrncpy(buf, osav, sizeof buf);
1460                                 free(osav);
1461                         }
1462 #endif
1463
1464                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
1465                                 buf[strlen(buf) - 1] = 0;
1466                         if ((bq == 0) &&
1467                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1468                                 wprintf("<blockquote>");
1469                                 bq = 1;
1470                         } else if ((bq == 1) &&
1471                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1472                                 wprintf("</blockquote>");
1473                                 bq = 0;
1474                         }
1475                         wprintf("<tt>");
1476                         url(buf);
1477                         msgescputs(buf);
1478                         wprintf("</tt><br />");
1479                 }
1480                 wprintf("</i><br />");
1481         }
1482
1483         /** HTML just gets escaped and stuffed back into the editor */
1484         else if (!strcasecmp(mime_content_type, "text/html")) {
1485                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1486                         strcat(buf, "\n");
1487                         msgescputs(buf);
1488                 }
1489         }
1490
1491         /** Unknown weirdness ... don't know how to handle this content type */
1492         else {
1493                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1494         }
1495
1496 ENDBODY:
1497         /** end of body handler */
1498
1499         /*
1500          * If there were attachments, we have to download them and insert them
1501          * into the attachment chain for the forwarded message we are composing.
1502          */
1503         if ( (forward_attachments) && (num_attachments) ) {
1504                 for (i=0; i<num_attachments; ++i) {
1505                         extract_token(buf, attachments, i, '\n', sizeof buf);
1506                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
1507                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
1508                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
1509                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
1510                         mime_length = extract_int(buf, 5);
1511
1512                         /*
1513                          * tracing  ... uncomment if necessary
1514                          *
1515                          */
1516                         lprintf(9, "fwd filename: %s\n", mime_filename);
1517                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
1518                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
1519                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
1520                         lprintf(9, "fwd length  : %d\n", mime_length);
1521
1522                         if ( (!strcasecmp(mime_disposition, "inline"))
1523                            || (!strcasecmp(mime_disposition, "attachment")) ) {
1524                 
1525                                 /* Create an attachment struct from this mime part... */
1526                                 att = malloc(sizeof(struct wc_attachment));
1527                                 memset(att, 0, sizeof(struct wc_attachment));
1528                                 att->length = mime_length;
1529                                 strcpy(att->content_type, mime_content_type);
1530                                 strcpy(att->filename, mime_filename);
1531                                 att->next = NULL;
1532                                 att->data = load_mimepart(msgnum, mime_partnum);
1533                 
1534                                 /* And add it to the list. */
1535                                 if (WC->first_attachment == NULL) {
1536                                         WC->first_attachment = att;
1537                                 }
1538                                 else {
1539                                         aptr = WC->first_attachment;
1540                                         while (aptr->next != NULL) aptr = aptr->next;
1541                                         aptr->next = att;
1542                                 }
1543                         }
1544
1545                 }
1546         }
1547
1548 #ifdef HAVE_ICONV
1549         if (ic != (iconv_t)(-1) ) {
1550                 iconv_close(ic);
1551         }
1552 #endif
1553
1554         if (attachments != NULL) {
1555                 free(attachments);
1556         }
1557 }
1558
1559 /**
1560  * \brief Display one row in the mailbox summary view
1561  *
1562  * \param num The row number to be displayed
1563  */
1564 void display_summarized(int num) {
1565         char datebuf[64];
1566
1567         wprintf("<tr id=\"m%ld\" style=\"width:100%%;font-weight:%s;background-color:#ffffff\" "
1568                 "onMouseDown=\"CtdlMoveMsgMouseDown(event,%ld)\">",
1569                 WC->summ[num].msgnum,
1570                 (WC->summ[num].is_new ? "bold" : "normal"),
1571                 WC->summ[num].msgnum
1572         );
1573
1574         wprintf("<td width=%d%%>", SUBJ_COL_WIDTH_PCT);
1575         escputs(WC->summ[num].subj);
1576         wprintf("</td>");
1577
1578         wprintf("<td width=%d%%>", SENDER_COL_WIDTH_PCT);
1579         escputs(WC->summ[num].from);
1580         wprintf("</td>");
1581
1582         wprintf("<td width=%d%%>", DATE_PLUS_BUTTONS_WIDTH_PCT);
1583         fmt_date(datebuf, WC->summ[num].date, 1);       /* brief */
1584         escputs(datebuf);
1585         wprintf("</td>");
1586
1587         wprintf("</tr>\n");
1588 }
1589
1590
1591
1592 /**
1593  * \brief display the adressbook overview
1594  * \param msgnum the citadel message number
1595  * \param alpha what????
1596  */
1597 void display_addressbook(long msgnum, char alpha) {
1598         char buf[SIZ];
1599         char mime_partnum[SIZ];
1600         char mime_filename[SIZ];
1601         char mime_content_type[SIZ];
1602         char mime_disposition[SIZ];
1603         int mime_length;
1604         char vcard_partnum[SIZ];
1605         char *vcard_source = NULL;
1606         struct message_summary summ;
1607
1608         memset(&summ, 0, sizeof(summ));
1609         safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
1610
1611         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1612         serv_puts(buf);
1613         serv_getln(buf, sizeof buf);
1614         if (buf[0] != '1') return;
1615
1616         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1617                 if (!strncasecmp(buf, "part=", 5)) {
1618                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1619                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1620                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1621                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1622                         mime_length = extract_int(&buf[5], 5);
1623
1624                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1625                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1626                                 strcpy(vcard_partnum, mime_partnum);
1627                         }
1628
1629                 }
1630         }
1631
1632         if (strlen(vcard_partnum) > 0) {
1633                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1634                 if (vcard_source != NULL) {
1635
1636                         /** Display the summary line */
1637                         display_vcard(vcard_source, alpha, 0, NULL);
1638
1639                         /** If it's my vCard I can edit it */
1640                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1641                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1642                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1643                         ) {
1644                                 wprintf("<a href=\"edit_vcard?"
1645                                         "msgnum=%ld?partnum=%s\">",
1646                                         msgnum, vcard_partnum);
1647                                 wprintf("[%s]</a>", _("edit"));
1648                         }
1649
1650                         free(vcard_source);
1651                 }
1652         }
1653
1654 }
1655
1656
1657
1658 /**
1659  * \brief  If it's an old "Firstname Lastname" style record, try to convert it.
1660  * \param namebuf name to analyze, reverse if nescessary
1661  */
1662 void lastfirst_firstlast(char *namebuf) {
1663         char firstname[SIZ];
1664         char lastname[SIZ];
1665         int i;
1666
1667         if (namebuf == NULL) return;
1668         if (strchr(namebuf, ';') != NULL) return;
1669
1670         i = num_tokens(namebuf, ' ');
1671         if (i < 2) return;
1672
1673         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1674         remove_token(namebuf, i-1, ' ');
1675         strcpy(firstname, namebuf);
1676         sprintf(namebuf, "%s; %s", lastname, firstname);
1677 }
1678
1679 /**
1680  * \brief fetch what??? name
1681  * \param msgnum the citadel message number
1682  * \param namebuf where to put the name in???
1683  */
1684 void fetch_ab_name(long msgnum, char *namebuf) {
1685         char buf[SIZ];
1686         char mime_partnum[SIZ];
1687         char mime_filename[SIZ];
1688         char mime_content_type[SIZ];
1689         char mime_disposition[SIZ];
1690         int mime_length;
1691         char vcard_partnum[SIZ];
1692         char *vcard_source = NULL;
1693         int i;
1694         struct message_summary summ;
1695
1696         if (namebuf == NULL) return;
1697         strcpy(namebuf, "");
1698
1699         memset(&summ, 0, sizeof(summ));
1700         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1701
1702         sprintf(buf, "MSG0 %ld|0", msgnum);     /** unfortunately we need the mime info now */
1703         serv_puts(buf);
1704         serv_getln(buf, sizeof buf);
1705         if (buf[0] != '1') return;
1706
1707         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1708                 if (!strncasecmp(buf, "part=", 5)) {
1709                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1710                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1711                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1712                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1713                         mime_length = extract_int(&buf[5], 5);
1714
1715                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1716                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1717                                 strcpy(vcard_partnum, mime_partnum);
1718                         }
1719
1720                 }
1721         }
1722
1723         if (strlen(vcard_partnum) > 0) {
1724                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1725                 if (vcard_source != NULL) {
1726
1727                         /* Grab the name off the card */
1728                         display_vcard(vcard_source, 0, 0, namebuf);
1729
1730                         free(vcard_source);
1731                 }
1732         }
1733
1734         lastfirst_firstlast(namebuf);
1735         striplt(namebuf);
1736         for (i=0; i<strlen(namebuf); ++i) {
1737                 if (namebuf[i] != ';') return;
1738         }
1739         strcpy(namebuf, _("(no name)"));
1740 }
1741
1742
1743
1744 /**
1745  * \brief Record compare function for sorting address book indices
1746  * \param ab1 adressbook one
1747  * \param ab2 adressbook two
1748  */
1749 int abcmp(const void *ab1, const void *ab2) {
1750         return(strcasecmp(
1751                 (((const struct addrbookent *)ab1)->ab_name),
1752                 (((const struct addrbookent *)ab2)->ab_name)
1753         ));
1754 }
1755
1756
1757 /**
1758  * \brief Helper function for do_addrbook_view()
1759  * Converts a name into a three-letter tab label
1760  * \param tabbuf the tabbuffer to add name to
1761  * \param name the name to add to the tabbuffer
1762  */
1763 void nametab(char *tabbuf, char *name) {
1764         stresc(tabbuf, name, 0, 0);
1765         tabbuf[0] = toupper(tabbuf[0]);
1766         tabbuf[1] = tolower(tabbuf[1]);
1767         tabbuf[2] = tolower(tabbuf[2]);
1768         tabbuf[3] = 0;
1769 }
1770
1771
1772 /**
1773  * \brief Render the address book using info we gathered during the scan
1774  * \param addrbook the addressbook to render
1775  * \param num_ab the number of the addressbook
1776  */
1777 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
1778         int i = 0;
1779         int displayed = 0;
1780         int bg = 0;
1781         static int NAMESPERPAGE = 60;
1782         int num_pages = 0;
1783         int page = 0;
1784         int tabfirst = 0;
1785         char tabfirst_label[SIZ];
1786         int tablast = 0;
1787         char tablast_label[SIZ];
1788
1789         if (num_ab == 0) {
1790                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
1791                 wprintf(_("This address book is empty."));
1792                 wprintf("</i></div>\n");
1793                 return;
1794         }
1795
1796         if (num_ab > 1) {
1797                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
1798         }
1799
1800         num_pages = num_ab / NAMESPERPAGE;
1801
1802         page = atoi(bstr("page"));
1803
1804         wprintf("Page: ");
1805         for (i=0; i<=num_pages; ++i) {
1806                 if (i != page) {
1807                         wprintf("<a href=\"readfwd?page=%d\">", i);
1808                 }
1809                 else {
1810                         wprintf("<B>");
1811                 }
1812                 tabfirst = i * NAMESPERPAGE;
1813                 tablast = tabfirst + NAMESPERPAGE - 1;
1814                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1815                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1816                 nametab(tablast_label, addrbook[tablast].ab_name);
1817                 wprintf("[%s&nbsp;-&nbsp;%s]",
1818                         tabfirst_label, tablast_label
1819                 );
1820                 if (i != page) {
1821                         wprintf("</A>\n");
1822                 }
1823                 else {
1824                         wprintf("</B>\n");
1825                 }
1826         }
1827         wprintf("<br />\n");
1828
1829         wprintf("<table border=0 cellspacing=0 "
1830                 "cellpadding=3 width=100%%>\n"
1831         );
1832
1833         for (i=0; i<num_ab; ++i) {
1834
1835                 if ((i / NAMESPERPAGE) == page) {
1836
1837                         if ((displayed % 4) == 0) {
1838                                 if (displayed > 0) {
1839                                         wprintf("</tr>\n");
1840                                 }
1841                                 bg = 1 - bg;
1842                                 wprintf("<tr bgcolor=\"#%s\">",
1843                                         (bg ? "DDDDDD" : "FFFFFF")
1844                                 );
1845                         }
1846         
1847                         wprintf("<td>");
1848         
1849                         wprintf("<a href=\"readfwd?startmsg=%ld&is_singlecard=1",
1850                                 addrbook[i].ab_msgnum);
1851                         wprintf("?maxmsgs=1?summary=0?alpha=%s\">", bstr("alpha"));
1852                         vcard_n_prettyize(addrbook[i].ab_name);
1853                         escputs(addrbook[i].ab_name);
1854                         wprintf("</a></td>\n");
1855                         ++displayed;
1856                 }
1857         }
1858
1859         wprintf("</tr></table>\n");
1860 }
1861
1862
1863
1864 /**
1865  * \brief load message pointers from the server
1866  * \param servcmd the citadel command to send to the citserver
1867  * \param with_headers what headers???
1868  */
1869 int load_msg_ptrs(char *servcmd, int with_headers)
1870 {
1871         char buf[1024];
1872         time_t datestamp;
1873         char fullname[128];
1874         char nodename[128];
1875         char inetaddr[128];
1876         char subject[256];
1877         int nummsgs;
1878         int maxload = 0;
1879
1880         int num_summ_alloc = 0;
1881
1882         if (WC->summ != NULL) {
1883                 free(WC->summ);
1884                 WC->num_summ = 0;
1885                 WC->summ = NULL;
1886         }
1887         num_summ_alloc = 100;
1888         WC->num_summ = 0;
1889         WC->summ = malloc(num_summ_alloc * sizeof(struct message_summary));
1890
1891         nummsgs = 0;
1892         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1893         serv_puts(servcmd);
1894         serv_getln(buf, sizeof buf);
1895         if (buf[0] != '1') {
1896                 return (nummsgs);
1897         }
1898         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1899                 if (nummsgs < maxload) {
1900                         WC->msgarr[nummsgs] = extract_long(buf, 0);
1901                         datestamp = extract_long(buf, 1);
1902                         extract_token(fullname, buf, 2, '|', sizeof fullname);
1903                         extract_token(nodename, buf, 3, '|', sizeof nodename);
1904                         extract_token(inetaddr, buf, 4, '|', sizeof inetaddr);
1905                         extract_token(subject, buf, 5, '|', sizeof subject);
1906                         ++nummsgs;
1907
1908                         if (with_headers) {
1909                                 if (nummsgs > num_summ_alloc) {
1910                                         num_summ_alloc *= 2;
1911                                         WC->summ = realloc(WC->summ,
1912                                                 num_summ_alloc * sizeof(struct message_summary));
1913                                 }
1914                                 ++WC->num_summ;
1915
1916                                 memset(&WC->summ[nummsgs-1], 0, sizeof(struct message_summary));
1917                                 WC->summ[nummsgs-1].msgnum = WC->msgarr[nummsgs-1];
1918                                 safestrncpy(WC->summ[nummsgs-1].subj,
1919                                         _("(no subject)"), sizeof WC->summ[nummsgs-1].subj);
1920                                 if (strlen(fullname) > 0) {
1921                                         safestrncpy(WC->summ[nummsgs-1].from,
1922                                                 fullname, sizeof WC->summ[nummsgs-1].from);
1923                                 }
1924                                 if (strlen(subject) > 0) {
1925                                 safestrncpy(WC->summ[nummsgs-1].subj, subject,
1926                                         sizeof WC->summ[nummsgs-1].subj);
1927                                 }
1928 #ifdef HAVE_ICONV
1929                                 /** Handle subjects with RFC2047 encoding */
1930                                 utf8ify_rfc822_string(WC->summ[nummsgs-1].subj);
1931 #endif
1932                                 if (strlen(WC->summ[nummsgs-1].subj) > 75) {
1933                                         strcpy(&WC->summ[nummsgs-1].subj[72], "...");
1934                                 }
1935
1936                                 if (strlen(nodename) > 0) {
1937                                         if ( ((WC->room_flags & QR_NETWORK)
1938                                            || ((strcasecmp(nodename, serv_info.serv_nodename)
1939                                            && (strcasecmp(nodename, serv_info.serv_fqdn)))))
1940                                         ) {
1941                                                 strcat(WC->summ[nummsgs-1].from, " @ ");
1942                                                 strcat(WC->summ[nummsgs-1].from, nodename);
1943                                         }
1944                                 }
1945
1946                                 WC->summ[nummsgs-1].date = datestamp;
1947         
1948 #ifdef HAVE_ICONV
1949                                 /** Handle senders with RFC2047 encoding */
1950                                 utf8ify_rfc822_string(WC->summ[nummsgs-1].from);
1951 #endif
1952                                 if (strlen(WC->summ[nummsgs-1].from) > 25) {
1953                                         strcpy(&WC->summ[nummsgs-1].from[22], "...");
1954                                 }
1955                         }
1956                 }
1957         }
1958         return (nummsgs);
1959 }
1960
1961 /**
1962  * \brief qsort() compatible function to compare two longs in descending order.
1963  *
1964  * \param s1 first number to compare 
1965  * \param s2 second number to compare
1966  */
1967 int longcmp_r(const void *s1, const void *s2) {
1968         long l1;
1969         long l2;
1970
1971         l1 = *(long *)s1;
1972         l2 = *(long *)s2;
1973
1974         if (l1 > l2) return(-1);
1975         if (l1 < l2) return(+1);
1976         return(0);
1977 }
1978
1979  
1980 /**
1981  * \brief qsort() compatible function to compare two message summary structs by ascending subject.
1982  *
1983  * \param s1 first item to compare 
1984  * \param s2 second item to compare
1985  */
1986 int summcmp_subj(const void *s1, const void *s2) {
1987         struct message_summary *summ1;
1988         struct message_summary *summ2;
1989         
1990         summ1 = (struct message_summary *)s1;
1991         summ2 = (struct message_summary *)s2;
1992         return strcasecmp(summ1->subj, summ2->subj);
1993 }
1994
1995 /**
1996  * \brief qsort() compatible function to compare two message summary structs by descending subject.
1997  *
1998  * \param s1 first item to compare 
1999  * \param s2 second item to compare
2000  */
2001 int summcmp_rsubj(const void *s1, const void *s2) {
2002         struct message_summary *summ1;
2003         struct message_summary *summ2;
2004         
2005         summ1 = (struct message_summary *)s1;
2006         summ2 = (struct message_summary *)s2;
2007         return strcasecmp(summ2->subj, summ1->subj);
2008 }
2009
2010 /**
2011  * \brief qsort() compatible function to compare two message summary structs by ascending sender.
2012  *
2013  * \param s1 first item to compare 
2014  * \param s2 second item to compare
2015  */
2016 int summcmp_sender(const void *s1, const void *s2) {
2017         struct message_summary *summ1;
2018         struct message_summary *summ2;
2019         
2020         summ1 = (struct message_summary *)s1;
2021         summ2 = (struct message_summary *)s2;
2022         return strcasecmp(summ1->from, summ2->from);
2023 }
2024
2025 /**
2026  * \brief qsort() compatible function to compare two message summary structs by descending sender.
2027  *
2028  * \param s1 first item to compare 
2029  * \param s2 second item to compare
2030  */
2031 int summcmp_rsender(const void *s1, const void *s2) {
2032         struct message_summary *summ1;
2033         struct message_summary *summ2;
2034         
2035         summ1 = (struct message_summary *)s1;
2036         summ2 = (struct message_summary *)s2;
2037         return strcasecmp(summ2->from, summ1->from);
2038 }
2039
2040 /**
2041  * \brief qsort() compatible function to compare two message summary structs by ascending date.
2042  *
2043  * \param s1 first item to compare 
2044  * \param s2 second item to compare
2045  */
2046 int summcmp_date(const void *s1, const void *s2) {
2047         struct message_summary *summ1;
2048         struct message_summary *summ2;
2049         
2050         summ1 = (struct message_summary *)s1;
2051         summ2 = (struct message_summary *)s2;
2052
2053         if (summ1->date < summ2->date) return -1;
2054         else if (summ1->date > summ2->date) return +1;
2055         else return 0;
2056 }
2057
2058 /**
2059  * \brief qsort() compatible function to compare two message summary structs by descending date.
2060  *
2061  * \param s1 first item to compare 
2062  * \param s2 second item to compare
2063  */
2064 int summcmp_rdate(const void *s1, const void *s2) {
2065         struct message_summary *summ1;
2066         struct message_summary *summ2;
2067         
2068         summ1 = (struct message_summary *)s1;
2069         summ2 = (struct message_summary *)s2;
2070
2071         if (summ1->date < summ2->date) return +1;
2072         else if (summ1->date > summ2->date) return -1;
2073         else return 0;
2074 }
2075
2076
2077
2078 /**
2079  * \brief command loop for reading messages
2080  *
2081  * \param oper Set to "readnew" or "readold" or "readfwd" or "headers"
2082  */
2083 void readloop(char *oper)
2084 {
2085         char cmd[256];
2086         char buf[SIZ];
2087         char old_msgs[SIZ];
2088         int a, b;
2089         int nummsgs;
2090         long startmsg;
2091         int maxmsgs;
2092         long *displayed_msgs = NULL;
2093         int num_displayed = 0;
2094         int is_summary = 0;
2095         int is_addressbook = 0;
2096         int is_singlecard = 0;
2097         int is_calendar = 0;
2098         int is_tasks = 0;
2099         int is_notes = 0;
2100         int is_bbview = 0;
2101         int lo, hi;
2102         int lowest_displayed = (-1);
2103         int highest_displayed = 0;
2104         struct addrbookent *addrbook = NULL;
2105         int num_ab = 0;
2106         char *sortby = NULL;
2107         char sortpref_name[128];
2108         char sortpref_value[128];
2109         char *subjsort_button;
2110         char *sendsort_button;
2111         char *datesort_button;
2112         int bbs_reverse = 0;
2113
2114         if (WC->wc_view == VIEW_WIKI) {
2115                 sprintf(buf, "wiki?room=%s?page=home", WC->wc_roomname);
2116                 http_redirect(buf);
2117                 return;
2118         }
2119
2120         startmsg = atol(bstr("startmsg"));
2121         maxmsgs = atoi(bstr("maxmsgs"));
2122         is_summary = atoi(bstr("summary"));
2123         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
2124
2125         snprintf(sortpref_name, sizeof sortpref_name, "sort %s", WC->wc_roomname);
2126         get_preference(sortpref_name, sortpref_value, sizeof sortpref_value);
2127
2128         sortby = bstr("sortby");
2129         if ( (strlen(sortby) > 0) && (strcasecmp(sortby, sortpref_value)) ) {
2130                 set_preference(sortpref_name, sortby, 1);
2131         }
2132         if (strlen(sortby) == 0) sortby = sortpref_value;
2133
2134         /** mailbox sort */
2135         if (strlen(sortby) == 0) sortby = "rdate";
2136
2137         /** message board sort */
2138         if (!strcasecmp(sortby, "reverse")) {
2139                 bbs_reverse = 1;
2140         }
2141         else {
2142                 bbs_reverse = 0;
2143         }
2144
2145         output_headers(1, 1, 1, 0, 0, 0);
2146
2147         /**
2148          * When in summary mode, always show ALL messages instead of just
2149          * new or old.  Otherwise, show what the user asked for.
2150          */
2151         if (!strcmp(oper, "readnew")) {
2152                 strcpy(cmd, "MSGS NEW");
2153         }
2154         else if (!strcmp(oper, "readold")) {
2155                 strcpy(cmd, "MSGS OLD");
2156         }
2157         else if (!strcmp(oper, "do_search")) {
2158                 sprintf(cmd, "MSGS SEARCH|%s", bstr("query"));
2159         }
2160         else {
2161                 strcpy(cmd, "MSGS ALL");
2162         }
2163
2164         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
2165                 is_summary = 1;
2166                 if (!strcmp(oper, "do_search")) {
2167                         sprintf(cmd, "MSGS SEARCH|%s", bstr("query"));
2168                 }
2169                 else {
2170                         strcpy(cmd, "MSGS ALL");
2171                 }
2172         }
2173
2174         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
2175                 is_addressbook = 1;
2176                 if (!strcmp(oper, "do_search")) {
2177                         sprintf(cmd, "MSGS SEARCH|%s", bstr("query"));
2178                 }
2179                 else {
2180                         strcpy(cmd, "MSGS ALL");
2181                 }
2182                 maxmsgs = 9999999;
2183         }
2184
2185         if (is_summary) {                       /**< fetch header summary */
2186                 snprintf(cmd, sizeof cmd, "MSGS %s|%s||1",
2187                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
2188                         (!strcmp(oper, "do_search") ? bstr("query") : "")
2189                 );
2190                 startmsg = 1;
2191                 maxmsgs = 9999999;
2192         }
2193
2194         /**
2195          * Are we doing a summary view?  If so, we need to know old messages
2196          * and new messages, so we can do that pretty boldface thing for the
2197          * new messages.
2198          */
2199         strcpy(old_msgs, "");
2200         if (is_summary) {
2201                 serv_puts("GTSN");
2202                 serv_getln(buf, sizeof buf);
2203                 if (buf[0] == '2') {
2204                         strcpy(old_msgs, &buf[4]);
2205                 }
2206         }
2207
2208         is_singlecard = atoi(bstr("is_singlecard"));
2209
2210         if (WC->wc_default_view == VIEW_CALENDAR) {             /**< calendar */
2211                 is_calendar = 1;
2212                 strcpy(cmd, "MSGS ALL");
2213                 maxmsgs = 32767;
2214         }
2215         if (WC->wc_default_view == VIEW_TASKS) {                /**< tasks */
2216                 is_tasks = 1;
2217                 strcpy(cmd, "MSGS ALL");
2218                 maxmsgs = 32767;
2219         }
2220         if (WC->wc_default_view == VIEW_NOTES) {                /**< notes */
2221                 is_notes = 1;
2222                 strcpy(cmd, "MSGS ALL");
2223                 maxmsgs = 32767;
2224         }
2225
2226         if (is_notes) {
2227                 wprintf("<div align=center>%s</div>\n", _("Click on any note to edit it."));
2228                 wprintf("<div id=\"new_notes_here\"></div>\n");
2229         }
2230
2231         nummsgs = load_msg_ptrs(cmd, is_summary);
2232         if (nummsgs == 0) {
2233
2234                 if ((!is_tasks) && (!is_calendar) && (!is_notes) && (!is_addressbook)) {
2235                         wprintf("<div align=\"center\"><br /><em>");
2236                         if (!strcmp(oper, "readnew")) {
2237                                 wprintf(_("No new messages."));
2238                         } else if (!strcmp(oper, "readold")) {
2239                                 wprintf(_("No old messages."));
2240                         } else {
2241                                 wprintf(_("No messages here."));
2242                         }
2243                         wprintf("</em><br /></div>\n");
2244                 }
2245
2246                 goto DONE;
2247         }
2248
2249         if (is_summary) {
2250                 for (a = 0; a < nummsgs; ++a) {
2251                         /** Are you a new message, or an old message? */
2252                         if (is_summary) {
2253                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
2254                                         WC->summ[a].is_new = 0;
2255                                 }
2256                                 else {
2257                                         WC->summ[a].is_new = 1;
2258                                 }
2259                         }
2260                 }
2261         }
2262
2263         if (startmsg == 0L) {
2264                 if (bbs_reverse) {
2265                         startmsg = WC->msgarr[(nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0];
2266                 }
2267                 else {
2268                         startmsg = WC->msgarr[0];
2269                 }
2270         }
2271
2272         if (is_summary) {
2273                 if (!strcasecmp(sortby, "subject")) {
2274                         qsort(WC->summ, WC->num_summ,
2275                                 sizeof(struct message_summary), summcmp_subj);
2276                 }
2277                 else if (!strcasecmp(sortby, "rsubject")) {
2278                         qsort(WC->summ, WC->num_summ,
2279                                 sizeof(struct message_summary), summcmp_rsubj);
2280                 }
2281                 else if (!strcasecmp(sortby, "sender")) {
2282                         qsort(WC->summ, WC->num_summ,
2283                                 sizeof(struct message_summary), summcmp_sender);
2284                 }
2285                 else if (!strcasecmp(sortby, "rsender")) {
2286                         qsort(WC->summ, WC->num_summ,
2287                                 sizeof(struct message_summary), summcmp_rsender);
2288                 }
2289                 else if (!strcasecmp(sortby, "date")) {
2290                         qsort(WC->summ, WC->num_summ,
2291                                 sizeof(struct message_summary), summcmp_date);
2292                 }
2293                 else if (!strcasecmp(sortby, "rdate")) {
2294                         qsort(WC->summ, WC->num_summ,
2295                                 sizeof(struct message_summary), summcmp_rdate);
2296                 }
2297         }
2298
2299         if (!strcasecmp(sortby, "subject")) {
2300                 subjsort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsubject\"><img border=\"0\" src=\"static/down_pointer.gif\" /></a>" ;
2301         }
2302         else if (!strcasecmp(sortby, "rsubject")) {
2303                 subjsort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"static/up_pointer.gif\" /></a>" ;
2304         }
2305         else {
2306                 subjsort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"static/sort_none.gif\" /></a>" ;
2307         }
2308
2309         if (!strcasecmp(sortby, "sender")) {
2310                 sendsort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsender\"><img border=\"0\" src=\"static/down_pointer.gif\" /></a>" ;
2311         }
2312         else if (!strcasecmp(sortby, "rsender")) {
2313                 sendsort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"static/up_pointer.gif\" /></a>" ;
2314         }
2315         else {
2316                 sendsort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"static/sort_none.gif\" /></a>" ;
2317         }
2318
2319         if (!strcasecmp(sortby, "date")) {
2320                 datesort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"static/down_pointer.gif\" /></a>" ;
2321         }
2322         else if (!strcasecmp(sortby, "rdate")) {
2323                 datesort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"static/up_pointer.gif\" /></a>" ;
2324         }
2325         else {
2326                 datesort_button = "<a href=\"readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"static/sort_none.gif\" /></a>" ;
2327         }
2328
2329         if (is_summary) {
2330                 wprintf("</div>\n");            /** end of 'content' div */
2331
2332                 wprintf("<script language=\"javascript\" type=\"text/javascript\">"
2333                         " document.onkeydown = CtdlMsgListKeyPress;     "
2334                         " if (document.layers) {                        "
2335                         "       document.captureEvents(Event.KEYPRESS); "
2336                         " }                                             "
2337                         "</script>\n"
2338                 );
2339
2340                 /** note that Date and Delete are now in the same column */
2341                 wprintf("<div id=\"message_list_hdr\">"
2342                         "<div class=\"fix_scrollbar_bug\">"
2343                         "<table cellspacing=0 style=\"width:100%%\">"
2344                         "<tr>"
2345                 );
2346                 wprintf("<td width=%d%%><b><i>%s</i></b> %s</td>"
2347                         "<td width=%d%%><b><i>%s</i></b> %s</td>"
2348                         "<td width=%d%%><b><i>%s</i></b> %s"
2349                         "&nbsp;"
2350                         "<input type=\"submit\" name=\"delete_button\" style=\"font-size:6pt\" "
2351                         " onClick=\"CtdlDeleteSelectedMessages(event)\" "
2352                         " value=\"%s\">"
2353                         "</td>"
2354                         "</tr>\n"
2355                         ,
2356                         SUBJ_COL_WIDTH_PCT,
2357                         _("Subject"),   subjsort_button,
2358                         SENDER_COL_WIDTH_PCT,
2359                         _("Sender"),    sendsort_button,
2360                         DATE_PLUS_BUTTONS_WIDTH_PCT,
2361                         _("Date"),      datesort_button,
2362                         _("Delete")
2363                 );
2364                 wprintf("</table></div></div>\n");
2365
2366                 wprintf("<div id=\"message_list\">"
2367
2368                         "<div class=\"fix_scrollbar_bug\">\n"
2369
2370                         "<table class=\"mailbox_summary\" id=\"summary_headers\" rules=rows "
2371                         "cellspacing=0 style=\"width:100%%;-moz-user-select:none;\">"
2372                 );
2373         }
2374
2375         for (a = 0; a < nummsgs; ++a) {
2376                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
2377
2378                         /** Display the message */
2379                         if (is_summary) {
2380                                 display_summarized(a);
2381                         }
2382                         else if (is_addressbook) {
2383                                 fetch_ab_name(WC->msgarr[a], buf);
2384                                 ++num_ab;
2385                                 addrbook = realloc(addrbook,
2386                                         (sizeof(struct addrbookent) * num_ab) );
2387                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
2388                                         sizeof(addrbook[num_ab-1].ab_name));
2389                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
2390                         }
2391                         else if (is_calendar) {
2392                                 display_calendar(WC->msgarr[a]);
2393                         }
2394                         else if (is_tasks) {
2395                                 display_task(WC->msgarr[a]);
2396                         }
2397                         else if (is_notes) {
2398                                 display_note(WC->msgarr[a]);
2399                         }
2400                         else {
2401                                 if (displayed_msgs == NULL) {
2402                                         displayed_msgs = malloc(sizeof(long) *
2403                                                                 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
2404                                 }
2405                                 displayed_msgs[num_displayed] = WC->msgarr[a];
2406                         }
2407
2408                         if (lowest_displayed < 0) lowest_displayed = a;
2409                         highest_displayed = a;
2410
2411                         ++num_displayed;
2412                 }
2413         }
2414
2415         /**
2416          * Set the "is_bbview" variable if it appears that we are looking at
2417          * a classic bulletin board view.
2418          */
2419         if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
2420               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
2421                 is_bbview = 1;
2422         }
2423
2424         /** Output loop */
2425         if (displayed_msgs != NULL) {
2426                 if (bbs_reverse) {
2427                         qsort(displayed_msgs, num_displayed, sizeof(long), longcmp_r);
2428                 }
2429
2430                 /** if we do a split bbview in the future, begin messages div here */
2431
2432                 for (a=0; a<num_displayed; ++a) {
2433                         read_message(displayed_msgs[a], 0, "");
2434                 }
2435
2436                 /** if we do a split bbview in the future, end messages div here */
2437
2438                 free(displayed_msgs);
2439                 displayed_msgs = NULL;
2440         }
2441
2442         if (is_summary) {
2443                 wprintf("</table>"
2444                         "</div>\n");                    /**< end of 'fix_scrollbar_bug' div */
2445                 wprintf("</div>");                      /**< end of 'message_list' div */
2446
2447                 /** Here's the grab-it-to-resize-the-message-list widget */
2448                 wprintf("<div id=\"resize_msglist\" "
2449                         "onMouseDown=\"CtdlResizeMsgListMouseDown(event)\">"
2450                         "<div class=\"fix_scrollbar_bug\">"
2451                         "<table width=100%% border=3 cellspacing=0 "
2452                         "bgcolor=\"#cccccc\" "
2453                         "cellpadding=0><TR><TD> </td></tr></table>"
2454                         "</div></div>\n"
2455                 );
2456
2457                 wprintf("<div id=\"preview_pane\">");   /**< The preview pane will initially be empty */
2458         }
2459
2460         /**
2461          * Bump these because although we're thinking in zero base, the user
2462          * is a drooling idiot and is thinking in one base.
2463          */
2464         ++lowest_displayed;
2465         ++highest_displayed;
2466
2467         /**
2468          * If we're not currently looking at ALL requested
2469          * messages, then display the selector bar
2470          */
2471         if (is_bbview) {
2472                 /** begin bbview scroller */
2473                 wprintf("<form name=\"msgomatic\">");
2474                 wprintf(_("Reading #"), lowest_displayed, highest_displayed);
2475
2476                 wprintf("<select name=\"whichones\" size=\"1\" "
2477                         "OnChange=\"location.href=msgomatic.whichones.options"
2478                         "[selectedIndex].value\">\n");
2479
2480                 if (bbs_reverse) {
2481                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2482                                 hi = b + 1;
2483                                 lo = b - maxmsgs + 2;
2484                                 if (lo < 1) lo = 1;
2485                                 wprintf("<option %s value="
2486                                         "\"%s"
2487                                         "?startmsg=%ld"
2488                                         "?maxmsgs=%d"
2489                                         "?summary=%d\">"
2490                                         "%d-%d</option> \n",
2491                                         ((WC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2492                                         oper,
2493                                         WC->msgarr[lo-1],
2494                                         maxmsgs,
2495                                         is_summary,
2496                                         hi, lo);
2497                         }
2498                 }
2499                 else {
2500                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2501                                 lo = b + 1;
2502                                 hi = b + maxmsgs + 1;
2503                                 if (hi > nummsgs) hi = nummsgs;
2504                                 wprintf("<option %s value="
2505                                         "\"%s"
2506                                         "?startmsg=%ld"
2507                                         "?maxmsgs=%d"
2508                                         "?summary=%d\">"
2509                                         "%d-%d</option> \n",
2510                                         ((WC->msgarr[b] == startmsg) ? "selected" : ""),
2511                                         oper,
2512                                         WC->msgarr[lo-1],
2513                                         maxmsgs,
2514                                         is_summary,
2515                                         lo, hi);
2516                         }
2517                 }
2518
2519                 wprintf("<option value=\"%s?startmsg=%ld"
2520                         "?maxmsgs=9999999?summary=%d\">"
2521                         "ALL"
2522                         "</option> ",
2523                         oper,
2524                         WC->msgarr[0], is_summary);
2525
2526                 wprintf("</select> ");
2527                 wprintf(_("of %d messages."), nummsgs);
2528
2529                 /** forward/reverse */
2530                 wprintf("&nbsp;<select name=\"direction\" size=\"1\" "
2531                         "OnChange=\"location.href=msgomatic.direction.options"
2532                         "[selectedIndex].value\">\n"
2533                 );
2534
2535                 wprintf("<option %s value=\"%s?sortby=forward\">oldest to newest</option>\n",
2536                         (bbs_reverse ? "" : "selected"),
2537                         oper
2538                 );
2539         
2540                 wprintf("<option %s value=\"%s?sortby=reverse\">newest to oldest</option>\n",
2541                         (bbs_reverse ? "selected" : ""),
2542                         oper
2543                 );
2544         
2545                 wprintf("</select></form>\n");
2546                 /** end bbview scroller */
2547         }
2548
2549 DONE:
2550         if (is_tasks) {
2551                 do_tasks_view();        /** Render the task list */
2552         }
2553
2554         if (is_calendar) {
2555                 do_calendar_view();     /** Render the calendar */
2556         }
2557
2558         if (is_addressbook) {
2559                 do_addrbook_view(addrbook, num_ab);     /** Render the address book */
2560         }
2561
2562         /** Note: wDumpContent() will output one additional </div> tag. */
2563         wDumpContent(1);
2564         if (addrbook != NULL) free(addrbook);
2565
2566         /** free the summary */
2567         if (WC->summ != NULL) {
2568                 free(WC->summ);
2569                 WC->num_summ = 0;
2570                 WC->summ = NULL;
2571         }
2572 }
2573
2574
2575 /**
2576  * \brief Back end for post_message()
2577  * ... this is where the actual message gets transmitted to the server.
2578  */
2579 void post_mime_to_server(void) {
2580         char boundary[SIZ];
2581         int is_multipart = 0;
2582         static int seq = 0;
2583         struct wc_attachment *att;
2584         char *encoded;
2585         size_t encoded_length;
2586
2587         /** RFC2045 requires this, and some clients look for it... */
2588         serv_puts("MIME-Version: 1.0");
2589         serv_puts("X-Mailer: " SERVER);
2590
2591         /** If there are attachments, we have to do multipart/mixed */
2592         if (WC->first_attachment != NULL) {
2593                 is_multipart = 1;
2594         }
2595
2596         if (is_multipart) {
2597                 sprintf(boundary, "Citadel--Multipart--%s--%04x--%04x",
2598                         serv_info.serv_fqdn,
2599                         getpid(),
2600                         ++seq
2601                 );
2602
2603                 /** Remember, serv_printf() appends an extra newline */
2604                 serv_printf("Content-type: multipart/mixed; "
2605                         "boundary=\"%s\"\n", boundary);
2606                 serv_printf("This is a multipart message in MIME format.\n");
2607                 serv_printf("--%s", boundary);
2608         }
2609
2610         serv_puts("Content-type: text/html; charset=utf-8");
2611         serv_puts("Content-Transfer-Encoding: quoted-printable");
2612         serv_puts("");
2613         serv_puts("<html><body>\r\n");
2614         text_to_server_qp(bstr("msgtext"));     /** Transmit message in quoted-printable encoding */
2615         serv_puts("</body></html>\r\n");
2616         
2617         if (is_multipart) {
2618
2619                 /** Add in the attachments */
2620                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
2621
2622                         encoded_length = ((att->length * 150) / 100);
2623                         encoded = malloc(encoded_length);
2624                         if (encoded == NULL) break;
2625                         CtdlEncodeBase64(encoded, att->data, att->length, 1);
2626
2627                         serv_printf("--%s", boundary);
2628                         serv_printf("Content-type: %s", att->content_type);
2629                         serv_printf("Content-disposition: attachment; "
2630                                 "filename=\"%s\"", att->filename);
2631                         serv_puts("Content-transfer-encoding: base64");
2632                         serv_puts("");
2633                         serv_write(encoded, strlen(encoded));
2634                         serv_puts("");
2635                         serv_puts("");
2636                         free(encoded);
2637                 }
2638                 serv_printf("--%s--", boundary);
2639         }
2640
2641         serv_puts("000");
2642 }
2643
2644
2645 /**
2646  * \brief Post message (or don't post message)
2647  *
2648  * Note regarding the "dont_post" variable:
2649  * A random value (actually, it's just a timestamp) is inserted as a hidden
2650  * field called "postseq" when the display_enter page is generated.  This
2651  * value is checked when posting, using the static variable dont_post.  If a
2652  * user attempts to post twice using the same dont_post value, the message is
2653  * discarded.  This prevents the accidental double-saving of the same message
2654  * if the user happens to click the browser "back" button.
2655  */
2656 void post_message(void)
2657 {
2658         char buf[1024];
2659         char encoded_subject[1024];
2660         static long dont_post = (-1L);
2661         struct wc_attachment *att, *aptr;
2662         int is_anonymous = 0;
2663         char *display_name;
2664
2665         display_name = bstr("display_name");
2666         if (!strcmp(display_name, "__ANONYMOUS__")) {
2667                 display_name = "";
2668                 is_anonymous = 1;
2669         }
2670
2671         if (WC->upload_length > 0) {
2672
2673                 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WC->upload_length);
2674                 /** There's an attachment.  Save it to this struct... */
2675                 att = malloc(sizeof(struct wc_attachment));
2676                 memset(att, 0, sizeof(struct wc_attachment));
2677                 att->length = WC->upload_length;
2678                 strcpy(att->content_type, WC->upload_content_type);
2679                 strcpy(att->filename, WC->upload_filename);
2680                 att->next = NULL;
2681
2682                 /** And add it to the list. */
2683                 if (WC->first_attachment == NULL) {
2684                         WC->first_attachment = att;
2685                 }
2686                 else {
2687                         aptr = WC->first_attachment;
2688                         while (aptr->next != NULL) aptr = aptr->next;
2689                         aptr->next = att;
2690                 }
2691
2692                 /**
2693                  * Mozilla sends a simple filename, which is what we want,
2694                  * but Satan's Browser sends an entire pathname.  Reduce
2695                  * the path to just a filename if we need to.
2696                  */
2697                 while (num_tokens(att->filename, '/') > 1) {
2698                         remove_token(att->filename, 0, '/');
2699                 }
2700                 while (num_tokens(att->filename, '\\') > 1) {
2701                         remove_token(att->filename, 0, '\\');
2702                 }
2703
2704                 /**
2705                  * Transfer control of this memory from the upload struct
2706                  * to the attachment struct.
2707                  */
2708                 att->data = WC->upload;
2709                 WC->upload_length = 0;
2710                 WC->upload = NULL;
2711                 display_enter();
2712                 return;
2713         }
2714
2715         if (strlen(bstr("cancel_button")) > 0) {
2716                 lprintf(9, "%s:%d: cancel button was pressed\n", __FILE__, __LINE__);
2717                 sprintf(WC->ImportantMessage, 
2718                         _("Cancelled.  Message was not posted."));
2719         } else if (strlen(bstr("attach_button")) > 0) {
2720                 lprintf(9, "%s:%d: attach button was pressed\n", __FILE__, __LINE__);
2721                 display_enter();
2722                 return;
2723         } else if (atol(bstr("postseq")) == dont_post) {
2724                 lprintf(9, "%s:%d: postseq does not match\n", __FILE__, __LINE__);
2725                 sprintf(WC->ImportantMessage, 
2726                         _("Automatically cancelled because you have already "
2727                         "saved this message."));
2728         } else {
2729                 rfc2047encode(encoded_subject, sizeof encoded_subject, bstr("subject"));
2730                 sprintf(buf, "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s",
2731                         bstr("recp"),
2732                         is_anonymous,
2733                         encoded_subject,
2734                         display_name,
2735                         bstr("cc"),
2736                         bstr("bcc"),
2737                         bstr("wikipage")
2738                 );
2739                 serv_puts(buf);
2740                 serv_getln(buf, sizeof buf);
2741                 if (buf[0] == '4') {
2742                         post_mime_to_server();
2743                         if ( (strlen(bstr("recp")) > 0)
2744                            || (strlen(bstr("cc")) > 0)
2745                            || (strlen(bstr("bcc")) > 0)
2746                         ) {
2747                                 sprintf(WC->ImportantMessage, _("Message has been sent.\n"));
2748                         }
2749                         else {
2750                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
2751                         }
2752                         dont_post = atol(bstr("postseq"));
2753                 } else {
2754                         lprintf(9, "%s:%d: server post error\n", __FILE__, __LINE__);
2755                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
2756                         display_enter();
2757                         return;
2758                 }
2759         }
2760
2761         free_attachments(WC);
2762
2763         /**
2764          *  We may have been supplied with instructions regarding the location
2765          *  to which we must return after posting.  If found, go there.
2766          */
2767         if (strlen(bstr("return_to")) > 0) {
2768                 http_redirect(bstr("return_to"));
2769         }
2770         /**
2771          *  If we were editing a page in a wiki room, go to that page now.
2772          */
2773         else if (strlen(bstr("wikipage")) > 0) {
2774                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
2775                 http_redirect(buf);
2776         }
2777         /**
2778          *  Otherwise, just go to the "read messages" loop.
2779          */
2780         else {
2781                 readloop("readnew");
2782         }
2783 }
2784
2785
2786
2787
2788 /**
2789  * \brief display the message entry screen
2790  */
2791 void display_enter(void)
2792 {
2793         char buf[SIZ];
2794         char ebuf[SIZ];
2795         long now;
2796         char *display_name;
2797         struct wc_attachment *att;
2798         int recipient_required = 0;
2799         int recipient_bad = 0;
2800         int i;
2801         int is_anonymous = 0;
2802         long existing_page = (-1L);
2803
2804         now = time(NULL);
2805
2806         if (strlen(bstr("force_room")) > 0) {
2807                 gotoroom(bstr("force_room"));
2808         }
2809
2810         display_name = bstr("display_name");
2811         if (!strcmp(display_name, "__ANONYMOUS__")) {
2812                 display_name = "";
2813                 is_anonymous = 1;
2814         }
2815
2816         /** First test to see whether this is a room that requires recipients to be entered */
2817         serv_puts("ENT0 0");
2818         serv_getln(buf, sizeof buf);
2819         if (!strncmp(buf, "570", 3)) {          /** 570 means that we need a recipient here */
2820                 recipient_required = 1;
2821         }
2822         else if (buf[0] != '2') {               /** Any other error means that we cannot continue */
2823                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
2824                 readloop("readnew");
2825                 return;
2826         }
2827
2828         /**
2829          * Are we perhaps in an address book view?  If so, then an "enter
2830          * message" command really means "add new entry."
2831          */
2832         if (WC->wc_default_view == VIEW_ADDRESSBOOK) {
2833                 do_edit_vcard(-1, "", "");
2834                 return;
2835         }
2836
2837 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
2838         /**
2839          * Are we perhaps in a calendar room?  If so, then an "enter
2840          * message" command really means "add new calendar item."
2841          */
2842         if (WC->wc_default_view == VIEW_CALENDAR) {
2843                 display_edit_event();
2844                 return;
2845         }
2846
2847         /**
2848          * Are we perhaps in a tasks view?  If so, then an "enter
2849          * message" command really means "add new task."
2850          */
2851         if (WC->wc_default_view == VIEW_TASKS) {
2852                 display_edit_task();
2853                 return;
2854         }
2855 #endif
2856
2857         /**
2858          * Otherwise proceed normally.
2859          * Do a custom room banner with no navbar...
2860          */
2861         output_headers(1, 1, 2, 0, 0, 0);
2862         wprintf("<div id=\"banner\">\n");
2863         embed_room_banner(NULL, navbar_none);
2864         wprintf("</div>\n");
2865         wprintf("<div id=\"content\">\n"
2866                 "<div class=\"fix_scrollbar_bug\">"
2867                 "<table class=\"messages_background\"><tr><td>");
2868
2869         /** Now check our actual recipients if there are any */
2870         if (recipient_required) {
2871                 sprintf(buf, "ENT0 0|%s|%d|0||%s||%s|%s|%s",
2872                         bstr("recp"),
2873                         is_anonymous,
2874                         display_name,
2875                         bstr("cc"), bstr("bcc"), bstr("wikipage"));
2876                 serv_puts(buf);
2877                 serv_getln(buf, sizeof buf);
2878
2879                 if (!strncmp(buf, "570", 3)) {  /** 570 means we have an invalid recipient listed */
2880                         if (strlen(bstr("recp")) + strlen(bstr("cc")) + strlen(bstr("bcc")) > 0) {
2881                                 recipient_bad = 1;
2882                         }
2883                 }
2884                 else if (buf[0] != '2') {       /** Any other error means that we cannot continue */
2885                         wprintf("<em>%s</em><br />\n", &buf[4]);
2886                         goto DONE;
2887                 }
2888         }
2889
2890         /** If we got this far, we can display the message entry screen. */
2891
2892         /** begin message entry screen */
2893         wprintf("<form "
2894                 "enctype=\"multipart/form-data\" "
2895                 "method=\"POST\" "
2896                 "accept-charset=\"UTF-8\" "
2897                 "action=\"post\" "
2898                 "name=\"enterform\""
2899                 ">\n");
2900         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n", now);
2901         if (WC->wc_view == VIEW_WIKI) {
2902                 wprintf("<input type=\"hidden\" name=\"wikipage\" value=\"%s\">\n", bstr("wikipage"));
2903         }
2904         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"%s\">\n", bstr("return_to"));
2905
2906         /** header bar */
2907
2908         wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
2909         fmt_date(buf, now, 0);
2910         wprintf("%s ", buf);
2911
2912         wprintf(_(" <I>from</I> "));
2913
2914         wprintf("<select name=\"display_name\" size=1>\n");
2915
2916         serv_puts("GVSN");
2917         serv_getln(buf, sizeof buf);
2918         if (buf[0] == '1') {
2919                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2920                         wprintf("<option %s value=\"",
2921                                 ((!strcasecmp(bstr("display_name"), buf)) ? "selected" : "")
2922                         );
2923                         escputs(buf);
2924                         wprintf("\">");
2925                         escputs(buf);
2926                         wprintf("</option>\n");
2927                 }
2928         }
2929
2930         if (WC->room_flags & QR_ANONOPT) {
2931                 wprintf("<option %s value=\"__ANONYMOUS__\">%s</option>\n",
2932                         ((!strcasecmp(bstr("__ANONYMOUS__"), WC->wc_fullname)) ? "selected" : ""),
2933                         _("Anonymous")
2934                 );
2935         }
2936
2937         wprintf("</select>\n");
2938
2939         wprintf(_(" <I>in</I> "));
2940         escputs(WC->wc_roomname);
2941         wprintf("<br>\n");      /** header bar */
2942
2943         wprintf("<table border=\"0\" width=\"100%%\">\n");
2944         if (recipient_required) {
2945
2946                 wprintf("<tr><td>");
2947                 wprintf("<font size=-1>");
2948                 wprintf(_("To:"));
2949                 wprintf("</font>");
2950                 wprintf("</td><td>"
2951                         "<input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_id\" value=\"");
2952                 escputs(bstr("recp"));
2953                 wprintf("\" size=50 maxlength=1000 />");
2954                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
2955                 wprintf("</td><td></td></tr>\n");
2956
2957                 wprintf("<tr><td>");
2958                 wprintf("<font size=-1>");
2959                 wprintf(_("CC:"));
2960                 wprintf("</font>");
2961                 wprintf("</td><td>"
2962                         "<input autocomplete=\"off\" type=\"text\" name=\"cc\" id=\"cc_id\" value=\"");
2963                 escputs(bstr("cc"));
2964                 wprintf("\" size=50 maxlength=1000 />");
2965                 wprintf("<div class=\"auto_complete\" id=\"cc_name_choices\"></div>");
2966                 wprintf("</td><td></td></tr>\n");
2967
2968                 wprintf("<tr><td>");
2969                 wprintf("<font size=-1>");
2970                 wprintf(_("BCC:"));
2971                 wprintf("</font>");
2972                 wprintf("</td><td>"
2973                         "<input autocomplete=\"off\" type=\"text\" name=\"bcc\" id=\"bcc_id\" value=\"");
2974                 escputs(bstr("bcc"));
2975                 wprintf("\" size=50 maxlength=1000 />");
2976                 wprintf("<div class=\"auto_complete\" id=\"bcc_name_choices\"></div>");
2977                 wprintf("</td><td></td></tr>\n");
2978
2979                 /** Initialize the autocomplete ajax helpers (found in wclib.js) */
2980                 wprintf("<script type=\"text/javascript\">      \n"
2981                         " activate_entmsg_autocompleters();     \n"
2982                         "</script>                              \n"
2983                 );
2984         }
2985
2986         wprintf("<tr><td>");
2987         wprintf("<font size=-1>");
2988         wprintf(_("Subject (optional):"));
2989         wprintf("</font>");
2990         wprintf("</td><td>"
2991                 "<input type=\"text\" name=\"subject\" value=\"");
2992         escputs(bstr("subject"));
2993         wprintf("\" size=50 maxlength=70></td><td>\n");
2994
2995         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
2996         if (recipient_required) {
2997                 wprintf(_("Send message"));
2998         } else {
2999                 wprintf(_("Post message"));
3000         }
3001         wprintf("\">&nbsp;"
3002                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
3003         wprintf("</td></tr></table>\n");
3004
3005         wprintf("<center>");
3006
3007         wprintf("<textarea name=\"msgtext\" cols=\"80\" rows=\"15\">");
3008
3009         /** If we're continuing from a previous edit, put our partially-composed message back... */
3010         msgescputs(bstr("msgtext"));
3011
3012         /* If we're forwarding a message, insert it here... */
3013         if (atol(bstr("fwdquote")) > 0L) {
3014                 wprintf("<br><div align=center><i>");
3015                 wprintf(_("--- forwarded message ---"));
3016                 wprintf("</i></div><br>");
3017                 pullquote_message(atol(bstr("fwdquote")), 1, 1);
3018         }
3019
3020         /** If we're replying quoted, insert the quote here... */
3021         else if (atol(bstr("replyquote")) > 0L) {
3022                 wprintf("<br>"
3023                         "<blockquote>");
3024                 pullquote_message(atol(bstr("replyquote")), 0, 1);
3025                 wprintf("</blockquote><br>");
3026         }
3027
3028         /** If we're editing a wiki page, insert the existing page here... */
3029         else if (WC->wc_view == VIEW_WIKI) {
3030                 safestrncpy(buf, bstr("wikipage"), sizeof buf);
3031                 str_wiki_index(buf);
3032                 existing_page = locate_message_by_uid(buf);
3033                 if (existing_page >= 0L) {
3034                         pullquote_message(existing_page, 1, 0);
3035                 }
3036         }
3037
3038         /** Insert our signature if appropriate... */
3039         if ( (WC->is_mailbox) && (strcmp(bstr("sig_inserted"), "yes")) ) {
3040                 get_preference("use_sig", buf, sizeof buf);
3041                 if (!strcasecmp(buf, "yes")) {
3042                         get_preference("signature", ebuf, sizeof ebuf);
3043                         euid_unescapize(buf, ebuf);
3044                         wprintf("<br>--<br>");
3045                         for (i=0; i<strlen(buf); ++i) {
3046                                 if (buf[i] == '\n') {
3047                                         wprintf("<br>");
3048                                 }
3049                                 else if (buf[i] == '<') {
3050                                         wprintf("&lt;");
3051                                 }
3052                                 else if (buf[i] == '>') {
3053                                         wprintf("&gt;");
3054                                 }
3055                                 else if (buf[i] == '&') {
3056                                         wprintf("&amp;");
3057                                 }
3058                                 else if (buf[i] == '\"') {
3059                                         wprintf("&quot;");
3060                                 }
3061                                 else if (buf[i] == '\'') {
3062                                         wprintf("&#39;");
3063                                 }
3064                                 else if (isprint(buf[i])) {
3065                                         wprintf("%c", buf[i]);
3066                                 }
3067                         }
3068                 }
3069         }
3070
3071         wprintf("</textarea>");
3072         wprintf("</center><br />\n");
3073
3074         /**
3075          * The following script embeds the TinyMCE richedit control, and automatically
3076          * transforms the textarea into a richedit textarea.
3077          */
3078         wprintf(
3079                 "<script language=\"javascript\" type=\"text/javascript\" src=\"tiny_mce/tiny_mce.js\"></script>\n"
3080                 "<script language=\"javascript\" type=\"text/javascript\">"
3081                 "tinyMCE.init({"
3082                 "       mode : \"textareas\", width : \"100%%\", browsers : \"msie,gecko\", "
3083                 "       theme : \"advanced\", plugins : \"iespell\", "
3084                 "       theme_advanced_buttons1 : \"bold, italic, underline, strikethrough, justifyleft, justifycenter, justifyright, justifyfull, bullist, numlist, cut, copy, paste, link, image, help, forecolor, iespell, code\", "
3085                 "       theme_advanced_buttons2 : \"\", "
3086                 "       theme_advanced_buttons3 : \"\", "
3087                 "       content_css : \"static/webcit-tinymce.css\" "
3088                 "});"
3089                 "</script>\n"
3090         );
3091
3092
3093         /** Enumerate any attachments which are already in place... */
3094         wprintf("<img src=\"static/diskette_24x.gif\" border=0 "
3095                 "align=middle height=16 width=16> ");
3096         wprintf(_("Attachments:"));
3097         wprintf(" ");
3098         wprintf("<select name=\"which_attachment\" size=1>");
3099         for (att = WC->first_attachment; att != NULL; att = att->next) {
3100                 wprintf("<option value=\"");
3101                 urlescputs(att->filename);
3102                 wprintf("\">");
3103                 escputs(att->filename);
3104                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
3105                 wprintf("</option>\n");
3106         }
3107         wprintf("</select>");
3108
3109         /** Now offer the ability to attach additional files... */
3110         wprintf("&nbsp;&nbsp;&nbsp;");
3111         wprintf(_("Attach file:"));
3112         wprintf(" <input NAME=\"attachfile\" "
3113                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
3114                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
3115
3116         /** Seth asked for these to be at the top *and* bottom... */
3117         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
3118         if (recipient_required) {
3119                 wprintf(_("Send message"));
3120         } else {
3121                 wprintf(_("Post message"));
3122         }
3123         wprintf("\">&nbsp;"
3124                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
3125
3126         /** Make sure we only insert our signature once */
3127         if (strcmp(bstr("sig_inserted"), "yes")) {
3128                 wprintf("<INPUT TYPE=\"hidden\" NAME=\"sig_inserted\" VALUE=\"yes\">\n");
3129         }
3130
3131         wprintf("</form>\n");
3132
3133         wprintf("</td></tr></table></div>\n");
3134 DONE:   wDumpContent(1);
3135 }
3136
3137
3138
3139 /**
3140  * \brief delete a message
3141  */
3142 void delete_msg(void)
3143 {
3144         long msgid;
3145         char buf[SIZ];
3146
3147         msgid = atol(bstr("msgid"));
3148
3149         if (WC->wc_is_trash) {  /** Delete from Trash is a real delete */
3150                 serv_printf("DELE %ld", msgid); 
3151         }
3152         else {                  /** Otherwise move it to Trash */
3153                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
3154         }
3155
3156         serv_getln(buf, sizeof buf);
3157         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3158
3159         readloop("readnew");
3160 }
3161
3162
3163 /**
3164  * \brief move a message to another folder
3165  */
3166 void move_msg(void)
3167 {
3168         long msgid;
3169         char buf[SIZ];
3170
3171         msgid = atol(bstr("msgid"));
3172
3173         if (strlen(bstr("move_button")) > 0) {
3174                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
3175                 serv_puts(buf);
3176                 serv_getln(buf, sizeof buf);
3177                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
3178         } else {
3179                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
3180         }
3181
3182         readloop("readnew");
3183 }
3184
3185
3186
3187
3188
3189 /**
3190  * \brief Confirm move of a message
3191  */
3192 void confirm_move_msg(void)
3193 {
3194         long msgid;
3195         char buf[SIZ];
3196         char targ[SIZ];
3197
3198         msgid = atol(bstr("msgid"));
3199
3200
3201         output_headers(1, 1, 2, 0, 0, 0);
3202         wprintf("<div id=\"banner\">\n");
3203         wprintf("<TABLE WIDTH=100%% BORDER=0><TR><TD>");
3204         wprintf("<SPAN CLASS=\"titlebar\">");
3205         wprintf(_("Confirm move of message"));
3206         wprintf("</SPAN>\n");
3207         wprintf("</TD></TR></TABLE>\n");
3208         wprintf("</div>\n<div id=\"content\">\n");
3209
3210         wprintf("<CENTER>");
3211
3212         wprintf(_("Move this message to:"));
3213         wprintf("<br />\n");
3214
3215         wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
3216         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
3217
3218         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
3219         serv_puts("LKRA");
3220         serv_getln(buf, sizeof buf);
3221         if (buf[0] == '1') {
3222                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3223                         extract_token(targ, buf, 0, '|', sizeof targ);
3224                         wprintf("<OPTION>");
3225                         escputs(targ);
3226                         wprintf("\n");
3227                 }
3228         }
3229         wprintf("</SELECT>\n");
3230         wprintf("<br />\n");
3231
3232         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
3233         wprintf("&nbsp;");
3234         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
3235         wprintf("</form></CENTER>\n");
3236
3237         wprintf("</CENTER>\n");
3238         wDumpContent(1);
3239 }
3240
3241
3242 /*@}*/