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