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