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