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