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