]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
* Awesome new 2-pane layout with ajax functionality for mailbox view.
[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) {
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         wprintf("<TD ALIGN=RIGHT>\n");
676
677         /* Reply */
678         wprintf("<a href=\"/display_enter?recp=");
679         urlescputs(reply_to);
680         wprintf("?subject=");
681         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
682         urlescputs(m_subject);
683         wprintf("\">[Reply]</a> ");
684
685         if (WC->is_room_aide)  {
686         
687                 /* Move */
688                 wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
689                         msgnum);
690
691                 /* Delete */
692                 wprintf("<a href=\"/delete_msg?msgid=%ld\" "
693                         "onClick=\"return confirm('Delete this message?');\">"
694                         "[Delete]</a>", msgnum);
695                         
696         }
697
698         wprintf("</TD></TR></TABLE>\n");
699
700         /* Begin body */
701         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
702                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
703
704         /* 
705          * Learn the content type
706          */
707         strcpy(mime_content_type, "text/plain");
708         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
709                 if (!strcmp(buf, "000")) {
710                         wprintf("<I>unexpected end of message</I><br /><br />\n");
711                         goto ENDBODY;
712                 }
713                 if (!strncasecmp(buf, "Content-type: ", 14)) {
714                         safestrncpy(mime_content_type, &buf[14],
715                                 sizeof(mime_content_type));
716                         for (i=0; i<strlen(mime_content_type); ++i) {
717                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
718                                         safestrncpy(mime_charset, &mime_content_type[i+8],
719                                                 sizeof mime_charset);
720                                 }
721                         }
722                         for (i=0; i<strlen(mime_content_type); ++i) {
723                                 if (mime_content_type[i] == ';') {
724                                         mime_content_type[i] = 0;
725                                 }
726                         }
727                 }
728         }
729
730         /* Set up a character set conversion if we need to (and if we can) */
731 #ifdef HAVE_ICONV
732         if ( (strcasecmp(mime_charset, "us-ascii"))
733            && (strcasecmp(mime_charset, "UTF-8")) ) {
734                 ic = iconv_open("UTF-8", mime_charset);
735                 if (ic == (iconv_t)(-1) ) {
736                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
737                 }
738         }
739 #endif
740
741         /* Messages in legacy Citadel variformat get handled thusly... */
742         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
743                 fmout(NULL, "JUSTIFY");
744         }
745
746         /* Boring old 80-column fixed format text gets handled this way... */
747         else if (!strcasecmp(mime_content_type, "text/plain")) {
748                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
749                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
750                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
751
752 #ifdef HAVE_ICONV
753                         if (ic != (iconv_t)(-1) ) {
754                                 ibuf = buf;
755                                 ibuflen = strlen(ibuf);
756                                 obuflen = SIZ;
757                                 obuf = (char *) malloc(obuflen);
758                                 osav = obuf;
759                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
760                                 osav[SIZ-obuflen] = 0;
761                                 safestrncpy(buf, osav, sizeof buf);
762                                 free(osav);
763                         }
764 #endif
765
766                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
767                                 buf[strlen(buf) - 1] = 0;
768                         if ((bq == 0) &&
769                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
770                                 wprintf("<BLOCKQUOTE>");
771                                 bq = 1;
772                         } else if ((bq == 1) &&
773                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
774                                 wprintf("</BLOCKQUOTE>");
775                                 bq = 0;
776                         }
777                         wprintf("<TT>");
778                         url(buf);
779                         escputs(buf);
780                         wprintf("</TT><br />\n");
781                 }
782                 wprintf("</I><br />");
783         }
784
785         else /* HTML is fun, but we've got to strip it first */
786         if (!strcasecmp(mime_content_type, "text/html")) {
787                 output_html(mime_charset);
788         }
789
790         /* Unknown weirdness */
791         else {
792                 wprintf("I don't know how to display %s<br />\n",
793                         mime_content_type);
794                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
795         }
796
797         /* Afterwards, offer links to download attachments 'n' such */
798         if (strlen(mime_http) > 0) {
799                 wprintf("%s", mime_http);
800         }
801
802         /* Handler for vCard parts */
803         if (strlen(vcard_partnum) > 0) {
804                 part_source = load_mimepart(msgnum, vcard_partnum);
805                 if (part_source != NULL) {
806
807                         /* If it's my vCard I can edit it */
808                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
809                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
810                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
811                         ) {
812                                 wprintf("<A HREF=\"/edit_vcard?"
813                                         "msgnum=%ld?partnum=%s\">",
814                                         msgnum, vcard_partnum);
815                                 wprintf("[edit]</A>");
816                         }
817
818                         /* In all cases, display the full card */
819                         display_vcard(part_source, 0, 1, NULL);
820                 }
821         }
822
823         /* Handler for calendar parts */
824         if (strlen(cal_partnum) > 0) {
825                 part_source = load_mimepart(msgnum, cal_partnum);
826                 if (part_source != NULL) {
827                         cal_process_attachment(part_source,
828                                                 msgnum, cal_partnum);
829                 }
830         }
831
832         if (part_source) {
833                 free(part_source);
834                 part_source = NULL;
835         }
836
837 ENDBODY:
838         wprintf("</TD></TR></TABLE>\n");
839
840         /* end everythingamundo table */
841         wprintf("</TD></TR></TABLE>\n");
842         wprintf("</div><br />\n");
843
844 #ifdef HAVE_ICONV
845         if (ic != (iconv_t)(-1) ) {
846                 iconv_close(ic);
847         }
848 #endif
849 }
850
851
852
853 /*
854  * Unadorned HTML output of an individual message, suitable
855  * for placing in a hidden iframe, for printing, or whatever
856  */
857 void embed_message(void) {
858         long msgnum = 0L;
859         char *sourceiframe;
860         char *targetdiv;
861
862         msgnum = atol(bstr("msgnum"));
863         sourceiframe = bstr("sourceiframe");
864         targetdiv = bstr("targetdiv");
865
866         output_headers(1, 0, 0, 0, 0, 1, 0);
867         begin_burst();
868
869         wprintf("<html><head>");
870
871         /* If we're loading into a hidden iframe, chances are the caller told us
872          * about a target div somewhere that we need to copy into when we're done.
873          */
874         if (strlen(targetdiv) > 0) wprintf(
875 "                                                                       \n"
876 " <script type=\"text/javascript\">                                     \n"
877 "       function loaded_now_copy_it() {                                 \n"
878 "               parent.document.getElementById(\"%s\").innerHTML = parent.frames['%s'].document.body.innerHTML; \n"
879 "       }                                                                                       \n"
880 "</script>\n",
881                 targetdiv,
882                 sourceiframe
883         );
884
885         wprintf("</head>");
886         wprintf("<body");
887         if (strlen(targetdiv) > 0) {
888                 wprintf(" onLoad='loaded_now_copy_it();'");
889         }
890         wprintf(">\n");
891         read_message(msgnum);
892         wprintf("</body></html>\n");
893         wDumpContent(0);
894 }
895
896
897
898
899 void display_summarized(int num) {
900         char datebuf[64];
901
902         wprintf("<TD>");
903         if (WC->summ[num].is_new) wprintf("<B>");
904         wprintf("<A HREF=\"/msg?msgnum=%ld?sourceiframe=msgloader1?targetdiv=preview_pane\" target=\"msgloader1\">",
905                 WC->summ[num].msgnum);
906         escputs(WC->summ[num].subj);
907         wprintf("</A>");
908         if (WC->summ[num].is_new) wprintf("</B>");
909         wprintf("</TD><TD>");
910         if (WC->summ[num].is_new) wprintf("<B>");
911         escputs(WC->summ[num].from);
912         if (WC->summ[num].is_new) wprintf("</B>");
913         wprintf(" </TD><TD>");
914         if (WC->summ[num].is_new) wprintf("<B>");
915         fmt_date(datebuf, WC->summ[num].date, 1);       /* brief */
916         escputs(datebuf);
917         if (WC->summ[num].is_new) wprintf("</B>");
918         wprintf(" </TD>");
919         wprintf("<TD>"
920                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
921                 "</TD>\n",
922                 WC->summ[num].msgnum
923         );
924 }
925
926
927 void summarize_message(int num, long msgnum, int is_new) {
928         char buf[SIZ];
929
930         memset(&WC->summ[num], 0, sizeof(struct message_summary));
931         safestrncpy(WC->summ[num].subj, "(no subject)", sizeof WC->summ[num].subj);
932         WC->summ[num].is_new = is_new;
933         WC->summ[num].msgnum = msgnum;
934
935         /* ask for headers only with no MIME */
936         sprintf(buf, "MSG0 %ld|3", msgnum);
937         serv_puts(buf);
938         serv_getln(buf, sizeof buf);
939         if (buf[0] != '1') return;
940
941         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
942                 if (!strncasecmp(buf, "from=", 5)) {
943                         safestrncpy(WC->summ[num].from, &buf[5], sizeof WC->summ[num].from);
944                 }
945                 if (!strncasecmp(buf, "subj=", 5)) {
946                         if (strlen(&buf[5]) > 0) {
947                                 safestrncpy(WC->summ[num].subj, &buf[5],
948                                         sizeof WC->summ[num].subj);
949 #ifdef HAVE_ICONV
950                                 /* Handle subjects with RFC2047 encoding */
951                                 utf8ify_rfc822_string(WC->summ[num].subj);
952 #endif
953                                 if (strlen(WC->summ[num].subj) > 75) {
954                                         strcpy(&WC->summ[num].subj[72], "...");
955                                 }
956                         }
957                 }
958
959                 if (!strncasecmp(buf, "node=", 5)) {
960                         if ( ((WC->room_flags & QR_NETWORK)
961                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
962                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
963                         ) {
964                                 strcat(WC->summ[num].from, " @ ");
965                                 strcat(WC->summ[num].from, &buf[5]);
966                         }
967                 }
968
969                 if (!strncasecmp(buf, "rcpt=", 5)) {
970                         safestrncpy(WC->summ[num].to, &buf[5], sizeof WC->summ[num].to);
971                 }
972
973                 if (!strncasecmp(buf, "time=", 5)) {
974                         WC->summ[num].date = atol(&buf[5]);
975                 }
976         }
977         
978 #ifdef HAVE_ICONV
979         /* Handle senders with RFC2047 encoding */
980         utf8ify_rfc822_string(WC->summ[num].from);
981 #endif
982         if (strlen(WC->summ[num].from) > 25) {
983                 strcpy(&WC->summ[num].from[22], "...");
984         }
985 }
986
987
988
989 void display_addressbook(long msgnum, char alpha) {
990         char buf[SIZ];
991         char mime_partnum[SIZ];
992         char mime_filename[SIZ];
993         char mime_content_type[SIZ];
994         char mime_disposition[SIZ];
995         int mime_length;
996         char vcard_partnum[SIZ];
997         char *vcard_source = NULL;
998         struct message_summary summ;
999
1000         memset(&summ, 0, sizeof(summ));
1001         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1002
1003         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1004         serv_puts(buf);
1005         serv_getln(buf, sizeof buf);
1006         if (buf[0] != '1') return;
1007
1008         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1009                 if (!strncasecmp(buf, "part=", 5)) {
1010                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1011                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1012                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1013                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1014                         mime_length = extract_int(&buf[5], 5);
1015
1016                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1017                                 strcpy(vcard_partnum, mime_partnum);
1018                         }
1019
1020                 }
1021         }
1022
1023         if (strlen(vcard_partnum) > 0) {
1024                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1025                 if (vcard_source != NULL) {
1026
1027                         /* Display the summary line */
1028                         display_vcard(vcard_source, alpha, 0, NULL);
1029
1030                         /* If it's my vCard I can edit it */
1031                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1032                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1033                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1034                         ) {
1035                                 wprintf("<A HREF=\"/edit_vcard?"
1036                                         "msgnum=%ld?partnum=%s\">",
1037                                         msgnum, vcard_partnum);
1038                                 wprintf("[edit]</A>");
1039                         }
1040
1041                         free(vcard_source);
1042                 }
1043         }
1044
1045 }
1046
1047
1048
1049 /* If it's an old "Firstname Lastname" style record, try to
1050  * convert it.
1051  */
1052 void lastfirst_firstlast(char *namebuf) {
1053         char firstname[SIZ];
1054         char lastname[SIZ];
1055         int i;
1056
1057         if (namebuf == NULL) return;
1058         if (strchr(namebuf, ';') != NULL) return;
1059
1060         i = num_tokens(namebuf, ' ');
1061         if (i < 2) return;
1062
1063         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1064         remove_token(namebuf, i-1, ' ');
1065         strcpy(firstname, namebuf);
1066         sprintf(namebuf, "%s; %s", lastname, firstname);
1067 }
1068
1069
1070 void fetch_ab_name(long msgnum, char *namebuf) {
1071         char buf[SIZ];
1072         char mime_partnum[SIZ];
1073         char mime_filename[SIZ];
1074         char mime_content_type[SIZ];
1075         char mime_disposition[SIZ];
1076         int mime_length;
1077         char vcard_partnum[SIZ];
1078         char *vcard_source = NULL;
1079         int i;
1080         struct message_summary summ;
1081
1082         if (namebuf == NULL) return;
1083         strcpy(namebuf, "");
1084
1085         memset(&summ, 0, sizeof(summ));
1086         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1087
1088         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1089         serv_puts(buf);
1090         serv_getln(buf, sizeof buf);
1091         if (buf[0] != '1') return;
1092
1093         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1094                 if (!strncasecmp(buf, "part=", 5)) {
1095                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1096                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1097                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1098                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1099                         mime_length = extract_int(&buf[5], 5);
1100
1101                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1102                                 strcpy(vcard_partnum, mime_partnum);
1103                         }
1104
1105                 }
1106         }
1107
1108         if (strlen(vcard_partnum) > 0) {
1109                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1110                 if (vcard_source != NULL) {
1111
1112                         /* Grab the name off the card */
1113                         display_vcard(vcard_source, 0, 0, namebuf);
1114
1115                         free(vcard_source);
1116                 }
1117         }
1118
1119         lastfirst_firstlast(namebuf);
1120         striplt(namebuf);
1121         for (i=0; i<strlen(namebuf); ++i) {
1122                 if (namebuf[i] != ';') return;
1123         }
1124         strcpy(namebuf, "(no name)");
1125 }
1126
1127
1128
1129 /*
1130  * Record compare function for sorting address book indices
1131  */
1132 int abcmp(const void *ab1, const void *ab2) {
1133         return(strcasecmp(
1134                 (((const struct addrbookent *)ab1)->ab_name),
1135                 (((const struct addrbookent *)ab2)->ab_name)
1136         ));
1137 }
1138
1139
1140 /*
1141  * Helper function for do_addrbook_view()
1142  * Converts a name into a three-letter tab label
1143  */
1144 void nametab(char *tabbuf, char *name) {
1145         stresc(tabbuf, name, 0, 0);
1146         tabbuf[0] = toupper(tabbuf[0]);
1147         tabbuf[1] = tolower(tabbuf[1]);
1148         tabbuf[2] = tolower(tabbuf[2]);
1149         tabbuf[3] = 0;
1150 }
1151
1152
1153 /*
1154  * Render the address book using info we gathered during the scan
1155  */
1156 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
1157         int i = 0;
1158         int displayed = 0;
1159         int bg = 0;
1160         static int NAMESPERPAGE = 60;
1161         int num_pages = 0;
1162         int page = 0;
1163         int tabfirst = 0;
1164         char tabfirst_label[SIZ];
1165         int tablast = 0;
1166         char tablast_label[SIZ];
1167
1168         if (num_ab == 0) {
1169                 wprintf("<I>This address book is empty.</I>\n");
1170                 return;
1171         }
1172
1173         if (num_ab > 1) {
1174                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
1175         }
1176
1177         num_pages = num_ab / NAMESPERPAGE;
1178
1179         page = atoi(bstr("page"));
1180
1181         wprintf("Page: ");
1182         for (i=0; i<=num_pages; ++i) {
1183                 if (i != page) {
1184                         wprintf("<A HREF=\"/readfwd?page=%d\">", i);
1185                 }
1186                 else {
1187                         wprintf("<B>");
1188                 }
1189                 tabfirst = i * NAMESPERPAGE;
1190                 tablast = tabfirst + NAMESPERPAGE - 1;
1191                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1192                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1193                 nametab(tablast_label, addrbook[tablast].ab_name);
1194                 wprintf("[%s&nbsp;-&nbsp;%s]",
1195                         tabfirst_label, tablast_label
1196                 );
1197                 if (i != page) {
1198                         wprintf("</A>\n");
1199                 }
1200                 else {
1201                         wprintf("</B>\n");
1202                 }
1203         }
1204         wprintf("<br />\n");
1205
1206         wprintf("<TABLE border=0 cellspacing=0 "
1207                 "cellpadding=3 width=100%%>\n"
1208         );
1209
1210         for (i=0; i<num_ab; ++i) {
1211
1212                 if ((i / NAMESPERPAGE) == page) {
1213
1214                         if ((displayed % 4) == 0) {
1215                                 if (displayed > 0) {
1216                                         wprintf("</TR>\n");
1217                                 }
1218                                 bg = 1 - bg;
1219                                 wprintf("<TR BGCOLOR=\"#%s\">",
1220                                         (bg ? "DDDDDD" : "FFFFFF")
1221                                 );
1222                         }
1223         
1224                         wprintf("<TD>");
1225         
1226                         wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
1227                                 addrbook[i].ab_msgnum);
1228                         wprintf("?maxmsgs=1?summary=0?alpha=%s\">", bstr("alpha"));
1229                         vcard_n_prettyize(addrbook[i].ab_name);
1230                         escputs(addrbook[i].ab_name);
1231                         wprintf("</A></TD>\n");
1232                         ++displayed;
1233                 }
1234         }
1235
1236         wprintf("</TR></TABLE>\n");
1237 }
1238
1239
1240
1241 /* 
1242  * load message pointers from the server
1243  */
1244 int load_msg_ptrs(char *servcmd)
1245 {
1246         char buf[SIZ];
1247         int nummsgs;
1248         int maxload = 0;
1249
1250         nummsgs = 0;
1251         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1252         serv_puts(servcmd);
1253         serv_getln(buf, sizeof buf);
1254         if (buf[0] != '1') {
1255                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1256                 return (nummsgs);
1257         }
1258         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1259                 if (nummsgs < maxload) {
1260                         WC->msgarr[nummsgs] = atol(buf);
1261                         ++nummsgs;
1262                 }
1263         }
1264         return (nummsgs);
1265 }
1266
1267  
1268 int summcmp_subj(const void *s1, const void *s2) {
1269         struct message_summary *summ1;
1270         struct message_summary *summ2;
1271         
1272         summ1 = (struct message_summary *)s1;
1273         summ2 = (struct message_summary *)s2;
1274         return strcasecmp(summ1->subj, summ2->subj);
1275 }
1276
1277 int summcmp_rsubj(const void *s1, const void *s2) {
1278         struct message_summary *summ1;
1279         struct message_summary *summ2;
1280         
1281         summ1 = (struct message_summary *)s1;
1282         summ2 = (struct message_summary *)s2;
1283         return strcasecmp(summ2->subj, summ1->subj);
1284 }
1285
1286 int summcmp_sender(const void *s1, const void *s2) {
1287         struct message_summary *summ1;
1288         struct message_summary *summ2;
1289         
1290         summ1 = (struct message_summary *)s1;
1291         summ2 = (struct message_summary *)s2;
1292         return strcasecmp(summ1->from, summ2->from);
1293 }
1294
1295 int summcmp_rsender(const void *s1, const void *s2) {
1296         struct message_summary *summ1;
1297         struct message_summary *summ2;
1298         
1299         summ1 = (struct message_summary *)s1;
1300         summ2 = (struct message_summary *)s2;
1301         return strcasecmp(summ2->from, summ1->from);
1302 }
1303
1304 int summcmp_date(const void *s1, const void *s2) {
1305         struct message_summary *summ1;
1306         struct message_summary *summ2;
1307         
1308         summ1 = (struct message_summary *)s1;
1309         summ2 = (struct message_summary *)s2;
1310
1311         if (summ1->date < summ2->date) return -1;
1312         else if (summ1->date > summ2->date) return +1;
1313         else return 0;
1314 }
1315
1316 int summcmp_rdate(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 /*
1329  * command loop for reading messages
1330  */
1331 void readloop(char *oper)
1332 {
1333         char cmd[SIZ];
1334         char buf[SIZ];
1335         char old_msgs[SIZ];
1336         int is_new = 0;
1337         int a, b;
1338         int nummsgs;
1339         long startmsg;
1340         int maxmsgs;
1341         int num_displayed = 0;
1342         int is_summary = 0;
1343         int is_addressbook = 0;
1344         int is_singlecard = 0;
1345         int is_calendar = 0;
1346         int is_tasks = 0;
1347         int is_notes = 0;
1348         int remaining_messages;
1349         int lo, hi;
1350         int lowest_displayed = (-1);
1351         int highest_displayed = 0;
1352         long pn_previous = 0L;
1353         long pn_current = 0L;
1354         long pn_next = 0L;
1355         int bg = 0;
1356         struct addrbookent *addrbook = NULL;
1357         int num_ab = 0;
1358         char *sortby = NULL;
1359         char sortpref_name[128];
1360         char sortpref_value[128];
1361         char *subjsort_button;
1362         char *sendsort_button;
1363         char *datesort_button;
1364
1365         startmsg = atol(bstr("startmsg"));
1366         maxmsgs = atoi(bstr("maxmsgs"));
1367         is_summary = atoi(bstr("summary"));
1368         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1369
1370         snprintf(sortpref_name, sizeof sortpref_name, "sort %s", WC->wc_roomname);
1371         get_preference(sortpref_name, sortpref_value, sizeof sortpref_value);
1372
1373         sortby = bstr("sortby");
1374         if ( (strlen(sortby) > 0) && (strcasecmp(sortby, sortpref_value)) ) {
1375                 set_preference(sortpref_name, sortby, 1);
1376         }
1377         if (strlen(sortby) == 0) sortby = sortpref_value;
1378         if (strlen(sortby) == 0) sortby = "msgid";
1379
1380         output_headers(1, 1, 1, 0, 0, 0, 0);
1381
1382         /* When in summary mode, always show ALL messages instead of just
1383          * new or old.  Otherwise, show what the user asked for.
1384          */
1385         if (!strcmp(oper, "readnew")) {
1386                 strcpy(cmd, "MSGS NEW");
1387         }
1388         else if (!strcmp(oper, "readold")) {
1389                 strcpy(cmd, "MSGS OLD");
1390         }
1391         else {
1392                 strcpy(cmd, "MSGS ALL");
1393         }
1394
1395         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1396                 is_summary = 1;
1397                 strcpy(cmd, "MSGS ALL");
1398         }
1399
1400         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1401                 is_addressbook = 1;
1402                 strcpy(cmd, "MSGS ALL");
1403                 maxmsgs = 9999999;
1404         }
1405
1406         if (is_summary) {
1407                 strcpy(cmd, "MSGS ALL");
1408                 startmsg = 1;
1409                 maxmsgs = 9999999;
1410         }
1411
1412         /* Are we doing a summary view?  If so, we need to know old messages
1413          * and new messages, so we can do that pretty boldface thing for the
1414          * new messages.
1415          */
1416         strcpy(old_msgs, "");
1417         if (is_summary) {
1418                 serv_puts("GTSN");
1419                 serv_getln(buf, sizeof buf);
1420                 if (buf[0] == '2') {
1421                         strcpy(old_msgs, &buf[4]);
1422                 }
1423         }
1424
1425         is_singlecard = atoi(bstr("is_singlecard"));
1426
1427         if (WC->wc_view == VIEW_CALENDAR) {             /* calendar */
1428                 is_calendar = 1;
1429                 strcpy(cmd, "MSGS ALL");
1430                 maxmsgs = 32767;
1431         }
1432         if (WC->wc_view == VIEW_TASKS) {                /* tasks */
1433                 is_tasks = 1;
1434                 strcpy(cmd, "MSGS ALL");
1435                 maxmsgs = 32767;
1436         }
1437         if (WC->wc_view == VIEW_NOTES) {                /* notes */
1438                 is_notes = 1;
1439                 strcpy(cmd, "MSGS ALL");
1440                 maxmsgs = 32767;
1441         }
1442
1443         nummsgs = load_msg_ptrs(cmd);
1444         if (nummsgs == 0) {
1445
1446                 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1447                         if (!strcmp(oper, "readnew")) {
1448                                 wprintf("<EM>No new messages.</EM>\n");
1449                         } else if (!strcmp(oper, "readold")) {
1450                                 wprintf("<EM>No old messages.</EM>\n");
1451                         } else {
1452                                 wprintf("<EM>No messages here.</EM>\n");
1453                         }
1454                 }
1455
1456                 goto DONE;
1457         }
1458
1459         if (is_summary) {
1460                 if (WC->num_summ != 0) {
1461                         WC->num_summ = 0;
1462                         free(WC->summ);
1463                 }
1464                 WC->num_summ = nummsgs;
1465                 WC->summ = malloc(WC->num_summ*sizeof(struct message_summary));
1466                 for (a = 0; a < nummsgs; ++a) {
1467                         /* Gather summary information */
1468                         summarize_message(a, WC->msgarr[a], is_new);
1469
1470                         /* Are you a new message, or an old message? */
1471                         if (is_summary) {
1472                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1473                                         WC->summ[a].is_new = 0;
1474                                 }
1475                                 else {
1476                                         WC->summ[a].is_new = 1;
1477                                 }
1478                         }
1479                 }
1480         }
1481
1482         if (startmsg == 0L) startmsg = WC->msgarr[0];
1483         remaining_messages = 0;
1484
1485         for (a = 0; a < nummsgs; ++a) {
1486                 if (WC->msgarr[a] >= startmsg) {
1487                         ++remaining_messages;
1488                 }
1489         }
1490
1491         if (is_summary) {
1492                 if (!strcasecmp(sortby, "subject")) {
1493                         qsort(WC->summ, WC->num_summ,
1494                                 sizeof(struct message_summary), summcmp_subj);
1495                 }
1496                 else if (!strcasecmp(sortby, "rsubject")) {
1497                         qsort(WC->summ, WC->num_summ,
1498                                 sizeof(struct message_summary), summcmp_rsubj);
1499                 }
1500                 else if (!strcasecmp(sortby, "sender")) {
1501                         qsort(WC->summ, WC->num_summ,
1502                                 sizeof(struct message_summary), summcmp_sender);
1503                 }
1504                 else if (!strcasecmp(sortby, "rsender")) {
1505                         qsort(WC->summ, WC->num_summ,
1506                                 sizeof(struct message_summary), summcmp_rsender);
1507                 }
1508                 else if (!strcasecmp(sortby, "date")) {
1509                         qsort(WC->summ, WC->num_summ,
1510                                 sizeof(struct message_summary), summcmp_date);
1511                 }
1512                 else if (!strcasecmp(sortby, "rdate")) {
1513                         qsort(WC->summ, WC->num_summ,
1514                                 sizeof(struct message_summary), summcmp_rdate);
1515                 }
1516         }
1517
1518         if (!strcasecmp(sortby, "subject")) {
1519                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsubject\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1520         }
1521         else if (!strcasecmp(sortby, "rsubject")) {
1522                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1523         }
1524         else {
1525                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1526         }
1527
1528         if (!strcasecmp(sortby, "sender")) {
1529                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsender\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1530         }
1531         else if (!strcasecmp(sortby, "rsender")) {
1532                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1533         }
1534         else {
1535                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1536         }
1537
1538         if (!strcasecmp(sortby, "date")) {
1539                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1540         }
1541         else if (!strcasecmp(sortby, "rdate")) {
1542                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1543         }
1544         else {
1545                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1546         }
1547
1548         if (is_summary) {
1549                 wprintf("</div>");              /* end of 'content' div */
1550
1551                 wprintf("<div id=\"message_list\">"
1552
1553                         "<div id=\"fix_scrollbar_bug\">\n"
1554                         "<table border=0 cellspacing=0 "
1555                         "cellpadding=0 width=100%%>\n"
1556                         "<TR>"
1557                         "<TD align=center><b><i>Subject</i></b> %s</TD>"
1558                         "<TD align=center><b><i>Sender</i></b> %s</TD>"
1559                         "<TD align=center><b><i>Date</i></b> %s</TD>"
1560                         "<TD></TD>"
1561                         "</TR>\n"
1562                         ,
1563                         subjsort_button,
1564                         sendsort_button,
1565                         datesort_button
1566                 );
1567         }
1568
1569         for (a = 0; a < nummsgs; ++a) {
1570                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1571
1572                         /* Learn which msgs "Prev" & "Next" buttons go to */
1573                         pn_current = WC->msgarr[a];
1574                         if (a > 0) pn_previous = WC->msgarr[a-1];
1575                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1576
1577                         /* If a tabular view, set up the line */
1578                         if (is_summary) {
1579                                 bg = 1 - bg;
1580                                 wprintf("<TR BGCOLOR=\"#%s\">",
1581                                         (bg ? "DDDDDD" : "FFFFFF")
1582                                 );
1583                         }
1584
1585                         /* Display the message */
1586                         if (is_summary) {
1587                                 display_summarized(a);
1588                         }
1589                         else if (is_addressbook) {
1590                                 fetch_ab_name(WC->msgarr[a], buf);
1591                                 ++num_ab;
1592                                 addrbook = realloc(addrbook,
1593                                         (sizeof(struct addrbookent) * num_ab) );
1594                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1595                                         sizeof(addrbook[num_ab-1].ab_name));
1596                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1597                         }
1598                         else if (is_calendar) {
1599                                 display_calendar(WC->msgarr[a]);
1600                         }
1601                         else if (is_tasks) {
1602                                 display_task(WC->msgarr[a]);
1603                         }
1604                         else if (is_notes) {
1605                                 display_note(WC->msgarr[a]);
1606                         }
1607                         else {
1608                                 read_message(WC->msgarr[a]);
1609                         }
1610
1611                         /* If a tabular view, finish the line */
1612                         if (is_summary) {
1613                                 wprintf("</TR>\n");
1614                         }
1615
1616                         if (lowest_displayed < 0) lowest_displayed = a;
1617                         highest_displayed = a;
1618
1619                         ++num_displayed;
1620                         --remaining_messages;
1621                 }
1622         }
1623
1624         if (is_summary) {
1625                 wprintf("</table></div>\n");            /* end of 'fix_scrollbar_bug' div */
1626                 wprintf("</div>");                      /* end of 'message_list' div */
1627
1628                 /* Put the data transfer hidden iframe in a hidden div, to make it *really* hidden */
1629                 wprintf("<div display=\"hidden\">\n"
1630                         "<iframe name=\"msgloader1\" id=\"msgloader1\" width=\"1\"></iframe>\n"
1631                         "</div>\n"
1632                 );
1633
1634                 wprintf("<div id=\"preview_pane\">");   /* The preview pane will initially be empty */
1635         }
1636
1637         /* Bump these because although we're thinking in zero base, the user
1638          * is a drooling idiot and is thinking in one base.
1639          */
1640         ++lowest_displayed;
1641         ++highest_displayed;
1642
1643         /* If we're only looking at one message, do a prev/next thing */
1644         if (num_displayed == 1) {
1645            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1646
1647                 wprintf("<div id=\"fix_scrollbar_bug\">"
1648                         "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>"
1649                         "Reading #%d of %d messages.</TD>\n"
1650                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1651                         lowest_displayed, nummsgs);
1652
1653                 if (pn_previous > 0L) {
1654                         wprintf("<A HREF=\"/%s"
1655                                 "?startmsg=%ld"
1656                                 "?maxmsgs=1"
1657                                 "?summary=0\">"
1658                                 "Previous</A> \n",
1659                                         oper,
1660                                         pn_previous );
1661                 }
1662
1663                 if (pn_next > 0L) {
1664                         wprintf("<A HREF=\"/%s"
1665                                 "?startmsg=%ld"
1666                                 "?maxmsgs=1"
1667                                 "?summary=0\">"
1668                                 "Next</A> \n",
1669                                         oper,
1670                                         pn_next );
1671                 }
1672
1673                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1674                         "?maxmsgs=%d?summary=1\">"
1675                         "Summary"
1676                         "</A>",
1677                         oper,
1678                         WC->msgarr[0],
1679                         DEFAULT_MAXMSGS
1680                 );
1681
1682                 wprintf("</td></tr></table></div>\n");
1683             }
1684         }
1685
1686         /*
1687          * If we're not currently looking at ALL requested
1688          * messages, then display the selector bar
1689          */
1690         if (num_displayed > 1) {
1691            if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1692               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
1693
1694                 wprintf("<form name=\"msgomatic\" "
1695                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n");
1696
1697                 wprintf("Reading #", lowest_displayed, highest_displayed);
1698
1699                 wprintf("<select name=\"whichones\" size=\"1\" "
1700                         "OnChange=\"location.href=msgomatic.whichones.options"
1701                         "[selectedIndex].value\">\n");
1702
1703                 for (b=0; b<nummsgs; b = b + maxmsgs) {
1704                 lo = b+1;
1705                 hi = b+maxmsgs;
1706                 if (hi > nummsgs) hi = nummsgs;
1707                         wprintf("<option %s value="
1708                                 "\"/%s"
1709                                 "?startmsg=%ld"
1710                                 "?maxmsgs=%d"
1711                                 "?summary=%d\">"
1712                                 "%d-%d</option> \n",
1713                                 ((WC->msgarr[b] == startmsg) ? "selected" : ""),
1714                                 oper,
1715                                 WC->msgarr[b],
1716                                 maxmsgs,
1717                                 is_summary,
1718                                 lo, hi);
1719                 }
1720                 wprintf("<option value=\"/%s?startmsg=%ld"
1721                         "?maxmsgs=9999999?summary=%d\">"
1722                         "ALL"
1723                         "</option> ",
1724                         oper,
1725                         WC->msgarr[0], is_summary);
1726
1727                 wprintf("</select> of %d messages.", nummsgs);
1728                 wprintf("</form>\n");
1729             }
1730         }
1731
1732 DONE:
1733         if (is_tasks) {
1734                 do_tasks_view();        /* Render the task list */
1735         }
1736
1737         if (is_calendar) {
1738                 do_calendar_view();     /* Render the calendar */
1739         }
1740
1741         if (is_addressbook) {
1742                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
1743         }
1744
1745         /* Note: wDumpContent() will output one additional </div> tag.
1746          * Which div it is the end of depends on what mode we're viewing in.
1747          * If we're in summary mode, it's the end of the preview pane.
1748          * Otherwise it's the end of the usual content div.
1749          */
1750         wDumpContent(1);
1751         if (addrbook != NULL) free(addrbook);
1752
1753         /* free the summary */
1754         if (WC->num_summ != 0) {
1755                 WC->num_summ = 0;
1756                 free(WC->summ);
1757         }
1758
1759         /* If we got here via a mailbox view and are reading a single
1760          * message, mark it as "seen." We do this after rendering the web page
1761          * so it doesn't keep the user waiting.
1762          */
1763         if ( (maxmsgs == 1) && (WC->wc_view == VIEW_MAILBOX) ) {
1764                 serv_printf("SEEN %ld|1", startmsg);
1765                 serv_getln(buf, sizeof buf);
1766         }
1767 }
1768
1769
1770 /*
1771  * Back end for post_message() ... this is where the actual message
1772  * gets transmitted to the server.
1773  */
1774 void post_mime_to_server(void) {
1775         char boundary[SIZ];
1776         int is_multipart = 0;
1777         static int seq = 0;
1778         struct wc_attachment *att;
1779         char *encoded;
1780         size_t encoded_length;
1781
1782         /* If there are attachments, we have to do multipart/mixed */
1783         if (WC->first_attachment != NULL) {
1784                 is_multipart = 1;
1785         }
1786
1787         if (is_multipart) {
1788                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1789                         serv_info.serv_fqdn,
1790                         getpid(),
1791                         ++seq
1792                 );
1793
1794                 /* Remember, serv_printf() appends an extra newline */
1795                 serv_printf("Content-type: multipart/mixed; "
1796                         "boundary=\"%s\"\n", boundary);
1797                 serv_printf("This is a multipart message in MIME format.\n");
1798                 serv_printf("--%s", boundary);
1799         }
1800
1801         serv_puts("Content-type: text/html; charset=utf-8");
1802         serv_puts("");
1803         serv_puts("<HTML><BODY>\n");
1804         text_to_server(bstr("msgtext"), 0);
1805         serv_puts("</BODY></HTML>\n");
1806         
1807
1808         if (is_multipart) {
1809
1810                 /* Add in the attachments */
1811                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1812
1813                         encoded_length = ((att->length * 150) / 100);
1814                         encoded = malloc(encoded_length);
1815                         if (encoded == NULL) break;
1816                         CtdlEncodeBase64(encoded, att->data, att->length);
1817
1818                         serv_printf("--%s", boundary);
1819                         serv_printf("Content-type: %s", att->content_type);
1820                         serv_printf("Content-disposition: attachment; "
1821                                 "filename=\"%s\"", att->filename);
1822                         serv_puts("Content-transfer-encoding: base64");
1823                         serv_puts("");
1824                         serv_write(encoded, strlen(encoded));
1825                         serv_puts("");
1826                         serv_puts("");
1827                         free(encoded);
1828                 }
1829                 serv_printf("--%s--", boundary);
1830         }
1831
1832         serv_puts("000");
1833 }
1834
1835
1836 /*
1837  * Post message (or don't post message)
1838  *
1839  * Note regarding the "dont_post" variable:
1840  * A random value (actually, it's just a timestamp) is inserted as a hidden
1841  * field called "postseq" when the display_enter page is generated.  This
1842  * value is checked when posting, using the static variable dont_post.  If a
1843  * user attempts to post twice using the same dont_post value, the message is
1844  * discarded.  This prevents the accidental double-saving of the same message
1845  * if the user happens to click the browser "back" button.
1846  */
1847 void post_message(void)
1848 {
1849         char buf[SIZ];
1850         static long dont_post = (-1L);
1851         struct wc_attachment *att, *aptr;
1852
1853         if (WC->upload_length > 0) {
1854
1855                 /* There's an attachment.  Save it to this struct... */
1856                 att = malloc(sizeof(struct wc_attachment));
1857                 memset(att, 0, sizeof(struct wc_attachment));
1858                 att->length = WC->upload_length;
1859                 strcpy(att->content_type, WC->upload_content_type);
1860                 strcpy(att->filename, WC->upload_filename);
1861                 att->next = NULL;
1862
1863                 /* And add it to the list. */
1864                 if (WC->first_attachment == NULL) {
1865                         WC->first_attachment = att;
1866                 }
1867                 else {
1868                         aptr = WC->first_attachment;
1869                         while (aptr->next != NULL) aptr = aptr->next;
1870                         aptr->next = att;
1871                 }
1872
1873                 /* Netscape sends a simple filename, which is what we want,
1874                  * but Satan's browser sends an entire pathname.  Reduce
1875                  * the path to just a filename if we need to.
1876                  */
1877                 while (num_tokens(att->filename, '/') > 1) {
1878                         remove_token(att->filename, 0, '/');
1879                 }
1880                 while (num_tokens(att->filename, '\\') > 1) {
1881                         remove_token(att->filename, 0, '\\');
1882                 }
1883
1884                 /* Transfer control of this memory from the upload struct
1885                  * to the attachment struct.
1886                  */
1887                 att->data = WC->upload;
1888                 WC->upload_length = 0;
1889                 WC->upload = NULL;
1890                 display_enter();
1891                 return;
1892         }
1893
1894         if (!strcasecmp(bstr("sc"), "Cancel")) {
1895                 sprintf(WC->ImportantMessage, 
1896                         "Cancelled.  Message was not posted.");
1897         } else if (!strcasecmp(bstr("attach"), "Add")) {
1898                 display_enter();
1899                 return;
1900         } else if (atol(bstr("postseq")) == dont_post) {
1901                 sprintf(WC->ImportantMessage, 
1902                         "Automatically cancelled because you have already "
1903                         "saved this message.");
1904         } else {
1905                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1906                         bstr("recp"),
1907                         bstr("subject") );
1908                 serv_puts(buf);
1909                 serv_getln(buf, sizeof buf);
1910                 if (buf[0] == '4') {
1911                         post_mime_to_server();
1912                         if (strlen(bstr("recp")) > 0) {
1913                                 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1914                         }
1915                         else {
1916                                 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1917                         }
1918                         dont_post = atol(bstr("postseq"));
1919                 } else {
1920                         sprintf(WC->ImportantMessage, 
1921                                 "%s", &buf[4]);
1922                 }
1923         }
1924
1925         free_attachments(WC);
1926         readloop("readnew");
1927 }
1928
1929
1930
1931
1932 /*
1933  * display the message entry screen
1934  */
1935 void display_enter(void)
1936 {
1937         char buf[SIZ];
1938         long now;
1939         struct wc_attachment *att;
1940
1941         if (strlen(bstr("force_room")) > 0) {
1942                 gotoroom(bstr("force_room"));
1943         }
1944
1945         /* Are we perhaps in an address book view?  If so, then an "enter
1946          * message" command really means "add new entry."
1947          */
1948         if (WC->wc_view == VIEW_ADDRESSBOOK) {
1949                 do_edit_vcard(-1, "", "");
1950                 return;
1951         }
1952
1953 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1954         /* Are we perhaps in a calendar view?  If so, then an "enter
1955          * message" command really means "add new calendar item."
1956          */
1957         if (WC->wc_view == VIEW_CALENDAR) {
1958                 display_edit_event();
1959                 return;
1960         }
1961
1962         /* Are we perhaps in a tasks view?  If so, then an "enter
1963          * message" command really means "add new task."
1964          */
1965         if (WC->wc_view == VIEW_TASKS) {
1966                 display_edit_task();
1967                 return;
1968         }
1969 #endif
1970
1971         /* Otherwise proceed normally.  Do a custom room banner with no navbar... */
1972         output_headers(1, 1, 2, 0, 0, 0, 0);
1973         wprintf("<div id=\"banner\">\n");
1974         embed_room_banner(NULL, navbar_none);
1975         wprintf("</div>\n");
1976         wprintf("<div id=\"content\">\n"
1977                 "<div id=\"fix_scrollbar_bug\">"
1978                 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1979
1980         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1981         serv_puts(buf);
1982         serv_getln(buf, sizeof buf);
1983
1984         if (!strncmp(buf, "570", 3)) {
1985                 if (strlen(bstr("recp")) > 0) {
1986                         svprintf("RECPERROR", WCS_STRING,
1987                                 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1988                                 &buf[4]
1989                         );
1990                 }
1991                 do_template("prompt_for_recipient");
1992                 goto DONE;
1993         }
1994         if (buf[0] != '2') {
1995                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1996                 goto DONE;
1997         }
1998
1999         now = time(NULL);
2000         fmt_date(buf, now, 0);
2001         strcat(&buf[strlen(buf)], " <I>from</I> ");
2002         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
2003         if (strlen(bstr("recp")) > 0) {
2004                 strcat(&buf[strlen(buf)], " <I>to</I> ");
2005                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
2006         }
2007         strcat(&buf[strlen(buf)], " <I>in</I> ");
2008         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
2009
2010         /* begin message entry screen */
2011         // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
2012
2013         wprintf("<form enctype=\"multipart/form-data\" "
2014                 "method=\"POST\" action=\"/post\" "
2015                 "name=\"enterform\""
2016                 "onSubmit=\"return submitForm();\""
2017                 ">\n");
2018         wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
2019                 bstr("recp"));
2020         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
2021                 now);
2022
2023         wprintf("%s<br>\n", buf);       /* header bar */
2024         wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
2025                 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
2026         wprintf("<font size=-1>Subject (optional):</font>"
2027                 "<input type=\"text\" name=\"subject\" value=\"");
2028         escputs(bstr("subject"));
2029         wprintf("\" size=40 maxlength=70>"
2030                 "&nbsp;"
2031         );
2032
2033         wprintf("<input type=\"submit\" name=\"sc\" value=\"");
2034         if (strlen(bstr("recp")) > 0) {
2035                 wprintf("Send message");
2036         } else {
2037                 wprintf("Post message");
2038         }
2039         wprintf("\">&nbsp;"
2040                 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
2041
2042         wprintf("<center><script type=\"text/javascript\" "
2043                 "src=\"static/richtext.js\"></script>\n"
2044                 "<script type=\"text/javascript\">\n"
2045                 "function submitForm() { \n"
2046                 "  updateRTE('msgtext'); \n"
2047                 "  return true; \n"
2048                 "} \n"
2049                 "  \n"
2050                 "initRTE(\"static/\", \"static/\", \"\"); \n"
2051                 "</script> \n"
2052                 "<noscript>JavaScript must be enabled.</noscript> \n"
2053                 "<script type=\"text/javascript\"> \n"
2054                 "writeRichText('msgtext', '");
2055         msgescputs(bstr("msgtext"));
2056         wprintf("', '96%%', '200', true, false); \n"
2057                 "</script></center><br />\n");
2058
2059         /* Enumerate any attachments which are already in place... */
2060         wprintf("<img src=\"/static/diskette_24x.gif\" border=0 "
2061                 "align=middle height=16 width=16> Attachments: ");
2062         wprintf("<select name=\"which_attachment\" size=1>");
2063         for (att = WC->first_attachment; att != NULL; att = att->next) {
2064                 wprintf("<option value=\"");
2065                 urlescputs(att->filename);
2066                 wprintf("\">");
2067                 escputs(att->filename);
2068                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
2069                 wprintf("</option>\n");
2070         }
2071         wprintf("</select>");
2072
2073         /* Now offer the ability to attach additional files... */
2074         wprintf("&nbsp;&nbsp;&nbsp;"
2075                 "Attach file: <input NAME=\"attachfile\" "
2076                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
2077                 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
2078
2079         wprintf("</form>\n");
2080
2081         wprintf("</td></tr></table></div>\n");
2082 DONE:   wDumpContent(1);
2083 }
2084
2085
2086
2087
2088
2089
2090
2091
2092 void delete_msg(void)
2093 {
2094         long msgid;
2095         char buf[SIZ];
2096
2097         msgid = atol(bstr("msgid"));
2098
2099         output_headers(1, 1, 1, 0, 0, 0, 0);
2100
2101         sprintf(buf, "DELE %ld", msgid);
2102         serv_puts(buf);
2103         serv_getln(buf, sizeof buf);
2104         wprintf("<EM>%s</EM><br />\n", &buf[4]);
2105
2106         wDumpContent(1);
2107 }
2108
2109
2110
2111
2112 /*
2113  * Confirm move of a message
2114  */
2115 void confirm_move_msg(void)
2116 {
2117         long msgid;
2118         char buf[SIZ];
2119         char targ[SIZ];
2120
2121         msgid = atol(bstr("msgid"));
2122
2123         output_headers(1, 1, 1, 0, 0, 0, 0);
2124
2125         wprintf("<div id=\"fix_scrollbar_bug\">"
2126                 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
2127         wprintf("<font size=+1 color=\"#ffffff\"");
2128         wprintf("<b>Confirm move of message</b>\n");
2129         wprintf("</font></td></tr></table></div>\n");
2130
2131         wprintf("<CENTER>");
2132
2133         wprintf("Move this message to:<br />\n");
2134
2135         wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
2136         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
2137                 bstr("msgid"));
2138
2139
2140         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
2141         serv_puts("LKRA");
2142         serv_getln(buf, sizeof buf);
2143         if (buf[0] == '1') {
2144                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2145                         extract_token(targ, buf, 0, '|', sizeof targ);
2146                         wprintf("<OPTION>");
2147                         escputs(targ);
2148                         wprintf("\n");
2149                 }
2150         }
2151         wprintf("</SELECT>\n");
2152         wprintf("<br />\n");
2153
2154         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
2155         wprintf("&nbsp;");
2156         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
2157         wprintf("</form></CENTER>\n");
2158
2159         wprintf("</CENTER>\n");
2160         wDumpContent(1);
2161 }
2162
2163
2164
2165 void move_msg(void)
2166 {
2167         long msgid;
2168         char buf[SIZ];
2169
2170         msgid = atol(bstr("msgid"));
2171
2172         output_headers(1, 1, 1, 0, 0, 0, 0);
2173
2174         if (!strcasecmp(bstr("yesno"), "Move")) {
2175                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
2176                 serv_puts(buf);
2177                 serv_getln(buf, sizeof buf);
2178                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2179         } else {
2180                 wprintf("<EM>Message not moved.</EM><br />\n");
2181         }
2182
2183         wDumpContent(1);
2184 }
2185
2186 /*
2187  * This gets called when a user selects multiple messages in a summary
2188  * list and then clicks to perform a transformation of some sort on them
2189  * (such as deleting them).
2190  */
2191 void do_stuff_to_msgs(void) {
2192         char buf[SIZ];
2193         char sc[SIZ];
2194
2195         struct stuff_t {
2196                 struct stuff_t *next;
2197                 long msgnum;
2198         };
2199
2200         struct stuff_t *stuff = NULL;
2201         struct stuff_t *ptr;
2202
2203
2204         serv_puts("MSGS ALL");
2205         serv_getln(buf, sizeof buf);
2206
2207         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2208                 ptr = malloc(sizeof(struct stuff_t));
2209                 ptr->msgnum = atol(buf);
2210                 ptr->next = stuff;
2211                 stuff = ptr;
2212         }
2213
2214         strcpy(sc, bstr("sc"));
2215
2216         while (stuff != NULL) {
2217
2218                 sprintf(buf, "msg_%ld", stuff->msgnum);
2219                 if (!strcasecmp(bstr(buf), "yes")) {
2220
2221                         if (!strcasecmp(sc, "Delete selected")) {
2222                                 serv_printf("DELE %ld", stuff->msgnum);
2223                                 serv_getln(buf, sizeof buf);
2224                         }
2225
2226                 }
2227
2228                 ptr = stuff->next;
2229                 free(stuff);
2230                 stuff = ptr;
2231         }
2232
2233         readloop("readfwd");
2234 }