]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
* Save/Cancel buttons are now at the top *and* bottom of the message
[citadel.git] / webcit / messages.c
1 /*
2  * $Id$
3  *
4  * Functions which deal with the fetching and displaying of messages.
5  *
6  */
7
8 #include "webcit.h"
9 #include "vcard.h"
10 #include "webserver.h"
11
12
13 /* Address book entry (keep it short and sweet, it's just a quickie lookup
14  * which we can use to get to the real meat and bones later)
15  */
16 struct addrbookent {
17         char ab_name[64];
18         long ab_msgnum;
19 };
20
21
22
23 #ifdef HAVE_ICONV
24 /* Handle subjects with RFC2047 encoding, such as:
25  * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
26  */
27 void utf8ify_rfc822_string(char *buf) {
28         char *start, *end;
29         char newbuf[1024];
30         char charset[128];
31         char encoding[16];
32         char istr[1024];
33         iconv_t ic = (iconv_t)(-1) ;
34         char *ibuf;                   /* Buffer of characters to be converted */
35         char *obuf;                   /* Buffer for converted characters      */
36         size_t ibuflen;               /* Length of input buffer               */
37         size_t obuflen;               /* Length of output buffer              */
38         char *isav;                   /* Saved pointer to input buffer        */
39         char *osav;                   /* Saved pointer to output buffer       */
40
41         while (start=strstr(buf, "=?"), end=strstr(buf, "?="),
42                 ((start != NULL) && (end != NULL) && (end > start)) )
43         {
44                 extract_token(charset, start, 1, '?', sizeof charset);
45                 extract_token(encoding, start, 2, '?', sizeof encoding);
46                 extract_token(istr, start, 3, '?', sizeof istr);
47
48                 /*strcpy(start, "");
49                 ++end;
50                 ++end;*/
51
52                 ibuf = malloc(1024);
53                 isav = ibuf;
54                 if (!strcasecmp(encoding, "B")) {       /* base64 */
55                         ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
56                 }
57                 else if (!strcasecmp(encoding, "Q")) {  /* quoted-printable */
58                         ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, strlen(istr));
59                 }
60                 else {
61                         strcpy(ibuf, istr);             /* huh? */
62                         ibuflen = strlen(istr);
63                 }
64
65                 ic = iconv_open("UTF-8", charset);
66                 if (ic != (iconv_t)(-1) ) {
67                         obuf = malloc(1024);
68                         obuflen = 1024;
69                         obuf = (char *) malloc(obuflen);
70                         osav = obuf;
71                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
72                         osav[1024-obuflen] = 0;
73
74                         end = start;
75                         end++;
76                         strcpy(start, "");
77                         remove_token(end, 0, '?');
78                         remove_token(end, 0, '?');
79                         remove_token(end, 0, '?');
80                         remove_token(end, 0, '?');
81                         strcpy(end, &end[1]);
82
83                         snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
84                         strcpy(buf, newbuf);
85                         free(osav);
86                         iconv_close(ic);
87                 }
88                 else {
89                         snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
90                         strcpy(buf, newbuf);
91                 }
92
93                 free(isav);
94         }
95
96 }
97 #endif
98
99
100 /*
101  * Look for URL's embedded in a buffer and make them linkable.  We use a
102  * target window in order to keep the BBS session in its own window.
103  */
104 void url(buf)
105 char buf[];
106 {
107
108         int pos;
109         int start, end;
110         char ench;
111         char urlbuf[SIZ];
112         char outbuf[1024];
113
114         start = (-1);
115         end = strlen(buf);
116         ench = 0;
117
118         for (pos = 0; pos < strlen(buf); ++pos) {
119                 if (!strncasecmp(&buf[pos], "http://", 7))
120                         start = pos;
121                 if (!strncasecmp(&buf[pos], "ftp://", 6))
122                         start = pos;
123         }
124
125         if (start < 0)
126                 return;
127
128         if ((start > 0) && (buf[start - 1] == '<'))
129                 ench = '>';
130         if ((start > 0) && (buf[start - 1] == '['))
131                 ench = ']';
132         if ((start > 0) && (buf[start - 1] == '('))
133                 ench = ')';
134         if ((start > 0) && (buf[start - 1] == '{'))
135                 ench = '}';
136
137         for (pos = strlen(buf); pos > start; --pos) {
138                 if ((buf[pos] == ' ') || (buf[pos] == ench))
139                         end = pos;
140         }
141
142         strncpy(urlbuf, &buf[start], end - start);
143         urlbuf[end - start] = 0;
144
145         strncpy(outbuf, buf, start);
146         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
147                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
148         strcat(outbuf, &buf[end]);
149         if ( strlen(outbuf) < 250 )
150                 strcpy(buf, outbuf);
151 }
152
153
154 /*
155  * Turn a vCard "n" (name) field into something displayable.
156  */
157 void vcard_n_prettyize(char *name)
158 {
159         char *original_name;
160         int i;
161
162         original_name = strdup(name);
163         for (i=0; i<5; ++i) {
164                 if (strlen(original_name) > 0) {
165                         if (original_name[strlen(original_name)-1] == ' ') {
166                                 original_name[strlen(original_name)-1] = 0;
167                         }
168                         if (original_name[strlen(original_name)-1] == ';') {
169                                 original_name[strlen(original_name)-1] = 0;
170                         }
171                 }
172         }
173         strcpy(name, "");
174         for (i=0; i<strlen(original_name); ++i) {
175                 if (original_name[i] == ';') {
176                         strcat(name, ", ");
177                 }
178                 else {
179                         name[strlen(name)+1] = 0;
180                         name[strlen(name)] = original_name[i];
181                 }
182         }
183         free(original_name);
184 }
185
186
187
188
189 /* display_vcard() calls this after parsing the textual vCard into
190  * our 'struct vCard' data object.
191  * This gets called instead of display_parsed_vcard() if we are only looking
192  * to extract the person's name instead of displaying the card.
193  */
194 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
195         char *name;
196
197         strcpy(storename, "");
198
199         name = vcard_get_prop(v, "n", 1, 0, 0);
200         if (name != NULL) {
201                 strcpy(storename, name);
202                 /* vcard_n_prettyize(storename); */
203         }
204
205 }
206
207
208
209 /* display_vcard() calls this after parsing the textual vCard into
210  * our 'struct vCard' data object.
211  *
212  * Set 'full' to nonzero to display the full card, otherwise it will only
213  * show a summary line.
214  *
215  * This code is a bit ugly, so perhaps an explanation is due: we do this
216  * in two passes through the vCard fields.  On the first pass, we process
217  * fields we understand, and then render them in a pretty fashion at the
218  * end.  Then we make a second pass, outputting all the fields we don't
219  * understand in a simple two-column name/value format.
220  */
221 void display_parsed_vcard(struct vCard *v, int full) {
222         int i, j;
223         char buf[SIZ];
224         char *name;
225         int is_qp = 0;
226         int is_b64 = 0;
227         char *thisname, *thisvalue;
228         char firsttoken[SIZ];
229         int pass;
230
231         char displayname[SIZ];
232         char title[SIZ];
233         char org[SIZ];
234         char phone[SIZ];
235         char mailto[SIZ];
236
237         strcpy(displayname, "");
238         strcpy(phone, "");
239         strcpy(mailto, "");
240         strcpy(title, "");
241         strcpy(org, "");
242
243         if (!full) {
244                 wprintf("<TD>");
245                 name = vcard_get_prop(v, "fn", 1, 0, 0);
246                 if (name != NULL) {
247                         escputs(name);
248                 }
249                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
250                         strcpy(displayname, name);
251                         vcard_n_prettyize(displayname);
252                         escputs(displayname);
253                 }
254                 else {
255                         wprintf("&nbsp;");
256                 }
257                 wprintf("</TD>");
258                 return;
259         }
260
261         wprintf("<div align=center><table bgcolor=#aaaaaa width=50%%>");
262         for (pass=1; pass<=2; ++pass) {
263
264                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
265
266                         thisname = strdup(v->prop[i].name);
267                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
268         
269                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
270                                 extract_token(buf, thisname, j, ';', sizeof buf);
271                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
272                                         is_qp = 1;
273                                         remove_token(thisname, j, ';');
274                                 }
275                                 if (!strcasecmp(buf, "encoding=base64")) {
276                                         is_b64 = 1;
277                                         remove_token(thisname, j, ';');
278                                 }
279                         }
280         
281                         if (is_qp) {
282                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
283                                 j = CtdlDecodeQuotedPrintable(
284                                         thisvalue, v->prop[i].value,
285                                         strlen(v->prop[i].value) );
286                                 thisvalue[j] = 0;
287                         }
288                         else if (is_b64) {
289                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
290                                 CtdlDecodeBase64(
291                                         thisvalue, v->prop[i].value,
292                                         strlen(v->prop[i].value) );
293                         }
294                         else {
295                                 thisvalue = strdup(v->prop[i].value);
296                         }
297         
298                         /*** Various fields we may encounter ***/
299         
300                         /* N is name, but only if there's no FN already there */
301                         if (!strcasecmp(firsttoken, "n")) {
302                                 if (strlen(displayname) == 0) {
303                                         strcpy(displayname, thisvalue);
304                                         vcard_n_prettyize(displayname);
305                                 }
306                         }
307         
308                         /* FN (full name) is a true 'display name' field */
309                         else if (!strcasecmp(firsttoken, "fn")) {
310                                 strcpy(displayname, thisvalue);
311                         }
312
313                         /* title */
314                         else if (!strcasecmp(firsttoken, "title")) {
315                                 strcpy(title, thisvalue);
316                         }
317         
318                         /* organization */
319                         else if (!strcasecmp(firsttoken, "org")) {
320                                 strcpy(org, thisvalue);
321                         }
322         
323                         else if (!strcasecmp(firsttoken, "email")) {
324                                 if (strlen(mailto) > 0) strcat(mailto, "<br />");
325                                 strcat(mailto,
326                                         "<A HREF=\"/display_enter"
327                                         "?force_room=_MAIL_?recp=");
328                                 urlesc(&mailto[strlen(mailto)], thisvalue);
329                                 strcat(mailto, "\">");
330                                 urlesc(&mailto[strlen(mailto)], thisvalue);
331                                 strcat(mailto, "</A>");
332                         }
333                         else if (!strcasecmp(firsttoken, "tel")) {
334                                 if (strlen(phone) > 0) strcat(phone, "<br />");
335                                 strcat(phone, thisvalue);
336                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
337                                         extract_token(buf, thisname, j, ';', sizeof buf);
338                                         if (!strcasecmp(buf, "tel"))
339                                                 strcat(phone, "");
340                                         else if (!strcasecmp(buf, "work"))
341                                                 strcat(phone, _(" (work)"));
342                                         else if (!strcasecmp(buf, "home"))
343                                                 strcat(phone, _(" (home)"));
344                                         else if (!strcasecmp(buf, "cell"))
345                                                 strcat(phone, _(" (cell)"));
346                                         else {
347                                                 strcat(phone, " (");
348                                                 strcat(phone, buf);
349                                                 strcat(phone, ")");
350                                         }
351                                 }
352                         }
353                         else if (!strcasecmp(firsttoken, "adr")) {
354                                 if (pass == 2) {
355                                         wprintf("<TR><TD>");
356                                         wprintf(_("Address:"));
357                                         wprintf("</TD><TD>");
358                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
359                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
360                                                 if (strlen(buf) > 0) {
361                                                         escputs(buf);
362                                                         if (j<3) wprintf("<br />");
363                                                         else wprintf(" ");
364                                                 }
365                                         }
366                                         wprintf("</TD></TR>\n");
367                                 }
368                         }
369                         else if (!strcasecmp(firsttoken, "version")) {
370                                 /* ignore */
371                         }
372                         else if (!strcasecmp(firsttoken, "rev")) {
373                                 /* ignore */
374                         }
375                         else if (!strcasecmp(firsttoken, "label")) {
376                                 /* ignore */
377                         }
378                         else {
379
380                                 /*** Don't show extra fields.  They're ugly.
381                                 if (pass == 2) {
382                                         wprintf("<TR><TD>");
383                                         escputs(thisname);
384                                         wprintf("</TD><TD>");
385                                         escputs(thisvalue);
386                                         wprintf("</TD></TR>\n");
387                                 }
388                                 ***/
389                         }
390         
391                         free(thisname);
392                         free(thisvalue);
393                 }
394         
395                 if (pass == 1) {
396                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
397                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
398                         "<IMG ALIGN=CENTER SRC=\"/static/viewcontacts_48x.gif\">"
399                         "<FONT SIZE=+1><B>");
400                         escputs(displayname);
401                         wprintf("</B></FONT>");
402                         if (strlen(title) > 0) {
403                                 wprintf("<div align=right>");
404                                 escputs(title);
405                                 wprintf("</div>");
406                         }
407                         if (strlen(org) > 0) {
408                                 wprintf("<div align=right>");
409                                 escputs(org);
410                                 wprintf("</div>");
411                         }
412                         wprintf("</TD></TR>\n");
413                 
414                         if (strlen(phone) > 0)
415                                 wprintf("<TR><TD>");
416                                 wprintf(_("Telephone:"));
417                                 wprintf("</TD><TD>%s</TD></TR>\n", phone);
418                         if (strlen(mailto) > 0)
419                                 wprintf("<TR><TD>");
420                                 wprintf(_("E-mail:"));
421                                 wprintf("</TD><TD>%s</TD></TR>\n", mailto);
422                 }
423
424         }
425
426         wprintf("</table></div>\n");
427 }
428
429
430
431 /*
432  * Display a textual vCard
433  * (Converts to a vCard object and then calls the actual display function)
434  * Set 'full' to nonzero to display the whole card instead of a one-liner.
435  * Or, if "storename" is non-NULL, just store the person's name in that
436  * buffer instead of displaying the card at all.
437  */
438 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
439         struct vCard *v;
440         char *name;
441         char buf[SIZ];
442         char this_alpha = 0;
443
444         v = vcard_load(vcard_source);
445         if (v == NULL) return;
446
447         name = vcard_get_prop(v, "n", 1, 0, 0);
448         if (name != NULL) {
449                 strcpy(buf, name);
450                 this_alpha = buf[0];
451         }
452
453         if (storename != NULL) {
454                 fetchname_parsed_vcard(v, storename);
455         }
456         else if (       (alpha == 0)
457                         || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
458                         || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
459                 ) {
460                 display_parsed_vcard(v, full);
461         }
462
463         vcard_free(v);
464 }
465
466
467 /*
468  * I wanna SEE that message!
469  */
470 void read_message(long msgnum, int suppress_buttons) {
471         char buf[SIZ];
472         char mime_partnum[256];
473         char mime_filename[256];
474         char mime_content_type[256];
475         char mime_charset[256];
476         char mime_disposition[256];
477         int mime_length;
478         char mime_http[SIZ];
479         char m_subject[256];
480         char from[256];
481         char node[256];
482         char rfca[256];
483         char reply_to[512];
484         char now[256];
485         int format_type = 0;
486         int nhdr = 0;
487         int bq = 0;
488         int i = 0;
489         char vcard_partnum[256];
490         char cal_partnum[256];
491         char *part_source = NULL;
492 #ifdef HAVE_ICONV
493         iconv_t ic = (iconv_t)(-1) ;
494         char *ibuf;                   /* Buffer of characters to be converted */
495         char *obuf;                   /* Buffer for converted characters      */
496         size_t ibuflen;               /* Length of input buffer               */
497         size_t obuflen;               /* Length of output buffer              */
498         char *osav;                   /* Saved pointer to output buffer       */
499 #endif
500
501         strcpy(from, "");
502         strcpy(node, "");
503         strcpy(rfca, "");
504         strcpy(reply_to, "");
505         strcpy(vcard_partnum, "");
506         strcpy(cal_partnum, "");
507         strcpy(mime_http, "");
508         strcpy(mime_content_type, "text/plain");
509         strcpy(mime_charset, "us-ascii");
510
511         serv_printf("MSG4 %ld", msgnum);
512         serv_getln(buf, sizeof buf);
513         if (buf[0] != '1') {
514                 wprintf("<STRONG>");
515                 wprintf(_("ERROR:"));
516                 wprintf("</STRONG> %s<br />\n", &buf[4]);
517                 return;
518         }
519
520         /* begin everythingamundo table */
521         wprintf("<div id=\"fix_scrollbar_bug\">\n");
522         wprintf("<table width=100%% border=1 cellspacing=0 "
523                 "cellpadding=0><TR><TD>\n");
524
525         /* begin message header table */
526         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
527                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
528
529         wprintf("<SPAN CLASS=\"message_header\">");
530         strcpy(m_subject, "");
531
532         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
533                 if (!strcmp(buf, "000")) {
534                         wprintf("<I>");
535                         wprintf(_("unexpected end of message"));
536                         wprintf("</I><br /><br />\n");
537                         wprintf("</SPAN>\n");
538                         return;
539                 }
540                 if (!strncasecmp(buf, "nhdr=yes", 8))
541                         nhdr = 1;
542                 if (nhdr == 1)
543                         buf[0] = '_';
544                 if (!strncasecmp(buf, "type=", 5))
545                         format_type = atoi(&buf[5]);
546                 if (!strncasecmp(buf, "from=", 5)) {
547                         strcpy(from, &buf[5]);
548                         wprintf(_("from "));
549                         wprintf("<A HREF=\"/showuser?who=");
550 #ifdef HAVE_ICONV
551                         utf8ify_rfc822_string(from);
552 #endif
553                         urlescputs(from);
554                         wprintf("\">");
555                         escputs(from);
556                         wprintf("</A> ");
557                 }
558                 if (!strncasecmp(buf, "subj=", 5)) {
559                         strcpy(m_subject, &buf[5]);
560                 }
561                 if ((!strncasecmp(buf, "hnod=", 5))
562                     && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
563                         wprintf("(%s) ", &buf[5]);
564                 }
565                 if ((!strncasecmp(buf, "room=", 5))
566                     && (strcasecmp(&buf[5], WC->wc_roomname))
567                     && (strlen(&buf[5])>0) ) {
568                         wprintf(_("in "));
569                         wprintf("%s&gt; ", &buf[5]);
570                 }
571                 if (!strncasecmp(buf, "rfca=", 5)) {
572                         strcpy(rfca, &buf[5]);
573                         wprintf("&lt;");
574                         escputs(rfca);
575                         wprintf("&gt; ");
576                 }
577
578                 if (!strncasecmp(buf, "node=", 5)) {
579                         strcpy(node, &buf[5]);
580                         if ( ((WC->room_flags & QR_NETWORK)
581                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
582                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
583                         && (strlen(rfca)==0)
584                         ) {
585                                 wprintf("@%s ", &buf[5]);
586                         }
587                 }
588                 if (!strncasecmp(buf, "rcpt=", 5)) {
589                         wprintf(_("to "));
590                         wprintf("%s ", &buf[5]);
591                 }
592                 if (!strncasecmp(buf, "time=", 5)) {
593                         fmt_date(now, atol(&buf[5]), 0);
594                         wprintf("%s ", now);
595                 }
596
597                 if (!strncasecmp(buf, "part=", 5)) {
598                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
599                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
600                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
601                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
602                         mime_length = extract_int(&buf[5], 5);
603
604                         if (!strcasecmp(mime_disposition, "attachment")) {
605                                 snprintf(&mime_http[strlen(mime_http)],
606                                         (sizeof(mime_http) - strlen(mime_http) - 1),
607                                         "<A HREF=\"/output_mimepart?"
608                                         "msgnum=%ld?partnum=%s\" "
609                                         "TARGET=\"wc.%ld.%s\">"
610                                         "<IMG SRC=\"/static/diskette_24x.gif\" "
611                                         "BORDER=0 ALIGN=MIDDLE>\n"
612                                         "Part %s: %s (%s, %d bytes)</A><br />\n",
613                                         msgnum, mime_partnum,
614                                         msgnum, mime_partnum,
615                                         mime_partnum, mime_filename,
616                                         mime_content_type, mime_length);
617                         }
618
619                         if ((!strcasecmp(mime_disposition, "inline"))
620                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
621                                 snprintf(&mime_http[strlen(mime_http)],
622                                         (sizeof(mime_http) - strlen(mime_http) - 1),
623                                         "<IMG SRC=\"/output_mimepart?"
624                                         "msgnum=%ld?partnum=%s\">",
625                                         msgnum, mime_partnum);
626                         }
627
628                         /*** begin handler prep ***/
629                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
630                                 strcpy(vcard_partnum, mime_partnum);
631                         }
632
633                         if (!strcasecmp(mime_content_type, "text/calendar")) {
634                                 strcpy(cal_partnum, mime_partnum);
635                         }
636
637                         /*** end handler prep ***/
638
639                 }
640
641         }
642
643         /* Generate a reply-to address */
644         if (strlen(rfca) > 0) {
645                 strcpy(reply_to, rfca);
646         }
647         else {
648                 if ( (strlen(node) > 0)
649                    && (strcasecmp(node, serv_info.serv_nodename))
650                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
651                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
652                                 from, node);
653                 }
654                 else {
655                         snprintf(reply_to, sizeof(reply_to), "%s", from);
656                 }
657         }
658
659         if (nhdr == 1) {
660                 wprintf("****");
661         }
662
663         wprintf("</SPAN>");
664 #ifdef HAVE_ICONV
665         utf8ify_rfc822_string(m_subject);
666 #endif
667         if (strlen(m_subject) > 0) {
668                 wprintf("<br />"
669                         "<SPAN CLASS=\"message_subject\">");
670                 wprintf(_("Subject:"));
671                 wprintf(" %s</SPAN>", m_subject
672                 );
673         }
674         wprintf("</TD>\n");
675
676         /* start msg buttons */
677         if (!suppress_buttons) {
678                 wprintf("<td align=right>\n");
679
680                 /* Reply */
681                 wprintf("<a href=\"/display_enter?recp=");
682                 urlescputs(reply_to);
683                 wprintf("?subject=");
684                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
685                 urlescputs(m_subject);
686                 wprintf("\">[%s]</a> ", _("Reply"));
687
688                 /* Forward */
689                 if (WC->wc_view == VIEW_MAILBOX) {
690                         wprintf("<a href=\"/display_enter?pullquote=%ld?subject=", msgnum);
691                         if (strncasecmp(m_subject, "Fwd:", 4)) wprintf("Fwd:%20");
692                         urlescputs(m_subject);
693                         wprintf("\">[%s]</a> ", _("Forward"));
694                 }
695
696                 if (WC->is_room_aide)  {
697                         /* Move */
698                         wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[%s]</a> ",
699                                 msgnum, _("Move"));
700         
701                         /* Delete */
702                         wprintf("<a href=\"/delete_msg?msgid=%ld\" "
703                                 "onClick=\"return confirm('%s');\">"
704                                 "[%s]</a> ", msgnum, _("Delete this message?"), _("Delete")
705                         );
706                 }
707
708                 wprintf("<a href=\"/msg?msgnum=%ld?sourceiframe=msgloader1?print_it=yes\" target=\"msgloader1\">"
709                         "[%s]</a>", msgnum, _("Print"));
710
711                 wprintf("</td>");
712         }
713
714         wprintf("</TR></TABLE>\n");
715
716         /* Begin body */
717         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
718                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
719
720         /* 
721          * Learn the content type
722          */
723         strcpy(mime_content_type, "text/plain");
724         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
725                 if (!strcmp(buf, "000")) {
726                         wprintf("<I>");
727                         wprintf(_("unexpected end of message"));
728                         wprintf("</I><br /><br />\n");
729                         goto ENDBODY;
730                 }
731                 if (!strncasecmp(buf, "Content-type: ", 14)) {
732                         safestrncpy(mime_content_type, &buf[14],
733                                 sizeof(mime_content_type));
734                         for (i=0; i<strlen(mime_content_type); ++i) {
735                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
736                                         safestrncpy(mime_charset, &mime_content_type[i+8],
737                                                 sizeof mime_charset);
738                                 }
739                         }
740                         for (i=0; i<strlen(mime_content_type); ++i) {
741                                 if (mime_content_type[i] == ';') {
742                                         mime_content_type[i] = 0;
743                                 }
744                         }
745                 }
746         }
747
748         /* Set up a character set conversion if we need to (and if we can) */
749 #ifdef HAVE_ICONV
750         if ( (strcasecmp(mime_charset, "us-ascii"))
751            && (strcasecmp(mime_charset, "UTF-8")) ) {
752                 ic = iconv_open("UTF-8", mime_charset);
753                 if (ic == (iconv_t)(-1) ) {
754                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
755                 }
756         }
757 #endif
758
759         /* Messages in legacy Citadel variformat get handled thusly... */
760         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
761                 fmout("JUSTIFY");
762         }
763
764         /* Boring old 80-column fixed format text gets handled this way... */
765         else if ( (!strcasecmp(mime_content_type, "text/plain"))
766                 || (!strcasecmp(mime_content_type, "text")) ) {
767                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
768                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
769                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
770
771 #ifdef HAVE_ICONV
772                         if (ic != (iconv_t)(-1) ) {
773                                 ibuf = buf;
774                                 ibuflen = strlen(ibuf);
775                                 obuflen = SIZ;
776                                 obuf = (char *) malloc(obuflen);
777                                 osav = obuf;
778                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
779                                 osav[SIZ-obuflen] = 0;
780                                 safestrncpy(buf, osav, sizeof buf);
781                                 free(osav);
782                         }
783 #endif
784
785                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
786                                 buf[strlen(buf) - 1] = 0;
787                         if ((bq == 0) &&
788                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
789                                 wprintf("<BLOCKQUOTE>");
790                                 bq = 1;
791                         } else if ((bq == 1) &&
792                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
793                                 wprintf("</BLOCKQUOTE>");
794                                 bq = 0;
795                         }
796                         wprintf("<TT>");
797                         url(buf);
798                         escputs(buf);
799                         wprintf("</TT><br />\n");
800                 }
801                 wprintf("</I><br />");
802         }
803
804         else /* HTML is fun, but we've got to strip it first */
805         if (!strcasecmp(mime_content_type, "text/html")) {
806                 output_html(mime_charset);
807         }
808
809         /* Unknown weirdness */
810         else {
811                 wprintf(_("I don't know how to display %s"), mime_content_type);
812                 wprintf("<br />\n", mime_content_type);
813                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
814         }
815
816         /* Afterwards, offer links to download attachments 'n' such */
817         if (strlen(mime_http) > 0) {
818                 wprintf("%s", mime_http);
819         }
820
821         /* Handler for vCard parts */
822         if (strlen(vcard_partnum) > 0) {
823                 part_source = load_mimepart(msgnum, vcard_partnum);
824                 if (part_source != NULL) {
825
826                         /* If it's my vCard I can edit it */
827                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
828                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
829                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
830                         ) {
831                                 wprintf("<A HREF=\"/edit_vcard?"
832                                         "msgnum=%ld?partnum=%s\">",
833                                         msgnum, vcard_partnum);
834                                 wprintf("[%s]</A>", _("edit"));
835                         }
836
837                         /* In all cases, display the full card */
838                         display_vcard(part_source, 0, 1, NULL);
839                 }
840         }
841
842         /* Handler for calendar parts */
843         if (strlen(cal_partnum) > 0) {
844                 part_source = load_mimepart(msgnum, cal_partnum);
845                 if (part_source != NULL) {
846                         cal_process_attachment(part_source,
847                                                 msgnum, cal_partnum);
848                 }
849         }
850
851         if (part_source) {
852                 free(part_source);
853                 part_source = NULL;
854         }
855
856 ENDBODY:
857         wprintf("</TD></TR></TABLE>\n");
858
859         /* end everythingamundo table */
860         wprintf("</TD></TR></TABLE>\n");
861         wprintf("</div><br />\n");
862
863 #ifdef HAVE_ICONV
864         if (ic != (iconv_t)(-1) ) {
865                 iconv_close(ic);
866         }
867 #endif
868 }
869
870
871
872 /*
873  * Unadorned HTML output of an individual message, suitable
874  * for placing in a hidden iframe, for printing, or whatever
875  */
876 void embed_message(void) {
877         long msgnum = 0L;
878         char *sourceiframe;
879         char *targetdiv;
880         char *print_it;
881
882         msgnum = atol(bstr("msgnum"));
883         sourceiframe = bstr("sourceiframe");
884         targetdiv = bstr("targetdiv");
885         print_it = bstr("print_it");
886
887         output_headers(1, 0, 0, 0, 0, 1, 0);
888         begin_burst();
889
890         wprintf("<html><head>");
891
892         /* If we're loading into a hidden iframe, chances are the caller told us
893          * about a target div somewhere that we need to copy into when we're done.
894          */
895         if (strlen(targetdiv) > 0) wprintf(
896 "                                                                       \n"
897 " <script type=\"text/javascript\">                                     \n"
898 "       function loaded_now_copy_it() {                                 \n"
899 "               parent.document.getElementById(\"%s\").innerHTML = parent.frames['%s'].document.body.innerHTML; \n"
900 "       }                                                               \n"
901 "</script>\n",
902                 targetdiv,
903                 sourceiframe
904         );
905
906         if (!strcasecmp(print_it, "yes")) wprintf(
907 "                                                                       \n"
908 " <script type=\"text/javascript\">                                     \n"
909 "       function loaded_now_print_it() {                                \n"
910 "               parent.frames['%s'].focus();                            \n"
911 "               parent.frames['%s'].print();                            \n"
912 "       }                                                               \n"
913 "</script>\n",
914                 sourceiframe,
915                 sourceiframe
916         );
917
918         wprintf("</head>");
919         wprintf("<body");
920         if (strlen(targetdiv) > 0) {
921                 wprintf(" onLoad='loaded_now_copy_it();'");
922         }
923         if (!strcasecmp(print_it, "yes")) {
924                 wprintf(" onLoad='loaded_now_print_it();'");
925         }
926         wprintf(">\n");
927         read_message(msgnum, (!strcasecmp(print_it, "yes") ? 1 : 0) );
928         wprintf("</body></html>\n");
929         wDumpContent(0);
930 }
931
932
933
934
935 /*
936  * Read message in simple, JavaScript-embeddable form for 'forward'
937  * or 'reply quoted' operations.
938  *
939  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
940  *       in this function.  Doing so would throw a JavaScript error in the
941  *       'supplied text' argument to the editor.
942  */
943 void pullquote_message(long msgnum, int forward_attachments) {
944         char buf[SIZ];
945         char mime_partnum[256];
946         char mime_filename[256];
947         char mime_content_type[256];
948         char mime_charset[256];
949         char mime_disposition[256];
950         int mime_length;
951         char mime_http[SIZ];
952         char *attachments = NULL;
953         int num_attachments = 0;
954         struct wc_attachment *att, *aptr;
955         char m_subject[256];
956         char from[256];
957         char node[256];
958         char rfca[256];
959         char reply_to[512];
960         char now[256];
961         int format_type = 0;
962         int nhdr = 0;
963         int bq = 0;
964         int i = 0;
965 #ifdef HAVE_ICONV
966         iconv_t ic = (iconv_t)(-1) ;
967         char *ibuf;                   /* Buffer of characters to be converted */
968         char *obuf;                   /* Buffer for converted characters      */
969         size_t ibuflen;               /* Length of input buffer               */
970         size_t obuflen;               /* Length of output buffer              */
971         char *osav;                   /* Saved pointer to output buffer       */
972 #endif
973
974         strcpy(from, "");
975         strcpy(node, "");
976         strcpy(rfca, "");
977         strcpy(reply_to, "");
978         strcpy(mime_http, "");
979         strcpy(mime_content_type, "text/plain");
980         strcpy(mime_charset, "us-ascii");
981
982         serv_printf("MSG4 %ld", msgnum);
983         serv_getln(buf, sizeof buf);
984         if (buf[0] != '1') {
985                 wprintf(_("ERROR:"));
986                 wprintf("%s<br />", &buf[4]);
987                 return;
988         }
989
990         strcpy(m_subject, "");
991
992         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
993                 if (!strcmp(buf, "000")) {
994                         wprintf(_("unexpected end of message"));
995                         return;
996                 }
997                 if (!strncasecmp(buf, "nhdr=yes", 8))
998                         nhdr = 1;
999                 if (nhdr == 1)
1000                         buf[0] = '_';
1001                 if (!strncasecmp(buf, "type=", 5))
1002                         format_type = atoi(&buf[5]);
1003                 if (!strncasecmp(buf, "from=", 5)) {
1004                         strcpy(from, &buf[5]);
1005                         wprintf(_("from "));
1006 #ifdef HAVE_ICONV
1007                         utf8ify_rfc822_string(from);
1008 #endif
1009                         msgescputs(from);
1010                 }
1011                 if (!strncasecmp(buf, "subj=", 5)) {
1012                         strcpy(m_subject, &buf[5]);
1013                 }
1014                 if ((!strncasecmp(buf, "hnod=", 5))
1015                     && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
1016                         wprintf("(%s) ", &buf[5]);
1017                 }
1018                 if ((!strncasecmp(buf, "room=", 5))
1019                     && (strcasecmp(&buf[5], WC->wc_roomname))
1020                     && (strlen(&buf[5])>0) ) {
1021                         wprintf(_("in "));
1022                         wprintf("%s&gt; ", &buf[5]);
1023                 }
1024                 if (!strncasecmp(buf, "rfca=", 5)) {
1025                         strcpy(rfca, &buf[5]);
1026                         wprintf("&lt;");
1027                         msgescputs(rfca);
1028                         wprintf("&gt; ");
1029                 }
1030
1031                 if (!strncasecmp(buf, "node=", 5)) {
1032                         strcpy(node, &buf[5]);
1033                         if ( ((WC->room_flags & QR_NETWORK)
1034                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
1035                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
1036                         && (strlen(rfca)==0)
1037                         ) {
1038                                 wprintf("@%s ", &buf[5]);
1039                         }
1040                 }
1041                 if (!strncasecmp(buf, "rcpt=", 5)) {
1042                         wprintf(_("to "));
1043                         wprintf("%s ", &buf[5]);
1044                 }
1045                 if (!strncasecmp(buf, "time=", 5)) {
1046                         fmt_date(now, atol(&buf[5]), 0);
1047                         wprintf("%s ", now);
1048                 }
1049
1050                 /*
1051                  * Save attachment info for later.  We can't start downloading them
1052                  * yet because we're in the middle of a server transaction.
1053                  */
1054                 if (!strncasecmp(buf, "part=", 5)) {
1055                         ++num_attachments;
1056                         attachments = realloc(attachments, (num_attachments * 1024));
1057                         strcat(attachments, &buf[5]);
1058                         strcat(attachments, "\n");
1059                 }
1060
1061         }
1062
1063         wprintf("<br>");
1064
1065 #ifdef HAVE_ICONV
1066         utf8ify_rfc822_string(m_subject);
1067 #endif
1068         if (strlen(m_subject) > 0) {
1069                 wprintf(_("Subject:"));
1070                 wprintf(" ");
1071                 msgescputs(m_subject);
1072                 wprintf("<br />");
1073         }
1074
1075         /*
1076          * Begin body
1077          */
1078         wprintf("<br>");
1079
1080         /* 
1081          * Learn the content type
1082          */
1083         strcpy(mime_content_type, "text/plain");
1084         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
1085                 if (!strcmp(buf, "000")) {
1086                         wprintf(_("unexpected end of message"));
1087                         goto ENDBODY;
1088                 }
1089                 if (!strncasecmp(buf, "Content-type: ", 14)) {
1090                         safestrncpy(mime_content_type, &buf[14],
1091                                 sizeof(mime_content_type));
1092                         for (i=0; i<strlen(mime_content_type); ++i) {
1093                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1094                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1095                                                 sizeof mime_charset);
1096                                 }
1097                         }
1098                         for (i=0; i<strlen(mime_content_type); ++i) {
1099                                 if (mime_content_type[i] == ';') {
1100                                         mime_content_type[i] = 0;
1101                                 }
1102                         }
1103                 }
1104         }
1105
1106         /* Set up a character set conversion if we need to (and if we can) */
1107 #ifdef HAVE_ICONV
1108         if ( (strcasecmp(mime_charset, "us-ascii"))
1109            && (strcasecmp(mime_charset, "UTF-8")) ) {
1110                 ic = iconv_open("UTF-8", mime_charset);
1111                 if (ic == (iconv_t)(-1) ) {
1112                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
1113                 }
1114         }
1115 #endif
1116
1117         /* Messages in legacy Citadel variformat get handled thusly... */
1118         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1119                 pullquote_fmout();
1120         }
1121
1122         /* Boring old 80-column fixed format text gets handled this way... */
1123         else if (!strcasecmp(mime_content_type, "text/plain")) {
1124                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1125                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
1126                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
1127
1128 #ifdef HAVE_ICONV
1129                         if (ic != (iconv_t)(-1) ) {
1130                                 ibuf = buf;
1131                                 ibuflen = strlen(ibuf);
1132                                 obuflen = SIZ;
1133                                 obuf = (char *) malloc(obuflen);
1134                                 osav = obuf;
1135                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1136                                 osav[SIZ-obuflen] = 0;
1137                                 safestrncpy(buf, osav, sizeof buf);
1138                                 free(osav);
1139                         }
1140 #endif
1141
1142                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
1143                                 buf[strlen(buf) - 1] = 0;
1144                         if ((bq == 0) &&
1145                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
1146                                 wprintf("<BLOCKQUOTE>");
1147                                 bq = 1;
1148                         } else if ((bq == 1) &&
1149                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
1150                                 wprintf("</BLOCKQUOTE>");
1151                                 bq = 0;
1152                         }
1153                         wprintf("<TT>");
1154                         url(buf);
1155                         msgescputs(buf);
1156                         wprintf("</TT><br />");
1157                 }
1158                 wprintf("</I><br />");
1159         }
1160
1161         /* HTML just gets escaped and stuffed back into the editor */
1162         else if (!strcasecmp(mime_content_type, "text/html")) {
1163                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1164                         msgescputs(buf);
1165                 }
1166         }
1167
1168         /* Unknown weirdness ... don't know how to handle this content type */
1169         else {
1170                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1171         }
1172
1173 ENDBODY:
1174         /* end of body handler */
1175
1176         /*
1177          * If there were attachments, we have to download them and insert them
1178          * into the attachment chain for the forwarded message we are composing.
1179          */
1180         if (num_attachments) {
1181                 for (i=0; i<num_attachments; ++i) {
1182                         extract_token(buf, attachments, i, '\n', sizeof buf);
1183                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
1184                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
1185                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
1186                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
1187                         mime_length = extract_int(buf, 5);
1188
1189                         /*
1190                          * tracing  ... uncomment if necessary
1191                          *
1192                         lprintf(9, "fwd filename: %s\n", mime_filename);
1193                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
1194                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
1195                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
1196                         lprintf(9, "fwd length  : %d\n", mime_length);
1197                          */
1198
1199                         if ( (!strcasecmp(mime_disposition, "inline"))
1200                            || (!strcasecmp(mime_disposition, "attachment")) ) {
1201                 
1202                                 /* Create an attachment struct from this mime part... */
1203                                 att = malloc(sizeof(struct wc_attachment));
1204                                 memset(att, 0, sizeof(struct wc_attachment));
1205                                 att->length = mime_length;
1206                                 strcpy(att->content_type, mime_content_type);
1207                                 strcpy(att->filename, mime_filename);
1208                                 att->next = NULL;
1209                                 att->data = load_mimepart(msgnum, mime_partnum);
1210                 
1211                                 /* And add it to the list. */
1212                                 if (WC->first_attachment == NULL) {
1213                                         WC->first_attachment = att;
1214                                 }
1215                                 else {
1216                                         aptr = WC->first_attachment;
1217                                         while (aptr->next != NULL) aptr = aptr->next;
1218                                         aptr->next = att;
1219                                 }
1220                         }
1221
1222                 }
1223                 free(attachments);
1224         }
1225
1226 #ifdef HAVE_ICONV
1227         if (ic != (iconv_t)(-1) ) {
1228                 iconv_close(ic);
1229         }
1230 #endif
1231 }
1232
1233
1234
1235
1236
1237
1238 void display_summarized(int num) {
1239         char datebuf[64];
1240
1241         wprintf("<TD>");
1242         if (WC->summ[num].is_new) wprintf("<B>");
1243         wprintf("<A HREF=\"/msg?msgnum=%ld?sourceiframe=msgloader1?targetdiv=preview_pane\" target=\"msgloader1\">",
1244                 WC->summ[num].msgnum);
1245         escputs(WC->summ[num].subj);
1246         wprintf("</A>");
1247         if (WC->summ[num].is_new) wprintf("</B>");
1248         wprintf("</TD><TD>");
1249         if (WC->summ[num].is_new) wprintf("<B>");
1250         escputs(WC->summ[num].from);
1251         if (WC->summ[num].is_new) wprintf("</B>");
1252         wprintf(" </TD><TD>");
1253         if (WC->summ[num].is_new) wprintf("<B>");
1254         fmt_date(datebuf, WC->summ[num].date, 1);       /* brief */
1255         escputs(datebuf);
1256         if (WC->summ[num].is_new) wprintf("</B>");
1257         wprintf(" </TD>");
1258         wprintf("<TD>"
1259                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
1260                 "</TD>\n",
1261                 WC->summ[num].msgnum
1262         );
1263 }
1264
1265
1266
1267
1268
1269 void display_addressbook(long msgnum, char alpha) {
1270         char buf[SIZ];
1271         char mime_partnum[SIZ];
1272         char mime_filename[SIZ];
1273         char mime_content_type[SIZ];
1274         char mime_disposition[SIZ];
1275         int mime_length;
1276         char vcard_partnum[SIZ];
1277         char *vcard_source = NULL;
1278         struct message_summary summ;
1279
1280         memset(&summ, 0, sizeof(summ));
1281         safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
1282
1283         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1284         serv_puts(buf);
1285         serv_getln(buf, sizeof buf);
1286         if (buf[0] != '1') return;
1287
1288         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1289                 if (!strncasecmp(buf, "part=", 5)) {
1290                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1291                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1292                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1293                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1294                         mime_length = extract_int(&buf[5], 5);
1295
1296                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1297                                 strcpy(vcard_partnum, mime_partnum);
1298                         }
1299
1300                 }
1301         }
1302
1303         if (strlen(vcard_partnum) > 0) {
1304                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1305                 if (vcard_source != NULL) {
1306
1307                         /* Display the summary line */
1308                         display_vcard(vcard_source, alpha, 0, NULL);
1309
1310                         /* If it's my vCard I can edit it */
1311                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1312                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1313                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1314                         ) {
1315                                 wprintf("<A HREF=\"/edit_vcard?"
1316                                         "msgnum=%ld?partnum=%s\">",
1317                                         msgnum, vcard_partnum);
1318                                 wprintf("[%s]</A>", _("edit"));
1319                         }
1320
1321                         free(vcard_source);
1322                 }
1323         }
1324
1325 }
1326
1327
1328
1329 /* If it's an old "Firstname Lastname" style record, try to
1330  * convert it.
1331  */
1332 void lastfirst_firstlast(char *namebuf) {
1333         char firstname[SIZ];
1334         char lastname[SIZ];
1335         int i;
1336
1337         if (namebuf == NULL) return;
1338         if (strchr(namebuf, ';') != NULL) return;
1339
1340         i = num_tokens(namebuf, ' ');
1341         if (i < 2) return;
1342
1343         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1344         remove_token(namebuf, i-1, ' ');
1345         strcpy(firstname, namebuf);
1346         sprintf(namebuf, "%s; %s", lastname, firstname);
1347 }
1348
1349
1350 void fetch_ab_name(long msgnum, char *namebuf) {
1351         char buf[SIZ];
1352         char mime_partnum[SIZ];
1353         char mime_filename[SIZ];
1354         char mime_content_type[SIZ];
1355         char mime_disposition[SIZ];
1356         int mime_length;
1357         char vcard_partnum[SIZ];
1358         char *vcard_source = NULL;
1359         int i;
1360         struct message_summary summ;
1361
1362         if (namebuf == NULL) return;
1363         strcpy(namebuf, "");
1364
1365         memset(&summ, 0, sizeof(summ));
1366         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1367
1368         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1369         serv_puts(buf);
1370         serv_getln(buf, sizeof buf);
1371         if (buf[0] != '1') return;
1372
1373         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1374                 if (!strncasecmp(buf, "part=", 5)) {
1375                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1376                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1377                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1378                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1379                         mime_length = extract_int(&buf[5], 5);
1380
1381                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1382                                 strcpy(vcard_partnum, mime_partnum);
1383                         }
1384
1385                 }
1386         }
1387
1388         if (strlen(vcard_partnum) > 0) {
1389                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1390                 if (vcard_source != NULL) {
1391
1392                         /* Grab the name off the card */
1393                         display_vcard(vcard_source, 0, 0, namebuf);
1394
1395                         free(vcard_source);
1396                 }
1397         }
1398
1399         lastfirst_firstlast(namebuf);
1400         striplt(namebuf);
1401         for (i=0; i<strlen(namebuf); ++i) {
1402                 if (namebuf[i] != ';') return;
1403         }
1404         strcpy(namebuf, _("(no name)"));
1405 }
1406
1407
1408
1409 /*
1410  * Record compare function for sorting address book indices
1411  */
1412 int abcmp(const void *ab1, const void *ab2) {
1413         return(strcasecmp(
1414                 (((const struct addrbookent *)ab1)->ab_name),
1415                 (((const struct addrbookent *)ab2)->ab_name)
1416         ));
1417 }
1418
1419
1420 /*
1421  * Helper function for do_addrbook_view()
1422  * Converts a name into a three-letter tab label
1423  */
1424 void nametab(char *tabbuf, char *name) {
1425         stresc(tabbuf, name, 0, 0);
1426         tabbuf[0] = toupper(tabbuf[0]);
1427         tabbuf[1] = tolower(tabbuf[1]);
1428         tabbuf[2] = tolower(tabbuf[2]);
1429         tabbuf[3] = 0;
1430 }
1431
1432
1433 /*
1434  * Render the address book using info we gathered during the scan
1435  */
1436 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
1437         int i = 0;
1438         int displayed = 0;
1439         int bg = 0;
1440         static int NAMESPERPAGE = 60;
1441         int num_pages = 0;
1442         int page = 0;
1443         int tabfirst = 0;
1444         char tabfirst_label[SIZ];
1445         int tablast = 0;
1446         char tablast_label[SIZ];
1447
1448         if (num_ab == 0) {
1449                 wprintf("<I>");
1450                 wprintf(_("This address book is empty."));
1451                 wprintf("</I>\n");
1452                 return;
1453         }
1454
1455         if (num_ab > 1) {
1456                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
1457         }
1458
1459         num_pages = num_ab / NAMESPERPAGE;
1460
1461         page = atoi(bstr("page"));
1462
1463         wprintf("Page: ");
1464         for (i=0; i<=num_pages; ++i) {
1465                 if (i != page) {
1466                         wprintf("<A HREF=\"/readfwd?page=%d\">", i);
1467                 }
1468                 else {
1469                         wprintf("<B>");
1470                 }
1471                 tabfirst = i * NAMESPERPAGE;
1472                 tablast = tabfirst + NAMESPERPAGE - 1;
1473                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1474                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1475                 nametab(tablast_label, addrbook[tablast].ab_name);
1476                 wprintf("[%s&nbsp;-&nbsp;%s]",
1477                         tabfirst_label, tablast_label
1478                 );
1479                 if (i != page) {
1480                         wprintf("</A>\n");
1481                 }
1482                 else {
1483                         wprintf("</B>\n");
1484                 }
1485         }
1486         wprintf("<br />\n");
1487
1488         wprintf("<TABLE border=0 cellspacing=0 "
1489                 "cellpadding=3 width=100%%>\n"
1490         );
1491
1492         for (i=0; i<num_ab; ++i) {
1493
1494                 if ((i / NAMESPERPAGE) == page) {
1495
1496                         if ((displayed % 4) == 0) {
1497                                 if (displayed > 0) {
1498                                         wprintf("</TR>\n");
1499                                 }
1500                                 bg = 1 - bg;
1501                                 wprintf("<TR BGCOLOR=\"#%s\">",
1502                                         (bg ? "DDDDDD" : "FFFFFF")
1503                                 );
1504                         }
1505         
1506                         wprintf("<TD>");
1507         
1508                         wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
1509                                 addrbook[i].ab_msgnum);
1510                         wprintf("?maxmsgs=1?summary=0?alpha=%s\">", bstr("alpha"));
1511                         vcard_n_prettyize(addrbook[i].ab_name);
1512                         escputs(addrbook[i].ab_name);
1513                         wprintf("</A></TD>\n");
1514                         ++displayed;
1515                 }
1516         }
1517
1518         wprintf("</TR></TABLE>\n");
1519 }
1520
1521
1522
1523 /* 
1524  * load message pointers from the server
1525  */
1526 int load_msg_ptrs(char *servcmd, int with_headers)
1527 {
1528         char buf[1024];
1529         time_t datestamp;
1530         char displayname[128];
1531         char nodename[128];
1532         char inetaddr[128];
1533         char subject[256];
1534         int nummsgs;
1535         int maxload = 0;
1536
1537         int num_summ_alloc = 0;
1538
1539         if (with_headers) {
1540                 if (WC->num_summ != 0) {
1541                         free(WC->summ);
1542                         WC->num_summ = 0;
1543                 }
1544         }
1545         num_summ_alloc = 100;
1546         WC->num_summ = 0;
1547         WC->summ = malloc(num_summ_alloc * sizeof(struct message_summary));
1548
1549         nummsgs = 0;
1550         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1551         serv_puts(servcmd);
1552         serv_getln(buf, sizeof buf);
1553         if (buf[0] != '1') {
1554                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1555                 return (nummsgs);
1556         }
1557         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1558                 if (nummsgs < maxload) {
1559                         WC->msgarr[nummsgs] = extract_long(buf, 0);
1560                         datestamp = extract_long(buf, 1);
1561                         extract_token(displayname, buf, 2, '|', sizeof displayname);
1562                         extract_token(nodename, buf, 3, '|', sizeof nodename);
1563                         extract_token(inetaddr, buf, 4, '|', sizeof inetaddr);
1564                         extract_token(subject, buf, 5, '|', sizeof subject);
1565                         ++nummsgs;
1566
1567                         if (with_headers) {
1568                                 if (nummsgs > num_summ_alloc) {
1569                                         num_summ_alloc *= 2;
1570                                         WC->summ = realloc(WC->summ, num_summ_alloc * sizeof(struct message_summary));
1571                                 }
1572                                 ++WC->num_summ;
1573
1574                                 memset(&WC->summ[nummsgs-1], 0, sizeof(struct message_summary));
1575                                 WC->summ[nummsgs-1].msgnum = WC->msgarr[nummsgs-1];
1576                                 safestrncpy(WC->summ[nummsgs-1].subj, _("(no subject)"), sizeof WC->summ[nummsgs-1].subj);
1577                                 if (strlen(displayname) > 0) {
1578                                         safestrncpy(WC->summ[nummsgs-1].from, displayname, sizeof WC->summ[nummsgs-1].from);
1579                                 }
1580                                 if (strlen(subject) > 0) {
1581                                 safestrncpy(WC->summ[nummsgs-1].subj, subject,
1582                                         sizeof WC->summ[nummsgs-1].subj);
1583                                 }
1584 #ifdef HAVE_ICONV
1585                                 /* Handle subjects with RFC2047 encoding */
1586                                 utf8ify_rfc822_string(WC->summ[nummsgs-1].subj);
1587 #endif
1588                                 if (strlen(WC->summ[nummsgs-1].subj) > 75) {
1589                                         strcpy(&WC->summ[nummsgs-1].subj[72], "...");
1590                                 }
1591
1592                                 if (strlen(nodename) > 0) {
1593                                         if ( ((WC->room_flags & QR_NETWORK)
1594                                            || ((strcasecmp(nodename, serv_info.serv_nodename)
1595                                            && (strcasecmp(nodename, serv_info.serv_fqdn)))))
1596                                         ) {
1597                                                 strcat(WC->summ[nummsgs-1].from, " @ ");
1598                                                 strcat(WC->summ[nummsgs-1].from, nodename);
1599                                         }
1600                                 }
1601
1602                                 WC->summ[nummsgs-1].date = datestamp;
1603         
1604 #ifdef HAVE_ICONV
1605                                 /* Handle senders with RFC2047 encoding */
1606                                 utf8ify_rfc822_string(WC->summ[nummsgs-1].from);
1607 #endif
1608                                 if (strlen(WC->summ[nummsgs-1].from) > 25) {
1609                                         strcpy(&WC->summ[nummsgs-1].from[22], "...");
1610                                 }
1611                         }
1612                 }
1613         }
1614         return (nummsgs);
1615 }
1616
1617  
1618 int summcmp_subj(const void *s1, const void *s2) {
1619         struct message_summary *summ1;
1620         struct message_summary *summ2;
1621         
1622         summ1 = (struct message_summary *)s1;
1623         summ2 = (struct message_summary *)s2;
1624         return strcasecmp(summ1->subj, summ2->subj);
1625 }
1626
1627 int summcmp_rsubj(const void *s1, const void *s2) {
1628         struct message_summary *summ1;
1629         struct message_summary *summ2;
1630         
1631         summ1 = (struct message_summary *)s1;
1632         summ2 = (struct message_summary *)s2;
1633         return strcasecmp(summ2->subj, summ1->subj);
1634 }
1635
1636 int summcmp_sender(const void *s1, const void *s2) {
1637         struct message_summary *summ1;
1638         struct message_summary *summ2;
1639         
1640         summ1 = (struct message_summary *)s1;
1641         summ2 = (struct message_summary *)s2;
1642         return strcasecmp(summ1->from, summ2->from);
1643 }
1644
1645 int summcmp_rsender(const void *s1, const void *s2) {
1646         struct message_summary *summ1;
1647         struct message_summary *summ2;
1648         
1649         summ1 = (struct message_summary *)s1;
1650         summ2 = (struct message_summary *)s2;
1651         return strcasecmp(summ2->from, summ1->from);
1652 }
1653
1654 int summcmp_date(const void *s1, const void *s2) {
1655         struct message_summary *summ1;
1656         struct message_summary *summ2;
1657         
1658         summ1 = (struct message_summary *)s1;
1659         summ2 = (struct message_summary *)s2;
1660
1661         if (summ1->date < summ2->date) return -1;
1662         else if (summ1->date > summ2->date) return +1;
1663         else return 0;
1664 }
1665
1666 int summcmp_rdate(const void *s1, const void *s2) {
1667         struct message_summary *summ1;
1668         struct message_summary *summ2;
1669         
1670         summ1 = (struct message_summary *)s1;
1671         summ2 = (struct message_summary *)s2;
1672
1673         if (summ1->date < summ2->date) return +1;
1674         else if (summ1->date > summ2->date) return -1;
1675         else return 0;
1676 }
1677
1678 /*
1679  * command loop for reading messages
1680  */
1681 void readloop(char *oper)
1682 {
1683         char cmd[SIZ];
1684         char buf[SIZ];
1685         char old_msgs[SIZ];
1686         int a, b;
1687         int nummsgs;
1688         long startmsg;
1689         int maxmsgs;
1690         int num_displayed = 0;
1691         int is_summary = 0;
1692         int is_addressbook = 0;
1693         int is_singlecard = 0;
1694         int is_calendar = 0;
1695         int is_tasks = 0;
1696         int is_notes = 0;
1697         int remaining_messages;
1698         int lo, hi;
1699         int lowest_displayed = (-1);
1700         int highest_displayed = 0;
1701         long pn_previous = 0L;
1702         long pn_current = 0L;
1703         long pn_next = 0L;
1704         int bg = 0;
1705         struct addrbookent *addrbook = NULL;
1706         int num_ab = 0;
1707         char *sortby = NULL;
1708         char sortpref_name[128];
1709         char sortpref_value[128];
1710         char *subjsort_button;
1711         char *sendsort_button;
1712         char *datesort_button;
1713
1714         startmsg = atol(bstr("startmsg"));
1715         maxmsgs = atoi(bstr("maxmsgs"));
1716         is_summary = atoi(bstr("summary"));
1717         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1718
1719         snprintf(sortpref_name, sizeof sortpref_name, "sort %s", WC->wc_roomname);
1720         get_preference(sortpref_name, sortpref_value, sizeof sortpref_value);
1721
1722         sortby = bstr("sortby");
1723         if ( (strlen(sortby) > 0) && (strcasecmp(sortby, sortpref_value)) ) {
1724                 set_preference(sortpref_name, sortby, 1);
1725         }
1726         if (strlen(sortby) == 0) sortby = sortpref_value;
1727         if (strlen(sortby) == 0) sortby = "msgid";
1728
1729         output_headers(1, 1, 1, 0, 0, 0, 0);
1730
1731         /* When in summary mode, always show ALL messages instead of just
1732          * new or old.  Otherwise, show what the user asked for.
1733          */
1734         if (!strcmp(oper, "readnew")) {
1735                 strcpy(cmd, "MSGS NEW");
1736         }
1737         else if (!strcmp(oper, "readold")) {
1738                 strcpy(cmd, "MSGS OLD");
1739         }
1740         else {
1741                 strcpy(cmd, "MSGS ALL");
1742         }
1743
1744         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1745                 is_summary = 1;
1746                 strcpy(cmd, "MSGS ALL");
1747         }
1748
1749         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1750                 is_addressbook = 1;
1751                 strcpy(cmd, "MSGS ALL");
1752                 maxmsgs = 9999999;
1753         }
1754
1755         if (is_summary) {
1756                 strcpy(cmd, "MSGS ALL|||1");    /* fetch header summary */
1757                 startmsg = 1;
1758                 maxmsgs = 9999999;
1759         }
1760
1761         /* Are we doing a summary view?  If so, we need to know old messages
1762          * and new messages, so we can do that pretty boldface thing for the
1763          * new messages.
1764          */
1765         strcpy(old_msgs, "");
1766         if (is_summary) {
1767                 serv_puts("GTSN");
1768                 serv_getln(buf, sizeof buf);
1769                 if (buf[0] == '2') {
1770                         strcpy(old_msgs, &buf[4]);
1771                 }
1772         }
1773
1774         is_singlecard = atoi(bstr("is_singlecard"));
1775
1776         if (WC->wc_view == VIEW_CALENDAR) {             /* calendar */
1777                 is_calendar = 1;
1778                 strcpy(cmd, "MSGS ALL");
1779                 maxmsgs = 32767;
1780         }
1781         if (WC->wc_view == VIEW_TASKS) {                /* tasks */
1782                 is_tasks = 1;
1783                 strcpy(cmd, "MSGS ALL");
1784                 maxmsgs = 32767;
1785         }
1786         if (WC->wc_view == VIEW_NOTES) {                /* notes */
1787                 is_notes = 1;
1788                 strcpy(cmd, "MSGS ALL");
1789                 maxmsgs = 32767;
1790         }
1791
1792         nummsgs = load_msg_ptrs(cmd, is_summary);
1793         if (nummsgs == 0) {
1794
1795                 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1796                         wprintf("<em>");
1797                         if (!strcmp(oper, "readnew")) {
1798                                 wprintf(_("No new messages."));
1799                         } else if (!strcmp(oper, "readold")) {
1800                                 wprintf(_("No old messages."));
1801                         } else {
1802                                 wprintf(_("No messages here."));
1803                         }
1804                         wprintf("</em>\n");
1805                 }
1806
1807                 goto DONE;
1808         }
1809
1810         if (is_summary) {
1811                 for (a = 0; a < nummsgs; ++a) {
1812                         /* Are you a new message, or an old message? */
1813                         if (is_summary) {
1814                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1815                                         WC->summ[a].is_new = 0;
1816                                 }
1817                                 else {
1818                                         WC->summ[a].is_new = 1;
1819                                 }
1820                         }
1821                 }
1822         }
1823
1824         if (startmsg == 0L) startmsg = WC->msgarr[0];
1825         remaining_messages = 0;
1826
1827         for (a = 0; a < nummsgs; ++a) {
1828                 if (WC->msgarr[a] >= startmsg) {
1829                         ++remaining_messages;
1830                 }
1831         }
1832
1833         if (is_summary) {
1834                 if (!strcasecmp(sortby, "subject")) {
1835                         qsort(WC->summ, WC->num_summ,
1836                                 sizeof(struct message_summary), summcmp_subj);
1837                 }
1838                 else if (!strcasecmp(sortby, "rsubject")) {
1839                         qsort(WC->summ, WC->num_summ,
1840                                 sizeof(struct message_summary), summcmp_rsubj);
1841                 }
1842                 else if (!strcasecmp(sortby, "sender")) {
1843                         qsort(WC->summ, WC->num_summ,
1844                                 sizeof(struct message_summary), summcmp_sender);
1845                 }
1846                 else if (!strcasecmp(sortby, "rsender")) {
1847                         qsort(WC->summ, WC->num_summ,
1848                                 sizeof(struct message_summary), summcmp_rsender);
1849                 }
1850                 else if (!strcasecmp(sortby, "date")) {
1851                         qsort(WC->summ, WC->num_summ,
1852                                 sizeof(struct message_summary), summcmp_date);
1853                 }
1854                 else if (!strcasecmp(sortby, "rdate")) {
1855                         qsort(WC->summ, WC->num_summ,
1856                                 sizeof(struct message_summary), summcmp_rdate);
1857                 }
1858         }
1859
1860         if (!strcasecmp(sortby, "subject")) {
1861                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsubject\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1862         }
1863         else if (!strcasecmp(sortby, "rsubject")) {
1864                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1865         }
1866         else {
1867                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1868         }
1869
1870         if (!strcasecmp(sortby, "sender")) {
1871                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsender\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1872         }
1873         else if (!strcasecmp(sortby, "rsender")) {
1874                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1875         }
1876         else {
1877                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1878         }
1879
1880         if (!strcasecmp(sortby, "date")) {
1881                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1882         }
1883         else if (!strcasecmp(sortby, "rdate")) {
1884                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1885         }
1886         else {
1887                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1888         }
1889
1890         if (is_summary) {
1891                 wprintf("</div>");              /* end of 'content' div */
1892
1893                 wprintf("<div id=\"message_list\">"
1894
1895                         "<div id=\"fix_scrollbar_bug\">\n"
1896
1897                         "<form name=\"msgomatic\" "
1898                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n"
1899
1900                         "<table border=0 cellspacing=0 "
1901                         "cellpadding=0 width=100%%>\n"
1902                         "<TR>"
1903                         "<TD align=center><b><i>%s</i></b> %s</TD>"
1904                         "<TD align=center><b><i>%s</i></b> %s</TD>"
1905                         "<TD align=center><b><i>%s</i></b> %s</TD>"
1906                         "<TD><INPUT TYPE=\"submit\" NAME=\"delete_button\" "
1907                         "STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif;"
1908                         " font-size: 6pt;\" "
1909                         "VALUE=\"%s\"></TD>"
1910                         "</TR>\n"
1911                         ,
1912                         _("Subject"),   subjsort_button,
1913                         _("Sender"),    sendsort_button,
1914                         _("Date"),      datesort_button,
1915                         _("Delete")
1916                 );
1917         }
1918
1919         for (a = 0; a < nummsgs; ++a) {
1920                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1921
1922                         /* Learn which msgs "Prev" & "Next" buttons go to */
1923                         pn_current = WC->msgarr[a];
1924                         if (a > 0) pn_previous = WC->msgarr[a-1];
1925                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1926
1927                         /* If a tabular view, set up the line */
1928                         if (is_summary) {
1929                                 bg = 1 - bg;
1930                                 wprintf("<TR BGCOLOR=\"#%s\">",
1931                                         (bg ? "DDDDDD" : "FFFFFF")
1932                                 );
1933                         }
1934
1935                         /* Display the message */
1936                         if (is_summary) {
1937                                 display_summarized(a);
1938                         }
1939                         else if (is_addressbook) {
1940                                 fetch_ab_name(WC->msgarr[a], buf);
1941                                 ++num_ab;
1942                                 addrbook = realloc(addrbook,
1943                                         (sizeof(struct addrbookent) * num_ab) );
1944                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1945                                         sizeof(addrbook[num_ab-1].ab_name));
1946                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1947                         }
1948                         else if (is_calendar) {
1949                                 display_calendar(WC->msgarr[a]);
1950                         }
1951                         else if (is_tasks) {
1952                                 display_task(WC->msgarr[a]);
1953                         }
1954                         else if (is_notes) {
1955                                 display_note(WC->msgarr[a]);
1956                         }
1957                         else {
1958                                 read_message(WC->msgarr[a], 0);
1959                         }
1960
1961                         /* If a tabular view, finish the line */
1962                         if (is_summary) {
1963                                 wprintf("</TR>\n");
1964                         }
1965
1966                         if (lowest_displayed < 0) lowest_displayed = a;
1967                         highest_displayed = a;
1968
1969                         ++num_displayed;
1970                         --remaining_messages;
1971                 }
1972         }
1973
1974         if (is_summary) {
1975                 wprintf("</table></form>"
1976                         "</div>\n");                    /* end of 'fix_scrollbar_bug' div */
1977                 wprintf("</div>");                      /* end of 'message_list' div */
1978
1979                 wprintf("<div id=\"preview_pane\">");   /* The preview pane will initially be empty */
1980         }
1981
1982         /* Bump these because although we're thinking in zero base, the user
1983          * is a drooling idiot and is thinking in one base.
1984          */
1985         ++lowest_displayed;
1986         ++highest_displayed;
1987
1988         /* If we're only looking at one message, do a prev/next thing */
1989         if (num_displayed == 1) {
1990            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1991
1992                 wprintf("<div id=\"fix_scrollbar_bug\">"
1993                         "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>");
1994                 wprintf(_("Reading #%d of %d messages."), lowest_displayed, nummsgs);
1995                 wprintf("</TD><TD ALIGN=RIGHT><FONT SIZE=+1>");
1996
1997                 if (pn_previous > 0L) {
1998                         wprintf("<A HREF=\"/%s"
1999                                 "?startmsg=%ld"
2000                                 "?maxmsgs=1"
2001                                 "?summary=0\">"
2002                                 "%s</A> \n",
2003                                         oper,
2004                                         pn_previous,
2005                                         _("Previous"));
2006                 }
2007
2008                 if (pn_next > 0L) {
2009                         wprintf("<A HREF=\"/%s"
2010                                 "?startmsg=%ld"
2011                                 "?maxmsgs=1"
2012                                 "?summary=0\">"
2013                                 "%s</A> \n",
2014                                         oper,
2015                                         pn_next,
2016                                         _("Next"));
2017                 }
2018
2019                 wprintf("<A HREF=\"/%s?startmsg=%ld"
2020                         "?maxmsgs=%d?summary=1\">"
2021                         "%s"
2022                         "</A>",
2023                         oper,
2024                         WC->msgarr[0],
2025                         DEFAULT_MAXMSGS,
2026                         _("Summary")
2027                 );
2028
2029                 wprintf("</td></tr></table></div>\n");
2030             }
2031         }
2032
2033         /*
2034          * If we're not currently looking at ALL requested
2035          * messages, then display the selector bar
2036          */
2037         if (num_displayed > 1) {
2038            if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
2039               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
2040
2041                 wprintf("<form name=\"msgomatic\" "
2042                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n");
2043
2044                 wprintf(_("Reading #"), lowest_displayed, highest_displayed);
2045
2046                 wprintf("<select name=\"whichones\" size=\"1\" "
2047                         "OnChange=\"location.href=msgomatic.whichones.options"
2048                         "[selectedIndex].value\">\n");
2049
2050                 for (b=0; b<nummsgs; b = b + maxmsgs) {
2051                 lo = b+1;
2052                 hi = b+maxmsgs;
2053                 if (hi > nummsgs) hi = nummsgs;
2054                         wprintf("<option %s value="
2055                                 "\"/%s"
2056                                 "?startmsg=%ld"
2057                                 "?maxmsgs=%d"
2058                                 "?summary=%d\">"
2059                                 "%d-%d</option> \n",
2060                                 ((WC->msgarr[b] == startmsg) ? "selected" : ""),
2061                                 oper,
2062                                 WC->msgarr[b],
2063                                 maxmsgs,
2064                                 is_summary,
2065                                 lo, hi);
2066                 }
2067                 wprintf("<option value=\"/%s?startmsg=%ld"
2068                         "?maxmsgs=9999999?summary=%d\">"
2069                         "ALL"
2070                         "</option> ",
2071                         oper,
2072                         WC->msgarr[0], is_summary);
2073
2074                 wprintf("</select> ");
2075                 wprintf(_("of %d messages."), nummsgs);
2076                 wprintf("</form>\n");
2077             }
2078         }
2079
2080 DONE:
2081         if (is_tasks) {
2082                 do_tasks_view();        /* Render the task list */
2083         }
2084
2085         if (is_calendar) {
2086                 do_calendar_view();     /* Render the calendar */
2087         }
2088
2089         if (is_addressbook) {
2090                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
2091         }
2092
2093         /* Put the data transfer hidden iframe in a hidden div, to make it *really* hidden */
2094         wprintf("</div>"
2095                 "<div display=\"hidden\">\n"
2096                 "<iframe name=\"msgloader1\" id=\"msgloader1\" width=\"1\"></iframe>\n"
2097         );
2098
2099         /* Note: wDumpContent() will output one additional </div> tag. */
2100         wDumpContent(1);
2101         if (addrbook != NULL) free(addrbook);
2102
2103         /* free the summary */
2104         if (WC->num_summ != 0) {
2105                 WC->num_summ = 0;
2106                 free(WC->summ);
2107         }
2108
2109         /* If we got here via a mailbox view and are reading a single
2110          * message, mark it as "seen." We do this after rendering the web page
2111          * so it doesn't keep the user waiting.
2112          */
2113         if ( (maxmsgs == 1) && (WC->wc_view == VIEW_MAILBOX) ) {
2114                 serv_printf("SEEN %ld|1", startmsg);
2115                 serv_getln(buf, sizeof buf);
2116         }
2117 }
2118
2119
2120 /*
2121  * Back end for post_message() ... this is where the actual message
2122  * gets transmitted to the server.
2123  */
2124 void post_mime_to_server(void) {
2125         char boundary[SIZ];
2126         int is_multipart = 0;
2127         static int seq = 0;
2128         struct wc_attachment *att;
2129         char *encoded;
2130         size_t encoded_length;
2131
2132         /* RFC2045 requires this, and some clients look for it... */
2133         serv_puts("MIME-Version: 1.0");
2134
2135         /* If there are attachments, we have to do multipart/mixed */
2136         if (WC->first_attachment != NULL) {
2137                 is_multipart = 1;
2138         }
2139
2140         if (is_multipart) {
2141                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
2142                         serv_info.serv_fqdn,
2143                         getpid(),
2144                         ++seq
2145                 );
2146
2147                 /* Remember, serv_printf() appends an extra newline */
2148                 serv_printf("Content-type: multipart/mixed; "
2149                         "boundary=\"%s\"\n", boundary);
2150                 serv_printf("This is a multipart message in MIME format.\n");
2151                 serv_printf("--%s", boundary);
2152         }
2153
2154         serv_puts("Content-type: text/html; charset=utf-8");
2155         serv_puts("");
2156         serv_puts("<HTML><BODY>\n");
2157         text_to_server(bstr("msgtext"), 0);
2158         serv_puts("</BODY></HTML>\n");
2159         
2160
2161         if (is_multipart) {
2162
2163                 /* Add in the attachments */
2164                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
2165
2166                         encoded_length = ((att->length * 150) / 100);
2167                         encoded = malloc(encoded_length);
2168                         if (encoded == NULL) break;
2169                         CtdlEncodeBase64(encoded, att->data, att->length);
2170
2171                         serv_printf("--%s", boundary);
2172                         serv_printf("Content-type: %s", att->content_type);
2173                         serv_printf("Content-disposition: attachment; "
2174                                 "filename=\"%s\"", att->filename);
2175                         serv_puts("Content-transfer-encoding: base64");
2176                         serv_puts("");
2177                         serv_write(encoded, strlen(encoded));
2178                         serv_puts("");
2179                         serv_puts("");
2180                         free(encoded);
2181                 }
2182                 serv_printf("--%s--", boundary);
2183         }
2184
2185         serv_puts("000");
2186 }
2187
2188
2189 /*
2190  * Post message (or don't post message)
2191  *
2192  * Note regarding the "dont_post" variable:
2193  * A random value (actually, it's just a timestamp) is inserted as a hidden
2194  * field called "postseq" when the display_enter page is generated.  This
2195  * value is checked when posting, using the static variable dont_post.  If a
2196  * user attempts to post twice using the same dont_post value, the message is
2197  * discarded.  This prevents the accidental double-saving of the same message
2198  * if the user happens to click the browser "back" button.
2199  */
2200 void post_message(void)
2201 {
2202         char buf[SIZ];
2203         static long dont_post = (-1L);
2204         struct wc_attachment *att, *aptr;
2205
2206         if (WC->upload_length > 0) {
2207
2208                 /* There's an attachment.  Save it to this struct... */
2209                 att = malloc(sizeof(struct wc_attachment));
2210                 memset(att, 0, sizeof(struct wc_attachment));
2211                 att->length = WC->upload_length;
2212                 strcpy(att->content_type, WC->upload_content_type);
2213                 strcpy(att->filename, WC->upload_filename);
2214                 att->next = NULL;
2215
2216                 /* And add it to the list. */
2217                 if (WC->first_attachment == NULL) {
2218                         WC->first_attachment = att;
2219                 }
2220                 else {
2221                         aptr = WC->first_attachment;
2222                         while (aptr->next != NULL) aptr = aptr->next;
2223                         aptr->next = att;
2224                 }
2225
2226                 /* Mozilla sends a simple filename, which is what we want,
2227                  * but Satan's Browser sends an entire pathname.  Reduce
2228                  * the path to just a filename if we need to.
2229                  */
2230                 while (num_tokens(att->filename, '/') > 1) {
2231                         remove_token(att->filename, 0, '/');
2232                 }
2233                 while (num_tokens(att->filename, '\\') > 1) {
2234                         remove_token(att->filename, 0, '\\');
2235                 }
2236
2237                 /* Transfer control of this memory from the upload struct
2238                  * to the attachment struct.
2239                  */
2240                 att->data = WC->upload;
2241                 WC->upload_length = 0;
2242                 WC->upload = NULL;
2243                 display_enter();
2244                 return;
2245         }
2246
2247         if (strlen(bstr("cancel_button")) > 0) {
2248                 sprintf(WC->ImportantMessage, 
2249                         _("Cancelled.  Message was not posted."));
2250         } else if (strlen(bstr("attach_button")) > 0) {
2251                 display_enter();
2252                 return;
2253         } else if (atol(bstr("postseq")) == dont_post) {
2254                 sprintf(WC->ImportantMessage, 
2255                         _("Automatically cancelled because you have already "
2256                         "saved this message."));
2257         } else {
2258                 sprintf(buf, "ENT0 1|%s|0|4|%s",
2259                         bstr("recp"),
2260                         bstr("subject") );
2261                 serv_puts(buf);
2262                 serv_getln(buf, sizeof buf);
2263                 if (buf[0] == '4') {
2264                         post_mime_to_server();
2265                         if (strlen(bstr("recp")) > 0) {
2266                                 sprintf(WC->ImportantMessage, _("Message has been sent.\n"));
2267                         }
2268                         else {
2269                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
2270                         }
2271                         dont_post = atol(bstr("postseq"));
2272                 } else {
2273                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
2274                         display_enter();
2275                         return;
2276                 }
2277         }
2278
2279         free_attachments(WC);
2280         readloop("readnew");
2281 }
2282
2283
2284
2285
2286 /*
2287  * display the message entry screen
2288  */
2289 void display_enter(void)
2290 {
2291         char buf[SIZ];
2292         long now;
2293         struct wc_attachment *att;
2294         int recipient_required = 0;
2295         int recipient_bad = 0;
2296
2297         if (strlen(bstr("force_room")) > 0) {
2298                 gotoroom(bstr("force_room"));
2299         }
2300
2301         /* Are we perhaps in an address book view?  If so, then an "enter
2302          * message" command really means "add new entry."
2303          */
2304         if (WC->wc_view == VIEW_ADDRESSBOOK) {
2305                 do_edit_vcard(-1, "", "");
2306                 return;
2307         }
2308
2309 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
2310         /* Are we perhaps in a calendar view?  If so, then an "enter
2311          * message" command really means "add new calendar item."
2312          */
2313         if (WC->wc_view == VIEW_CALENDAR) {
2314                 display_edit_event();
2315                 return;
2316         }
2317
2318         /* Are we perhaps in a tasks view?  If so, then an "enter
2319          * message" command really means "add new task."
2320          */
2321         if (WC->wc_view == VIEW_TASKS) {
2322                 display_edit_task();
2323                 return;
2324         }
2325 #endif
2326
2327         /*
2328          * Otherwise proceed normally.
2329 `        * Do a custom room banner with no navbar...
2330          */
2331         output_headers(1, 1, 2, 0, 0, 0, 0);
2332         wprintf("<div id=\"banner\">\n");
2333         embed_room_banner(NULL, navbar_none);
2334         wprintf("</div>\n");
2335         wprintf("<div id=\"content\">\n"
2336                 "<div id=\"fix_scrollbar_bug\">"
2337                 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
2338
2339         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
2340         serv_puts(buf);
2341         serv_getln(buf, sizeof buf);
2342
2343         if (!strncmp(buf, "570", 3)) {
2344                 recipient_required = 1;
2345                 if (strlen(bstr("recp")) > 0) {
2346                         recipient_bad = 1;
2347                 }
2348         }
2349         else if (buf[0] != '2') {
2350                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2351                 goto DONE;
2352         }
2353
2354         now = time(NULL);
2355         fmt_date(buf, now, 0);
2356         strcat(&buf[strlen(buf)], _(" <I>from</I> "));
2357         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
2358         if (strlen(bstr("recp")) > 0) {
2359                 strcat(&buf[strlen(buf)], _(" <I>to</I> "));
2360                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
2361         }
2362         strcat(&buf[strlen(buf)], _(" <I>in</I> "));
2363         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
2364
2365         /* begin message entry screen */
2366
2367
2368         wprintf(
2369         "<style> div.auto_complete { width: 350px; background: #fff; } div.auto_complete ul { border:1px solid #888; margin:0; padding:0; width:100%; list-style-type:none; } div.auto_complete ul li { margin:0; padding:3px; } div.auto_complete ul li.selected { background-color: #ffc; } div.auto_complete ul strong.highlight { color: #800; margin:0; padding:0; } </style> \n "
2370         );
2371
2372         wprintf("<form enctype=\"multipart/form-data\" "
2373                 "method=\"POST\" action=\"/post\" "
2374                 "name=\"enterform\""
2375                 "onSubmit=\"return submitForm();\""
2376                 ">\n");
2377         wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
2378                 bstr("recp"));
2379         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
2380                 now);
2381
2382         wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
2383         wprintf("%s<br>\n", buf);       /* header bar */
2384
2385         wprintf("<table border=\"0\" width=\"100%%\">\n");
2386         if (recipient_required) {
2387                 wprintf("<tr><td>");
2388                 wprintf("<font size=-1>");
2389                 wprintf(_("To:"));
2390                 wprintf("</font>");
2391                 wprintf("</td><td>"
2392                         "<input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_name\" value=\"");
2393                 escputs(bstr("recp"));
2394                 wprintf("\" size=50 maxlength=70>");
2395         
2396                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
2397         
2398                 wprintf("<script type=\"text/javascript\">                                      "
2399                         " new Ajax.Autocompleter('recp_name', 'recp_name_choices',              "
2400                         "       '/recp_autocomplete', {} );                             "
2401                         "</script>\n                                                            "
2402                 );
2403
2404                 wprintf("</td><td></td></tr>\n");
2405         }
2406
2407         wprintf("<tr><td>");
2408         wprintf("<font size=-1>");
2409         wprintf(_("Subject (optional):"));
2410         wprintf("</font>");
2411         wprintf("</td><td>"
2412                 "<input type=\"text\" name=\"subject\" value=\"");
2413         escputs(bstr("subject"));
2414         wprintf("\" size=50 maxlength=70></td><td>\n");
2415
2416         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
2417         if (recipient_required) {
2418                 wprintf(_("Send message"));
2419         } else {
2420                 wprintf(_("Post message"));
2421         }
2422         wprintf("\">&nbsp;"
2423                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
2424         wprintf("</td></tr></table>\n");
2425
2426         wprintf("<center><script type=\"text/javascript\" "
2427                 "src=\"static/richtext.js\"></script>\n"
2428                 "<script type=\"text/javascript\">\n"
2429                 "function submitForm() { \n"
2430                 "  updateRTE('msgtext'); \n"
2431                 "  return true; \n"
2432                 "} \n"
2433                 "  \n"
2434                 "initRTE(\"static/\", \"static/\", \"\"); \n"
2435                 "</script> \n"
2436                 "<noscript>JavaScript must be enabled.</noscript> \n"
2437                 "<script type=\"text/javascript\"> \n"
2438                 "writeRichText('msgtext', '");
2439         msgescputs(bstr("msgtext"));
2440         if (atol(bstr("pullquote")) > 0L) {
2441                 wprintf("<br><div align=center><i>");
2442                 wprintf(_("--- forwarded message ---"));
2443                 wprintf("</i></div><br>");
2444                 pullquote_message(atol(bstr("pullquote")), 1);
2445         }
2446         wprintf("', '96%%', '200', true, false); \n"
2447                 "</script></center><br />\n");
2448
2449         /* Enumerate any attachments which are already in place... */
2450         wprintf("<img src=\"/static/diskette_24x.gif\" border=0 "
2451                 "align=middle height=16 width=16> Attachments: ");
2452         wprintf("<select name=\"which_attachment\" size=1>");
2453         for (att = WC->first_attachment; att != NULL; att = att->next) {
2454                 wprintf("<option value=\"");
2455                 urlescputs(att->filename);
2456                 wprintf("\">");
2457                 escputs(att->filename);
2458                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
2459                 wprintf("</option>\n");
2460         }
2461         wprintf("</select>");
2462
2463         /* Now offer the ability to attach additional files... */
2464         wprintf("&nbsp;&nbsp;&nbsp;"
2465                 "Attach file: <input NAME=\"attachfile\" "
2466                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
2467                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
2468
2469         /* Seth asked for these to be at the top *and* bottom... */
2470         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
2471         if (recipient_required) {
2472                 wprintf(_("Send message"));
2473         } else {
2474                 wprintf(_("Post message"));
2475         }
2476         wprintf("\">&nbsp;"
2477                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
2478
2479         wprintf("</form>\n");
2480
2481         wprintf("</td></tr></table></div>\n");
2482 DONE:   wDumpContent(1);
2483 }
2484
2485
2486
2487
2488
2489
2490
2491
2492 void delete_msg(void)
2493 {
2494         long msgid;
2495         char buf[SIZ];
2496
2497         msgid = atol(bstr("msgid"));
2498
2499         output_headers(1, 1, 1, 0, 0, 0, 0);
2500
2501         sprintf(buf, "DELE %ld", msgid);
2502         serv_puts(buf);
2503         serv_getln(buf, sizeof buf);
2504         wprintf("<EM>%s</EM><br />\n", &buf[4]);
2505
2506         wDumpContent(1);
2507 }
2508
2509
2510
2511
2512 /*
2513  * Confirm move of a message
2514  */
2515 void confirm_move_msg(void)
2516 {
2517         long msgid;
2518         char buf[SIZ];
2519         char targ[SIZ];
2520
2521         msgid = atol(bstr("msgid"));
2522
2523         output_headers(1, 1, 1, 0, 0, 0, 0);
2524
2525         wprintf("<div id=\"fix_scrollbar_bug\">"
2526                 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
2527         wprintf("<font size=+1 color=\"#ffffff\"");
2528         wprintf("<b>");
2529         wprintf(_("Confirm move of message"));
2530         wprintf("</b>\n");
2531         wprintf("</font></td></tr></table></div>\n");
2532
2533         wprintf("<CENTER>");
2534
2535         wprintf(_("Move this message to:"));
2536         wprintf("<br />\n");
2537
2538         wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
2539         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
2540                 bstr("msgid"));
2541
2542
2543         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
2544         serv_puts("LKRA");
2545         serv_getln(buf, sizeof buf);
2546         if (buf[0] == '1') {
2547                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2548                         extract_token(targ, buf, 0, '|', sizeof targ);
2549                         wprintf("<OPTION>");
2550                         escputs(targ);
2551                         wprintf("\n");
2552                 }
2553         }
2554         wprintf("</SELECT>\n");
2555         wprintf("<br />\n");
2556
2557         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
2558         wprintf("&nbsp;");
2559         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
2560         wprintf("</form></CENTER>\n");
2561
2562         wprintf("</CENTER>\n");
2563         wDumpContent(1);
2564 }
2565
2566
2567
2568 void move_msg(void)
2569 {
2570         long msgid;
2571         char buf[SIZ];
2572
2573         msgid = atol(bstr("msgid"));
2574
2575         output_headers(1, 1, 1, 0, 0, 0, 0);
2576
2577         if (strlen(bstr("move_button")) > 0) {
2578                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
2579                 serv_puts(buf);
2580                 serv_getln(buf, sizeof buf);
2581                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2582         } else {
2583                 wprintf("<EM>");
2584                 wprintf(_("The message was not moved."));
2585                 wprintf("</EM><br />\n");
2586         }
2587
2588         wDumpContent(1);
2589 }
2590
2591 /*
2592  * This gets called when a user selects multiple messages in a summary
2593  * list and then clicks to perform a transformation of some sort on them
2594  * (such as deleting them).
2595  */
2596 void do_stuff_to_msgs(void) {
2597         char buf[256];
2598
2599         struct stuff_t {
2600                 struct stuff_t *next;
2601                 long msgnum;
2602         };
2603
2604         struct stuff_t *stuff = NULL;
2605         struct stuff_t *ptr;
2606         int delete_button_pressed = 0;
2607
2608
2609         serv_puts("MSGS ALL");
2610         serv_getln(buf, sizeof buf);
2611
2612         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2613                 ptr = malloc(sizeof(struct stuff_t));
2614                 ptr->msgnum = atol(buf);
2615                 ptr->next = stuff;
2616                 stuff = ptr;
2617         }
2618
2619         if (strlen(bstr("delete_button")) > 0) {
2620                 delete_button_pressed = 1;
2621         }
2622
2623         while (stuff != NULL) {
2624
2625                 sprintf(buf, "msg_%ld", stuff->msgnum);
2626                 if (!strcasecmp(bstr(buf), "yes")) {
2627
2628                         if (delete_button_pressed) {
2629                                 serv_printf("DELE %ld", stuff->msgnum);
2630                                 serv_getln(buf, sizeof buf);
2631                         }
2632
2633                 }
2634
2635                 ptr = stuff->next;
2636                 free(stuff);
2637                 stuff = ptr;
2638         }
2639
2640         readloop("readfwd");
2641 }