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