4 * Functions which deal with the fetching and displaying of messages.
14 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
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)
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.
58 for (pos = 0; pos < strlen(buf); ++pos) {
59 if (!strncasecmp(&buf[pos], "http://", 7))
61 if (!strncasecmp(&buf[pos], "ftp://", 6))
68 if ((start > 0) && (buf[start - 1] == '<'))
70 if ((start > 0) && (buf[start - 1] == '['))
72 if ((start > 0) && (buf[start - 1] == '('))
74 if ((start > 0) && (buf[start - 1] == '{'))
77 for (pos = strlen(buf); pos > start; --pos) {
78 if ((buf[pos] == ' ') || (buf[pos] == ench))
82 strncpy(urlbuf, &buf[start], end - start);
83 urlbuf[end - start] = 0;
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 )
94 /* display_vcard() calls this after parsing the textual vCard into
95 * our 'struct vCard' data object.
96 * This gets called instead of display_parsed_vcard() if we are only looking
97 * to extract the person's name instead of displaying the card.
99 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
102 strcpy(storename, "");
103 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
104 if (!strcasecmp(v->prop[i].name, "n")) {
105 strcpy(storename, v->prop[i].value);
112 /* display_vcard() calls this after parsing the textual vCard into
113 * our 'struct vCard' data object.
115 * Set 'full' to nonzero to display the full card, otherwise it will only
116 * show a summary line.
118 * This code is a bit ugly, so perhaps an explanation is due: we do this
119 * in two passes through the vCard fields. On the first pass, we process
120 * fields we understand, and then render them in a pretty fashion at the
121 * end. Then we make a second pass, outputting all the fields we don't
122 * understand in a simple two-column name/value format.
124 void display_parsed_vcard(struct vCard *v, int full) {
130 char *thisname, *thisvalue;
131 char firsttoken[SIZ];
134 char displayname[SIZ];
138 strcpy(displayname, "");
144 name = vcard_get_prop(v, "fn", 1, 0, 0);
145 if (name == NULL) name = vcard_get_prop(v, "n", 1, 0, 0);
157 wprintf("<TABLE bgcolor=#888888>");
158 for (pass=1; pass<=2; ++pass) {
160 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
162 thisname = strdup(v->prop[i].name);
163 extract_token(firsttoken, thisname, 0, ';');
165 for (j=0; j<num_tokens(thisname, ';'); ++j) {
166 extract_token(buf, thisname, j, ';');
167 if (!strcasecmp(buf, "encoding=quoted-printable")) {
169 remove_token(thisname, j, ';');
171 if (!strcasecmp(buf, "encoding=base64")) {
173 remove_token(thisname, j, ';');
178 thisvalue = malloc(strlen(v->prop[i].value) + 50);
179 j = CtdlDecodeQuotedPrintable(
180 thisvalue, v->prop[i].value,
181 strlen(v->prop[i].value) );
185 thisvalue = malloc(strlen(v->prop[i].value) + 50);
187 thisvalue, v->prop[i].value,
188 strlen(v->prop[i].value) );
191 thisvalue = strdup(v->prop[i].value);
194 /*** Various fields we may encounter ***/
196 /* N is name, but only if there's no FN already there */
197 if (!strcasecmp(firsttoken, "n")) {
198 if (strlen(displayname) == 0) {
199 strcpy(displayname, thisvalue);
203 /* FN (full name) is a true 'display name' field */
204 else if (!strcasecmp(firsttoken, "fn")) {
205 strcpy(displayname, thisvalue);
208 else if (!strcasecmp(firsttoken, "email")) {
209 if (strlen(mailto) > 0) strcat(mailto, "<BR>");
211 "<A HREF=\"/display_enter"
212 "?force_room=_MAIL_&recp=");
213 urlesc(&mailto[strlen(mailto)], thisvalue);
214 strcat(mailto, "\">");
215 urlesc(&mailto[strlen(mailto)], thisvalue);
216 strcat(mailto, "</A>");
218 else if (!strcasecmp(firsttoken, "tel")) {
219 if (strlen(phone) > 0) strcat(phone, "<BR>");
220 strcat(phone, thisvalue);
221 for (j=0; j<num_tokens(thisname, ';'); ++j) {
222 extract_token(buf, thisname, j, ';');
223 if (!strcasecmp(buf, "tel"))
225 else if (!strcasecmp(buf, "work"))
226 strcat(phone, " (work)");
227 else if (!strcasecmp(buf, "home"))
228 strcat(phone, " (home)");
229 else if (!strcasecmp(buf, "cell"))
230 strcat(phone, " (cell)");
238 else if (!strcasecmp(firsttoken, "adr")) {
240 wprintf("<TR><TD>Address:</TD><TD>");
241 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
242 extract_token(buf, thisvalue, j, ';');
243 if (strlen(buf) > 0) {
248 wprintf("</TD></TR>\n");
251 else if (!strcasecmp(firsttoken, "version")) {
254 else if (!strcasecmp(firsttoken, "rev")) {
257 else if (!strcasecmp(firsttoken, "label")) {
264 wprintf("</TD><TD>");
266 wprintf("</TD></TR>\n");
275 wprintf("<TR BGCOLOR=\"#AAAAAA\">"
276 "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
277 "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
278 "<FONT SIZE=+1><B>");
279 escputs(displayname);
280 wprintf("</B></FONT></TD></TR>\n");
282 if (strlen(phone) > 0)
283 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
284 if (strlen(mailto) > 0)
285 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
290 wprintf("</TABLE>\n");
296 * Display a textual vCard
297 * (Converts to a vCard object and then calls the actual display function)
298 * Set 'full' to nonzero to display the whole card instead of a one-liner.
299 * Or, if "storename" is non-NULL, just store the person's name in that
300 * buffer instead of displaying the card at all.
302 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
308 v = vcard_load(vcard_source);
309 if (v == NULL) return;
311 name = vcard_get_prop(v, "n", 1, 0, 0);
317 if (storename != NULL) {
318 fetchname_parsed_vcard(v, storename);
320 else if ( (alpha == 0)
321 || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
322 || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
323 display_parsed_vcard(v, full);
333 * I wanna SEE that message!
335 void read_message(long msgnum) {
337 char mime_partnum[SIZ];
338 char mime_filename[SIZ];
339 char mime_content_type[SIZ];
340 char mime_disposition[SIZ];
352 char vcard_partnum[SIZ];
353 char cal_partnum[SIZ];
354 char *part_source = NULL;
359 strcpy(reply_to, "");
360 strcpy(vcard_partnum, "");
361 strcpy(cal_partnum, "");
362 strcpy(mime_http, "");
364 serv_printf("MSG4 %ld", msgnum);
367 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
371 /* begin everythingamundo table */
372 wprintf("<table width=100% border=1 cellspacing=0 "
373 "cellpadding=0><TR><TD>\n");
375 /* begin message header table */
376 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
377 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
379 wprintf("<SPAN CLASS=\"message_header\">");
380 strcpy(m_subject, "");
382 while (serv_gets(buf), strcasecmp(buf, "text")) {
383 if (!strcmp(buf, "000")) {
384 wprintf("<I>unexpected end of message</I><BR><BR>\n");
385 wprintf("</SPAN>\n");
388 if (!strncasecmp(buf, "nhdr=yes", 8))
392 if (!strncasecmp(buf, "type=", 5))
393 format_type = atoi(&buf[5]);
394 if (!strncasecmp(buf, "from=", 5)) {
395 strcpy(from, &buf[5]);
396 wprintf("from <A HREF=\"/showuser&who=");
402 if (!strncasecmp(buf, "subj=", 5))
403 strcpy(m_subject, &buf[5]);
404 if ((!strncasecmp(buf, "hnod=", 5))
405 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
406 wprintf("(%s) ", &buf[5]);
407 if ((!strncasecmp(buf, "room=", 5))
408 && (strcasecmp(&buf[5], WC->wc_roomname))
409 && (strlen(&buf[5])>0) )
410 wprintf("in %s> ", &buf[5]);
411 if (!strncasecmp(buf, "rfca=", 5)) {
412 strcpy(rfca, &buf[5]);
418 if (!strncasecmp(buf, "node=", 5)) {
419 strcpy(node, &buf[5]);
420 if ( ((WC->room_flags & QR_NETWORK)
421 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
422 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
425 wprintf("@%s ", &buf[5]);
428 if (!strncasecmp(buf, "rcpt=", 5))
429 wprintf("to %s ", &buf[5]);
430 if (!strncasecmp(buf, "time=", 5)) {
431 fmt_date(now, atol(&buf[5]));
435 if (!strncasecmp(buf, "part=", 5)) {
436 extract(mime_filename, &buf[5], 1);
437 extract(mime_partnum, &buf[5], 2);
438 extract(mime_disposition, &buf[5], 3);
439 extract(mime_content_type, &buf[5], 4);
440 mime_length = extract_int(&buf[5], 5);
442 if (!strcasecmp(mime_disposition, "attachment")) {
443 snprintf(&mime_http[strlen(mime_http)],
444 (sizeof(mime_http) - strlen(mime_http) - 1),
445 "<A HREF=\"/output_mimepart?"
446 "msgnum=%ld&partnum=%s\" "
447 "TARGET=\"wc.%ld.%s\">"
448 "<IMG SRC=\"/static/attachment.gif\" "
449 "BORDER=0 ALIGN=MIDDLE>\n"
450 "Part %s: %s (%s, %d bytes)</A><BR>\n",
451 msgnum, mime_partnum,
452 msgnum, mime_partnum,
453 mime_partnum, mime_filename,
454 mime_content_type, mime_length);
457 if ((!strcasecmp(mime_disposition, "inline"))
458 && (!strncasecmp(mime_content_type, "image/", 6)) ){
459 snprintf(&mime_http[strlen(mime_http)],
460 (sizeof(mime_http) - strlen(mime_http) - 1),
461 "<IMG SRC=\"/output_mimepart?"
462 "msgnum=%ld&partnum=%s\">",
463 msgnum, mime_partnum);
466 /*** begin handler prep ***/
467 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
468 strcpy(vcard_partnum, mime_partnum);
471 if (!strcasecmp(mime_content_type, "text/calendar")) {
472 strcpy(cal_partnum, mime_partnum);
475 /*** end handler prep ***/
481 /* Generate a reply-to address */
482 if (strlen(rfca) > 0) {
483 strcpy(reply_to, rfca);
486 if ( (strlen(node) > 0)
487 && (strcasecmp(node, serv_info.serv_nodename))
488 && (strcasecmp(node, serv_info.serv_humannode)) ) {
489 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
493 snprintf(reply_to, sizeof(reply_to), "%s", from);
501 wprintf("</SPAN></TD>");
503 wprintf("<TD ALIGN=RIGHT>\n"
504 "<TABLE BORDER=0><TR>\n");
506 /*** "Read" button is now superfluous
508 ***wprintf("<TD BGCOLOR=\"#AAAADD\">"
509 *** "<A HREF=\"/readfwd?startmsg=%ld", msgnum);
510 ***wprintf("&maxmsgs=1&summary=0\">Read</A>"
511 *** "</TD>\n", msgnum);
514 wprintf("<TD BGCOLOR=\"#AAAADD\">"
515 "<A HREF=\"/display_enter?recp=");
516 urlescputs(reply_to);
517 if (!strncasecmp(m_subject, "Re:", 2)) {
518 wprintf("&subject=");
521 else if (strlen(m_subject) > 0) {
522 wprintf("&subject=Re:%%20");
525 wprintf("\"><FONT SIZE=-1>Reply</FONT></A></TD>\n", msgnum);
527 if (WC->is_room_aide) {
528 wprintf("<TD BGCOLOR=\"#AAAADD\">"
529 "<A HREF=\"/confirm_move_msg"
531 "\"><FONT SIZE=-1>Move</FONT></A>"
534 wprintf("<TD BGCOLOR=\"#AAAADD\">"
535 "<A HREF=\"/delete_msg"
537 "onClick=\"return confirm('Delete this message?');\""
538 "><FONT SIZE=-1>Del</FONT></A>"
542 wprintf("</TR></TABLE>\n"
545 if (strlen(m_subject) > 0) {
547 "<SPAN CLASS=\"message_subject\">"
550 "</TD><TD> </TD></TR>\n", m_subject);
553 wprintf("</TR></TABLE>\n");
556 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
557 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
560 * Learn the content type
562 strcpy(mime_content_type, "text/plain");
563 while (serv_gets(buf), (strlen(buf) > 0)) {
564 if (!strcmp(buf, "000")) {
565 wprintf("<I>unexpected end of message</I><BR><BR>\n");
568 if (!strncasecmp(buf, "Content-type: ", 14)) {
569 safestrncpy(mime_content_type, &buf[14],
570 sizeof(mime_content_type));
574 /* Messages in legacy Citadel variformat get handled thusly... */
575 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
576 fmout(NULL, "JUSTIFY");
579 /* Boring old 80-column fixed format text gets handled this way... */
580 else if (!strcasecmp(mime_content_type, "text/plain")) {
581 while (serv_gets(buf), strcmp(buf, "000")) {
582 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
583 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
584 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
585 buf[strlen(buf) - 1] = 0;
587 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
588 wprintf("<SPAN CLASS=\"pull_quote\">");
590 } else if ((bq == 1) &&
591 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
598 wprintf("</TT><BR>\n");
603 else /* HTML is fun, but we've got to strip it first */
604 if (!strcasecmp(mime_content_type, "text/html")) {
608 /* Unknown weirdness */
610 wprintf("I don't know how to display %s<BR>\n",
612 while (serv_gets(buf), strcmp(buf, "000")) { }
616 /* Afterwards, offer links to download attachments 'n' such */
617 if (strlen(mime_http) > 0) {
618 wprintf("%s", mime_http);
621 /* Handler for vCard parts */
622 if (strlen(vcard_partnum) > 0) {
623 part_source = load_mimepart(msgnum, vcard_partnum);
624 if (part_source != NULL) {
626 /* If it's my vCard I can edit it */
627 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
628 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
629 wprintf("<A HREF=\"/edit_vcard?"
630 "msgnum=%ld&partnum=%s\">",
631 msgnum, vcard_partnum);
632 wprintf("(edit)</A>");
635 /* In all cases, display the full card */
636 display_vcard(part_source, 0, 1, NULL);
640 /* Handler for calendar parts */
641 if (strlen(cal_partnum) > 0) {
642 part_source = load_mimepart(msgnum, cal_partnum);
643 if (part_source != NULL) {
644 cal_process_attachment(part_source,
645 msgnum, cal_partnum);
655 wprintf("</TD></TR></TABLE>\n");
657 /* end everythingamundo table */
658 wprintf("</TD></TR></TABLE><BR>\n");
662 void summarize_message(long msgnum) {
673 memset(&summ, 0, sizeof(summ));
674 strcpy(summ.subj, "(no subject)");
676 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
679 if (buf[0] != '1') return;
681 while (serv_gets(buf), strcmp(buf, "000")) {
682 if (!strncasecmp(buf, "from=", 5)) {
683 strcpy(summ.from, &buf[5]);
685 if (!strncasecmp(buf, "subj=", 5)) {
686 strcpy(summ.subj, &buf[5]);
688 if (!strncasecmp(buf, "rfca=", 5)) {
689 strcat(summ.from, " <");
690 strcat(summ.from, &buf[5]);
691 strcat(summ.from, ">");
694 if (!strncasecmp(buf, "node=", 5)) {
695 if ( ((WC->room_flags & QR_NETWORK)
696 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
697 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
699 strcat(summ.from, " @ ");
700 strcat(summ.from, &buf[5]);
704 if (!strncasecmp(buf, "rcpt=", 5)) {
705 strcpy(summ.to, &buf[5]);
708 if (!strncasecmp(buf, "time=", 5)) {
709 fmt_date(summ.date, atol(&buf[5]));
713 wprintf("<TD><A HREF=\"/readfwd?startmsg=%ld"
714 "&maxmsgs=1&summary=0\">",
717 wprintf("</A></TD><TD>");
719 wprintf(" </TD><TD>");
723 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
731 void display_addressbook(long msgnum, char alpha) {
733 char mime_partnum[SIZ];
734 char mime_filename[SIZ];
735 char mime_content_type[SIZ];
736 char mime_disposition[SIZ];
738 char vcard_partnum[SIZ];
739 char *vcard_source = NULL;
749 memset(&summ, 0, sizeof(summ));
750 strcpy(summ.subj, "(no subject)");
752 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
755 if (buf[0] != '1') return;
757 while (serv_gets(buf), strcmp(buf, "000")) {
758 if (!strncasecmp(buf, "part=", 5)) {
759 extract(mime_filename, &buf[5], 1);
760 extract(mime_partnum, &buf[5], 2);
761 extract(mime_disposition, &buf[5], 3);
762 extract(mime_content_type, &buf[5], 4);
763 mime_length = extract_int(&buf[5], 5);
765 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
766 strcpy(vcard_partnum, mime_partnum);
772 if (strlen(vcard_partnum) > 0) {
773 vcard_source = load_mimepart(msgnum, vcard_partnum);
774 if (vcard_source != NULL) {
776 /* Display the summary line */
777 display_vcard(vcard_source, alpha, 0, NULL);
779 /* If it's my vCard I can edit it */
780 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
781 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
782 wprintf("<A HREF=\"/edit_vcard?"
783 "msgnum=%ld&partnum=%s\">",
784 msgnum, vcard_partnum);
785 wprintf("(edit)</A>");
796 /* If it's an old "Firstname Lastname" style record, try to
799 void lastfirst_firstlast(char *namebuf) {
804 if (namebuf == NULL) return;
805 if (strchr(namebuf, ';') != NULL) return;
807 i = num_tokens(namebuf, ' ');
810 extract_token(lastname, namebuf, i-1, ' ');
811 remove_token(namebuf, i-1, ' ');
812 strcpy(firstname, namebuf);
813 sprintf(namebuf, "%s; %s", lastname, firstname);
817 void fetch_ab_name(long msgnum, char *namebuf) {
819 char mime_partnum[SIZ];
820 char mime_filename[SIZ];
821 char mime_content_type[SIZ];
822 char mime_disposition[SIZ];
824 char vcard_partnum[SIZ];
825 char *vcard_source = NULL;
835 if (namebuf == NULL) return;
838 memset(&summ, 0, sizeof(summ));
839 strcpy(summ.subj, "(no subject)");
841 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
844 if (buf[0] != '1') return;
846 while (serv_gets(buf), strcmp(buf, "000")) {
847 if (!strncasecmp(buf, "part=", 5)) {
848 extract(mime_filename, &buf[5], 1);
849 extract(mime_partnum, &buf[5], 2);
850 extract(mime_disposition, &buf[5], 3);
851 extract(mime_content_type, &buf[5], 4);
852 mime_length = extract_int(&buf[5], 5);
854 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
855 strcpy(vcard_partnum, mime_partnum);
861 if (strlen(vcard_partnum) > 0) {
862 vcard_source = load_mimepart(msgnum, vcard_partnum);
863 if (vcard_source != NULL) {
865 /* Grab the name off the card */
866 display_vcard(vcard_source, 0, 0, namebuf);
872 lastfirst_firstlast(namebuf);
878 * Record compare function for sorting address book indices
880 int abcmp(const void *ab1, const void *ab2) {
882 (((const struct addrbookent *)ab1)->ab_name),
883 (((const struct addrbookent *)ab2)->ab_name)
889 * Render the address book using info we gathered during the scan
891 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
896 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
899 wprintf("<TABLE border=0 cellspacing=0 "
900 "cellpadding=3 width=100%%>\n"
903 for (i=0; i<num_ab; ++i) {
910 wprintf("<TR BGCOLOR=\"#%s\">",
911 (bg ? "DDDDDD" : "FFFFFF")
916 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
917 addrbook[i].ab_msgnum);
918 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
919 escputs(addrbook[i].ab_name);
920 wprintf("</A></TD>\n");
923 wprintf("</TR></TABLE>\n");
929 * load message pointers from the server
931 int load_msg_ptrs(char *servcmd)
940 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
943 while (serv_gets(buf), strcmp(buf, "000")) {
944 WC->msgarr[nummsgs] = atol(buf);
945 /* FIXME check for overflow */
953 * command loop for reading messages
955 void readloop(char *oper)
963 int num_displayed = 0;
965 int is_addressbook = 0;
966 int is_singlecard = 0;
969 int remaining_messages;
971 int lowest_displayed = (-1);
972 int highest_displayed = 0;
973 long pn_previous = 0L;
974 long pn_current = 0L;
979 struct addrbookent *addrbook = NULL;
982 startmsg = atol(bstr("startmsg"));
983 maxmsgs = atoi(bstr("maxmsgs"));
984 is_summary = atoi(bstr("summary"));
985 if (maxmsgs == 0) maxmsgs = 20;
989 if (!strcmp(oper, "readnew")) {
990 strcpy(cmd, "MSGS NEW");
991 } else if (!strcmp(oper, "readold")) {
992 strcpy(cmd, "MSGS OLD");
994 strcpy(cmd, "MSGS ALL");
997 /* FIXME put in the correct constant #defs */
998 if ((WC->wc_view == 1) && (maxmsgs > 1)) {
1000 strcpy(cmd, "MSGS ALL");
1004 if ((WC->wc_view == 2) && (maxmsgs > 1)) {
1006 strcpy(cmd, "MSGS ALL");
1010 is_singlecard = atoi(bstr("is_singlecard"));
1012 /* Display the letter indices across the top */
1013 if ((is_addressbook) || (is_singlecard)) {
1014 if (strlen(bstr("alpha")) == 0) {
1018 strcpy(buf, bstr("alpha"));
1022 for (i='1'; i<='z'; ++i) if ((i=='1')||(islower(i))) {
1023 if ((i != alpha) || (is_singlecard)) {
1024 wprintf("<A HREF=\"/readfwd?alpha=%c\">", i);
1026 if (i == alpha) wprintf("<FONT SIZE=+2>");
1028 wprintf("%c", toupper(i));
1033 if (i == alpha) wprintf("</FONT>");
1034 if ((i != alpha) || (is_singlecard)) {
1040 wprintf("<HR width=100%%>\n");
1043 if (WC->wc_view == 3) { /* calendar */
1045 strcpy(cmd, "MSGS ALL");
1048 if (WC->wc_view == 4) { /* tasks */
1050 strcpy(cmd, "MSGS ALL");
1055 nummsgs = load_msg_ptrs(cmd);
1058 if ((!is_tasks) && (!is_calendar)) {
1059 if (!strcmp(oper, "readnew")) {
1060 wprintf("<EM>No new messages in this room.</EM>\n");
1061 } else if (!strcmp(oper, "readold")) {
1062 wprintf("<EM>No old messages in this room.</EM>\n");
1064 wprintf("<EM>This room is empty.</EM>\n");
1071 if (startmsg == 0L) startmsg = WC->msgarr[0];
1072 remaining_messages = 0;
1074 for (a = 0; a < nummsgs; ++a) {
1075 if (WC->msgarr[a] >= startmsg) {
1076 ++remaining_messages;
1081 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n"
1082 "<TABLE border=0 cellspacing=0 "
1083 "cellpadding=0 width=100%%>\n"
1085 "<TD><I>Subject</I></TD>"
1086 "<TD><I>Sender</I></TD>"
1087 "<TD><I>Date</I></TD>"
1093 for (a = 0; a < nummsgs; ++a) {
1094 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1096 /* Learn which msgs "Prev" & "Next" buttons go to */
1097 pn_current = WC->msgarr[a];
1098 if (a > 0) pn_previous = WC->msgarr[a-1];
1099 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1101 /* If a tabular view, set up the line */
1104 wprintf("<TR BGCOLOR=\"#%s\">",
1105 (bg ? "DDDDDD" : "FFFFFF")
1109 /* Display the message */
1111 summarize_message(WC->msgarr[a]);
1113 else if (is_addressbook) {
1114 fetch_ab_name(WC->msgarr[a], buf);
1115 if ((strlen(buf) > 0) && (isalpha(buf[0]))) {
1116 ab_alpha = tolower(buf[0]);
1121 if (alpha == ab_alpha) {
1123 addrbook = realloc(addrbook,
1124 (sizeof(struct addrbookent) * num_ab) );
1125 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1126 sizeof(addrbook[num_ab-1].ab_name));
1127 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1130 else if (is_calendar) {
1131 display_calendar(WC->msgarr[a]);
1133 else if (is_tasks) {
1134 display_task(WC->msgarr[a]);
1137 read_message(WC->msgarr[a]);
1140 /* If a tabular view, finish the line */
1145 if (lowest_displayed < 0) lowest_displayed = a;
1146 highest_displayed = a;
1149 --remaining_messages;
1154 wprintf("</TABLE>\n");
1161 /* Bump these because although we're thinking in zero base, the user
1162 * is a drooling idiot and is thinking in one base.
1165 ++highest_displayed;
1167 /* If we're only looking at one message, do a prev/next thing */
1168 if (num_displayed == 1) {
1169 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1172 "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1173 "Reading #%d of %d messages.</TD>\n"
1174 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1175 lowest_displayed, nummsgs);
1178 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1179 "VALUE=\"Delete selected\">\n");
1182 if (pn_previous > 0L) {
1183 wprintf("<A HREF=\"/%s"
1193 wprintf("<A HREF=\"/%s"
1202 wprintf("<A HREF=\"/%s?startmsg=%ld"
1203 "&maxmsgs=999999&summary=1\">"
1209 wprintf("</TD></TR></TABLE></CENTER>\n");
1215 * If we're not currently looking at ALL requested
1216 * messages, then display the selector bar
1218 if (num_displayed > 1) {
1219 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1221 "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1222 "Reading #%d-%d of %d messages.</TD>\n"
1223 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1224 lowest_displayed, highest_displayed, nummsgs);
1227 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1228 "VALUE=\"Delete selected\">\n");
1232 for (b=0; b<nummsgs; b = b + maxmsgs) {
1235 if (hi > nummsgs) hi = nummsgs;
1236 if (WC->msgarr[b] != startmsg) {
1237 wprintf("<A HREF=\"/%s"
1249 wprintf("%d-%d \n", lo, hi);
1253 wprintf("<A HREF=\"/%s?startmsg=%ld"
1254 "&maxmsgs=999999&summary=%d\">"
1258 WC->msgarr[0], is_summary);
1260 wprintf("<A HREF=\"/%s?startmsg=%ld"
1261 "&maxmsgs=999999&summary=1\">"
1267 wprintf("</TD></TR></TABLE></CENTER>\n");
1270 if (is_summary) wprintf("</FORM>\n");
1274 wprintf("<A HREF=\"/display_edit_task?msgnum=0\">"
1275 "Add new task</A>\n"
1280 do_calendar_view(); /* Render the calendar */
1283 if (is_addressbook) {
1284 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1288 if (addrbook != NULL) free(addrbook);
1293 * Back end for post_message() ... this is where the actual message
1294 * gets transmitted to the server.
1296 void post_mime_to_server(void) {
1298 int is_multipart = 0;
1300 struct wc_attachment *att;
1302 size_t encoded_length;
1304 /* If there are attachments, we have to do multipart/mixed */
1305 if (WC->first_attachment != NULL) {
1310 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1311 serv_info.serv_fqdn,
1316 /* Remember, serv_printf() appends an extra newline */
1317 serv_printf("Content-type: multipart/mixed; "
1318 "boundary=\"%s\"\n", boundary);
1319 serv_printf("This is a multipart message in MIME format.\n");
1320 serv_printf("--%s", boundary);
1323 serv_puts("Content-type: text/html");
1325 serv_puts("<HTML><BODY>\n");
1326 text_to_server(bstr("msgtext"), 0);
1327 serv_puts("</BODY></HTML>\n");
1332 /* Add in the attachments */
1333 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1335 encoded_length = ((att->length * 150) / 100);
1336 encoded = malloc(encoded_length);
1337 if (encoded == NULL) break;
1338 CtdlEncodeBase64(encoded, att->data, att->length);
1340 serv_printf("--%s", boundary);
1341 serv_printf("Content-type: %s", att->content_type);
1342 serv_printf("Content-disposition: attachment; "
1343 "filename=\"%s\"", att->filename);
1344 serv_puts("Content-transfer-encoding: base64");
1346 serv_write(encoded, strlen(encoded));
1351 serv_printf("--%s--", boundary);
1359 * Post message (or don't post message)
1361 * Note regarding the "dont_post" variable:
1362 * A random value (actually, it's just a timestamp) is inserted as a hidden
1363 * field called "postseq" when the display_enter page is generated. This
1364 * value is checked when posting, using the static variable dont_post. If a
1365 * user attempts to post twice using the same dont_post value, the message is
1366 * discarded. This prevents the accidental double-saving of the same message
1367 * if the user happens to click the browser "back" button.
1369 void post_message(void)
1372 static long dont_post = (-1L);
1373 struct wc_attachment *att;
1375 if (WC->upload_length > 0) {
1377 att = malloc(sizeof(struct wc_attachment));
1378 memset(att, 0, sizeof(struct wc_attachment));
1379 att->next = WC->first_attachment;
1380 WC->first_attachment = att;
1381 att->length = WC->upload_length;
1382 strcpy(att->content_type, WC->upload_content_type);
1383 strcpy(att->filename, WC->upload_filename);
1385 /* Netscape sends a simple filename, which is what we want,
1386 * but Satan's browser sends an entire pathname. Reduce
1387 * the path to just a filename if we need to.
1389 while (num_tokens(att->filename, '/') > 1) {
1390 remove_token(att->filename, 0, '/');
1392 while (num_tokens(att->filename, '\\') > 1) {
1393 remove_token(att->filename, 0, '\\');
1396 /* Transfer control of this memory from the upload struct
1397 * to the attachment struct.
1399 att->data = WC->upload;
1400 WC->upload_length = 0;
1406 if (strcasecmp(bstr("sc"), "Save message")) {
1407 sprintf(WC->ImportantMessage,
1408 "Cancelled. Message was not posted.");
1409 } else if (atol(bstr("postseq")) == dont_post) {
1410 sprintf(WC->ImportantMessage,
1411 "Automatically cancelled because you have already "
1412 "saved this message.");
1414 sprintf(buf, "ENT0 1|%s|0|4|%s",
1419 if (buf[0] == '4') {
1420 post_mime_to_server();
1421 sprintf(WC->ImportantMessage,
1422 "Message has been posted.\n");
1423 dont_post = atol(bstr("postseq"));
1425 sprintf(WC->ImportantMessage,
1430 free_attachments(WC);
1431 readloop("readnew");
1438 * display the message entry screen
1440 void display_enter(void)
1444 struct wc_attachment *att;
1446 if (strlen(bstr("force_room")) > 0) {
1447 gotoroom(bstr("force_room"), 0);
1450 /* Are we perhaps in an address book view? If so, then an "enter
1451 * message" command really means "add new entry."
1453 if (WC->wc_view == 2) {
1454 do_edit_vcard(-1, "", "");
1458 /* Otherwise proceed normally */
1460 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1464 if (!strncmp(buf, "570", 3)) {
1465 if (strlen(bstr("recp")) > 0) {
1466 svprintf("RECPERROR", WCS_STRING,
1467 "<SPAN CLASS=\"errormsg\">%s</SPAN><BR>\n",
1471 do_template("prompt_for_recipient");
1474 if (buf[0] != '2') {
1475 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1481 strcat(&buf[strlen(buf)], " <I>from</I> ");
1482 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1483 if (strlen(bstr("recp")) > 0) {
1484 strcat(&buf[strlen(buf)], " <I>to</I> ");
1485 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1487 strcat(&buf[strlen(buf)], " <I>in</I> ");
1488 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1489 svprintf("BOXTITLE", WCS_STRING, buf);
1490 do_template("beginbox");
1492 wprintf("<CENTER>\n");
1494 wprintf("<FORM ENCTYPE=\"multipart/form-data\" "
1495 "METHOD=\"POST\" ACTION=\"/post\" "
1496 "NAME=\"enterform\""
1497 "onSubmit=\"return submitForm();\""
1499 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
1501 wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
1503 wprintf("<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \">");
1504 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1505 wprintf("<FONT SIZE=-1>Subject (optional):</FONT>"
1506 "<INPUT TYPE=\"text\" NAME=\"subject\" VALUE=\"");
1507 escputs(bstr("subject"));
1508 wprintf("\" MAXLENGTH=70>"
1512 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
1513 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
1515 wprintf("<SCRIPT language=\"JavaScript\" type=\"text/javascript\" "
1516 "src=\"static/richtext_compressed.js\"></SCRIPT>\n"
1517 "<SCRIPT language=\"JavaScript\" type=\"text/javascript\">\n"
1518 "function submitForm() { \n"
1519 " updateRTE('msgtext'); \n"
1523 "initRTE(\"static/\", \"static/\", \"\"); \n"
1525 "<noscript>JAVASCRIPT MUST BE ENABLED.</noscript> \n"
1526 "<SCRIPT language=\"javascript\" type=\"text/javascript\"> \n"
1527 "writeRichText('msgtext', '");
1528 msgescputs(bstr("msgtext"));
1529 wprintf("', '100%%', 200, true, false); \n"
1533 * Before we had the richedit widget, we did it this way...
1535 wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=25 COLS=80 "
1537 escputs(bstr("msgtext"));
1538 wprintf("</TEXTAREA><BR>\n");
1541 /* Enumerate any attachments which are already in place... */
1542 for (att = WC->first_attachment; att != NULL; att = att->next) {
1543 wprintf("<IMG SRC=\"/static/attachment.gif\" "
1544 "BORDER=0 ALIGN=MIDDLE> Attachment: ");
1545 escputs(att->filename);
1546 wprintf(" (%s, %d bytes)<BR>\n",
1547 att->content_type, att->length);
1550 /* Now offer the ability to attach additional files... */
1551 wprintf(" "
1552 "Attach file: <input NAME=\"attachfile\" "
1553 "SIZE=48 TYPE=\"file\">\n "
1554 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1556 wprintf("</FORM></CENTER>\n");
1557 do_template("endbox");
1558 DONE: wDumpContent(1);
1568 void delete_msg(void)
1573 msgid = atol(bstr("msgid"));
1577 sprintf(buf, "DELE %ld", msgid);
1580 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1589 * Confirm move of a message
1591 void confirm_move_msg(void)
1597 msgid = atol(bstr("msgid"));
1601 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1602 wprintf("<FONT SIZE=+1 COLOR=\"#FFFFFF\"");
1603 wprintf("<B>Confirm move of message</B>\n");
1604 wprintf("</FONT></TD></TR></TABLE>\n");
1606 wprintf("<CENTER>");
1608 wprintf("Please select the room to which you would like this message moved:<BR>\n");
1610 wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1611 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1615 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1618 if (buf[0] == '1') {
1619 while (serv_gets(buf), strcmp(buf, "000")) {
1620 extract(targ, buf, 0);
1621 wprintf("<OPTION>");
1626 wprintf("</SELECT>\n");
1629 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1630 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1631 wprintf("</FORM></CENTER>\n");
1633 wprintf("</CENTER>\n");
1644 msgid = atol(bstr("msgid"));
1648 if (!strcasecmp(bstr("yesno"), "Move")) {
1649 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1652 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1654 wprintf("<EM>Message not moved.</EM><BR>\n");
1662 void do_stuff_to_msgs(void) {
1667 struct stuff_t *next;
1671 struct stuff_t *stuff = NULL;
1672 struct stuff_t *ptr;
1675 serv_puts("MSGS ALL");
1678 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1679 ptr = malloc(sizeof(struct stuff_t));
1680 ptr->msgnum = atol(buf);
1685 strcpy(sc, bstr("sc"));
1687 while (stuff != NULL) {
1689 sprintf(buf, "msg_%ld", stuff->msgnum);
1690 if (!strcasecmp(bstr(buf), "yes")) {
1692 if (!strcasecmp(sc, "Delete selected")) {
1693 serv_printf("DELE %ld", stuff->msgnum);
1704 readloop("readfwd");