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