* "Print" button on each message. It even uses a hidden iframe to
[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 <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26
27 #ifdef HAVE_ICONV
28 #include <iconv.h>
29 #endif
30
31 #include "webcit.h"
32 #include "vcard.h"
33 #include "webserver.h"
34
35
36 /* Address book entry (keep it short and sweet, it's just a quickie lookup
37  * which we can use to get to the real meat and bones later)
38  */
39 struct addrbookent {
40         char ab_name[64];
41         long ab_msgnum;
42 };
43
44
45
46 #ifdef HAVE_ICONV
47 /* Handle subjects with RFC2047 encoding, such as:
48  * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
49  */
50 void utf8ify_rfc822_string(char *buf) {
51         char *start, *end;
52         char newbuf[1024];
53         char charset[128];
54         char encoding[16];
55         char istr[1024];
56         iconv_t ic = (iconv_t)(-1) ;
57         char *ibuf;                   /* Buffer of characters to be converted */
58         char *obuf;                   /* Buffer for converted characters      */
59         size_t ibuflen;               /* Length of input buffer               */
60         size_t obuflen;               /* Length of output buffer              */
61         char *isav;                   /* Saved pointer to input buffer        */
62         char *osav;                   /* Saved pointer to output buffer       */
63
64         while (start=strstr(buf, "=?"), end=strstr(buf, "?="),
65                 ((start != NULL) && (end != NULL) && (end > start)) )
66         {
67                 extract_token(charset, start, 1, '?', sizeof charset);
68                 extract_token(encoding, start, 2, '?', sizeof encoding);
69                 extract_token(istr, start, 3, '?', sizeof istr);
70
71                 strcpy(start, "");
72                 ++end;
73                 ++end;
74
75                 ibuf = malloc(1024);
76                 isav = ibuf;
77                 if (!strcasecmp(encoding, "B")) {       /* base64 */
78                         ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
79                 }
80                 else if (!strcasecmp(encoding, "Q")) {  /* quoted-printable */
81                         ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, strlen(istr));
82                 }
83                 else {
84                         strcpy(ibuf, istr);             /* huh? */
85                         ibuflen = strlen(istr);
86                 }
87
88                 ic = iconv_open("UTF-8", charset);
89                 if (ic != (iconv_t)(-1) ) {
90                         obuf = malloc(1024);
91                         obuflen = 1024;
92                         obuf = (char *) malloc(obuflen);
93                         osav = obuf;
94                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
95                         osav[1024-obuflen] = 0;
96                         snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
97                         strcpy(buf, newbuf);
98                         free(osav);
99                         iconv_close(ic);
100                 }
101                 else {
102                         snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
103                         strcpy(buf, newbuf);
104                 }
105
106                 free(isav);
107         }
108
109 }
110 #endif
111
112
113 /*
114  * Look for URL's embedded in a buffer and make them linkable.  We use a
115  * target window in order to keep the BBS session in its own window.
116  */
117 void url(buf)
118 char buf[];
119 {
120
121         int pos;
122         int start, end;
123         char ench;
124         char urlbuf[SIZ];
125         char outbuf[1024];
126
127         start = (-1);
128         end = strlen(buf);
129         ench = 0;
130
131         for (pos = 0; pos < strlen(buf); ++pos) {
132                 if (!strncasecmp(&buf[pos], "http://", 7))
133                         start = pos;
134                 if (!strncasecmp(&buf[pos], "ftp://", 6))
135                         start = pos;
136         }
137
138         if (start < 0)
139                 return;
140
141         if ((start > 0) && (buf[start - 1] == '<'))
142                 ench = '>';
143         if ((start > 0) && (buf[start - 1] == '['))
144                 ench = ']';
145         if ((start > 0) && (buf[start - 1] == '('))
146                 ench = ')';
147         if ((start > 0) && (buf[start - 1] == '{'))
148                 ench = '}';
149
150         for (pos = strlen(buf); pos > start; --pos) {
151                 if ((buf[pos] == ' ') || (buf[pos] == ench))
152                         end = pos;
153         }
154
155         strncpy(urlbuf, &buf[start], end - start);
156         urlbuf[end - start] = 0;
157
158         strncpy(outbuf, buf, start);
159         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
160                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
161         strcat(outbuf, &buf[end]);
162         if ( strlen(outbuf) < 250 )
163                 strcpy(buf, outbuf);
164 }
165
166
167 /*
168  * Turn a vCard "n" (name) field into something displayable.
169  */
170 void vcard_n_prettyize(char *name)
171 {
172         char *original_name;
173         int i;
174
175         original_name = strdup(name);
176         for (i=0; i<5; ++i) {
177                 if (strlen(original_name) > 0) {
178                         if (original_name[strlen(original_name)-1] == ' ') {
179                                 original_name[strlen(original_name)-1] = 0;
180                         }
181                         if (original_name[strlen(original_name)-1] == ';') {
182                                 original_name[strlen(original_name)-1] = 0;
183                         }
184                 }
185         }
186         strcpy(name, "");
187         for (i=0; i<strlen(original_name); ++i) {
188                 if (original_name[i] == ';') {
189                         strcat(name, ", ");
190                 }
191                 else {
192                         name[strlen(name)+1] = 0;
193                         name[strlen(name)] = original_name[i];
194                 }
195         }
196         free(original_name);
197 }
198
199
200
201
202 /* display_vcard() calls this after parsing the textual vCard into
203  * our 'struct vCard' data object.
204  * This gets called instead of display_parsed_vcard() if we are only looking
205  * to extract the person's name instead of displaying the card.
206  */
207 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
208         char *name;
209
210         strcpy(storename, "");
211
212         name = vcard_get_prop(v, "n", 1, 0, 0);
213         if (name != NULL) {
214                 strcpy(storename, name);
215                 /* vcard_n_prettyize(storename); */
216         }
217
218 }
219
220
221
222 /* display_vcard() calls this after parsing the textual vCard into
223  * our 'struct vCard' data object.
224  *
225  * Set 'full' to nonzero to display the full card, otherwise it will only
226  * show a summary line.
227  *
228  * This code is a bit ugly, so perhaps an explanation is due: we do this
229  * in two passes through the vCard fields.  On the first pass, we process
230  * fields we understand, and then render them in a pretty fashion at the
231  * end.  Then we make a second pass, outputting all the fields we don't
232  * understand in a simple two-column name/value format.
233  */
234 void display_parsed_vcard(struct vCard *v, int full) {
235         int i, j;
236         char buf[SIZ];
237         char *name;
238         int is_qp = 0;
239         int is_b64 = 0;
240         char *thisname, *thisvalue;
241         char firsttoken[SIZ];
242         int pass;
243
244         char displayname[SIZ];
245         char title[SIZ];
246         char org[SIZ];
247         char phone[SIZ];
248         char mailto[SIZ];
249
250         strcpy(displayname, "");
251         strcpy(phone, "");
252         strcpy(mailto, "");
253         strcpy(title, "");
254         strcpy(org, "");
255
256         if (!full) {
257                 wprintf("<TD>");
258                 name = vcard_get_prop(v, "fn", 1, 0, 0);
259                 if (name != NULL) {
260                         escputs(name);
261                 }
262                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
263                         strcpy(displayname, name);
264                         vcard_n_prettyize(displayname);
265                         escputs(displayname);
266                 }
267                 else {
268                         wprintf("&nbsp;");
269                 }
270                 wprintf("</TD>");
271                 return;
272         }
273
274         wprintf("<div align=center><table bgcolor=#aaaaaa width=50%%>");
275         for (pass=1; pass<=2; ++pass) {
276
277                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
278
279                         thisname = strdup(v->prop[i].name);
280                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
281         
282                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
283                                 extract_token(buf, thisname, j, ';', sizeof buf);
284                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
285                                         is_qp = 1;
286                                         remove_token(thisname, j, ';');
287                                 }
288                                 if (!strcasecmp(buf, "encoding=base64")) {
289                                         is_b64 = 1;
290                                         remove_token(thisname, j, ';');
291                                 }
292                         }
293         
294                         if (is_qp) {
295                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
296                                 j = CtdlDecodeQuotedPrintable(
297                                         thisvalue, v->prop[i].value,
298                                         strlen(v->prop[i].value) );
299                                 thisvalue[j] = 0;
300                         }
301                         else if (is_b64) {
302                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
303                                 CtdlDecodeBase64(
304                                         thisvalue, v->prop[i].value,
305                                         strlen(v->prop[i].value) );
306                         }
307                         else {
308                                 thisvalue = strdup(v->prop[i].value);
309                         }
310         
311                         /*** Various fields we may encounter ***/
312         
313                         /* N is name, but only if there's no FN already there */
314                         if (!strcasecmp(firsttoken, "n")) {
315                                 if (strlen(displayname) == 0) {
316                                         strcpy(displayname, thisvalue);
317                                         vcard_n_prettyize(displayname);
318                                 }
319                         }
320         
321                         /* FN (full name) is a true 'display name' field */
322                         else if (!strcasecmp(firsttoken, "fn")) {
323                                 strcpy(displayname, thisvalue);
324                         }
325
326                         /* title */
327                         else if (!strcasecmp(firsttoken, "title")) {
328                                 strcpy(title, thisvalue);
329                         }
330         
331                         /* organization */
332                         else if (!strcasecmp(firsttoken, "org")) {
333                                 strcpy(org, thisvalue);
334                         }
335         
336                         else if (!strcasecmp(firsttoken, "email")) {
337                                 if (strlen(mailto) > 0) strcat(mailto, "<br />");
338                                 strcat(mailto,
339                                         "<A HREF=\"/display_enter"
340                                         "?force_room=_MAIL_?recp=");
341                                 urlesc(&mailto[strlen(mailto)], thisvalue);
342                                 strcat(mailto, "\">");
343                                 urlesc(&mailto[strlen(mailto)], thisvalue);
344                                 strcat(mailto, "</A>");
345                         }
346                         else if (!strcasecmp(firsttoken, "tel")) {
347                                 if (strlen(phone) > 0) strcat(phone, "<br />");
348                                 strcat(phone, thisvalue);
349                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
350                                         extract_token(buf, thisname, j, ';', sizeof buf);
351                                         if (!strcasecmp(buf, "tel"))
352                                                 strcat(phone, "");
353                                         else if (!strcasecmp(buf, "work"))
354                                                 strcat(phone, " (work)");
355                                         else if (!strcasecmp(buf, "home"))
356                                                 strcat(phone, " (home)");
357                                         else if (!strcasecmp(buf, "cell"))
358                                                 strcat(phone, " (cell)");
359                                         else {
360                                                 strcat(phone, " (");
361                                                 strcat(phone, buf);
362                                                 strcat(phone, ")");
363                                         }
364                                 }
365                         }
366                         else if (!strcasecmp(firsttoken, "adr")) {
367                                 if (pass == 2) {
368                                         wprintf("<TR><TD>Address:</TD><TD>");
369                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
370                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
371                                                 if (strlen(buf) > 0) {
372                                                         escputs(buf);
373                                                         if (j<3) wprintf("<br />");
374                                                         else wprintf(" ");
375                                                 }
376                                         }
377                                         wprintf("</TD></TR>\n");
378                                 }
379                         }
380                         else if (!strcasecmp(firsttoken, "version")) {
381                                 /* ignore */
382                         }
383                         else if (!strcasecmp(firsttoken, "rev")) {
384                                 /* ignore */
385                         }
386                         else if (!strcasecmp(firsttoken, "label")) {
387                                 /* ignore */
388                         }
389                         else {
390
391                                 /*** Don't show extra fields.  They're ugly.
392                                 if (pass == 2) {
393                                         wprintf("<TR><TD>");
394                                         escputs(thisname);
395                                         wprintf("</TD><TD>");
396                                         escputs(thisvalue);
397                                         wprintf("</TD></TR>\n");
398                                 }
399                                 ***/
400                         }
401         
402                         free(thisname);
403                         free(thisvalue);
404                 }
405         
406                 if (pass == 1) {
407                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
408                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
409                         "<IMG ALIGN=CENTER SRC=\"/static/viewcontacts_48x.gif\">"
410                         "<FONT SIZE=+1><B>");
411                         escputs(displayname);
412                         wprintf("</B></FONT>");
413                         if (strlen(title) > 0) {
414                                 wprintf("<div align=right>");
415                                 escputs(title);
416                                 wprintf("</div>");
417                         }
418                         if (strlen(org) > 0) {
419                                 wprintf("<div align=right>");
420                                 escputs(org);
421                                 wprintf("</div>");
422                         }
423                         wprintf("</TD></TR>\n");
424                 
425                         if (strlen(phone) > 0)
426                                 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
427                         if (strlen(mailto) > 0)
428                                 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
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
476 /*
477  * I wanna SEE that message!
478  */
479 void read_message(long msgnum, int suppress_buttons) {
480         char buf[SIZ];
481         char mime_partnum[256];
482         char mime_filename[256];
483         char mime_content_type[256];
484         char mime_charset[256];
485         char mime_disposition[256];
486         int mime_length;
487         char mime_http[SIZ];
488         char m_subject[256];
489         char from[256];
490         char node[256];
491         char rfca[256];
492         char reply_to[512];
493         char now[256];
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(vcard_partnum, "");
515         strcpy(cal_partnum, "");
516         strcpy(mime_http, "");
517         strcpy(mime_content_type, "text/plain");
518         strcpy(mime_charset, "us-ascii");
519
520         serv_printf("MSG4 %ld", msgnum);
521         serv_getln(buf, sizeof buf);
522         if (buf[0] != '1') {
523                 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
524                 return;
525         }
526
527         /* begin everythingamundo table */
528         wprintf("<div id=\"fix_scrollbar_bug\">\n");
529         wprintf("<table width=100%% border=1 cellspacing=0 "
530                 "cellpadding=0><TR><TD>\n");
531
532         /* begin message header table */
533         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
534                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
535
536         wprintf("<SPAN CLASS=\"message_header\">");
537         strcpy(m_subject, "");
538
539         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
540                 if (!strcmp(buf, "000")) {
541                         wprintf("<I>unexpected end of message</I><br /><br />\n");
542                         wprintf("</SPAN>\n");
543                         return;
544                 }
545                 if (!strncasecmp(buf, "nhdr=yes", 8))
546                         nhdr = 1;
547                 if (nhdr == 1)
548                         buf[0] = '_';
549                 if (!strncasecmp(buf, "type=", 5))
550                         format_type = atoi(&buf[5]);
551                 if (!strncasecmp(buf, "from=", 5)) {
552                         strcpy(from, &buf[5]);
553                         wprintf("from <A HREF=\"/showuser?who=");
554 #ifdef HAVE_ICONV
555                         utf8ify_rfc822_string(from);
556 #endif
557                         urlescputs(from);
558                         wprintf("\">");
559                         escputs(from);
560                         wprintf("</A> ");
561                 }
562                 if (!strncasecmp(buf, "subj=", 5))
563                         strcpy(m_subject, &buf[5]);
564                 if ((!strncasecmp(buf, "hnod=", 5))
565                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
566                         wprintf("(%s) ", &buf[5]);
567                 if ((!strncasecmp(buf, "room=", 5))
568                     && (strcasecmp(&buf[5], WC->wc_roomname))
569                     && (strlen(&buf[5])>0) )
570                         wprintf("in %s> ", &buf[5]);
571                 if (!strncasecmp(buf, "rfca=", 5)) {
572                         strcpy(rfca, &buf[5]);
573                         wprintf("&lt;");
574                         escputs(rfca);
575                         wprintf("&gt; ");
576                 }
577
578                 if (!strncasecmp(buf, "node=", 5)) {
579                         strcpy(node, &buf[5]);
580                         if ( ((WC->room_flags & QR_NETWORK)
581                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
582                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
583                         && (strlen(rfca)==0)
584                         ) {
585                                 wprintf("@%s ", &buf[5]);
586                         }
587                 }
588                 if (!strncasecmp(buf, "rcpt=", 5))
589                         wprintf("to %s ", &buf[5]);
590                 if (!strncasecmp(buf, "time=", 5)) {
591                         fmt_date(now, atol(&buf[5]), 0);
592                         wprintf("%s ", now);
593                 }
594
595                 if (!strncasecmp(buf, "part=", 5)) {
596                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
597                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
598                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
599                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
600                         mime_length = extract_int(&buf[5], 5);
601
602                         if (!strcasecmp(mime_disposition, "attachment")) {
603                                 snprintf(&mime_http[strlen(mime_http)],
604                                         (sizeof(mime_http) - strlen(mime_http) - 1),
605                                         "<A HREF=\"/output_mimepart?"
606                                         "msgnum=%ld?partnum=%s\" "
607                                         "TARGET=\"wc.%ld.%s\">"
608                                         "<IMG SRC=\"/static/diskette_24x.gif\" "
609                                         "BORDER=0 ALIGN=MIDDLE>\n"
610                                         "Part %s: %s (%s, %d bytes)</A><br />\n",
611                                         msgnum, mime_partnum,
612                                         msgnum, mime_partnum,
613                                         mime_partnum, mime_filename,
614                                         mime_content_type, mime_length);
615                         }
616
617                         if ((!strcasecmp(mime_disposition, "inline"))
618                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
619                                 snprintf(&mime_http[strlen(mime_http)],
620                                         (sizeof(mime_http) - strlen(mime_http) - 1),
621                                         "<IMG SRC=\"/output_mimepart?"
622                                         "msgnum=%ld?partnum=%s\">",
623                                         msgnum, mime_partnum);
624                         }
625
626                         /*** begin handler prep ***/
627                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
628                                 strcpy(vcard_partnum, mime_partnum);
629                         }
630
631                         if (!strcasecmp(mime_content_type, "text/calendar")) {
632                                 strcpy(cal_partnum, mime_partnum);
633                         }
634
635                         /*** end handler prep ***/
636
637                 }
638
639         }
640
641         /* Generate a reply-to address */
642         if (strlen(rfca) > 0) {
643                 strcpy(reply_to, rfca);
644         }
645         else {
646                 if ( (strlen(node) > 0)
647                    && (strcasecmp(node, serv_info.serv_nodename))
648                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
649                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
650                                 from, node);
651                 }
652                 else {
653                         snprintf(reply_to, sizeof(reply_to), "%s", from);
654                 }
655         }
656
657         if (nhdr == 1) {
658                 wprintf("****");
659         }
660
661         wprintf("</SPAN>");
662 #ifdef HAVE_ICONV
663         utf8ify_rfc822_string(m_subject);
664 #endif
665         if (strlen(m_subject) > 0) {
666                 wprintf("<br />"
667                         "<SPAN CLASS=\"message_subject\">"
668                         "Subject: %s"
669                         "</SPAN>", m_subject
670                 );
671         }
672         wprintf("</TD>\n");
673
674         /* start msg buttons */
675         if (!suppress_buttons) {
676                 wprintf("<td align=right>\n");
677
678                 /* Reply */
679                 wprintf("<a href=\"/display_enter?recp=");
680                 urlescputs(reply_to);
681                 wprintf("?subject=");
682                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
683                 urlescputs(m_subject);
684                 wprintf("\">[Reply]</a> ");
685
686                 if (WC->is_room_aide)  {
687                 
688                         /* Move */
689                         wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
690                                 msgnum);
691         
692                         /* Delete */
693                         wprintf("<a href=\"/delete_msg?msgid=%ld\" "
694                                 "onClick=\"return confirm('Delete this message?');\">"
695                                 "[Delete]</a> ", msgnum);
696                                 
697                 }
698
699                 wprintf("<a href=\"/msg?msgnum=%ld?print_it=yes\" target=\"msgloader1\">"
700                         "[Print]</a>", msgnum);
701
702                 wprintf("</td>");
703         }
704
705         wprintf("</TR></TABLE>\n");
706
707         /* Begin body */
708         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
709                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
710
711         /* 
712          * Learn the content type
713          */
714         strcpy(mime_content_type, "text/plain");
715         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
716                 if (!strcmp(buf, "000")) {
717                         wprintf("<I>unexpected end of message</I><br /><br />\n");
718                         goto ENDBODY;
719                 }
720                 if (!strncasecmp(buf, "Content-type: ", 14)) {
721                         safestrncpy(mime_content_type, &buf[14],
722                                 sizeof(mime_content_type));
723                         for (i=0; i<strlen(mime_content_type); ++i) {
724                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
725                                         safestrncpy(mime_charset, &mime_content_type[i+8],
726                                                 sizeof mime_charset);
727                                 }
728                         }
729                         for (i=0; i<strlen(mime_content_type); ++i) {
730                                 if (mime_content_type[i] == ';') {
731                                         mime_content_type[i] = 0;
732                                 }
733                         }
734                 }
735         }
736
737         /* Set up a character set conversion if we need to (and if we can) */
738 #ifdef HAVE_ICONV
739         if ( (strcasecmp(mime_charset, "us-ascii"))
740            && (strcasecmp(mime_charset, "UTF-8")) ) {
741                 ic = iconv_open("UTF-8", mime_charset);
742                 if (ic == (iconv_t)(-1) ) {
743                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
744                 }
745         }
746 #endif
747
748         /* Messages in legacy Citadel variformat get handled thusly... */
749         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
750                 fmout(NULL, "JUSTIFY");
751         }
752
753         /* Boring old 80-column fixed format text gets handled this way... */
754         else if (!strcasecmp(mime_content_type, "text/plain")) {
755                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
756                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
757                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
758
759 #ifdef HAVE_ICONV
760                         if (ic != (iconv_t)(-1) ) {
761                                 ibuf = buf;
762                                 ibuflen = strlen(ibuf);
763                                 obuflen = SIZ;
764                                 obuf = (char *) malloc(obuflen);
765                                 osav = obuf;
766                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
767                                 osav[SIZ-obuflen] = 0;
768                                 safestrncpy(buf, osav, sizeof buf);
769                                 free(osav);
770                         }
771 #endif
772
773                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
774                                 buf[strlen(buf) - 1] = 0;
775                         if ((bq == 0) &&
776                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
777                                 wprintf("<BLOCKQUOTE>");
778                                 bq = 1;
779                         } else if ((bq == 1) &&
780                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
781                                 wprintf("</BLOCKQUOTE>");
782                                 bq = 0;
783                         }
784                         wprintf("<TT>");
785                         url(buf);
786                         escputs(buf);
787                         wprintf("</TT><br />\n");
788                 }
789                 wprintf("</I><br />");
790         }
791
792         else /* HTML is fun, but we've got to strip it first */
793         if (!strcasecmp(mime_content_type, "text/html")) {
794                 output_html(mime_charset);
795         }
796
797         /* Unknown weirdness */
798         else {
799                 wprintf("I don't know how to display %s<br />\n",
800                         mime_content_type);
801                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
802         }
803
804         /* Afterwards, offer links to download attachments 'n' such */
805         if (strlen(mime_http) > 0) {
806                 wprintf("%s", mime_http);
807         }
808
809         /* Handler for vCard parts */
810         if (strlen(vcard_partnum) > 0) {
811                 part_source = load_mimepart(msgnum, vcard_partnum);
812                 if (part_source != NULL) {
813
814                         /* If it's my vCard I can edit it */
815                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
816                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
817                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
818                         ) {
819                                 wprintf("<A HREF=\"/edit_vcard?"
820                                         "msgnum=%ld?partnum=%s\">",
821                                         msgnum, vcard_partnum);
822                                 wprintf("[edit]</A>");
823                         }
824
825                         /* In all cases, display the full card */
826                         display_vcard(part_source, 0, 1, NULL);
827                 }
828         }
829
830         /* Handler for calendar parts */
831         if (strlen(cal_partnum) > 0) {
832                 part_source = load_mimepart(msgnum, cal_partnum);
833                 if (part_source != NULL) {
834                         cal_process_attachment(part_source,
835                                                 msgnum, cal_partnum);
836                 }
837         }
838
839         if (part_source) {
840                 free(part_source);
841                 part_source = NULL;
842         }
843
844 ENDBODY:
845         wprintf("</TD></TR></TABLE>\n");
846
847         /* end everythingamundo table */
848         wprintf("</TD></TR></TABLE>\n");
849         wprintf("</div><br />\n");
850
851 #ifdef HAVE_ICONV
852         if (ic != (iconv_t)(-1) ) {
853                 iconv_close(ic);
854         }
855 #endif
856 }
857
858
859
860 /*
861  * Unadorned HTML output of an individual message, suitable
862  * for placing in a hidden iframe, for printing, or whatever
863  */
864 void embed_message(void) {
865         long msgnum = 0L;
866         char *sourceiframe;
867         char *targetdiv;
868         char *print_it;
869
870         msgnum = atol(bstr("msgnum"));
871         sourceiframe = bstr("sourceiframe");
872         targetdiv = bstr("targetdiv");
873         print_it = bstr("print_it");
874
875         output_headers(1, 0, 0, 0, 0, 1, 0);
876         begin_burst();
877
878         wprintf("<html><head>");
879
880         /* If we're loading into a hidden iframe, chances are the caller told us
881          * about a target div somewhere that we need to copy into when we're done.
882          */
883         if (strlen(targetdiv) > 0) wprintf(
884 "                                                                       \n"
885 " <script type=\"text/javascript\">                                     \n"
886 "       function loaded_now_copy_it() {                                 \n"
887 "               parent.document.getElementById(\"%s\").innerHTML = parent.frames['%s'].document.body.innerHTML; \n"
888 "       }                                                                                       \n"
889 "</script>\n",
890                 targetdiv,
891                 sourceiframe
892         );
893
894         wprintf("</head>");
895         wprintf("<body");
896         if (strlen(targetdiv) > 0) {
897                 wprintf(" onLoad='loaded_now_copy_it();'");
898         }
899         if (!strcasecmp(print_it, "yes")) {
900                 wprintf(" onLoad='window.print();'");
901         }
902         wprintf(">\n");
903         read_message(msgnum, (!strcasecmp(print_it, "yes") ? 1 : 0) );
904         wprintf("</body></html>\n");
905         wDumpContent(0);
906 }
907
908
909
910
911 void display_summarized(int num) {
912         char datebuf[64];
913
914         wprintf("<TD>");
915         if (WC->summ[num].is_new) wprintf("<B>");
916         wprintf("<A HREF=\"/msg?msgnum=%ld?sourceiframe=msgloader1?targetdiv=preview_pane\" target=\"msgloader1\">",
917                 WC->summ[num].msgnum);
918         escputs(WC->summ[num].subj);
919         wprintf("</A>");
920         if (WC->summ[num].is_new) wprintf("</B>");
921         wprintf("</TD><TD>");
922         if (WC->summ[num].is_new) wprintf("<B>");
923         escputs(WC->summ[num].from);
924         if (WC->summ[num].is_new) wprintf("</B>");
925         wprintf(" </TD><TD>");
926         if (WC->summ[num].is_new) wprintf("<B>");
927         fmt_date(datebuf, WC->summ[num].date, 1);       /* brief */
928         escputs(datebuf);
929         if (WC->summ[num].is_new) wprintf("</B>");
930         wprintf(" </TD>");
931         wprintf("<TD>"
932                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
933                 "</TD>\n",
934                 WC->summ[num].msgnum
935         );
936 }
937
938
939 void summarize_message(int num, long msgnum, int is_new) {
940         char buf[SIZ];
941
942         memset(&WC->summ[num], 0, sizeof(struct message_summary));
943         safestrncpy(WC->summ[num].subj, "(no subject)", sizeof WC->summ[num].subj);
944         WC->summ[num].is_new = is_new;
945         WC->summ[num].msgnum = msgnum;
946
947         /* ask for headers only with no MIME */
948         sprintf(buf, "MSG0 %ld|3", msgnum);
949         serv_puts(buf);
950         serv_getln(buf, sizeof buf);
951         if (buf[0] != '1') return;
952
953         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
954                 if (!strncasecmp(buf, "from=", 5)) {
955                         safestrncpy(WC->summ[num].from, &buf[5], sizeof WC->summ[num].from);
956                 }
957                 if (!strncasecmp(buf, "subj=", 5)) {
958                         if (strlen(&buf[5]) > 0) {
959                                 safestrncpy(WC->summ[num].subj, &buf[5],
960                                         sizeof WC->summ[num].subj);
961 #ifdef HAVE_ICONV
962                                 /* Handle subjects with RFC2047 encoding */
963                                 utf8ify_rfc822_string(WC->summ[num].subj);
964 #endif
965                                 if (strlen(WC->summ[num].subj) > 75) {
966                                         strcpy(&WC->summ[num].subj[72], "...");
967                                 }
968                         }
969                 }
970
971                 if (!strncasecmp(buf, "node=", 5)) {
972                         if ( ((WC->room_flags & QR_NETWORK)
973                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
974                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
975                         ) {
976                                 strcat(WC->summ[num].from, " @ ");
977                                 strcat(WC->summ[num].from, &buf[5]);
978                         }
979                 }
980
981                 if (!strncasecmp(buf, "rcpt=", 5)) {
982                         safestrncpy(WC->summ[num].to, &buf[5], sizeof WC->summ[num].to);
983                 }
984
985                 if (!strncasecmp(buf, "time=", 5)) {
986                         WC->summ[num].date = atol(&buf[5]);
987                 }
988         }
989         
990 #ifdef HAVE_ICONV
991         /* Handle senders with RFC2047 encoding */
992         utf8ify_rfc822_string(WC->summ[num].from);
993 #endif
994         if (strlen(WC->summ[num].from) > 25) {
995                 strcpy(&WC->summ[num].from[22], "...");
996         }
997 }
998
999
1000
1001 void display_addressbook(long msgnum, char alpha) {
1002         char buf[SIZ];
1003         char mime_partnum[SIZ];
1004         char mime_filename[SIZ];
1005         char mime_content_type[SIZ];
1006         char mime_disposition[SIZ];
1007         int mime_length;
1008         char vcard_partnum[SIZ];
1009         char *vcard_source = NULL;
1010         struct message_summary summ;
1011
1012         memset(&summ, 0, sizeof(summ));
1013         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1014
1015         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1016         serv_puts(buf);
1017         serv_getln(buf, sizeof buf);
1018         if (buf[0] != '1') return;
1019
1020         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1021                 if (!strncasecmp(buf, "part=", 5)) {
1022                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1023                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1024                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1025                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1026                         mime_length = extract_int(&buf[5], 5);
1027
1028                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1029                                 strcpy(vcard_partnum, mime_partnum);
1030                         }
1031
1032                 }
1033         }
1034
1035         if (strlen(vcard_partnum) > 0) {
1036                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1037                 if (vcard_source != NULL) {
1038
1039                         /* Display the summary line */
1040                         display_vcard(vcard_source, alpha, 0, NULL);
1041
1042                         /* If it's my vCard I can edit it */
1043                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1044                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1045                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1046                         ) {
1047                                 wprintf("<A HREF=\"/edit_vcard?"
1048                                         "msgnum=%ld?partnum=%s\">",
1049                                         msgnum, vcard_partnum);
1050                                 wprintf("[edit]</A>");
1051                         }
1052
1053                         free(vcard_source);
1054                 }
1055         }
1056
1057 }
1058
1059
1060
1061 /* If it's an old "Firstname Lastname" style record, try to
1062  * convert it.
1063  */
1064 void lastfirst_firstlast(char *namebuf) {
1065         char firstname[SIZ];
1066         char lastname[SIZ];
1067         int i;
1068
1069         if (namebuf == NULL) return;
1070         if (strchr(namebuf, ';') != NULL) return;
1071
1072         i = num_tokens(namebuf, ' ');
1073         if (i < 2) return;
1074
1075         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1076         remove_token(namebuf, i-1, ' ');
1077         strcpy(firstname, namebuf);
1078         sprintf(namebuf, "%s; %s", lastname, firstname);
1079 }
1080
1081
1082 void fetch_ab_name(long msgnum, char *namebuf) {
1083         char buf[SIZ];
1084         char mime_partnum[SIZ];
1085         char mime_filename[SIZ];
1086         char mime_content_type[SIZ];
1087         char mime_disposition[SIZ];
1088         int mime_length;
1089         char vcard_partnum[SIZ];
1090         char *vcard_source = NULL;
1091         int i;
1092         struct message_summary summ;
1093
1094         if (namebuf == NULL) return;
1095         strcpy(namebuf, "");
1096
1097         memset(&summ, 0, sizeof(summ));
1098         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1099
1100         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1101         serv_puts(buf);
1102         serv_getln(buf, sizeof buf);
1103         if (buf[0] != '1') return;
1104
1105         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1106                 if (!strncasecmp(buf, "part=", 5)) {
1107                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1108                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1109                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1110                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1111                         mime_length = extract_int(&buf[5], 5);
1112
1113                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1114                                 strcpy(vcard_partnum, mime_partnum);
1115                         }
1116
1117                 }
1118         }
1119
1120         if (strlen(vcard_partnum) > 0) {
1121                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1122                 if (vcard_source != NULL) {
1123
1124                         /* Grab the name off the card */
1125                         display_vcard(vcard_source, 0, 0, namebuf);
1126
1127                         free(vcard_source);
1128                 }
1129         }
1130
1131         lastfirst_firstlast(namebuf);
1132         striplt(namebuf);
1133         for (i=0; i<strlen(namebuf); ++i) {
1134                 if (namebuf[i] != ';') return;
1135         }
1136         strcpy(namebuf, "(no name)");
1137 }
1138
1139
1140
1141 /*
1142  * Record compare function for sorting address book indices
1143  */
1144 int abcmp(const void *ab1, const void *ab2) {
1145         return(strcasecmp(
1146                 (((const struct addrbookent *)ab1)->ab_name),
1147                 (((const struct addrbookent *)ab2)->ab_name)
1148         ));
1149 }
1150
1151
1152 /*
1153  * Helper function for do_addrbook_view()
1154  * Converts a name into a three-letter tab label
1155  */
1156 void nametab(char *tabbuf, char *name) {
1157         stresc(tabbuf, name, 0, 0);
1158         tabbuf[0] = toupper(tabbuf[0]);
1159         tabbuf[1] = tolower(tabbuf[1]);
1160         tabbuf[2] = tolower(tabbuf[2]);
1161         tabbuf[3] = 0;
1162 }
1163
1164
1165 /*
1166  * Render the address book using info we gathered during the scan
1167  */
1168 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
1169         int i = 0;
1170         int displayed = 0;
1171         int bg = 0;
1172         static int NAMESPERPAGE = 60;
1173         int num_pages = 0;
1174         int page = 0;
1175         int tabfirst = 0;
1176         char tabfirst_label[SIZ];
1177         int tablast = 0;
1178         char tablast_label[SIZ];
1179
1180         if (num_ab == 0) {
1181                 wprintf("<I>This address book is empty.</I>\n");
1182                 return;
1183         }
1184
1185         if (num_ab > 1) {
1186                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
1187         }
1188
1189         num_pages = num_ab / NAMESPERPAGE;
1190
1191         page = atoi(bstr("page"));
1192
1193         wprintf("Page: ");
1194         for (i=0; i<=num_pages; ++i) {
1195                 if (i != page) {
1196                         wprintf("<A HREF=\"/readfwd?page=%d\">", i);
1197                 }
1198                 else {
1199                         wprintf("<B>");
1200                 }
1201                 tabfirst = i * NAMESPERPAGE;
1202                 tablast = tabfirst + NAMESPERPAGE - 1;
1203                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1204                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1205                 nametab(tablast_label, addrbook[tablast].ab_name);
1206                 wprintf("[%s&nbsp;-&nbsp;%s]",
1207                         tabfirst_label, tablast_label
1208                 );
1209                 if (i != page) {
1210                         wprintf("</A>\n");
1211                 }
1212                 else {
1213                         wprintf("</B>\n");
1214                 }
1215         }
1216         wprintf("<br />\n");
1217
1218         wprintf("<TABLE border=0 cellspacing=0 "
1219                 "cellpadding=3 width=100%%>\n"
1220         );
1221
1222         for (i=0; i<num_ab; ++i) {
1223
1224                 if ((i / NAMESPERPAGE) == page) {
1225
1226                         if ((displayed % 4) == 0) {
1227                                 if (displayed > 0) {
1228                                         wprintf("</TR>\n");
1229                                 }
1230                                 bg = 1 - bg;
1231                                 wprintf("<TR BGCOLOR=\"#%s\">",
1232                                         (bg ? "DDDDDD" : "FFFFFF")
1233                                 );
1234                         }
1235         
1236                         wprintf("<TD>");
1237         
1238                         wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
1239                                 addrbook[i].ab_msgnum);
1240                         wprintf("?maxmsgs=1?summary=0?alpha=%s\">", bstr("alpha"));
1241                         vcard_n_prettyize(addrbook[i].ab_name);
1242                         escputs(addrbook[i].ab_name);
1243                         wprintf("</A></TD>\n");
1244                         ++displayed;
1245                 }
1246         }
1247
1248         wprintf("</TR></TABLE>\n");
1249 }
1250
1251
1252
1253 /* 
1254  * load message pointers from the server
1255  */
1256 int load_msg_ptrs(char *servcmd)
1257 {
1258         char buf[SIZ];
1259         int nummsgs;
1260         int maxload = 0;
1261
1262         nummsgs = 0;
1263         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1264         serv_puts(servcmd);
1265         serv_getln(buf, sizeof buf);
1266         if (buf[0] != '1') {
1267                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1268                 return (nummsgs);
1269         }
1270         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1271                 if (nummsgs < maxload) {
1272                         WC->msgarr[nummsgs] = atol(buf);
1273                         ++nummsgs;
1274                 }
1275         }
1276         return (nummsgs);
1277 }
1278
1279  
1280 int summcmp_subj(const void *s1, const void *s2) {
1281         struct message_summary *summ1;
1282         struct message_summary *summ2;
1283         
1284         summ1 = (struct message_summary *)s1;
1285         summ2 = (struct message_summary *)s2;
1286         return strcasecmp(summ1->subj, summ2->subj);
1287 }
1288
1289 int summcmp_rsubj(const void *s1, const void *s2) {
1290         struct message_summary *summ1;
1291         struct message_summary *summ2;
1292         
1293         summ1 = (struct message_summary *)s1;
1294         summ2 = (struct message_summary *)s2;
1295         return strcasecmp(summ2->subj, summ1->subj);
1296 }
1297
1298 int summcmp_sender(const void *s1, const void *s2) {
1299         struct message_summary *summ1;
1300         struct message_summary *summ2;
1301         
1302         summ1 = (struct message_summary *)s1;
1303         summ2 = (struct message_summary *)s2;
1304         return strcasecmp(summ1->from, summ2->from);
1305 }
1306
1307 int summcmp_rsender(const void *s1, const void *s2) {
1308         struct message_summary *summ1;
1309         struct message_summary *summ2;
1310         
1311         summ1 = (struct message_summary *)s1;
1312         summ2 = (struct message_summary *)s2;
1313         return strcasecmp(summ2->from, summ1->from);
1314 }
1315
1316 int summcmp_date(const void *s1, const void *s2) {
1317         struct message_summary *summ1;
1318         struct message_summary *summ2;
1319         
1320         summ1 = (struct message_summary *)s1;
1321         summ2 = (struct message_summary *)s2;
1322
1323         if (summ1->date < summ2->date) return -1;
1324         else if (summ1->date > summ2->date) return +1;
1325         else return 0;
1326 }
1327
1328 int summcmp_rdate(const void *s1, const void *s2) {
1329         struct message_summary *summ1;
1330         struct message_summary *summ2;
1331         
1332         summ1 = (struct message_summary *)s1;
1333         summ2 = (struct message_summary *)s2;
1334
1335         if (summ1->date < summ2->date) return +1;
1336         else if (summ1->date > summ2->date) return -1;
1337         else return 0;
1338 }
1339
1340 /*
1341  * command loop for reading messages
1342  */
1343 void readloop(char *oper)
1344 {
1345         char cmd[SIZ];
1346         char buf[SIZ];
1347         char old_msgs[SIZ];
1348         int is_new = 0;
1349         int a, b;
1350         int nummsgs;
1351         long startmsg;
1352         int maxmsgs;
1353         int num_displayed = 0;
1354         int is_summary = 0;
1355         int is_addressbook = 0;
1356         int is_singlecard = 0;
1357         int is_calendar = 0;
1358         int is_tasks = 0;
1359         int is_notes = 0;
1360         int remaining_messages;
1361         int lo, hi;
1362         int lowest_displayed = (-1);
1363         int highest_displayed = 0;
1364         long pn_previous = 0L;
1365         long pn_current = 0L;
1366         long pn_next = 0L;
1367         int bg = 0;
1368         struct addrbookent *addrbook = NULL;
1369         int num_ab = 0;
1370         char *sortby = NULL;
1371         char sortpref_name[128];
1372         char sortpref_value[128];
1373         char *subjsort_button;
1374         char *sendsort_button;
1375         char *datesort_button;
1376
1377         startmsg = atol(bstr("startmsg"));
1378         maxmsgs = atoi(bstr("maxmsgs"));
1379         is_summary = atoi(bstr("summary"));
1380         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1381
1382         snprintf(sortpref_name, sizeof sortpref_name, "sort %s", WC->wc_roomname);
1383         get_preference(sortpref_name, sortpref_value, sizeof sortpref_value);
1384
1385         sortby = bstr("sortby");
1386         if ( (strlen(sortby) > 0) && (strcasecmp(sortby, sortpref_value)) ) {
1387                 set_preference(sortpref_name, sortby, 1);
1388         }
1389         if (strlen(sortby) == 0) sortby = sortpref_value;
1390         if (strlen(sortby) == 0) sortby = "msgid";
1391
1392         output_headers(1, 1, 1, 0, 0, 0, 0);
1393
1394         /* When in summary mode, always show ALL messages instead of just
1395          * new or old.  Otherwise, show what the user asked for.
1396          */
1397         if (!strcmp(oper, "readnew")) {
1398                 strcpy(cmd, "MSGS NEW");
1399         }
1400         else if (!strcmp(oper, "readold")) {
1401                 strcpy(cmd, "MSGS OLD");
1402         }
1403         else {
1404                 strcpy(cmd, "MSGS ALL");
1405         }
1406
1407         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1408                 is_summary = 1;
1409                 strcpy(cmd, "MSGS ALL");
1410         }
1411
1412         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1413                 is_addressbook = 1;
1414                 strcpy(cmd, "MSGS ALL");
1415                 maxmsgs = 9999999;
1416         }
1417
1418         if (is_summary) {
1419                 strcpy(cmd, "MSGS ALL");
1420                 startmsg = 1;
1421                 maxmsgs = 9999999;
1422         }
1423
1424         /* Are we doing a summary view?  If so, we need to know old messages
1425          * and new messages, so we can do that pretty boldface thing for the
1426          * new messages.
1427          */
1428         strcpy(old_msgs, "");
1429         if (is_summary) {
1430                 serv_puts("GTSN");
1431                 serv_getln(buf, sizeof buf);
1432                 if (buf[0] == '2') {
1433                         strcpy(old_msgs, &buf[4]);
1434                 }
1435         }
1436
1437         is_singlecard = atoi(bstr("is_singlecard"));
1438
1439         if (WC->wc_view == VIEW_CALENDAR) {             /* calendar */
1440                 is_calendar = 1;
1441                 strcpy(cmd, "MSGS ALL");
1442                 maxmsgs = 32767;
1443         }
1444         if (WC->wc_view == VIEW_TASKS) {                /* tasks */
1445                 is_tasks = 1;
1446                 strcpy(cmd, "MSGS ALL");
1447                 maxmsgs = 32767;
1448         }
1449         if (WC->wc_view == VIEW_NOTES) {                /* notes */
1450                 is_notes = 1;
1451                 strcpy(cmd, "MSGS ALL");
1452                 maxmsgs = 32767;
1453         }
1454
1455         nummsgs = load_msg_ptrs(cmd);
1456         if (nummsgs == 0) {
1457
1458                 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1459                         if (!strcmp(oper, "readnew")) {
1460                                 wprintf("<EM>No new messages.</EM>\n");
1461                         } else if (!strcmp(oper, "readold")) {
1462                                 wprintf("<EM>No old messages.</EM>\n");
1463                         } else {
1464                                 wprintf("<EM>No messages here.</EM>\n");
1465                         }
1466                 }
1467
1468                 goto DONE;
1469         }
1470
1471         if (is_summary) {
1472                 if (WC->num_summ != 0) {
1473                         WC->num_summ = 0;
1474                         free(WC->summ);
1475                 }
1476                 WC->num_summ = nummsgs;
1477                 WC->summ = malloc(WC->num_summ*sizeof(struct message_summary));
1478                 for (a = 0; a < nummsgs; ++a) {
1479                         /* Gather summary information */
1480                         summarize_message(a, WC->msgarr[a], is_new);
1481
1482                         /* Are you a new message, or an old message? */
1483                         if (is_summary) {
1484                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1485                                         WC->summ[a].is_new = 0;
1486                                 }
1487                                 else {
1488                                         WC->summ[a].is_new = 1;
1489                                 }
1490                         }
1491                 }
1492         }
1493
1494         if (startmsg == 0L) startmsg = WC->msgarr[0];
1495         remaining_messages = 0;
1496
1497         for (a = 0; a < nummsgs; ++a) {
1498                 if (WC->msgarr[a] >= startmsg) {
1499                         ++remaining_messages;
1500                 }
1501         }
1502
1503         if (is_summary) {
1504                 if (!strcasecmp(sortby, "subject")) {
1505                         qsort(WC->summ, WC->num_summ,
1506                                 sizeof(struct message_summary), summcmp_subj);
1507                 }
1508                 else if (!strcasecmp(sortby, "rsubject")) {
1509                         qsort(WC->summ, WC->num_summ,
1510                                 sizeof(struct message_summary), summcmp_rsubj);
1511                 }
1512                 else if (!strcasecmp(sortby, "sender")) {
1513                         qsort(WC->summ, WC->num_summ,
1514                                 sizeof(struct message_summary), summcmp_sender);
1515                 }
1516                 else if (!strcasecmp(sortby, "rsender")) {
1517                         qsort(WC->summ, WC->num_summ,
1518                                 sizeof(struct message_summary), summcmp_rsender);
1519                 }
1520                 else if (!strcasecmp(sortby, "date")) {
1521                         qsort(WC->summ, WC->num_summ,
1522                                 sizeof(struct message_summary), summcmp_date);
1523                 }
1524                 else if (!strcasecmp(sortby, "rdate")) {
1525                         qsort(WC->summ, WC->num_summ,
1526                                 sizeof(struct message_summary), summcmp_rdate);
1527                 }
1528         }
1529
1530         if (!strcasecmp(sortby, "subject")) {
1531                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsubject\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1532         }
1533         else if (!strcasecmp(sortby, "rsubject")) {
1534                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1535         }
1536         else {
1537                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1538         }
1539
1540         if (!strcasecmp(sortby, "sender")) {
1541                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsender\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1542         }
1543         else if (!strcasecmp(sortby, "rsender")) {
1544                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1545         }
1546         else {
1547                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1548         }
1549
1550         if (!strcasecmp(sortby, "date")) {
1551                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1552         }
1553         else if (!strcasecmp(sortby, "rdate")) {
1554                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1555         }
1556         else {
1557                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1558         }
1559
1560         if (is_summary) {
1561                 wprintf("</div>");              /* end of 'content' div */
1562
1563                 wprintf("<div id=\"message_list\">"
1564
1565                         "<div id=\"fix_scrollbar_bug\">\n"
1566
1567                         "<form name=\"msgomatic\" "
1568                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n"
1569
1570                         "<table border=0 cellspacing=0 "
1571                         "cellpadding=0 width=100%%>\n"
1572                         "<TR>"
1573                         "<TD align=center><b><i>Subject</i></b> %s</TD>"
1574                         "<TD align=center><b><i>Sender</i></b> %s</TD>"
1575                         "<TD align=center><b><i>Date</i></b> %s</TD>"
1576                         "<TD><INPUT TYPE=\"submit\" NAME=\"sc\" "
1577                         "STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif;"
1578                         " font-size: 6pt;\" "
1579                         "VALUE=\"Delete\"></TD>"
1580                         "</TR>\n"
1581                         ,
1582                         subjsort_button,
1583                         sendsort_button,
1584                         datesort_button
1585                 );
1586         }
1587
1588         for (a = 0; a < nummsgs; ++a) {
1589                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1590
1591                         /* Learn which msgs "Prev" & "Next" buttons go to */
1592                         pn_current = WC->msgarr[a];
1593                         if (a > 0) pn_previous = WC->msgarr[a-1];
1594                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1595
1596                         /* If a tabular view, set up the line */
1597                         if (is_summary) {
1598                                 bg = 1 - bg;
1599                                 wprintf("<TR BGCOLOR=\"#%s\">",
1600                                         (bg ? "DDDDDD" : "FFFFFF")
1601                                 );
1602                         }
1603
1604                         /* Display the message */
1605                         if (is_summary) {
1606                                 display_summarized(a);
1607                         }
1608                         else if (is_addressbook) {
1609                                 fetch_ab_name(WC->msgarr[a], buf);
1610                                 ++num_ab;
1611                                 addrbook = realloc(addrbook,
1612                                         (sizeof(struct addrbookent) * num_ab) );
1613                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1614                                         sizeof(addrbook[num_ab-1].ab_name));
1615                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1616                         }
1617                         else if (is_calendar) {
1618                                 display_calendar(WC->msgarr[a]);
1619                         }
1620                         else if (is_tasks) {
1621                                 display_task(WC->msgarr[a]);
1622                         }
1623                         else if (is_notes) {
1624                                 display_note(WC->msgarr[a]);
1625                         }
1626                         else {
1627                                 read_message(WC->msgarr[a], 0);
1628                         }
1629
1630                         /* If a tabular view, finish the line */
1631                         if (is_summary) {
1632                                 wprintf("</TR>\n");
1633                         }
1634
1635                         if (lowest_displayed < 0) lowest_displayed = a;
1636                         highest_displayed = a;
1637
1638                         ++num_displayed;
1639                         --remaining_messages;
1640                 }
1641         }
1642
1643         if (is_summary) {
1644                 wprintf("</table></form>"
1645                         "</div>\n");                    /* end of 'fix_scrollbar_bug' div */
1646                 wprintf("</div>");                      /* end of 'message_list' div */
1647
1648                 wprintf("<div id=\"preview_pane\">");   /* The preview pane will initially be empty */
1649         }
1650
1651         /* Bump these because although we're thinking in zero base, the user
1652          * is a drooling idiot and is thinking in one base.
1653          */
1654         ++lowest_displayed;
1655         ++highest_displayed;
1656
1657         /* If we're only looking at one message, do a prev/next thing */
1658         if (num_displayed == 1) {
1659            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1660
1661                 wprintf("<div id=\"fix_scrollbar_bug\">"
1662                         "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>"
1663                         "Reading #%d of %d messages.</TD>\n"
1664                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1665                         lowest_displayed, nummsgs);
1666
1667                 if (pn_previous > 0L) {
1668                         wprintf("<A HREF=\"/%s"
1669                                 "?startmsg=%ld"
1670                                 "?maxmsgs=1"
1671                                 "?summary=0\">"
1672                                 "Previous</A> \n",
1673                                         oper,
1674                                         pn_previous );
1675                 }
1676
1677                 if (pn_next > 0L) {
1678                         wprintf("<A HREF=\"/%s"
1679                                 "?startmsg=%ld"
1680                                 "?maxmsgs=1"
1681                                 "?summary=0\">"
1682                                 "Next</A> \n",
1683                                         oper,
1684                                         pn_next );
1685                 }
1686
1687                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1688                         "?maxmsgs=%d?summary=1\">"
1689                         "Summary"
1690                         "</A>",
1691                         oper,
1692                         WC->msgarr[0],
1693                         DEFAULT_MAXMSGS
1694                 );
1695
1696                 wprintf("</td></tr></table></div>\n");
1697             }
1698         }
1699
1700         /*
1701          * If we're not currently looking at ALL requested
1702          * messages, then display the selector bar
1703          */
1704         if (num_displayed > 1) {
1705            if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1706               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
1707
1708                 wprintf("<form name=\"msgomatic\" "
1709                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n");
1710
1711                 wprintf("Reading #", lowest_displayed, highest_displayed);
1712
1713                 wprintf("<select name=\"whichones\" size=\"1\" "
1714                         "OnChange=\"location.href=msgomatic.whichones.options"
1715                         "[selectedIndex].value\">\n");
1716
1717                 for (b=0; b<nummsgs; b = b + maxmsgs) {
1718                 lo = b+1;
1719                 hi = b+maxmsgs;
1720                 if (hi > nummsgs) hi = nummsgs;
1721                         wprintf("<option %s value="
1722                                 "\"/%s"
1723                                 "?startmsg=%ld"
1724                                 "?maxmsgs=%d"
1725                                 "?summary=%d\">"
1726                                 "%d-%d</option> \n",
1727                                 ((WC->msgarr[b] == startmsg) ? "selected" : ""),
1728                                 oper,
1729                                 WC->msgarr[b],
1730                                 maxmsgs,
1731                                 is_summary,
1732                                 lo, hi);
1733                 }
1734                 wprintf("<option value=\"/%s?startmsg=%ld"
1735                         "?maxmsgs=9999999?summary=%d\">"
1736                         "ALL"
1737                         "</option> ",
1738                         oper,
1739                         WC->msgarr[0], is_summary);
1740
1741                 wprintf("</select> of %d messages.", nummsgs);
1742                 wprintf("</form>\n");
1743             }
1744         }
1745
1746 DONE:
1747         if (is_tasks) {
1748                 do_tasks_view();        /* Render the task list */
1749         }
1750
1751         if (is_calendar) {
1752                 do_calendar_view();     /* Render the calendar */
1753         }
1754
1755         if (is_addressbook) {
1756                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
1757         }
1758
1759         /* Put the data transfer hidden iframe in a hidden div, to make it *really* hidden */
1760         wprintf("</div>"
1761                 "<div display=\"hidden\">\n"
1762                 "<iframe name=\"msgloader1\" id=\"msgloader1\" width=\"1\"></iframe>\n"
1763         );
1764
1765         /* Note: wDumpContent() will output one additional </div> tag. */
1766         wDumpContent(1);
1767         if (addrbook != NULL) free(addrbook);
1768
1769         /* free the summary */
1770         if (WC->num_summ != 0) {
1771                 WC->num_summ = 0;
1772                 free(WC->summ);
1773         }
1774
1775         /* If we got here via a mailbox view and are reading a single
1776          * message, mark it as "seen." We do this after rendering the web page
1777          * so it doesn't keep the user waiting.
1778          */
1779         if ( (maxmsgs == 1) && (WC->wc_view == VIEW_MAILBOX) ) {
1780                 serv_printf("SEEN %ld|1", startmsg);
1781                 serv_getln(buf, sizeof buf);
1782         }
1783 }
1784
1785
1786 /*
1787  * Back end for post_message() ... this is where the actual message
1788  * gets transmitted to the server.
1789  */
1790 void post_mime_to_server(void) {
1791         char boundary[SIZ];
1792         int is_multipart = 0;
1793         static int seq = 0;
1794         struct wc_attachment *att;
1795         char *encoded;
1796         size_t encoded_length;
1797
1798         /* If there are attachments, we have to do multipart/mixed */
1799         if (WC->first_attachment != NULL) {
1800                 is_multipart = 1;
1801         }
1802
1803         if (is_multipart) {
1804                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1805                         serv_info.serv_fqdn,
1806                         getpid(),
1807                         ++seq
1808                 );
1809
1810                 /* Remember, serv_printf() appends an extra newline */
1811                 serv_printf("Content-type: multipart/mixed; "
1812                         "boundary=\"%s\"\n", boundary);
1813                 serv_printf("This is a multipart message in MIME format.\n");
1814                 serv_printf("--%s", boundary);
1815         }
1816
1817         serv_puts("Content-type: text/html; charset=utf-8");
1818         serv_puts("");
1819         serv_puts("<HTML><BODY>\n");
1820         text_to_server(bstr("msgtext"), 0);
1821         serv_puts("</BODY></HTML>\n");
1822         
1823
1824         if (is_multipart) {
1825
1826                 /* Add in the attachments */
1827                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1828
1829                         encoded_length = ((att->length * 150) / 100);
1830                         encoded = malloc(encoded_length);
1831                         if (encoded == NULL) break;
1832                         CtdlEncodeBase64(encoded, att->data, att->length);
1833
1834                         serv_printf("--%s", boundary);
1835                         serv_printf("Content-type: %s", att->content_type);
1836                         serv_printf("Content-disposition: attachment; "
1837                                 "filename=\"%s\"", att->filename);
1838                         serv_puts("Content-transfer-encoding: base64");
1839                         serv_puts("");
1840                         serv_write(encoded, strlen(encoded));
1841                         serv_puts("");
1842                         serv_puts("");
1843                         free(encoded);
1844                 }
1845                 serv_printf("--%s--", boundary);
1846         }
1847
1848         serv_puts("000");
1849 }
1850
1851
1852 /*
1853  * Post message (or don't post message)
1854  *
1855  * Note regarding the "dont_post" variable:
1856  * A random value (actually, it's just a timestamp) is inserted as a hidden
1857  * field called "postseq" when the display_enter page is generated.  This
1858  * value is checked when posting, using the static variable dont_post.  If a
1859  * user attempts to post twice using the same dont_post value, the message is
1860  * discarded.  This prevents the accidental double-saving of the same message
1861  * if the user happens to click the browser "back" button.
1862  */
1863 void post_message(void)
1864 {
1865         char buf[SIZ];
1866         static long dont_post = (-1L);
1867         struct wc_attachment *att, *aptr;
1868
1869         if (WC->upload_length > 0) {
1870
1871                 /* There's an attachment.  Save it to this struct... */
1872                 att = malloc(sizeof(struct wc_attachment));
1873                 memset(att, 0, sizeof(struct wc_attachment));
1874                 att->length = WC->upload_length;
1875                 strcpy(att->content_type, WC->upload_content_type);
1876                 strcpy(att->filename, WC->upload_filename);
1877                 att->next = NULL;
1878
1879                 /* And add it to the list. */
1880                 if (WC->first_attachment == NULL) {
1881                         WC->first_attachment = att;
1882                 }
1883                 else {
1884                         aptr = WC->first_attachment;
1885                         while (aptr->next != NULL) aptr = aptr->next;
1886                         aptr->next = att;
1887                 }
1888
1889                 /* Netscape sends a simple filename, which is what we want,
1890                  * but Satan's browser sends an entire pathname.  Reduce
1891                  * the path to just a filename if we need to.
1892                  */
1893                 while (num_tokens(att->filename, '/') > 1) {
1894                         remove_token(att->filename, 0, '/');
1895                 }
1896                 while (num_tokens(att->filename, '\\') > 1) {
1897                         remove_token(att->filename, 0, '\\');
1898                 }
1899
1900                 /* Transfer control of this memory from the upload struct
1901                  * to the attachment struct.
1902                  */
1903                 att->data = WC->upload;
1904                 WC->upload_length = 0;
1905                 WC->upload = NULL;
1906                 display_enter();
1907                 return;
1908         }
1909
1910         if (!strcasecmp(bstr("sc"), "Cancel")) {
1911                 sprintf(WC->ImportantMessage, 
1912                         "Cancelled.  Message was not posted.");
1913         } else if (!strcasecmp(bstr("attach"), "Add")) {
1914                 display_enter();
1915                 return;
1916         } else if (atol(bstr("postseq")) == dont_post) {
1917                 sprintf(WC->ImportantMessage, 
1918                         "Automatically cancelled because you have already "
1919                         "saved this message.");
1920         } else {
1921                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1922                         bstr("recp"),
1923                         bstr("subject") );
1924                 serv_puts(buf);
1925                 serv_getln(buf, sizeof buf);
1926                 if (buf[0] == '4') {
1927                         post_mime_to_server();
1928                         if (strlen(bstr("recp")) > 0) {
1929                                 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1930                         }
1931                         else {
1932                                 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1933                         }
1934                         dont_post = atol(bstr("postseq"));
1935                 } else {
1936                         sprintf(WC->ImportantMessage, 
1937                                 "%s", &buf[4]);
1938                 }
1939         }
1940
1941         free_attachments(WC);
1942         readloop("readnew");
1943 }
1944
1945
1946
1947
1948 /*
1949  * display the message entry screen
1950  */
1951 void display_enter(void)
1952 {
1953         char buf[SIZ];
1954         long now;
1955         struct wc_attachment *att;
1956
1957         if (strlen(bstr("force_room")) > 0) {
1958                 gotoroom(bstr("force_room"));
1959         }
1960
1961         /* Are we perhaps in an address book view?  If so, then an "enter
1962          * message" command really means "add new entry."
1963          */
1964         if (WC->wc_view == VIEW_ADDRESSBOOK) {
1965                 do_edit_vcard(-1, "", "");
1966                 return;
1967         }
1968
1969 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1970         /* Are we perhaps in a calendar view?  If so, then an "enter
1971          * message" command really means "add new calendar item."
1972          */
1973         if (WC->wc_view == VIEW_CALENDAR) {
1974                 display_edit_event();
1975                 return;
1976         }
1977
1978         /* Are we perhaps in a tasks view?  If so, then an "enter
1979          * message" command really means "add new task."
1980          */
1981         if (WC->wc_view == VIEW_TASKS) {
1982                 display_edit_task();
1983                 return;
1984         }
1985 #endif
1986
1987         /* Otherwise proceed normally.  Do a custom room banner with no navbar... */
1988         output_headers(1, 1, 2, 0, 0, 0, 0);
1989         wprintf("<div id=\"banner\">\n");
1990         embed_room_banner(NULL, navbar_none);
1991         wprintf("</div>\n");
1992         wprintf("<div id=\"content\">\n"
1993                 "<div id=\"fix_scrollbar_bug\">"
1994                 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1995
1996         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1997         serv_puts(buf);
1998         serv_getln(buf, sizeof buf);
1999
2000         if (!strncmp(buf, "570", 3)) {
2001                 if (strlen(bstr("recp")) > 0) {
2002                         svprintf("RECPERROR", WCS_STRING,
2003                                 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
2004                                 &buf[4]
2005                         );
2006                 }
2007                 do_template("prompt_for_recipient");
2008                 goto DONE;
2009         }
2010         if (buf[0] != '2') {
2011                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2012                 goto DONE;
2013         }
2014
2015         now = time(NULL);
2016         fmt_date(buf, now, 0);
2017         strcat(&buf[strlen(buf)], " <I>from</I> ");
2018         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
2019         if (strlen(bstr("recp")) > 0) {
2020                 strcat(&buf[strlen(buf)], " <I>to</I> ");
2021                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
2022         }
2023         strcat(&buf[strlen(buf)], " <I>in</I> ");
2024         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
2025
2026         /* begin message entry screen */
2027         // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
2028
2029         wprintf("<form enctype=\"multipart/form-data\" "
2030                 "method=\"POST\" action=\"/post\" "
2031                 "name=\"enterform\""
2032                 "onSubmit=\"return submitForm();\""
2033                 ">\n");
2034         wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
2035                 bstr("recp"));
2036         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
2037                 now);
2038
2039         wprintf("%s<br>\n", buf);       /* header bar */
2040         wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
2041                 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
2042         wprintf("<font size=-1>Subject (optional):</font>"
2043                 "<input type=\"text\" name=\"subject\" value=\"");
2044         escputs(bstr("subject"));
2045         wprintf("\" size=40 maxlength=70>"
2046                 "&nbsp;"
2047         );
2048
2049         wprintf("<input type=\"submit\" name=\"sc\" value=\"");
2050         if (strlen(bstr("recp")) > 0) {
2051                 wprintf("Send message");
2052         } else {
2053                 wprintf("Post message");
2054         }
2055         wprintf("\">&nbsp;"
2056                 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
2057
2058         wprintf("<center><script type=\"text/javascript\" "
2059                 "src=\"static/richtext.js\"></script>\n"
2060                 "<script type=\"text/javascript\">\n"
2061                 "function submitForm() { \n"
2062                 "  updateRTE('msgtext'); \n"
2063                 "  return true; \n"
2064                 "} \n"
2065                 "  \n"
2066                 "initRTE(\"static/\", \"static/\", \"\"); \n"
2067                 "</script> \n"
2068                 "<noscript>JavaScript must be enabled.</noscript> \n"
2069                 "<script type=\"text/javascript\"> \n"
2070                 "writeRichText('msgtext', '");
2071         msgescputs(bstr("msgtext"));
2072         wprintf("', '96%%', '200', true, false); \n"
2073                 "</script></center><br />\n");
2074
2075         /* Enumerate any attachments which are already in place... */
2076         wprintf("<img src=\"/static/diskette_24x.gif\" border=0 "
2077                 "align=middle height=16 width=16> Attachments: ");
2078         wprintf("<select name=\"which_attachment\" size=1>");
2079         for (att = WC->first_attachment; att != NULL; att = att->next) {
2080                 wprintf("<option value=\"");
2081                 urlescputs(att->filename);
2082                 wprintf("\">");
2083                 escputs(att->filename);
2084                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
2085                 wprintf("</option>\n");
2086         }
2087         wprintf("</select>");
2088
2089         /* Now offer the ability to attach additional files... */
2090         wprintf("&nbsp;&nbsp;&nbsp;"
2091                 "Attach file: <input NAME=\"attachfile\" "
2092                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
2093                 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
2094
2095         wprintf("</form>\n");
2096
2097         wprintf("</td></tr></table></div>\n");
2098 DONE:   wDumpContent(1);
2099 }
2100
2101
2102
2103
2104
2105
2106
2107
2108 void delete_msg(void)
2109 {
2110         long msgid;
2111         char buf[SIZ];
2112
2113         msgid = atol(bstr("msgid"));
2114
2115         output_headers(1, 1, 1, 0, 0, 0, 0);
2116
2117         sprintf(buf, "DELE %ld", msgid);
2118         serv_puts(buf);
2119         serv_getln(buf, sizeof buf);
2120         wprintf("<EM>%s</EM><br />\n", &buf[4]);
2121
2122         wDumpContent(1);
2123 }
2124
2125
2126
2127
2128 /*
2129  * Confirm move of a message
2130  */
2131 void confirm_move_msg(void)
2132 {
2133         long msgid;
2134         char buf[SIZ];
2135         char targ[SIZ];
2136
2137         msgid = atol(bstr("msgid"));
2138
2139         output_headers(1, 1, 1, 0, 0, 0, 0);
2140
2141         wprintf("<div id=\"fix_scrollbar_bug\">"
2142                 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
2143         wprintf("<font size=+1 color=\"#ffffff\"");
2144         wprintf("<b>Confirm move of message</b>\n");
2145         wprintf("</font></td></tr></table></div>\n");
2146
2147         wprintf("<CENTER>");
2148
2149         wprintf("Move this message to:<br />\n");
2150
2151         wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
2152         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
2153                 bstr("msgid"));
2154
2155
2156         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
2157         serv_puts("LKRA");
2158         serv_getln(buf, sizeof buf);
2159         if (buf[0] == '1') {
2160                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2161                         extract_token(targ, buf, 0, '|', sizeof targ);
2162                         wprintf("<OPTION>");
2163                         escputs(targ);
2164                         wprintf("\n");
2165                 }
2166         }
2167         wprintf("</SELECT>\n");
2168         wprintf("<br />\n");
2169
2170         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
2171         wprintf("&nbsp;");
2172         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
2173         wprintf("</form></CENTER>\n");
2174
2175         wprintf("</CENTER>\n");
2176         wDumpContent(1);
2177 }
2178
2179
2180
2181 void move_msg(void)
2182 {
2183         long msgid;
2184         char buf[SIZ];
2185
2186         msgid = atol(bstr("msgid"));
2187
2188         output_headers(1, 1, 1, 0, 0, 0, 0);
2189
2190         if (!strcasecmp(bstr("yesno"), "Move")) {
2191                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
2192                 serv_puts(buf);
2193                 serv_getln(buf, sizeof buf);
2194                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2195         } else {
2196                 wprintf("<EM>Message not moved.</EM><br />\n");
2197         }
2198
2199         wDumpContent(1);
2200 }
2201
2202 /*
2203  * This gets called when a user selects multiple messages in a summary
2204  * list and then clicks to perform a transformation of some sort on them
2205  * (such as deleting them).
2206  */
2207 void do_stuff_to_msgs(void) {
2208         char buf[SIZ];
2209         char sc[SIZ];
2210
2211         struct stuff_t {
2212                 struct stuff_t *next;
2213                 long msgnum;
2214         };
2215
2216         struct stuff_t *stuff = NULL;
2217         struct stuff_t *ptr;
2218
2219
2220         serv_puts("MSGS ALL");
2221         serv_getln(buf, sizeof buf);
2222
2223         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2224                 ptr = malloc(sizeof(struct stuff_t));
2225                 ptr->msgnum = atol(buf);
2226                 ptr->next = stuff;
2227                 stuff = ptr;
2228         }
2229
2230         strcpy(sc, bstr("sc"));
2231
2232         while (stuff != NULL) {
2233
2234                 sprintf(buf, "msg_%ld", stuff->msgnum);
2235                 if (!strcasecmp(bstr(buf), "yes")) {
2236
2237                         if (!strcasecmp(sc, "Delete")) {
2238                                 serv_printf("DELE %ld", stuff->msgnum);
2239                                 serv_getln(buf, sizeof buf);
2240                         }
2241
2242                 }
2243
2244                 ptr = stuff->next;
2245                 free(stuff);
2246                 stuff = ptr;
2247         }
2248
2249         readloop("readfwd");
2250 }