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