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