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