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