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