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