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