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