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