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