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