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