* HTML messages in foreign character sets are now converted to UTF-8 for
[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 <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26
27 #ifdef HAVE_ICONV
28 #include <iconv.h>
29 #endif
30
31 #include "webcit.h"
32 #include "vcard.h"
33 #include "webserver.h"
34
35
36 /* Address book entry (keep it short and sweet, it's just a quickie lookup
37  * which we can use to get to the real meat and bones later)
38  */
39 struct addrbookent {
40         char ab_name[64];
41         long ab_msgnum;
42 };
43
44
45
46 #ifdef HAVE_ICONV
47 /* Handle subjects with RFC2047 encoding, such as:
48  * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
49  */
50 void utf8ify_rfc822_string(char *buf) {
51         char *start, *end;
52         char newbuf[1024];
53         char charset[128];
54         char encoding[16];
55         char istr[1024];
56         iconv_t ic = (iconv_t)(-1) ;
57         char *ibuf;                   /* Buffer of characters to be converted */
58         char *obuf;                   /* Buffer for converted characters      */
59         size_t ibuflen;               /* Length of input buffer               */
60         size_t obuflen;               /* Length of output buffer              */
61         char *isav;                   /* Saved pointer to input buffer        */
62         char *osav;                   /* Saved pointer to output buffer       */
63
64         while (start=strstr(buf, "=?"), end=strstr(buf, "?="),
65                 ((start != NULL) && (end != NULL) && (end > start)) )
66         {
67                 extract_token(charset, start, 1, '?', sizeof charset);
68                 extract_token(encoding, start, 2, '?', sizeof encoding);
69                 extract_token(istr, start, 3, '?', sizeof istr);
70
71                 strcpy(start, "");
72                 ++end;
73                 ++end;
74
75                 ibuf = malloc(1024);
76                 isav = ibuf;
77                 if (!strcasecmp(encoding, "B")) {       /* base64 */
78                         ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
79                 }
80                 else if (!strcasecmp(encoding, "Q")) {  /* quoted-printable */
81                         ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, strlen(istr));
82                 }
83                 else {
84                         strcpy(ibuf, istr);             /* huh? */
85                         ibuflen = strlen(istr);
86                 }
87
88                 ic = iconv_open("UTF-8", charset);
89                 if (ic != (iconv_t)(-1) ) {
90                         obuf = malloc(1024);
91                         obuflen = 1024;
92                         obuf = (char *) malloc(obuflen);
93                         osav = obuf;
94                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
95                         osav[1024-obuflen] = 0;
96                         snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
97                         strcpy(buf, newbuf);
98                         free(osav);
99                         iconv_close(ic);
100                 }
101                 else {
102                         snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
103                         strcpy(buf, newbuf);
104                 }
105
106                 free(isav);
107         }
108
109 }
110 #endif
111
112
113 /*
114  * Look for URL's embedded in a buffer and make them linkable.  We use a
115  * target window in order to keep the BBS session in its own window.
116  */
117 void url(buf)
118 char buf[];
119 {
120
121         int pos;
122         int start, end;
123         char ench;
124         char urlbuf[SIZ];
125         char outbuf[1024];
126
127         start = (-1);
128         end = strlen(buf);
129         ench = 0;
130
131         for (pos = 0; pos < strlen(buf); ++pos) {
132                 if (!strncasecmp(&buf[pos], "http://", 7))
133                         start = pos;
134                 if (!strncasecmp(&buf[pos], "ftp://", 6))
135                         start = pos;
136         }
137
138         if (start < 0)
139                 return;
140
141         if ((start > 0) && (buf[start - 1] == '<'))
142                 ench = '>';
143         if ((start > 0) && (buf[start - 1] == '['))
144                 ench = ']';
145         if ((start > 0) && (buf[start - 1] == '('))
146                 ench = ')';
147         if ((start > 0) && (buf[start - 1] == '{'))
148                 ench = '}';
149
150         for (pos = strlen(buf); pos > start; --pos) {
151                 if ((buf[pos] == ' ') || (buf[pos] == ench))
152                         end = pos;
153         }
154
155         strncpy(urlbuf, &buf[start], end - start);
156         urlbuf[end - start] = 0;
157
158         strncpy(outbuf, buf, start);
159         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
160                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
161         strcat(outbuf, &buf[end]);
162         if ( strlen(outbuf) < 250 )
163                 strcpy(buf, outbuf);
164 }
165
166
167 /*
168  * Turn a vCard "n" (name) field into something displayable.
169  */
170 void vcard_n_prettyize(char *name)
171 {
172         char *original_name;
173         int i;
174
175         original_name = strdup(name);
176         for (i=0; i<5; ++i) {
177                 if (strlen(original_name) > 0) {
178                         if (original_name[strlen(original_name)-1] == ' ') {
179                                 original_name[strlen(original_name)-1] = 0;
180                         }
181                         if (original_name[strlen(original_name)-1] == ';') {
182                                 original_name[strlen(original_name)-1] = 0;
183                         }
184                 }
185         }
186         strcpy(name, "");
187         for (i=0; i<strlen(original_name); ++i) {
188                 if (original_name[i] == ';') {
189                         strcat(name, ", ");
190                 }
191                 else {
192                         name[strlen(name)+1] = 0;
193                         name[strlen(name)] = original_name[i];
194                 }
195         }
196         free(original_name);
197 }
198
199
200
201
202 /* display_vcard() calls this after parsing the textual vCard into
203  * our 'struct vCard' data object.
204  * This gets called instead of display_parsed_vcard() if we are only looking
205  * to extract the person's name instead of displaying the card.
206  */
207 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
208         char *name;
209
210         strcpy(storename, "");
211
212         name = vcard_get_prop(v, "n", 1, 0, 0);
213         if (name != NULL) {
214                 strcpy(storename, name);
215                 /* vcard_n_prettyize(storename); */
216         }
217
218 }
219
220
221
222 /* display_vcard() calls this after parsing the textual vCard into
223  * our 'struct vCard' data object.
224  *
225  * Set 'full' to nonzero to display the full card, otherwise it will only
226  * show a summary line.
227  *
228  * This code is a bit ugly, so perhaps an explanation is due: we do this
229  * in two passes through the vCard fields.  On the first pass, we process
230  * fields we understand, and then render them in a pretty fashion at the
231  * end.  Then we make a second pass, outputting all the fields we don't
232  * understand in a simple two-column name/value format.
233  */
234 void display_parsed_vcard(struct vCard *v, int full) {
235         int i, j;
236         char buf[SIZ];
237         char *name;
238         int is_qp = 0;
239         int is_b64 = 0;
240         char *thisname, *thisvalue;
241         char firsttoken[SIZ];
242         int pass;
243
244         char displayname[SIZ];
245         char title[SIZ];
246         char org[SIZ];
247         char phone[SIZ];
248         char mailto[SIZ];
249
250         strcpy(displayname, "");
251         strcpy(phone, "");
252         strcpy(mailto, "");
253         strcpy(title, "");
254         strcpy(org, "");
255
256         if (!full) {
257                 wprintf("<TD>");
258                 name = vcard_get_prop(v, "fn", 1, 0, 0);
259                 if (name != NULL) {
260                         escputs(name);
261                 }
262                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
263                         strcpy(displayname, name);
264                         vcard_n_prettyize(displayname);
265                         escputs(displayname);
266                 }
267                 else {
268                         wprintf("&nbsp;");
269                 }
270                 wprintf("</TD>");
271                 return;
272         }
273
274         wprintf("<div align=center><table bgcolor=#aaaaaa width=50%%>");
275         for (pass=1; pass<=2; ++pass) {
276
277                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
278
279                         thisname = strdup(v->prop[i].name);
280                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
281         
282                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
283                                 extract_token(buf, thisname, j, ';', sizeof buf);
284                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
285                                         is_qp = 1;
286                                         remove_token(thisname, j, ';');
287                                 }
288                                 if (!strcasecmp(buf, "encoding=base64")) {
289                                         is_b64 = 1;
290                                         remove_token(thisname, j, ';');
291                                 }
292                         }
293         
294                         if (is_qp) {
295                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
296                                 j = CtdlDecodeQuotedPrintable(
297                                         thisvalue, v->prop[i].value,
298                                         strlen(v->prop[i].value) );
299                                 thisvalue[j] = 0;
300                         }
301                         else if (is_b64) {
302                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
303                                 CtdlDecodeBase64(
304                                         thisvalue, v->prop[i].value,
305                                         strlen(v->prop[i].value) );
306                         }
307                         else {
308                                 thisvalue = strdup(v->prop[i].value);
309                         }
310         
311                         /*** Various fields we may encounter ***/
312         
313                         /* N is name, but only if there's no FN already there */
314                         if (!strcasecmp(firsttoken, "n")) {
315                                 if (strlen(displayname) == 0) {
316                                         strcpy(displayname, thisvalue);
317                                         vcard_n_prettyize(displayname);
318                                 }
319                         }
320         
321                         /* FN (full name) is a true 'display name' field */
322                         else if (!strcasecmp(firsttoken, "fn")) {
323                                 strcpy(displayname, thisvalue);
324                         }
325
326                         /* title */
327                         else if (!strcasecmp(firsttoken, "title")) {
328                                 strcpy(title, thisvalue);
329                         }
330         
331                         /* organization */
332                         else if (!strcasecmp(firsttoken, "org")) {
333                                 strcpy(org, thisvalue);
334                         }
335         
336                         else if (!strcasecmp(firsttoken, "email")) {
337                                 if (strlen(mailto) > 0) strcat(mailto, "<br />");
338                                 strcat(mailto,
339                                         "<A HREF=\"/display_enter"
340                                         "?force_room=_MAIL_&recp=");
341                                 urlesc(&mailto[strlen(mailto)], thisvalue);
342                                 strcat(mailto, "\">");
343                                 urlesc(&mailto[strlen(mailto)], thisvalue);
344                                 strcat(mailto, "</A>");
345                         }
346                         else if (!strcasecmp(firsttoken, "tel")) {
347                                 if (strlen(phone) > 0) strcat(phone, "<br />");
348                                 strcat(phone, thisvalue);
349                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
350                                         extract_token(buf, thisname, j, ';', sizeof buf);
351                                         if (!strcasecmp(buf, "tel"))
352                                                 strcat(phone, "");
353                                         else if (!strcasecmp(buf, "work"))
354                                                 strcat(phone, " (work)");
355                                         else if (!strcasecmp(buf, "home"))
356                                                 strcat(phone, " (home)");
357                                         else if (!strcasecmp(buf, "cell"))
358                                                 strcat(phone, " (cell)");
359                                         else {
360                                                 strcat(phone, " (");
361                                                 strcat(phone, buf);
362                                                 strcat(phone, ")");
363                                         }
364                                 }
365                         }
366                         else if (!strcasecmp(firsttoken, "adr")) {
367                                 if (pass == 2) {
368                                         wprintf("<TR><TD>Address:</TD><TD>");
369                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
370                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
371                                                 if (strlen(buf) > 0) {
372                                                         escputs(buf);
373                                                         if (j<3) wprintf("<br />");
374                                                         else wprintf(" ");
375                                                 }
376                                         }
377                                         wprintf("</TD></TR>\n");
378                                 }
379                         }
380                         else if (!strcasecmp(firsttoken, "version")) {
381                                 /* ignore */
382                         }
383                         else if (!strcasecmp(firsttoken, "rev")) {
384                                 /* ignore */
385                         }
386                         else if (!strcasecmp(firsttoken, "label")) {
387                                 /* ignore */
388                         }
389                         else {
390
391                                 /*** Don't show extra fields.  They're ugly.
392                                 if (pass == 2) {
393                                         wprintf("<TR><TD>");
394                                         escputs(thisname);
395                                         wprintf("</TD><TD>");
396                                         escputs(thisvalue);
397                                         wprintf("</TD></TR>\n");
398                                 }
399                                 ***/
400                         }
401         
402                         free(thisname);
403                         free(thisvalue);
404                 }
405         
406                 if (pass == 1) {
407                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
408                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
409                         "<IMG ALIGN=CENTER SRC=\"/static/viewcontacts_48x.gif\">"
410                         "<FONT SIZE=+1><B>");
411                         escputs(displayname);
412                         wprintf("</B></FONT>");
413                         if (strlen(title) > 0) {
414                                 wprintf("<div align=right>");
415                                 escputs(title);
416                                 wprintf("</div>");
417                         }
418                         if (strlen(org) > 0) {
419                                 wprintf("<div align=right>");
420                                 escputs(org);
421                                 wprintf("</div>");
422                         }
423                         wprintf("</TD></TR>\n");
424                 
425                         if (strlen(phone) > 0)
426                                 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
427                         if (strlen(mailto) > 0)
428                                 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
429                 }
430
431         }
432
433         wprintf("</table></div>\n");
434 }
435
436
437
438 /*
439  * Display a textual vCard
440  * (Converts to a vCard object and then calls the actual display function)
441  * Set 'full' to nonzero to display the whole card instead of a one-liner.
442  * Or, if "storename" is non-NULL, just store the person's name in that
443  * buffer instead of displaying the card at all.
444  */
445 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
446         struct vCard *v;
447         char *name;
448         char buf[SIZ];
449         char this_alpha = 0;
450
451         v = vcard_load(vcard_source);
452         if (v == NULL) return;
453
454         name = vcard_get_prop(v, "n", 1, 0, 0);
455         if (name != NULL) {
456                 strcpy(buf, name);
457                 this_alpha = buf[0];
458         }
459
460         if (storename != NULL) {
461                 fetchname_parsed_vcard(v, storename);
462         }
463         else if (       (alpha == 0)
464                         || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
465                         || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
466                 ) {
467                 display_parsed_vcard(v, full);
468         }
469
470         vcard_free(v);
471 }
472
473
474
475
476 /*
477  * I wanna SEE that message!
478  */
479 void read_message(long msgnum) {
480         char buf[SIZ];
481         char mime_partnum[256];
482         char mime_filename[256];
483         char mime_content_type[256];
484         char mime_charset[256];
485         char mime_disposition[256];
486         int mime_length;
487         char mime_http[SIZ];
488         char m_subject[256];
489         char from[256];
490         char node[256];
491         char rfca[256];
492         char reply_to[512];
493         char now[256];
494         int format_type = 0;
495         int nhdr = 0;
496         int bq = 0;
497         int i = 0;
498         char vcard_partnum[256];
499         char cal_partnum[256];
500         char *part_source = NULL;
501 #ifdef HAVE_ICONV
502         iconv_t ic = (iconv_t)(-1) ;
503         char *ibuf;                   /* Buffer of characters to be converted */
504         char *obuf;                   /* Buffer for converted characters      */
505         size_t ibuflen;               /* Length of input buffer               */
506         size_t obuflen;               /* Length of output buffer              */
507         char *osav;                   /* Saved pointer to output buffer       */
508 #endif
509
510         strcpy(from, "");
511         strcpy(node, "");
512         strcpy(rfca, "");
513         strcpy(reply_to, "");
514         strcpy(vcard_partnum, "");
515         strcpy(cal_partnum, "");
516         strcpy(mime_http, "");
517         strcpy(mime_content_type, "text/plain");
518         strcpy(mime_charset, "us-ascii");
519
520         serv_printf("MSG4 %ld", msgnum);
521         serv_getln(buf, sizeof buf);
522         if (buf[0] != '1') {
523                 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
524                 return;
525         }
526
527         /* begin everythingamundo table */
528         wprintf("<div id=\"fix_scrollbar_bug\">\n");
529         wprintf("<table width=100%% border=1 cellspacing=0 "
530                 "cellpadding=0><TR><TD>\n");
531
532         /* begin message header table */
533         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
534                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
535
536         wprintf("<SPAN CLASS=\"message_header\">");
537         strcpy(m_subject, "");
538
539         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
540                 if (!strcmp(buf, "000")) {
541                         wprintf("<I>unexpected end of message</I><br /><br />\n");
542                         wprintf("</SPAN>\n");
543                         return;
544                 }
545                 if (!strncasecmp(buf, "nhdr=yes", 8))
546                         nhdr = 1;
547                 if (nhdr == 1)
548                         buf[0] = '_';
549                 if (!strncasecmp(buf, "type=", 5))
550                         format_type = atoi(&buf[5]);
551                 if (!strncasecmp(buf, "from=", 5)) {
552                         strcpy(from, &buf[5]);
553                         wprintf("from <A HREF=\"/showuser&who=");
554 #ifdef HAVE_ICONV
555                         utf8ify_rfc822_string(from);
556 #endif
557                         urlescputs(from);
558                         wprintf("\">");
559                         escputs(from);
560                         wprintf("</A> ");
561                 }
562                 if (!strncasecmp(buf, "subj=", 5))
563                         strcpy(m_subject, &buf[5]);
564                 if ((!strncasecmp(buf, "hnod=", 5))
565                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
566                         wprintf("(%s) ", &buf[5]);
567                 if ((!strncasecmp(buf, "room=", 5))
568                     && (strcasecmp(&buf[5], WC->wc_roomname))
569                     && (strlen(&buf[5])>0) )
570                         wprintf("in %s> ", &buf[5]);
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 %s ", &buf[5]);
590                 if (!strncasecmp(buf, "time=", 5)) {
591                         fmt_date(now, atol(&buf[5]), 0);
592                         wprintf("%s ", now);
593                 }
594
595                 if (!strncasecmp(buf, "part=", 5)) {
596                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
597                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
598                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
599                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
600                         mime_length = extract_int(&buf[5], 5);
601
602                         if (!strcasecmp(mime_disposition, "attachment")) {
603                                 snprintf(&mime_http[strlen(mime_http)],
604                                         (sizeof(mime_http) - strlen(mime_http) - 1),
605                                         "<A HREF=\"/output_mimepart?"
606                                         "msgnum=%ld&partnum=%s\" "
607                                         "TARGET=\"wc.%ld.%s\">"
608                                         "<IMG SRC=\"/static/diskette_24x.gif\" "
609                                         "BORDER=0 ALIGN=MIDDLE>\n"
610                                         "Part %s: %s (%s, %d bytes)</A><br />\n",
611                                         msgnum, mime_partnum,
612                                         msgnum, mime_partnum,
613                                         mime_partnum, mime_filename,
614                                         mime_content_type, mime_length);
615                         }
616
617                         if ((!strcasecmp(mime_disposition, "inline"))
618                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
619                                 snprintf(&mime_http[strlen(mime_http)],
620                                         (sizeof(mime_http) - strlen(mime_http) - 1),
621                                         "<IMG SRC=\"/output_mimepart?"
622                                         "msgnum=%ld&partnum=%s\">",
623                                         msgnum, mime_partnum);
624                         }
625
626                         /*** begin handler prep ***/
627                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
628                                 strcpy(vcard_partnum, mime_partnum);
629                         }
630
631                         if (!strcasecmp(mime_content_type, "text/calendar")) {
632                                 strcpy(cal_partnum, mime_partnum);
633                         }
634
635                         /*** end handler prep ***/
636
637                 }
638
639         }
640
641         /* Generate a reply-to address */
642         if (strlen(rfca) > 0) {
643                 strcpy(reply_to, rfca);
644         }
645         else {
646                 if ( (strlen(node) > 0)
647                    && (strcasecmp(node, serv_info.serv_nodename))
648                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
649                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
650                                 from, node);
651                 }
652                 else {
653                         snprintf(reply_to, sizeof(reply_to), "%s", from);
654                 }
655         }
656
657         if (nhdr == 1) {
658                 wprintf("****");
659         }
660
661         wprintf("</SPAN>");
662 #ifdef HAVE_ICONV
663         utf8ify_rfc822_string(m_subject);
664 #endif
665         if (strlen(m_subject) > 0) {
666                 wprintf("<br />"
667                         "<SPAN CLASS=\"message_subject\">"
668                         "Subject: %s"
669                         "</SPAN>", m_subject
670                 );
671         }
672         wprintf("</TD>\n");
673
674         /* start msg buttons */
675         wprintf("<TD ALIGN=RIGHT>\n");
676
677         /* Reply */
678         wprintf("<a href=\"/display_enter?recp=");
679         urlescputs(reply_to);
680         wprintf("&subject=");
681         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
682         urlescputs(m_subject);
683         wprintf("\">[Reply]</a> ");
684
685         if (WC->is_room_aide)  {
686         
687                 /* Move */
688                 wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
689                         msgnum);
690
691                 /* Delete */
692                 wprintf("<a href=\"/delete_msg?msgid=%ld\" "
693                         "onClick=\"return confirm('Delete this message?');\">"
694                         "[Delete]</a>", msgnum);
695                         
696         }
697
698         wprintf("</TD></TR></TABLE>\n");
699
700         /* Begin body */
701         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
702                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
703
704         /* 
705          * Learn the content type
706          */
707         strcpy(mime_content_type, "text/plain");
708         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
709                 if (!strcmp(buf, "000")) {
710                         wprintf("<I>unexpected end of message</I><br /><br />\n");
711                         goto ENDBODY;
712                 }
713                 if (!strncasecmp(buf, "Content-type: ", 14)) {
714                         safestrncpy(mime_content_type, &buf[14],
715                                 sizeof(mime_content_type));
716                         for (i=0; i<strlen(mime_content_type); ++i) {
717                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
718                                         safestrncpy(mime_charset, &mime_content_type[i+8],
719                                                 sizeof mime_charset);
720                                 }
721                         }
722                         for (i=0; i<strlen(mime_content_type); ++i) {
723                                 if (mime_content_type[i] == ';') {
724                                         mime_content_type[i] = 0;
725                                 }
726                         }
727                 }
728         }
729
730         /* Set up a character set conversion if we need to (and if we can) */
731 #ifdef HAVE_ICONV
732         if ( (strcasecmp(mime_charset, "us-ascii"))
733            && (strcasecmp(mime_charset, "UTF-8")) ) {
734                 ic = iconv_open("UTF-8", mime_charset);
735                 if (ic == (iconv_t)(-1) ) {
736                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
737                 }
738         }
739 #endif
740
741         /* Messages in legacy Citadel variformat get handled thusly... */
742         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
743                 fmout(NULL, "JUSTIFY");
744         }
745
746         /* Boring old 80-column fixed format text gets handled this way... */
747         else if (!strcasecmp(mime_content_type, "text/plain")) {
748                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
749                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
750                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
751
752 #ifdef HAVE_ICONV
753                         if (ic != (iconv_t)(-1) ) {
754                                 ibuf = buf;
755                                 ibuflen = strlen(ibuf);
756                                 obuflen = SIZ;
757                                 obuf = (char *) malloc(obuflen);
758                                 osav = obuf;
759                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
760                                 osav[SIZ-obuflen] = 0;
761                                 safestrncpy(buf, osav, sizeof buf);
762                                 free(osav);
763                         }
764 #endif
765
766                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
767                                 buf[strlen(buf) - 1] = 0;
768                         if ((bq == 0) &&
769                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
770                                 wprintf("<BLOCKQUOTE>");
771                                 bq = 1;
772                         } else if ((bq == 1) &&
773                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
774                                 wprintf("</BLOCKQUOTE>");
775                                 bq = 0;
776                         }
777                         wprintf("<TT>");
778                         url(buf);
779                         escputs(buf);
780                         wprintf("</TT><br />\n");
781                 }
782                 wprintf("</I><br />");
783         }
784
785         else /* HTML is fun, but we've got to strip it first */
786         if (!strcasecmp(mime_content_type, "text/html")) {
787                 output_html(mime_charset);
788         }
789
790         /* Unknown weirdness */
791         else {
792                 wprintf("I don't know how to display %s<br />\n",
793                         mime_content_type);
794                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
795         }
796
797         /* Afterwards, offer links to download attachments 'n' such */
798         if (strlen(mime_http) > 0) {
799                 wprintf("%s", mime_http);
800         }
801
802         /* Handler for vCard parts */
803         if (strlen(vcard_partnum) > 0) {
804                 part_source = load_mimepart(msgnum, vcard_partnum);
805                 if (part_source != NULL) {
806
807                         /* If it's my vCard I can edit it */
808                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
809                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
810                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
811                         ) {
812                                 wprintf("<A HREF=\"/edit_vcard?"
813                                         "msgnum=%ld&partnum=%s\">",
814                                         msgnum, vcard_partnum);
815                                 wprintf("[edit]</A>");
816                         }
817
818                         /* In all cases, display the full card */
819                         display_vcard(part_source, 0, 1, NULL);
820                 }
821         }
822
823         /* Handler for calendar parts */
824         if (strlen(cal_partnum) > 0) {
825                 part_source = load_mimepart(msgnum, cal_partnum);
826                 if (part_source != NULL) {
827                         cal_process_attachment(part_source,
828                                                 msgnum, cal_partnum);
829                 }
830         }
831
832         if (part_source) {
833                 free(part_source);
834                 part_source = NULL;
835         }
836
837 ENDBODY:
838         wprintf("</TD></TR></TABLE>\n");
839
840         /* end everythingamundo table */
841         wprintf("</TD></TR></TABLE>\n");
842         wprintf("</div><br />\n");
843
844 #ifdef HAVE_ICONV
845         if (ic != (iconv_t)(-1) ) {
846                 iconv_close(ic);
847         }
848 #endif
849 }
850
851
852 void summarize_message(long msgnum, int is_new) {
853         char buf[SIZ];
854
855         struct {
856                 char date[SIZ];
857                 char from[SIZ];
858                 char to[SIZ];
859                 char subj[SIZ];
860                 int hasattachments;
861         } summ;
862
863         memset(&summ, 0, sizeof(summ));
864         strcpy(summ.subj, "(no subject)");
865
866         /* ask for headers only with no MIME */
867         sprintf(buf, "MSG0 %ld|3", msgnum);
868         serv_puts(buf);
869         serv_getln(buf, sizeof buf);
870         if (buf[0] != '1') return;
871
872         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
873                 if (!strncasecmp(buf, "from=", 5)) {
874                         strcpy(summ.from, &buf[5]);
875                 }
876                 if (!strncasecmp(buf, "subj=", 5)) {
877                         if (strlen(&buf[5]) > 0) {
878                                 strcpy(summ.subj, &buf[5]);
879 #ifdef HAVE_ICONV
880                                 /* Handle subjects with RFC2047 encoding */
881                                 utf8ify_rfc822_string(summ.subj);
882 #endif
883                                 if (strlen(summ.subj) > 75) {
884                                         strcpy(&summ.subj[72], "...");
885                                 }
886                         }
887                 }
888                 /* if (!strncasecmp(buf, "rfca=", 5)) {
889                         strcat(summ.from, " <");
890                         strcat(summ.from, &buf[5]);
891                         strcat(summ.from, ">");
892                 } */
893
894                 if (!strncasecmp(buf, "node=", 5)) {
895                         if ( ((WC->room_flags & QR_NETWORK)
896                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
897                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
898                         ) {
899                                 strcat(summ.from, " @ ");
900                                 strcat(summ.from, &buf[5]);
901                         }
902                 }
903
904                 if (!strncasecmp(buf, "rcpt=", 5)) {
905                         strcpy(summ.to, &buf[5]);
906                 }
907
908                 if (!strncasecmp(buf, "time=", 5)) {
909                         fmt_date(summ.date, atol(&buf[5]), 1);  /* brief */
910                 }
911         }
912         
913 #ifdef HAVE_ICONV
914         /* Handle senders with RFC2047 encoding */
915         utf8ify_rfc822_string(summ.from);
916 #endif
917         if (strlen(summ.from) > 25) {
918                 strcpy(&summ.from[22], "...");
919         }
920
921         wprintf("<TD>");
922         if (is_new) wprintf("<B>");
923         wprintf("<A HREF=\"/readfwd?startmsg=%ld"
924                 "&maxmsgs=1&summary=0\">", 
925                 msgnum);
926         escputs(summ.subj);
927         wprintf("</A>");
928         if (is_new) wprintf("</B>");
929         wprintf("</TD><TD>");
930         if (is_new) wprintf("<B>");
931         escputs(summ.from);
932         if (is_new) wprintf("</B>");
933         wprintf(" </TD><TD>");
934         if (is_new) wprintf("<B>");
935         escputs(summ.date);
936         if (is_new) wprintf("</B>");
937         wprintf(" </TD>");
938         wprintf("<TD>"
939                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
940                 "</TD>\n");
941
942         return;
943 }
944
945
946
947 void display_addressbook(long msgnum, char alpha) {
948         char buf[SIZ];
949         char mime_partnum[SIZ];
950         char mime_filename[SIZ];
951         char mime_content_type[SIZ];
952         char mime_disposition[SIZ];
953         int mime_length;
954         char vcard_partnum[SIZ];
955         char *vcard_source = NULL;
956
957         struct {
958                 char date[SIZ];
959                 char from[SIZ];
960                 char to[SIZ];
961                 char subj[SIZ];
962                 int hasattachments;
963         } summ;
964
965         memset(&summ, 0, sizeof(summ));
966         strcpy(summ.subj, "(no subject)");
967
968         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
969         serv_puts(buf);
970         serv_getln(buf, sizeof buf);
971         if (buf[0] != '1') return;
972
973         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
974                 if (!strncasecmp(buf, "part=", 5)) {
975                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
976                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
977                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
978                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
979                         mime_length = extract_int(&buf[5], 5);
980
981                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
982                                 strcpy(vcard_partnum, mime_partnum);
983                         }
984
985                 }
986         }
987
988         if (strlen(vcard_partnum) > 0) {
989                 vcard_source = load_mimepart(msgnum, vcard_partnum);
990                 if (vcard_source != NULL) {
991
992                         /* Display the summary line */
993                         display_vcard(vcard_source, alpha, 0, NULL);
994
995                         /* If it's my vCard I can edit it */
996                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
997                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
998                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
999                         ) {
1000                                 wprintf("<A HREF=\"/edit_vcard?"
1001                                         "msgnum=%ld&partnum=%s\">",
1002                                         msgnum, vcard_partnum);
1003                                 wprintf("[edit]</A>");
1004                         }
1005
1006                         free(vcard_source);
1007                 }
1008         }
1009
1010 }
1011
1012
1013
1014 /* If it's an old "Firstname Lastname" style record, try to
1015  * convert it.
1016  */
1017 void lastfirst_firstlast(char *namebuf) {
1018         char firstname[SIZ];
1019         char lastname[SIZ];
1020         int i;
1021
1022         if (namebuf == NULL) return;
1023         if (strchr(namebuf, ';') != NULL) return;
1024
1025         i = num_tokens(namebuf, ' ');
1026         if (i < 2) return;
1027
1028         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1029         remove_token(namebuf, i-1, ' ');
1030         strcpy(firstname, namebuf);
1031         sprintf(namebuf, "%s; %s", lastname, firstname);
1032 }
1033
1034
1035 void fetch_ab_name(long msgnum, char *namebuf) {
1036         char buf[SIZ];
1037         char mime_partnum[SIZ];
1038         char mime_filename[SIZ];
1039         char mime_content_type[SIZ];
1040         char mime_disposition[SIZ];
1041         int mime_length;
1042         char vcard_partnum[SIZ];
1043         char *vcard_source = NULL;
1044         int i;
1045
1046         struct {
1047                 char date[SIZ];
1048                 char from[SIZ];
1049                 char to[SIZ];
1050                 char subj[SIZ];
1051                 int hasattachments;
1052         } summ;
1053
1054         if (namebuf == NULL) return;
1055         strcpy(namebuf, "");
1056
1057         memset(&summ, 0, sizeof(summ));
1058         strcpy(summ.subj, "(no subject)");
1059
1060         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1061         serv_puts(buf);
1062         serv_getln(buf, sizeof buf);
1063         if (buf[0] != '1') return;
1064
1065         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1066                 if (!strncasecmp(buf, "part=", 5)) {
1067                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1068                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1069                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1070                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1071                         mime_length = extract_int(&buf[5], 5);
1072
1073                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1074                                 strcpy(vcard_partnum, mime_partnum);
1075                         }
1076
1077                 }
1078         }
1079
1080         if (strlen(vcard_partnum) > 0) {
1081                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1082                 if (vcard_source != NULL) {
1083
1084                         /* Grab the name off the card */
1085                         display_vcard(vcard_source, 0, 0, namebuf);
1086
1087                         free(vcard_source);
1088                 }
1089         }
1090
1091         lastfirst_firstlast(namebuf);
1092         striplt(namebuf);
1093         for (i=0; i<strlen(namebuf); ++i) {
1094                 if (namebuf[i] != ';') return;
1095         }
1096         strcpy(namebuf, "(no name)");
1097 }
1098
1099
1100
1101 /*
1102  * Record compare function for sorting address book indices
1103  */
1104 int abcmp(const void *ab1, const void *ab2) {
1105         return(strcasecmp(
1106                 (((const struct addrbookent *)ab1)->ab_name),
1107                 (((const struct addrbookent *)ab2)->ab_name)
1108         ));
1109 }
1110
1111
1112 /*
1113  * Helper function for do_addrbook_view()
1114  * Converts a name into a three-letter tab label
1115  */
1116 void nametab(char *tabbuf, char *name) {
1117         stresc(tabbuf, name, 0, 0);
1118         tabbuf[0] = toupper(tabbuf[0]);
1119         tabbuf[1] = tolower(tabbuf[1]);
1120         tabbuf[2] = tolower(tabbuf[2]);
1121         tabbuf[3] = 0;
1122 }
1123
1124
1125 /*
1126  * Render the address book using info we gathered during the scan
1127  */
1128 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
1129         int i = 0;
1130         int displayed = 0;
1131         int bg = 0;
1132         static int NAMESPERPAGE = 60;
1133         int num_pages = 0;
1134         int page = 0;
1135         int tabfirst = 0;
1136         char tabfirst_label[SIZ];
1137         int tablast = 0;
1138         char tablast_label[SIZ];
1139
1140         if (num_ab == 0) {
1141                 wprintf("<I>This address book is empty.</I>\n");
1142                 return;
1143         }
1144
1145         if (num_ab > 1) {
1146                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
1147         }
1148
1149         num_pages = num_ab / NAMESPERPAGE;
1150
1151         page = atoi(bstr("page"));
1152
1153         wprintf("Page: ");
1154         for (i=0; i<=num_pages; ++i) {
1155                 if (i != page) {
1156                         wprintf("<A HREF=\"/readfwd?page=%d\">", i);
1157                 }
1158                 else {
1159                         wprintf("<B>");
1160                 }
1161                 tabfirst = i * NAMESPERPAGE;
1162                 tablast = tabfirst + NAMESPERPAGE - 1;
1163                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1164                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1165                 nametab(tablast_label, addrbook[tablast].ab_name);
1166                 wprintf("[%s&nbsp;-&nbsp;%s]",
1167                         tabfirst_label, tablast_label
1168                 );
1169                 if (i != page) {
1170                         wprintf("</A>\n");
1171                 }
1172                 else {
1173                         wprintf("</B>\n");
1174                 }
1175         }
1176         wprintf("<br />\n");
1177
1178         wprintf("<TABLE border=0 cellspacing=0 "
1179                 "cellpadding=3 width=100%%>\n"
1180         );
1181
1182         for (i=0; i<num_ab; ++i) {
1183
1184                 if ((i / NAMESPERPAGE) == page) {
1185
1186                         if ((displayed % 4) == 0) {
1187                                 if (displayed > 0) {
1188                                         wprintf("</TR>\n");
1189                                 }
1190                                 bg = 1 - bg;
1191                                 wprintf("<TR BGCOLOR=\"#%s\">",
1192                                         (bg ? "DDDDDD" : "FFFFFF")
1193                                 );
1194                         }
1195         
1196                         wprintf("<TD>");
1197         
1198                         wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
1199                                 addrbook[i].ab_msgnum);
1200                         wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
1201                         vcard_n_prettyize(addrbook[i].ab_name);
1202                         escputs(addrbook[i].ab_name);
1203                         wprintf("</A></TD>\n");
1204                         ++displayed;
1205                 }
1206         }
1207
1208         wprintf("</TR></TABLE>\n");
1209 }
1210
1211
1212
1213 /* 
1214  * load message pointers from the server
1215  */
1216 int load_msg_ptrs(char *servcmd)
1217 {
1218         char buf[SIZ];
1219         int nummsgs;
1220         int maxload = 0;
1221
1222         nummsgs = 0;
1223         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1224         serv_puts(servcmd);
1225         serv_getln(buf, sizeof buf);
1226         if (buf[0] != '1') {
1227                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1228                 return (nummsgs);
1229         }
1230         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1231                 if (nummsgs < maxload) {
1232                         WC->msgarr[nummsgs] = atol(buf);
1233                         ++nummsgs;
1234                 }
1235         }
1236         return (nummsgs);
1237 }
1238
1239
1240 /*
1241  * command loop for reading messages
1242  */
1243 void readloop(char *oper)
1244 {
1245         char cmd[SIZ];
1246         char buf[SIZ];
1247         char old_msgs[SIZ];
1248         int is_new = 0;
1249         int a, b;
1250         int nummsgs;
1251         long startmsg;
1252         int maxmsgs;
1253         int num_displayed = 0;
1254         int is_summary = 0;
1255         int is_addressbook = 0;
1256         int is_singlecard = 0;
1257         int is_calendar = 0;
1258         int is_tasks = 0;
1259         int is_notes = 0;
1260         int remaining_messages;
1261         int lo, hi;
1262         int lowest_displayed = (-1);
1263         int highest_displayed = 0;
1264         long pn_previous = 0L;
1265         long pn_current = 0L;
1266         long pn_next = 0L;
1267         int bg = 0;
1268         struct addrbookent *addrbook = NULL;
1269         int num_ab = 0;
1270
1271         startmsg = atol(bstr("startmsg"));
1272         maxmsgs = atoi(bstr("maxmsgs"));
1273         is_summary = atoi(bstr("summary"));
1274         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1275
1276         output_headers(1, 1, 1, 0, 0, 0, 0);
1277
1278         /* When in summary mode, always show ALL messages instead of just
1279          * new or old.  Otherwise, show what the user asked for.
1280          */
1281         if (!strcmp(oper, "readnew")) {
1282                 strcpy(cmd, "MSGS NEW");
1283         }
1284         else if (!strcmp(oper, "readold")) {
1285                 strcpy(cmd, "MSGS OLD");
1286         }
1287         else {
1288                 strcpy(cmd, "MSGS ALL");
1289         }
1290
1291         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1292                 is_summary = 1;
1293                 strcpy(cmd, "MSGS ALL");
1294         }
1295
1296         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1297                 is_addressbook = 1;
1298                 strcpy(cmd, "MSGS ALL");
1299                 maxmsgs = 32767;
1300         }
1301
1302         if (is_summary) {
1303                 strcpy(cmd, "MSGS ALL");
1304         }
1305
1306         /* Are we doing a summary view?  If so, we need to know old messages
1307          * and new messages, so we can do that pretty boldface thing for the
1308          * new messages.
1309          */
1310         strcpy(old_msgs, "");
1311         if (is_summary) {
1312                 serv_puts("GTSN");
1313                 serv_getln(buf, sizeof buf);
1314                 if (buf[0] == '2') {
1315                         strcpy(old_msgs, &buf[4]);
1316                 }
1317         }
1318
1319         is_singlecard = atoi(bstr("is_singlecard"));
1320
1321         if (WC->wc_view == VIEW_CALENDAR) {             /* calendar */
1322                 is_calendar = 1;
1323                 strcpy(cmd, "MSGS ALL");
1324                 maxmsgs = 32767;
1325         }
1326         if (WC->wc_view == VIEW_TASKS) {                /* tasks */
1327                 is_tasks = 1;
1328                 strcpy(cmd, "MSGS ALL");
1329                 maxmsgs = 32767;
1330         }
1331         if (WC->wc_view == VIEW_NOTES) {                /* notes */
1332                 is_notes = 1;
1333                 strcpy(cmd, "MSGS ALL");
1334                 maxmsgs = 32767;
1335         }
1336
1337         nummsgs = load_msg_ptrs(cmd);
1338         if (nummsgs == 0) {
1339
1340                 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1341                         if (!strcmp(oper, "readnew")) {
1342                                 wprintf("<EM>No new messages.</EM>\n");
1343                         } else if (!strcmp(oper, "readold")) {
1344                                 wprintf("<EM>No old messages.</EM>\n");
1345                         } else {
1346                                 wprintf("<EM>No messages here.</EM>\n");
1347                         }
1348                 }
1349
1350                 goto DONE;
1351         }
1352
1353         if (startmsg == 0L) startmsg = WC->msgarr[0];
1354         remaining_messages = 0;
1355
1356         for (a = 0; a < nummsgs; ++a) {
1357                 if (WC->msgarr[a] >= startmsg) {
1358                         ++remaining_messages;
1359                 }
1360         }
1361
1362         wprintf("<form name=\"msgomatic\" "
1363                 "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
1364         if (is_summary) {
1365                 wprintf("<div id=\"fix_scrollbar_bug\">"
1366                         "<table border=0 cellspacing=0 "
1367                         "cellpadding=0 width=100%%>\n"
1368                         "<TR>"
1369                         "<TD align=center><b><i>Subject</i></b></TD>"
1370                         "<TD align=center><b><i>Sender</i></b></TD>"
1371                         "<TD align=center><b><i>Date</i></b></TD>"
1372                         "<TD></TD>"
1373                         "</TR>\n"
1374                 );
1375         }
1376
1377         for (a = 0; a < nummsgs; ++a) {
1378                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1379
1380                         /* Are you a new message, or an old message? */
1381                         is_new = 0;
1382                         if (is_summary) {
1383                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1384                                         is_new = 0;
1385                                 }
1386                                 else {
1387                                         is_new = 1;
1388                                 }
1389                         }
1390
1391                         /* Learn which msgs "Prev" & "Next" buttons go to */
1392                         pn_current = WC->msgarr[a];
1393                         if (a > 0) pn_previous = WC->msgarr[a-1];
1394                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1395
1396                         /* If a tabular view, set up the line */
1397                         if (is_summary) {
1398                                 bg = 1 - bg;
1399                                 wprintf("<TR BGCOLOR=\"#%s\">",
1400                                         (bg ? "DDDDDD" : "FFFFFF")
1401                                 );
1402                         }
1403
1404                         /* Display the message */
1405                         if (is_summary) {
1406                                 summarize_message(WC->msgarr[a], is_new);
1407                         }
1408                         else if (is_addressbook) {
1409                                 fetch_ab_name(WC->msgarr[a], buf);
1410                                 ++num_ab;
1411                                 addrbook = realloc(addrbook,
1412                                         (sizeof(struct addrbookent) * num_ab) );
1413                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1414                                         sizeof(addrbook[num_ab-1].ab_name));
1415                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1416                         }
1417                         else if (is_calendar) {
1418                                 display_calendar(WC->msgarr[a]);
1419                         }
1420                         else if (is_tasks) {
1421                                 display_task(WC->msgarr[a]);
1422                         }
1423                         else if (is_notes) {
1424                                 display_note(WC->msgarr[a]);
1425                         }
1426                         else {
1427                                 read_message(WC->msgarr[a]);
1428                         }
1429
1430                         /* If a tabular view, finish the line */
1431                         if (is_summary) {
1432                                 wprintf("</TR>\n");
1433                         }
1434
1435                         if (lowest_displayed < 0) lowest_displayed = a;
1436                         highest_displayed = a;
1437
1438                         ++num_displayed;
1439                         --remaining_messages;
1440                 }
1441         }
1442
1443         if (is_summary) {
1444                 wprintf("</table></div>\n");
1445         }
1446
1447         /* Bump these because although we're thinking in zero base, the user
1448          * is a drooling idiot and is thinking in one base.
1449          */
1450         ++lowest_displayed;
1451         ++highest_displayed;
1452
1453         /* If we're only looking at one message, do a prev/next thing */
1454         if (num_displayed == 1) {
1455            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1456
1457                 wprintf("<div id=\"fix_scrollbar_bug\">"
1458                         "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>"
1459                         "Reading #%d of %d messages.</TD>\n"
1460                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1461                         lowest_displayed, nummsgs);
1462
1463                 if (is_summary) {
1464                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1465                                 "VALUE=\"Delete selected\">\n");
1466                 }
1467
1468                 if (pn_previous > 0L) {
1469                         wprintf("<A HREF=\"/%s"
1470                                 "?startmsg=%ld"
1471                                 "&maxmsgs=1"
1472                                 "&summary=0\">"
1473                                 "Previous</A> \n",
1474                                         oper,
1475                                         pn_previous );
1476                 }
1477
1478                 if (pn_next > 0L) {
1479                         wprintf("<A HREF=\"/%s"
1480                                 "?startmsg=%ld"
1481                                 "&maxmsgs=1"
1482                                 "&summary=0\">"
1483                                 "Next</A> \n",
1484                                         oper,
1485                                         pn_next );
1486                 }
1487
1488                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1489                         "&maxmsgs=%d&summary=1\">"
1490                         "Summary"
1491                         "</A>",
1492                         oper,
1493                         WC->msgarr[0],
1494                         DEFAULT_MAXMSGS
1495                 );
1496
1497                 wprintf("</td></tr></table></div>\n");
1498             }
1499         }
1500
1501         /*
1502          * If we're not currently looking at ALL requested
1503          * messages, then display the selector bar
1504          */
1505         if (num_displayed > 1) {
1506            if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1507               && (!is_notes) && (!is_singlecard)) {
1508
1509                 wprintf("Reading #", lowest_displayed, highest_displayed);
1510
1511                 wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
1512                         "OnChange=\"location.href=msgomatic.whichones.options"
1513                         "[selectedIndex].value\">\n");
1514
1515                 for (b=0; b<nummsgs; b = b + maxmsgs) {
1516                 lo = b+1;
1517                 hi = b+maxmsgs;
1518                 if (hi > nummsgs) hi = nummsgs;
1519                         wprintf("<OPTION %s VALUE="
1520                                 "\"/%s"
1521                                 "?startmsg=%ld"
1522                                 "&maxmsgs=%d"
1523                                 "&summary=%d\">"
1524                                 "%d-%d</OPTION> \n",
1525                                 ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
1526                                 oper,
1527                                 WC->msgarr[b],
1528                                 maxmsgs,
1529                                 is_summary,
1530                                 lo, hi);
1531                 }
1532                 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1533                         "&maxmsgs=9999999&summary=%d\">"
1534                         "ALL"
1535                         "</OPTION> ",
1536                         oper,
1537                         WC->msgarr[0], is_summary);
1538
1539                 wprintf("</SELECT> of %d messages.", nummsgs);
1540
1541                 if (is_summary) {
1542                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1543                                 "VALUE=\"Delete selected\">\n");
1544                 }
1545
1546             }
1547         }
1548         wprintf("</form>\n");
1549
1550 DONE:
1551         if (is_tasks) {
1552                 do_tasks_view();        /* Render the task list */
1553         }
1554
1555         if (is_calendar) {
1556                 do_calendar_view();     /* Render the calendar */
1557         }
1558
1559         if (is_addressbook) {
1560                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
1561         }
1562
1563         wDumpContent(1);
1564         if (addrbook != NULL) free(addrbook);
1565
1566         /* If we got here via a mailbox view and are reading a single
1567          * message, mark it as "seen." We do this after rendering the web page
1568          * so it doesn't keep the user waiting.
1569          */
1570         if ( (maxmsgs == 1) && (WC->wc_view == VIEW_MAILBOX) ) {
1571                 serv_printf("SEEN %ld|1", startmsg);
1572                 serv_getln(buf, sizeof buf);
1573         }
1574 }
1575
1576
1577 /*
1578  * Back end for post_message() ... this is where the actual message
1579  * gets transmitted to the server.
1580  */
1581 void post_mime_to_server(void) {
1582         char boundary[SIZ];
1583         int is_multipart = 0;
1584         static int seq = 0;
1585         struct wc_attachment *att;
1586         char *encoded;
1587         size_t encoded_length;
1588
1589         /* If there are attachments, we have to do multipart/mixed */
1590         if (WC->first_attachment != NULL) {
1591                 is_multipart = 1;
1592         }
1593
1594         if (is_multipart) {
1595                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1596                         serv_info.serv_fqdn,
1597                         getpid(),
1598                         ++seq
1599                 );
1600
1601                 /* Remember, serv_printf() appends an extra newline */
1602                 serv_printf("Content-type: multipart/mixed; "
1603                         "boundary=\"%s\"\n", boundary);
1604                 serv_printf("This is a multipart message in MIME format.\n");
1605                 serv_printf("--%s", boundary);
1606         }
1607
1608         serv_puts("Content-type: text/html; charset=utf-8");
1609         serv_puts("");
1610         serv_puts("<HTML><BODY>\n");
1611         text_to_server(bstr("msgtext"), 0);
1612         serv_puts("</BODY></HTML>\n");
1613         
1614
1615         if (is_multipart) {
1616
1617                 /* Add in the attachments */
1618                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1619
1620                         encoded_length = ((att->length * 150) / 100);
1621                         encoded = malloc(encoded_length);
1622                         if (encoded == NULL) break;
1623                         CtdlEncodeBase64(encoded, att->data, att->length);
1624
1625                         serv_printf("--%s", boundary);
1626                         serv_printf("Content-type: %s", att->content_type);
1627                         serv_printf("Content-disposition: attachment; "
1628                                 "filename=\"%s\"", att->filename);
1629                         serv_puts("Content-transfer-encoding: base64");
1630                         serv_puts("");
1631                         serv_write(encoded, strlen(encoded));
1632                         serv_puts("");
1633                         serv_puts("");
1634                         free(encoded);
1635                 }
1636                 serv_printf("--%s--", boundary);
1637         }
1638
1639         serv_puts("000");
1640 }
1641
1642
1643 /*
1644  * Post message (or don't post message)
1645  *
1646  * Note regarding the "dont_post" variable:
1647  * A random value (actually, it's just a timestamp) is inserted as a hidden
1648  * field called "postseq" when the display_enter page is generated.  This
1649  * value is checked when posting, using the static variable dont_post.  If a
1650  * user attempts to post twice using the same dont_post value, the message is
1651  * discarded.  This prevents the accidental double-saving of the same message
1652  * if the user happens to click the browser "back" button.
1653  */
1654 void post_message(void)
1655 {
1656         char buf[SIZ];
1657         static long dont_post = (-1L);
1658         struct wc_attachment *att, *aptr;
1659
1660         if (WC->upload_length > 0) {
1661
1662                 /* There's an attachment.  Save it to this struct... */
1663                 att = malloc(sizeof(struct wc_attachment));
1664                 memset(att, 0, sizeof(struct wc_attachment));
1665                 att->length = WC->upload_length;
1666                 strcpy(att->content_type, WC->upload_content_type);
1667                 strcpy(att->filename, WC->upload_filename);
1668                 att->next = NULL;
1669
1670                 /* And add it to the list. */
1671                 if (WC->first_attachment == NULL) {
1672                         WC->first_attachment = att;
1673                 }
1674                 else {
1675                         aptr = WC->first_attachment;
1676                         while (aptr->next != NULL) aptr = aptr->next;
1677                         aptr->next = att;
1678                 }
1679
1680                 /* Netscape sends a simple filename, which is what we want,
1681                  * but Satan's browser sends an entire pathname.  Reduce
1682                  * the path to just a filename if we need to.
1683                  */
1684                 while (num_tokens(att->filename, '/') > 1) {
1685                         remove_token(att->filename, 0, '/');
1686                 }
1687                 while (num_tokens(att->filename, '\\') > 1) {
1688                         remove_token(att->filename, 0, '\\');
1689                 }
1690
1691                 /* Transfer control of this memory from the upload struct
1692                  * to the attachment struct.
1693                  */
1694                 att->data = WC->upload;
1695                 WC->upload_length = 0;
1696                 WC->upload = NULL;
1697                 display_enter();
1698                 return;
1699         }
1700
1701         if (!strcasecmp(bstr("sc"), "Cancel")) {
1702                 sprintf(WC->ImportantMessage, 
1703                         "Cancelled.  Message was not posted.");
1704         } else if (!strcasecmp(bstr("attach"), "Add")) {
1705                 display_enter();
1706                 return;
1707         } else if (atol(bstr("postseq")) == dont_post) {
1708                 sprintf(WC->ImportantMessage, 
1709                         "Automatically cancelled because you have already "
1710                         "saved this message.");
1711         } else {
1712                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1713                         bstr("recp"),
1714                         bstr("subject") );
1715                 serv_puts(buf);
1716                 serv_getln(buf, sizeof buf);
1717                 if (buf[0] == '4') {
1718                         post_mime_to_server();
1719                         if (strlen(bstr("recp")) > 0) {
1720                                 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1721                         }
1722                         else {
1723                                 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1724                         }
1725                         dont_post = atol(bstr("postseq"));
1726                 } else {
1727                         sprintf(WC->ImportantMessage, 
1728                                 "%s", &buf[4]);
1729                 }
1730         }
1731
1732         free_attachments(WC);
1733         readloop("readnew");
1734 }
1735
1736
1737
1738
1739 /*
1740  * display the message entry screen
1741  */
1742 void display_enter(void)
1743 {
1744         char buf[SIZ];
1745         long now;
1746         struct wc_attachment *att;
1747
1748         if (strlen(bstr("force_room")) > 0) {
1749                 gotoroom(bstr("force_room"));
1750         }
1751
1752         /* Are we perhaps in an address book view?  If so, then an "enter
1753          * message" command really means "add new entry."
1754          */
1755         if (WC->wc_view == VIEW_ADDRESSBOOK) {
1756                 do_edit_vcard(-1, "", "");
1757                 return;
1758         }
1759
1760 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1761         /* Are we perhaps in a calendar view?  If so, then an "enter
1762          * message" command really means "add new calendar item."
1763          */
1764         if (WC->wc_view == VIEW_CALENDAR) {
1765                 display_edit_event();
1766                 return;
1767         }
1768
1769         /* Are we perhaps in a tasks view?  If so, then an "enter
1770          * message" command really means "add new task."
1771          */
1772         if (WC->wc_view == VIEW_TASKS) {
1773                 display_edit_task();
1774                 return;
1775         }
1776 #endif
1777
1778         /* Otherwise proceed normally.  Do a custom room banner with no navbar... */
1779         output_headers(1, 1, 2, 0, 0, 0, 0);
1780         wprintf("<div id=\"banner\">\n");
1781         embed_room_banner(NULL, navbar_none);
1782         wprintf("</div>\n");
1783         wprintf("<div id=\"content\">\n"
1784                 "<div id=\"fix_scrollbar_bug\">"
1785                 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1786
1787         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1788         serv_puts(buf);
1789         serv_getln(buf, sizeof buf);
1790
1791         if (!strncmp(buf, "570", 3)) {
1792                 if (strlen(bstr("recp")) > 0) {
1793                         svprintf("RECPERROR", WCS_STRING,
1794                                 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1795                                 &buf[4]
1796                         );
1797                 }
1798                 do_template("prompt_for_recipient");
1799                 goto DONE;
1800         }
1801         if (buf[0] != '2') {
1802                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1803                 goto DONE;
1804         }
1805
1806         now = time(NULL);
1807         fmt_date(buf, now, 0);
1808         strcat(&buf[strlen(buf)], " <I>from</I> ");
1809         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1810         if (strlen(bstr("recp")) > 0) {
1811                 strcat(&buf[strlen(buf)], " <I>to</I> ");
1812                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1813         }
1814         strcat(&buf[strlen(buf)], " <I>in</I> ");
1815         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1816
1817         /* begin message entry screen */
1818         // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1819
1820         wprintf("<form enctype=\"multipart/form-data\" "
1821                 "method=\"POST\" action=\"/post\" "
1822                 "name=\"enterform\""
1823                 "onSubmit=\"return submitForm();\""
1824                 ">\n");
1825         wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1826                 bstr("recp"));
1827         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1828                 now);
1829
1830         wprintf("%s<br>\n", buf);       /* header bar */
1831         wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
1832                 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1833         wprintf("<font size=-1>Subject (optional):</font>"
1834                 "<input type=\"text\" name=\"subject\" value=\"");
1835         escputs(bstr("subject"));
1836         wprintf("\" size=40 maxlength=70>"
1837                 "&nbsp;"
1838         );
1839
1840         wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1841         if (strlen(bstr("recp")) > 0) {
1842                 wprintf("Send message");
1843         } else {
1844                 wprintf("Post message");
1845         }
1846         wprintf("\">&nbsp;"
1847                 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1848
1849         wprintf("<center><script type=\"text/javascript\" "
1850                 "src=\"static/richtext.js\"></script>\n"
1851                 "<script type=\"text/javascript\">\n"
1852                 "function submitForm() { \n"
1853                 "  updateRTE('msgtext'); \n"
1854                 "  return true; \n"
1855                 "} \n"
1856                 "  \n"
1857                 "initRTE(\"static/\", \"static/\", \"\"); \n"
1858                 "</script> \n"
1859                 "<noscript>JavaScript must be enabled.</noscript> \n"
1860                 "<script type=\"text/javascript\"> \n"
1861                 "writeRichText('msgtext', '");
1862         msgescputs(bstr("msgtext"));
1863         wprintf("', '96%%', '200', true, false); \n"
1864                 "</script></center><br />\n");
1865
1866         /* Enumerate any attachments which are already in place... */
1867         wprintf("<img src=\"/static/diskette_24x.gif\" border=0 "
1868                 "align=middle height=16 width=16> Attachments: ");
1869         wprintf("<select name=\"which_attachment\" size=1>");
1870         for (att = WC->first_attachment; att != NULL; att = att->next) {
1871                 wprintf("<option value=\"");
1872                 urlescputs(att->filename);
1873                 wprintf("\">");
1874                 escputs(att->filename);
1875                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
1876                 wprintf("</option>\n");
1877         }
1878         wprintf("</select>");
1879
1880         /* Now offer the ability to attach additional files... */
1881         wprintf("&nbsp;&nbsp;&nbsp;"
1882                 "Attach file: <input NAME=\"attachfile\" "
1883                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
1884                 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1885
1886         wprintf("</form>\n");
1887
1888         wprintf("</td></tr></table></div>\n");
1889 DONE:   wDumpContent(1);
1890 }
1891
1892
1893
1894
1895
1896
1897
1898
1899 void delete_msg(void)
1900 {
1901         long msgid;
1902         char buf[SIZ];
1903
1904         msgid = atol(bstr("msgid"));
1905
1906         output_headers(1, 1, 1, 0, 0, 0, 0);
1907
1908         sprintf(buf, "DELE %ld", msgid);
1909         serv_puts(buf);
1910         serv_getln(buf, sizeof buf);
1911         wprintf("<EM>%s</EM><br />\n", &buf[4]);
1912
1913         wDumpContent(1);
1914 }
1915
1916
1917
1918
1919 /*
1920  * Confirm move of a message
1921  */
1922 void confirm_move_msg(void)
1923 {
1924         long msgid;
1925         char buf[SIZ];
1926         char targ[SIZ];
1927
1928         msgid = atol(bstr("msgid"));
1929
1930         output_headers(1, 1, 1, 0, 0, 0, 0);
1931
1932         wprintf("<div id=\"fix_scrollbar_bug\">"
1933                 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
1934         wprintf("<font size=+1 color=\"#ffffff\"");
1935         wprintf("<b>Confirm move of message</b>\n");
1936         wprintf("</font></td></tr></table></div>\n");
1937
1938         wprintf("<CENTER>");
1939
1940         wprintf("Move this message to:<br />\n");
1941
1942         wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1943         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1944                 bstr("msgid"));
1945
1946
1947         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1948         serv_puts("LKRA");
1949         serv_getln(buf, sizeof buf);
1950         if (buf[0] == '1') {
1951                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1952                         extract_token(targ, buf, 0, '|', sizeof targ);
1953                         wprintf("<OPTION>");
1954                         escputs(targ);
1955                         wprintf("\n");
1956                 }
1957         }
1958         wprintf("</SELECT>\n");
1959         wprintf("<br />\n");
1960
1961         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1962         wprintf("&nbsp;");
1963         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1964         wprintf("</form></CENTER>\n");
1965
1966         wprintf("</CENTER>\n");
1967         wDumpContent(1);
1968 }
1969
1970
1971
1972 void move_msg(void)
1973 {
1974         long msgid;
1975         char buf[SIZ];
1976
1977         msgid = atol(bstr("msgid"));
1978
1979         output_headers(1, 1, 1, 0, 0, 0, 0);
1980
1981         if (!strcasecmp(bstr("yesno"), "Move")) {
1982                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1983                 serv_puts(buf);
1984                 serv_getln(buf, sizeof buf);
1985                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1986         } else {
1987                 wprintf("<EM>Message not moved.</EM><br />\n");
1988         }
1989
1990         wDumpContent(1);
1991 }
1992
1993 /*
1994  * This gets called when a user selects multiple messages in a summary
1995  * list and then clicks to perform a transformation of some sort on them
1996  * (such as deleting them).
1997  */
1998 void do_stuff_to_msgs(void) {
1999         char buf[SIZ];
2000         char sc[SIZ];
2001
2002         struct stuff_t {
2003                 struct stuff_t *next;
2004                 long msgnum;
2005         };
2006
2007         struct stuff_t *stuff = NULL;
2008         struct stuff_t *ptr;
2009
2010
2011         serv_puts("MSGS ALL");
2012         serv_getln(buf, sizeof buf);
2013
2014         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2015                 ptr = malloc(sizeof(struct stuff_t));
2016                 ptr->msgnum = atol(buf);
2017                 ptr->next = stuff;
2018                 stuff = ptr;
2019         }
2020
2021         strcpy(sc, bstr("sc"));
2022
2023         while (stuff != NULL) {
2024
2025                 sprintf(buf, "msg_%ld", stuff->msgnum);
2026                 if (!strcasecmp(bstr(buf), "yes")) {
2027
2028                         if (!strcasecmp(sc, "Delete selected")) {
2029                                 serv_printf("DELE %ld", stuff->msgnum);
2030                                 serv_getln(buf, sizeof buf);
2031                         }
2032
2033                 }
2034
2035                 ptr = stuff->next;
2036                 free(stuff);
2037                 stuff = ptr;
2038         }
2039
2040         readloop("readfwd");
2041 }