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