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 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
372 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
374 wprintf("<SPAN CLASS=\"message_header\">");
375 strcpy(m_subject, "");
377 while (serv_gets(buf), strcasecmp(buf, "text")) {
378 if (!strcmp(buf, "000")) {
379 wprintf("<I>unexpected end of message</I><BR><BR>\n");
380 wprintf("</SPAN>\n");
383 if (!strncasecmp(buf, "nhdr=yes", 8))
387 if (!strncasecmp(buf, "type=", 5))
388 format_type = atoi(&buf[5]);
389 if (!strncasecmp(buf, "from=", 5)) {
390 strcpy(from, &buf[5]);
391 wprintf("from <A HREF=\"/showuser&who=");
397 if (!strncasecmp(buf, "subj=", 5))
398 strcpy(m_subject, &buf[5]);
399 if ((!strncasecmp(buf, "hnod=", 5))
400 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
401 wprintf("(%s) ", &buf[5]);
402 if ((!strncasecmp(buf, "room=", 5))
403 && (strcasecmp(&buf[5], WC->wc_roomname))
404 && (strlen(&buf[5])>0) )
405 wprintf("in %s> ", &buf[5]);
406 if (!strncasecmp(buf, "rfca=", 5)) {
407 strcpy(rfca, &buf[5]);
413 if (!strncasecmp(buf, "node=", 5)) {
414 strcpy(node, &buf[5]);
415 if ( ((WC->room_flags & QR_NETWORK)
416 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
417 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
420 wprintf("@%s ", &buf[5]);
423 if (!strncasecmp(buf, "rcpt=", 5))
424 wprintf("to %s ", &buf[5]);
425 if (!strncasecmp(buf, "time=", 5)) {
426 fmt_date(now, atol(&buf[5]));
430 if (!strncasecmp(buf, "part=", 5)) {
431 extract(mime_filename, &buf[5], 1);
432 extract(mime_partnum, &buf[5], 2);
433 extract(mime_disposition, &buf[5], 3);
434 extract(mime_content_type, &buf[5], 4);
435 mime_length = extract_int(&buf[5], 5);
437 if (!strcasecmp(mime_disposition, "attachment")) {
438 snprintf(&mime_http[strlen(mime_http)],
439 (sizeof(mime_http) - strlen(mime_http) - 1),
440 "<A HREF=\"/output_mimepart?"
441 "msgnum=%ld&partnum=%s\" "
442 "TARGET=\"wc.%ld.%s\">"
443 "<IMG SRC=\"/static/attachment.gif\" "
444 "BORDER=0 ALIGN=MIDDLE>\n"
445 "Part %s: %s (%s, %d bytes)</A><BR>\n",
446 msgnum, mime_partnum,
447 msgnum, mime_partnum,
448 mime_partnum, mime_filename,
449 mime_content_type, mime_length);
452 if ((!strcasecmp(mime_disposition, "inline"))
453 && (!strncasecmp(mime_content_type, "image/", 6)) ){
454 snprintf(&mime_http[strlen(mime_http)],
455 (sizeof(mime_http) - strlen(mime_http) - 1),
456 "<IMG SRC=\"/output_mimepart?"
457 "msgnum=%ld&partnum=%s\">",
458 msgnum, mime_partnum);
461 /*** begin handler prep ***/
462 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
463 strcpy(vcard_partnum, mime_partnum);
466 if (!strcasecmp(mime_content_type, "text/calendar")) {
467 strcpy(cal_partnum, mime_partnum);
470 /*** end handler prep ***/
476 /* Generate a reply-to address */
477 if (strlen(rfca) > 0) {
478 strcpy(reply_to, rfca);
481 if ( (strlen(node) > 0)
482 && (strcasecmp(node, serv_info.serv_nodename))
483 && (strcasecmp(node, serv_info.serv_humannode)) ) {
484 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
488 snprintf(reply_to, sizeof(reply_to), "%s", from);
496 wprintf("</SPAN></TD>");
498 wprintf("<TD ALIGN=RIGHT>\n"
499 "<TABLE BORDER=0><TR>\n");
501 /*** "Read" button is now superfluous
503 ***wprintf("<TD BGCOLOR=\"#AAAADD\">"
504 *** "<A HREF=\"/readfwd?startmsg=%ld", msgnum);
505 ***wprintf("&maxmsgs=1&summary=0\">Read</A>"
506 *** "</TD>\n", msgnum);
509 wprintf("<TD BGCOLOR=\"#AAAADD\">"
510 "<A HREF=\"/display_enter?recp=");
511 urlescputs(reply_to);
512 wprintf("\"><FONT SIZE=-1>Reply</FONT></A>"
515 if (WC->is_room_aide) {
516 wprintf("<TD BGCOLOR=\"#AAAADD\">"
517 "<A HREF=\"/confirm_move_msg"
519 "\"><FONT SIZE=-1>Move</FONT></A>"
522 wprintf("<TD BGCOLOR=\"#AAAADD\">"
523 "<A HREF=\"/delete_msg"
525 "onClick=\"return confirm('Delete this message?');\""
526 "><FONT SIZE=-1>Del</FONT></A>"
530 wprintf("</TR></TABLE>\n"
533 if (strlen(m_subject) > 0) {
535 "<SPAN CLASS=\"message_subject\">"
538 "</TD><TD> </TD></TR>\n", m_subject);
541 wprintf("</TR></TABLE>\n");
544 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
545 "CELLPADDING=0 CELLSPACING=0><TR><TD>");
548 * Learn the content type
550 strcpy(mime_content_type, "text/plain");
551 while (serv_gets(buf), (strlen(buf) > 0)) {
552 if (!strcmp(buf, "000")) {
553 wprintf("<I>unexpected end of message</I><BR><BR>\n");
556 if (!strncasecmp(buf, "Content-type: ", 14)) {
557 safestrncpy(mime_content_type, &buf[14],
558 sizeof(mime_content_type));
562 /* Messages in legacy Citadel variformat get handled thusly... */
563 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
564 fmout(NULL, "JUSTIFY");
567 /* Boring old 80-column fixed format text gets handled this way... */
568 else if (!strcasecmp(mime_content_type, "text/plain")) {
569 while (serv_gets(buf), strcmp(buf, "000")) {
570 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
571 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
572 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
573 buf[strlen(buf) - 1] = 0;
575 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
576 wprintf("<SPAN CLASS=\"pull_quote\">");
578 } else if ((bq == 1) &&
579 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
586 wprintf("</TT><BR>\n");
591 else /* HTML is fun, but we've got to strip it first */
592 if (!strcasecmp(mime_content_type, "text/html")) {
596 /* Unknown weirdness */
598 wprintf("I don't know how to display %s<BR>\n",
600 while (serv_gets(buf), strcmp(buf, "000")) { }
604 /* Afterwards, offer links to download attachments 'n' such */
605 if (strlen(mime_http) > 0) {
606 wprintf("%s", mime_http);
609 /* Handler for vCard parts */
610 if (strlen(vcard_partnum) > 0) {
611 part_source = load_mimepart(msgnum, vcard_partnum);
612 if (part_source != NULL) {
614 /* If it's my vCard I can edit it */
615 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
616 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
617 wprintf("<A HREF=\"/edit_vcard?"
618 "msgnum=%ld&partnum=%s\">",
619 msgnum, vcard_partnum);
620 wprintf("(edit)</A>");
623 /* In all cases, display the full card */
624 display_vcard(part_source, 0, 1, NULL);
628 /* Handler for calendar parts */
629 if (strlen(cal_partnum) > 0) {
630 part_source = load_mimepart(msgnum, cal_partnum);
631 if (part_source != NULL) {
632 cal_process_attachment(part_source,
633 msgnum, cal_partnum);
643 wprintf("</TD></TR></TABLE>\n");
647 void summarize_message(long msgnum) {
658 memset(&summ, 0, sizeof(summ));
659 strcpy(summ.subj, "(no subject)");
661 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
664 if (buf[0] != '1') return;
666 while (serv_gets(buf), strcmp(buf, "000")) {
667 if (!strncasecmp(buf, "from=", 5)) {
668 strcpy(summ.from, &buf[5]);
670 if (!strncasecmp(buf, "subj=", 5)) {
671 strcpy(summ.subj, &buf[5]);
673 if (!strncasecmp(buf, "rfca=", 5)) {
674 strcat(summ.from, " <");
675 strcat(summ.from, &buf[5]);
676 strcat(summ.from, ">");
679 if (!strncasecmp(buf, "node=", 5)) {
680 if ( ((WC->room_flags & QR_NETWORK)
681 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
682 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
684 strcat(summ.from, " @ ");
685 strcat(summ.from, &buf[5]);
689 if (!strncasecmp(buf, "rcpt=", 5)) {
690 strcpy(summ.to, &buf[5]);
693 if (!strncasecmp(buf, "time=", 5)) {
694 fmt_date(summ.date, atol(&buf[5]));
698 wprintf("<TD><A HREF=\"/readfwd?startmsg=%ld"
699 "&maxmsgs=1&summary=0\">",
702 wprintf("</A></TD><TD>");
704 wprintf(" </TD><TD>");
708 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
716 void display_addressbook(long msgnum, char alpha) {
718 char mime_partnum[SIZ];
719 char mime_filename[SIZ];
720 char mime_content_type[SIZ];
721 char mime_disposition[SIZ];
723 char vcard_partnum[SIZ];
724 char *vcard_source = NULL;
734 memset(&summ, 0, sizeof(summ));
735 strcpy(summ.subj, "(no subject)");
737 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
740 if (buf[0] != '1') return;
742 while (serv_gets(buf), strcmp(buf, "000")) {
743 if (!strncasecmp(buf, "part=", 5)) {
744 extract(mime_filename, &buf[5], 1);
745 extract(mime_partnum, &buf[5], 2);
746 extract(mime_disposition, &buf[5], 3);
747 extract(mime_content_type, &buf[5], 4);
748 mime_length = extract_int(&buf[5], 5);
750 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
751 strcpy(vcard_partnum, mime_partnum);
757 if (strlen(vcard_partnum) > 0) {
758 vcard_source = load_mimepart(msgnum, vcard_partnum);
759 if (vcard_source != NULL) {
761 /* Display the summary line */
762 display_vcard(vcard_source, alpha, 0, NULL);
764 /* If it's my vCard I can edit it */
765 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
766 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
767 wprintf("<A HREF=\"/edit_vcard?"
768 "msgnum=%ld&partnum=%s\">",
769 msgnum, vcard_partnum);
770 wprintf("(edit)</A>");
781 /* If it's an old "Firstname Lastname" style record, try to
784 void lastfirst_firstlast(char *namebuf) {
789 if (namebuf == NULL) return;
790 if (strchr(namebuf, ';') != NULL) return;
792 i = num_tokens(namebuf, ' ');
795 extract_token(lastname, namebuf, i-1, ' ');
796 remove_token(namebuf, i-1, ' ');
797 strcpy(firstname, namebuf);
798 sprintf(namebuf, "%s; %s", lastname, firstname);
802 void fetch_ab_name(long msgnum, char *namebuf) {
804 char mime_partnum[SIZ];
805 char mime_filename[SIZ];
806 char mime_content_type[SIZ];
807 char mime_disposition[SIZ];
809 char vcard_partnum[SIZ];
810 char *vcard_source = NULL;
820 if (namebuf == NULL) return;
823 memset(&summ, 0, sizeof(summ));
824 strcpy(summ.subj, "(no subject)");
826 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
829 if (buf[0] != '1') return;
831 while (serv_gets(buf), strcmp(buf, "000")) {
832 if (!strncasecmp(buf, "part=", 5)) {
833 extract(mime_filename, &buf[5], 1);
834 extract(mime_partnum, &buf[5], 2);
835 extract(mime_disposition, &buf[5], 3);
836 extract(mime_content_type, &buf[5], 4);
837 mime_length = extract_int(&buf[5], 5);
839 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
840 strcpy(vcard_partnum, mime_partnum);
846 if (strlen(vcard_partnum) > 0) {
847 vcard_source = load_mimepart(msgnum, vcard_partnum);
848 if (vcard_source != NULL) {
850 /* Grab the name off the card */
851 display_vcard(vcard_source, 0, 0, namebuf);
857 lastfirst_firstlast(namebuf);
863 * Record compare function for sorting address book indices
865 int abcmp(const void *ab1, const void *ab2) {
867 (((const struct addrbookent *)ab1)->ab_name),
868 (((const struct addrbookent *)ab2)->ab_name)
874 * Render the address book using info we gathered during the scan
876 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
881 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
884 wprintf("<TABLE border=0 cellspacing=0 "
885 "cellpadding=3 width=100%%>\n"
888 for (i=0; i<num_ab; ++i) {
895 wprintf("<TR BGCOLOR=\"#%s\">",
896 (bg ? "DDDDDD" : "FFFFFF")
901 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
902 addrbook[i].ab_msgnum);
903 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
904 escputs(addrbook[i].ab_name);
905 wprintf("</A></TD>\n");
908 wprintf("</TR></TABLE>\n");
914 * load message pointers from the server
916 int load_msg_ptrs(char *servcmd)
925 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
928 while (serv_gets(buf), strcmp(buf, "000")) {
929 WC->msgarr[nummsgs] = atol(buf);
930 /* FIXME check for overflow */
938 * command loop for reading messages
940 void readloop(char *oper)
948 int num_displayed = 0;
950 int is_addressbook = 0;
951 int is_singlecard = 0;
954 int remaining_messages;
956 int lowest_displayed = (-1);
957 int highest_displayed = 0;
958 long pn_previous = 0L;
959 long pn_current = 0L;
964 struct addrbookent *addrbook = NULL;
967 startmsg = atol(bstr("startmsg"));
968 maxmsgs = atoi(bstr("maxmsgs"));
969 is_summary = atoi(bstr("summary"));
970 if (maxmsgs == 0) maxmsgs = 20;
974 if (!strcmp(oper, "readnew")) {
975 strcpy(cmd, "MSGS NEW");
976 } else if (!strcmp(oper, "readold")) {
977 strcpy(cmd, "MSGS OLD");
979 strcpy(cmd, "MSGS ALL");
982 /* FIXME put in the correct constant #defs */
983 if ((WC->wc_view == 1) && (maxmsgs > 1)) {
985 strcpy(cmd, "MSGS ALL");
989 if ((WC->wc_view == 2) && (maxmsgs > 1)) {
991 strcpy(cmd, "MSGS ALL");
995 is_singlecard = atoi(bstr("is_singlecard"));
997 /* Display the letter indices across the top */
998 if ((is_addressbook) || (is_singlecard)) {
999 if (strlen(bstr("alpha")) == 0) {
1003 strcpy(buf, bstr("alpha"));
1007 for (i='1'; i<='z'; ++i) if ((i=='1')||(islower(i))) {
1008 if ((i != alpha) || (is_singlecard)) {
1009 wprintf("<A HREF=\"/readfwd?alpha=%c\">", i);
1011 if (i == alpha) wprintf("<FONT SIZE=+2>");
1013 wprintf("%c", toupper(i));
1018 if (i == alpha) wprintf("</FONT>");
1019 if ((i != alpha) || (is_singlecard)) {
1025 wprintf("<HR width=100%%>\n");
1028 if (WC->wc_view == 3) { /* calendar */
1030 strcpy(cmd, "MSGS ALL");
1033 if (WC->wc_view == 4) { /* tasks */
1035 strcpy(cmd, "MSGS ALL");
1040 nummsgs = load_msg_ptrs(cmd);
1043 if ((!is_tasks) && (!is_calendar)) {
1044 if (!strcmp(oper, "readnew")) {
1045 wprintf("<EM>No new messages in this room.</EM>\n");
1046 } else if (!strcmp(oper, "readold")) {
1047 wprintf("<EM>No old messages in this room.</EM>\n");
1049 wprintf("<EM>This room is empty.</EM>\n");
1056 if (startmsg == 0L) startmsg = WC->msgarr[0];
1057 remaining_messages = 0;
1059 for (a = 0; a < nummsgs; ++a) {
1060 if (WC->msgarr[a] >= startmsg) {
1061 ++remaining_messages;
1066 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n"
1067 "<TABLE border=0 cellspacing=0 "
1068 "cellpadding=0 width=100%%>\n"
1070 "<TD><I>Subject</I></TD>"
1071 "<TD><I>Sender</I></TD>"
1072 "<TD><I>Date</I></TD>"
1078 for (a = 0; a < nummsgs; ++a) {
1079 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1081 /* Learn which msgs "Prev" & "Next" buttons go to */
1082 pn_current = WC->msgarr[a];
1083 if (a > 0) pn_previous = WC->msgarr[a-1];
1084 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1086 /* If a tabular view, set up the line */
1089 wprintf("<TR BGCOLOR=\"#%s\">",
1090 (bg ? "DDDDDD" : "FFFFFF")
1094 /* Display the message */
1096 summarize_message(WC->msgarr[a]);
1098 else if (is_addressbook) {
1099 fetch_ab_name(WC->msgarr[a], buf);
1100 if ((strlen(buf) > 0) && (isalpha(buf[0]))) {
1101 ab_alpha = tolower(buf[0]);
1106 if (alpha == ab_alpha) {
1108 addrbook = realloc(addrbook,
1109 (sizeof(struct addrbookent) * num_ab) );
1110 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1111 sizeof(addrbook[num_ab-1].ab_name));
1112 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1115 else if (is_calendar) {
1116 display_calendar(WC->msgarr[a]);
1118 else if (is_tasks) {
1119 display_task(WC->msgarr[a]);
1122 read_message(WC->msgarr[a]);
1125 /* If a tabular view, finish the line */
1130 if (lowest_displayed < 0) lowest_displayed = a;
1131 highest_displayed = a;
1134 --remaining_messages;
1139 wprintf("</TABLE>\n");
1146 /* Bump these because although we're thinking in zero base, the user
1147 * is a drooling idiot and is thinking in one base.
1150 ++highest_displayed;
1152 /* If we're only looking at one message, do a prev/next thing */
1153 if (num_displayed == 1) {
1154 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1157 "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1158 "Reading #%d of %d messages.</TD>\n"
1159 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1160 lowest_displayed, nummsgs);
1163 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1164 "VALUE=\"Delete selected\">\n");
1167 if (pn_previous > 0L) {
1168 wprintf("<A HREF=\"/%s"
1178 wprintf("<A HREF=\"/%s"
1187 wprintf("<A HREF=\"/%s?startmsg=%ld"
1188 "&maxmsgs=999999&summary=1\">"
1194 wprintf("</TD></TR></TABLE></CENTER>\n");
1200 * If we're not currently looking at ALL requested
1201 * messages, then display the selector bar
1203 if (num_displayed > 1) {
1204 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1206 "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1207 "Reading #%d-%d of %d messages.</TD>\n"
1208 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1209 lowest_displayed, highest_displayed, nummsgs);
1212 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1213 "VALUE=\"Delete selected\">\n");
1217 for (b=0; b<nummsgs; b = b + maxmsgs) {
1220 if (hi > nummsgs) hi = nummsgs;
1221 if (WC->msgarr[b] != startmsg) {
1222 wprintf("<A HREF=\"/%s"
1234 wprintf("%d-%d \n", lo, hi);
1238 wprintf("<A HREF=\"/%s?startmsg=%ld"
1239 "&maxmsgs=999999&summary=%d\">"
1243 WC->msgarr[0], is_summary);
1245 wprintf("<A HREF=\"/%s?startmsg=%ld"
1246 "&maxmsgs=999999&summary=1\">"
1252 wprintf("</TD></TR></TABLE></CENTER>\n");
1255 if (is_summary) wprintf("</FORM>\n");
1259 wprintf("<A HREF=\"/display_edit_task?msgnum=0\">"
1260 "Add new task</A>\n"
1265 do_calendar_view(); /* Render the calendar */
1268 if (is_addressbook) {
1269 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1273 if (addrbook != NULL) free(addrbook);
1278 * Back end for post_message() ... this is where the actual message
1279 * gets transmitted to the server.
1281 void post_mime_to_server(void) {
1283 int is_multipart = 0;
1285 struct wc_attachment *att;
1287 size_t encoded_length;
1289 /* If there are attachments, we have to do multipart/mixed */
1290 if (WC->first_attachment != NULL) {
1295 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1296 serv_info.serv_fqdn,
1301 /* Remember, serv_printf() appends an extra newline */
1302 serv_printf("Content-type: multipart/mixed; "
1303 "boundary=\"%s\"\n", boundary);
1304 serv_printf("This is a multipart message in MIME format.\n");
1305 serv_printf("--%s", boundary);
1308 serv_puts("Content-type: text/html");
1310 text_to_server(bstr("msgtext"), 1);
1314 /* Add in the attachments */
1315 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1317 encoded_length = ((att->length * 150) / 100);
1318 encoded = malloc(encoded_length);
1319 if (encoded == NULL) break;
1320 CtdlEncodeBase64(encoded, att->data, att->length);
1322 serv_printf("--%s", boundary);
1323 serv_printf("Content-type: %s", att->content_type);
1324 serv_printf("Content-disposition: attachment; "
1325 "filename=\"%s\"", att->filename);
1326 serv_puts("Content-transfer-encoding: base64");
1328 serv_write(encoded, strlen(encoded));
1333 serv_printf("--%s--", boundary);
1341 * Post message (or don't post message)
1343 * Note regarding the "dont_post" variable:
1344 * A random value (actually, it's just a timestamp) is inserted as a hidden
1345 * field called "postseq" when the display_enter page is generated. This
1346 * value is checked when posting, using the static variable dont_post. If a
1347 * user attempts to post twice using the same dont_post value, the message is
1348 * discarded. This prevents the accidental double-saving of the same message
1349 * if the user happens to click the browser "back" button.
1351 void post_message(void)
1354 static long dont_post = (-1L);
1355 struct wc_attachment *att;
1357 if (WC->upload_length > 0) {
1359 att = malloc(sizeof(struct wc_attachment));
1360 memset(att, 0, sizeof(struct wc_attachment));
1361 att->next = WC->first_attachment;
1362 WC->first_attachment = att;
1363 att->length = WC->upload_length;
1364 strcpy(att->content_type, WC->upload_content_type);
1365 strcpy(att->filename, WC->upload_filename);
1367 /* Netscape sends a simple filename, which is what we want,
1368 * but Satan's browser sends an entire pathname. Reduce
1369 * the path to just a filename if we need to.
1371 while (num_tokens(att->filename, '/') > 1) {
1372 remove_token(att->filename, 0, '/');
1374 while (num_tokens(att->filename, '\\') > 1) {
1375 remove_token(att->filename, 0, '\\');
1378 /* Transfer control of this memory from the upload struct
1379 * to the attachment struct.
1381 att->data = WC->upload;
1382 WC->upload_length = 0;
1388 if (strcasecmp(bstr("sc"), "Save message")) {
1389 sprintf(WC->ImportantMessage,
1390 "Cancelled. Message was not posted.");
1391 } else if (atol(bstr("postseq")) == dont_post) {
1392 sprintf(WC->ImportantMessage,
1393 "Automatically cancelled because you have already "
1394 "saved this message.");
1396 sprintf(buf, "ENT0 1|%s|0|4|%s",
1401 if (buf[0] == '4') {
1402 post_mime_to_server();
1403 sprintf(WC->ImportantMessage,
1404 "Message has been posted.\n");
1405 dont_post = atol(bstr("postseq"));
1407 sprintf(WC->ImportantMessage,
1412 free_attachments(WC);
1413 readloop("readnew");
1420 * display the message entry screen
1422 void display_enter(void)
1426 struct wc_attachment *att;
1428 if (strlen(bstr("force_room")) > 0) {
1429 gotoroom(bstr("force_room"), 0);
1432 /* Are we perhaps in an address book view? If so, then an "enter
1433 * message" command really means "add new entry."
1435 if (WC->wc_view == 2) {
1436 do_edit_vcard(-1, "", "");
1440 /* Otherwise proceed normally */
1442 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1446 if (!strncmp(buf, "570", 3)) {
1447 if (strlen(bstr("recp")) > 0) {
1448 svprintf("RECPERROR", WCS_STRING,
1449 "<SPAN CLASS=\"errormsg\">%s</SPAN><BR>\n",
1453 do_template("prompt_for_recipient");
1456 if (buf[0] != '2') {
1457 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1463 strcat(&buf[strlen(buf)], " <I>from</I> ");
1464 stresc(&buf[strlen(buf)], WC->wc_username, 1);
1465 if (strlen(bstr("recp")) > 0) {
1466 strcat(&buf[strlen(buf)], " <I>to</I> ");
1467 stresc(&buf[strlen(buf)], bstr("recp"), 1);
1469 strcat(&buf[strlen(buf)], " <I>in</I> ");
1470 stresc(&buf[strlen(buf)], WC->wc_roomname, 1);
1471 svprintf("BOXTITLE", WCS_STRING, buf);
1472 do_template("beginbox");
1474 wprintf("<CENTER>\n");
1476 wprintf("<FORM ENCTYPE=\"multipart/form-data\" "
1477 "METHOD=\"POST\" ACTION=\"/post\" "
1478 "NAME=\"enterform\">\n");
1479 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
1481 wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
1483 wprintf("<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \" "
1484 "onLoad=\"document.enterform.msgtext.focus();\" >");
1485 wprintf("<FONT SIZE=-1>Subject (optional):</FONT>"
1486 "<INPUT TYPE=\"text\" NAME=\"subject\" VALUE=\"");
1487 escputs(bstr("subject"));
1488 wprintf("\" MAXLENGTH=70>"
1489 " "
1490 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
1491 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
1493 wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=25 COLS=80 "
1495 escputs(bstr("msgtext"));
1496 wprintf("</TEXTAREA><BR>\n");
1498 /* Enumerate any attachments which are already in place... */
1499 for (att = WC->first_attachment; att != NULL; att = att->next) {
1500 wprintf("<IMG SRC=\"/static/attachment.gif\" "
1501 "BORDER=0 ALIGN=MIDDLE> Attachment: ");
1502 escputs(att->filename);
1503 wprintf(" (%s, %d bytes)<BR>\n",
1504 att->content_type, att->length);
1507 /* Now offer the ability to attach additional files... */
1508 wprintf("Attach file: <input NAME=\"attachfile\" "
1509 "SIZE=48 TYPE=\"file\">\n "
1510 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1512 wprintf("</FORM></CENTER>\n");
1513 do_template("endbox");
1514 DONE: wDumpContent(1);
1524 void delete_msg(void)
1529 msgid = atol(bstr("msgid"));
1533 sprintf(buf, "DELE %ld", msgid);
1536 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1545 * Confirm move of a message
1547 void confirm_move_msg(void)
1553 msgid = atol(bstr("msgid"));
1557 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1558 wprintf("<FONT SIZE=+1 COLOR=\"#FFFFFF\"");
1559 wprintf("<B>Confirm move of message</B>\n");
1560 wprintf("</FONT></TD></TR></TABLE>\n");
1562 wprintf("<CENTER>");
1564 wprintf("Please select the room to which you would like this message moved:<BR>\n");
1566 wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1567 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1571 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1574 if (buf[0] == '1') {
1575 while (serv_gets(buf), strcmp(buf, "000")) {
1576 extract(targ, buf, 0);
1577 wprintf("<OPTION>");
1582 wprintf("</SELECT>\n");
1585 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1586 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1587 wprintf("</FORM></CENTER>\n");
1589 wprintf("</CENTER>\n");
1600 msgid = atol(bstr("msgid"));
1604 if (!strcasecmp(bstr("yesno"), "Move")) {
1605 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1608 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1610 wprintf("<EM>Message not moved.</EM><BR>\n");
1618 void do_stuff_to_msgs(void) {
1623 struct stuff_t *next;
1627 struct stuff_t *stuff = NULL;
1628 struct stuff_t *ptr;
1631 serv_puts("MSGS ALL");
1634 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1635 ptr = malloc(sizeof(struct stuff_t));
1636 ptr->msgnum = atol(buf);
1641 strcpy(sc, bstr("sc"));
1643 while (stuff != NULL) {
1645 sprintf(buf, "msg_%ld", stuff->msgnum);
1646 if (!strcasecmp(bstr(buf), "yes")) {
1648 if (!strcasecmp(sc, "Delete selected")) {
1649 serv_printf("DELE %ld", stuff->msgnum);
1660 readloop("readfwd");