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