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