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