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