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