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