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