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