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