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