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