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