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