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