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