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