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);
106 if ((strlen(storename)>0) && (storename[0] != ';')) {
107 while(storename[strlen(storename)-1] == ';') {
108 storename[strlen(storename)-1] = 0;
117 /* display_vcard() calls this after parsing the textual vCard into
118 * our 'struct vCard' data object.
120 * Set 'full' to nonzero to display the full card, otherwise it will only
121 * show a summary line.
123 * This code is a bit ugly, so perhaps an explanation is due: we do this
124 * in two passes through the vCard fields. On the first pass, we process
125 * fields we understand, and then render them in a pretty fashion at the
126 * end. Then we make a second pass, outputting all the fields we don't
127 * understand in a simple two-column name/value format.
129 void display_parsed_vcard(struct vCard *v, int full) {
135 char *thisname, *thisvalue;
136 char firsttoken[SIZ];
139 char displayname[SIZ];
143 strcpy(displayname, "");
149 name = vcard_get_prop(v, "fn", 1, 0, 0);
153 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
163 wprintf("<TABLE bgcolor=#888888>");
164 for (pass=1; pass<=2; ++pass) {
166 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
168 thisname = strdup(v->prop[i].name);
169 extract_token(firsttoken, thisname, 0, ';');
171 for (j=0; j<num_tokens(thisname, ';'); ++j) {
172 extract_token(buf, thisname, j, ';');
173 if (!strcasecmp(buf, "encoding=quoted-printable")) {
175 remove_token(thisname, j, ';');
177 if (!strcasecmp(buf, "encoding=base64")) {
179 remove_token(thisname, j, ';');
184 thisvalue = malloc(strlen(v->prop[i].value) + 50);
185 j = CtdlDecodeQuotedPrintable(
186 thisvalue, v->prop[i].value,
187 strlen(v->prop[i].value) );
191 thisvalue = malloc(strlen(v->prop[i].value) + 50);
193 thisvalue, v->prop[i].value,
194 strlen(v->prop[i].value) );
197 thisvalue = strdup(v->prop[i].value);
200 /*** Various fields we may encounter ***/
202 /* N is name, but only if there's no FN already there */
203 if (!strcasecmp(firsttoken, "n")) {
204 if (strlen(displayname) == 0) {
205 strcpy(displayname, thisvalue);
209 /* FN (full name) is a true 'display name' field */
210 else if (!strcasecmp(firsttoken, "fn")) {
211 strcpy(displayname, thisvalue);
214 else if (!strcasecmp(firsttoken, "email")) {
215 if (strlen(mailto) > 0) strcat(mailto, "<br />");
217 "<A HREF=\"/display_enter"
218 "?force_room=_MAIL_&recp=");
219 urlesc(&mailto[strlen(mailto)], thisvalue);
220 strcat(mailto, "\">");
221 urlesc(&mailto[strlen(mailto)], thisvalue);
222 strcat(mailto, "</A>");
224 else if (!strcasecmp(firsttoken, "tel")) {
225 if (strlen(phone) > 0) strcat(phone, "<br />");
226 strcat(phone, thisvalue);
227 for (j=0; j<num_tokens(thisname, ';'); ++j) {
228 extract_token(buf, thisname, j, ';');
229 if (!strcasecmp(buf, "tel"))
231 else if (!strcasecmp(buf, "work"))
232 strcat(phone, " (work)");
233 else if (!strcasecmp(buf, "home"))
234 strcat(phone, " (home)");
235 else if (!strcasecmp(buf, "cell"))
236 strcat(phone, " (cell)");
244 else if (!strcasecmp(firsttoken, "adr")) {
246 wprintf("<TR><TD>Address:</TD><TD>");
247 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
248 extract_token(buf, thisvalue, j, ';');
249 if (strlen(buf) > 0) {
254 wprintf("</TD></TR>\n");
257 else if (!strcasecmp(firsttoken, "version")) {
260 else if (!strcasecmp(firsttoken, "rev")) {
263 else if (!strcasecmp(firsttoken, "label")) {
270 wprintf("</TD><TD>");
272 wprintf("</TD></TR>\n");
281 wprintf("<TR BGCOLOR=\"#AAAAAA\">"
282 "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
283 "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
284 "<FONT SIZE=+1><B>");
285 escputs(displayname);
286 wprintf("</B></FONT></TD></TR>\n");
288 if (strlen(phone) > 0)
289 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
290 if (strlen(mailto) > 0)
291 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
296 wprintf("</TABLE>\n");
302 * Display a textual vCard
303 * (Converts to a vCard object and then calls the actual display function)
304 * Set 'full' to nonzero to display the whole card instead of a one-liner.
305 * Or, if "storename" is non-NULL, just store the person's name in that
306 * buffer instead of displaying the card at all.
308 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
314 v = vcard_load(vcard_source);
315 if (v == NULL) return;
317 name = vcard_get_prop(v, "n", 1, 0, 0);
323 if (storename != NULL) {
324 fetchname_parsed_vcard(v, storename);
326 else if ( (alpha == 0)
327 || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
328 || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
329 display_parsed_vcard(v, full);
339 * I wanna SEE that message!
341 void read_message(long msgnum) {
343 char mime_partnum[SIZ];
344 char mime_filename[SIZ];
345 char mime_content_type[SIZ];
346 char mime_disposition[SIZ];
358 char vcard_partnum[SIZ];
359 char cal_partnum[SIZ];
360 char *part_source = NULL;
365 strcpy(reply_to, "");
366 strcpy(vcard_partnum, "");
367 strcpy(cal_partnum, "");
368 strcpy(mime_http, "");
370 serv_printf("MSG4 %ld", msgnum);
373 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
377 /* begin everythingamundo table */
378 wprintf("<div style=\"margin-right:1px\">\n");
379 wprintf("<table width=100%% border=1 cellspacing=0 "
380 "cellpadding=0><TR><TD>\n");
382 /* begin message header table */
383 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
384 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
386 wprintf("<SPAN CLASS=\"message_header\">");
387 strcpy(m_subject, "");
389 while (serv_gets(buf), strcasecmp(buf, "text")) {
390 if (!strcmp(buf, "000")) {
391 wprintf("<I>unexpected end of message</I><br /><br />\n");
392 wprintf("</SPAN>\n");
395 if (!strncasecmp(buf, "nhdr=yes", 8))
399 if (!strncasecmp(buf, "type=", 5))
400 format_type = atoi(&buf[5]);
401 if (!strncasecmp(buf, "from=", 5)) {
402 strcpy(from, &buf[5]);
403 wprintf("from <A HREF=\"/showuser&who=");
409 if (!strncasecmp(buf, "subj=", 5))
410 strcpy(m_subject, &buf[5]);
411 if ((!strncasecmp(buf, "hnod=", 5))
412 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
413 wprintf("(%s) ", &buf[5]);
414 if ((!strncasecmp(buf, "room=", 5))
415 && (strcasecmp(&buf[5], WC->wc_roomname))
416 && (strlen(&buf[5])>0) )
417 wprintf("in %s> ", &buf[5]);
418 if (!strncasecmp(buf, "rfca=", 5)) {
419 strcpy(rfca, &buf[5]);
425 if (!strncasecmp(buf, "node=", 5)) {
426 strcpy(node, &buf[5]);
427 if ( ((WC->room_flags & QR_NETWORK)
428 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
429 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
432 wprintf("@%s ", &buf[5]);
435 if (!strncasecmp(buf, "rcpt=", 5))
436 wprintf("to %s ", &buf[5]);
437 if (!strncasecmp(buf, "time=", 5)) {
438 fmt_date(now, atol(&buf[5]));
442 if (!strncasecmp(buf, "part=", 5)) {
443 extract(mime_filename, &buf[5], 1);
444 extract(mime_partnum, &buf[5], 2);
445 extract(mime_disposition, &buf[5], 3);
446 extract(mime_content_type, &buf[5], 4);
447 mime_length = extract_int(&buf[5], 5);
449 if (!strcasecmp(mime_disposition, "attachment")) {
450 snprintf(&mime_http[strlen(mime_http)],
451 (sizeof(mime_http) - strlen(mime_http) - 1),
452 "<A HREF=\"/output_mimepart?"
453 "msgnum=%ld&partnum=%s\" "
454 "TARGET=\"wc.%ld.%s\">"
455 "<IMG SRC=\"/static/attachment.gif\" "
456 "BORDER=0 ALIGN=MIDDLE>\n"
457 "Part %s: %s (%s, %d bytes)</A><br />\n",
458 msgnum, mime_partnum,
459 msgnum, mime_partnum,
460 mime_partnum, mime_filename,
461 mime_content_type, mime_length);
464 if ((!strcasecmp(mime_disposition, "inline"))
465 && (!strncasecmp(mime_content_type, "image/", 6)) ){
466 snprintf(&mime_http[strlen(mime_http)],
467 (sizeof(mime_http) - strlen(mime_http) - 1),
468 "<IMG SRC=\"/output_mimepart?"
469 "msgnum=%ld&partnum=%s\">",
470 msgnum, mime_partnum);
473 /*** begin handler prep ***/
474 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
475 strcpy(vcard_partnum, mime_partnum);
478 if (!strcasecmp(mime_content_type, "text/calendar")) {
479 strcpy(cal_partnum, mime_partnum);
482 /*** end handler prep ***/
488 /* Generate a reply-to address */
489 if (strlen(rfca) > 0) {
490 strcpy(reply_to, rfca);
493 if ( (strlen(node) > 0)
494 && (strcasecmp(node, serv_info.serv_nodename))
495 && (strcasecmp(node, serv_info.serv_humannode)) ) {
496 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
500 snprintf(reply_to, sizeof(reply_to), "%s", from);
509 if (strlen(m_subject) > 0) {
511 "<SPAN CLASS=\"message_subject\">"
518 /* start msg buttons */
519 wprintf("<TD ALIGN=RIGHT>\n");
520 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_one_msg\">\n");
521 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%ld\">\n",
523 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
527 if (!strncasecmp(m_subject, "Re:", 2)) {
528 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"");
532 else if (strlen(m_subject) > 0) {
533 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"Re: ");
538 wprintf("<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\" VALUE=\"Reply\">\n");
540 if (WC->is_room_aide) {
541 wprintf("<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\"VALUE=\"Move\">\n"
542 "<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\" VALUE=\"Delete\""
543 "onClick=\"return confirm('Delete this message?');\">\n");
547 "</TD></TR></TABLE>\n");
550 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
551 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
554 * Learn the content type
556 strcpy(mime_content_type, "text/plain");
557 while (serv_gets(buf), (strlen(buf) > 0)) {
558 if (!strcmp(buf, "000")) {
559 wprintf("<I>unexpected end of message</I><br /><br />\n");
562 if (!strncasecmp(buf, "Content-type: ", 14)) {
563 safestrncpy(mime_content_type, &buf[14],
564 sizeof(mime_content_type));
568 /* Messages in legacy Citadel variformat get handled thusly... */
569 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
570 fmout(NULL, "JUSTIFY");
573 /* Boring old 80-column fixed format text gets handled this way... */
574 else if (!strcasecmp(mime_content_type, "text/plain")) {
575 while (serv_gets(buf), strcmp(buf, "000")) {
576 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
577 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
578 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
579 buf[strlen(buf) - 1] = 0;
581 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
582 wprintf("<BLOCKQUOTE>");
584 } else if ((bq == 1) &&
585 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
586 wprintf("</BLOCKQUOTE>");
592 wprintf("</TT><br />\n");
594 wprintf("</I><br />");
597 else /* HTML is fun, but we've got to strip it first */
598 if (!strcasecmp(mime_content_type, "text/html")) {
602 /* Unknown weirdness */
604 wprintf("I don't know how to display %s<br />\n",
606 while (serv_gets(buf), strcmp(buf, "000")) { }
610 /* Afterwards, offer links to download attachments 'n' such */
611 if (strlen(mime_http) > 0) {
612 wprintf("%s", mime_http);
615 /* Handler for vCard parts */
616 if (strlen(vcard_partnum) > 0) {
617 part_source = load_mimepart(msgnum, vcard_partnum);
618 if (part_source != NULL) {
620 /* If it's my vCard I can edit it */
621 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
622 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
623 wprintf("<A HREF=\"/edit_vcard?"
624 "msgnum=%ld&partnum=%s\">",
625 msgnum, vcard_partnum);
626 wprintf("(edit)</A>");
629 /* In all cases, display the full card */
630 display_vcard(part_source, 0, 1, NULL);
634 /* Handler for calendar parts */
635 if (strlen(cal_partnum) > 0) {
636 part_source = load_mimepart(msgnum, cal_partnum);
637 if (part_source != NULL) {
638 cal_process_attachment(part_source,
639 msgnum, cal_partnum);
649 wprintf("</TD></TR></TABLE>\n");
651 /* end everythingamundo table */
652 wprintf("</TD></TR></TABLE>\n");
657 void summarize_message(long msgnum, int is_new) {
668 memset(&summ, 0, sizeof(summ));
669 strcpy(summ.subj, "(no subject)");
671 sprintf(buf, "MSG0 %ld|3", msgnum); /* ask for headers only with no MIME */
674 if (buf[0] != '1') return;
676 while (serv_gets(buf), strcmp(buf, "000")) {
677 if (!strncasecmp(buf, "from=", 5)) {
678 strcpy(summ.from, &buf[5]);
680 if (!strncasecmp(buf, "subj=", 5)) {
681 strcpy(summ.subj, &buf[5]);
683 if (!strncasecmp(buf, "rfca=", 5)) {
684 strcat(summ.from, " <");
685 strcat(summ.from, &buf[5]);
686 strcat(summ.from, ">");
689 if (!strncasecmp(buf, "node=", 5)) {
690 if ( ((WC->room_flags & QR_NETWORK)
691 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
692 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
694 strcat(summ.from, " @ ");
695 strcat(summ.from, &buf[5]);
699 if (!strncasecmp(buf, "rcpt=", 5)) {
700 strcpy(summ.to, &buf[5]);
703 if (!strncasecmp(buf, "time=", 5)) {
704 fmt_date(summ.date, atol(&buf[5]));
709 if (is_new) wprintf("<B>");
710 wprintf("<A HREF=\"/readfwd?startmsg=%ld"
711 "&maxmsgs=1&summary=0\">",
715 if (is_new) wprintf("</B>");
716 wprintf("</TD><TD>");
717 if (is_new) wprintf("<B>");
719 if (is_new) wprintf("</B>");
720 wprintf(" </TD><TD>");
721 if (is_new) wprintf("<B>");
723 if (is_new) wprintf("</B>");
726 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
734 void display_addressbook(long msgnum, char alpha) {
736 char mime_partnum[SIZ];
737 char mime_filename[SIZ];
738 char mime_content_type[SIZ];
739 char mime_disposition[SIZ];
741 char vcard_partnum[SIZ];
742 char *vcard_source = NULL;
752 memset(&summ, 0, sizeof(summ));
753 strcpy(summ.subj, "(no subject)");
755 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
758 if (buf[0] != '1') return;
760 while (serv_gets(buf), strcmp(buf, "000")) {
761 if (!strncasecmp(buf, "part=", 5)) {
762 extract(mime_filename, &buf[5], 1);
763 extract(mime_partnum, &buf[5], 2);
764 extract(mime_disposition, &buf[5], 3);
765 extract(mime_content_type, &buf[5], 4);
766 mime_length = extract_int(&buf[5], 5);
768 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
769 strcpy(vcard_partnum, mime_partnum);
775 if (strlen(vcard_partnum) > 0) {
776 vcard_source = load_mimepart(msgnum, vcard_partnum);
777 if (vcard_source != NULL) {
779 /* Display the summary line */
780 display_vcard(vcard_source, alpha, 0, NULL);
782 /* If it's my vCard I can edit it */
783 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
784 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
785 wprintf("<A HREF=\"/edit_vcard?"
786 "msgnum=%ld&partnum=%s\">",
787 msgnum, vcard_partnum);
788 wprintf("(edit)</A>");
799 /* If it's an old "Firstname Lastname" style record, try to
802 void lastfirst_firstlast(char *namebuf) {
807 if (namebuf == NULL) return;
808 if (strchr(namebuf, ';') != NULL) return;
810 i = num_tokens(namebuf, ' ');
813 extract_token(lastname, namebuf, i-1, ' ');
814 remove_token(namebuf, i-1, ' ');
815 strcpy(firstname, namebuf);
816 sprintf(namebuf, "%s; %s", lastname, firstname);
820 void fetch_ab_name(long msgnum, char *namebuf) {
822 char mime_partnum[SIZ];
823 char mime_filename[SIZ];
824 char mime_content_type[SIZ];
825 char mime_disposition[SIZ];
827 char vcard_partnum[SIZ];
828 char *vcard_source = NULL;
839 if (namebuf == NULL) return;
842 memset(&summ, 0, sizeof(summ));
843 strcpy(summ.subj, "(no subject)");
845 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
848 if (buf[0] != '1') return;
850 while (serv_gets(buf), strcmp(buf, "000")) {
851 if (!strncasecmp(buf, "part=", 5)) {
852 extract(mime_filename, &buf[5], 1);
853 extract(mime_partnum, &buf[5], 2);
854 extract(mime_disposition, &buf[5], 3);
855 extract(mime_content_type, &buf[5], 4);
856 mime_length = extract_int(&buf[5], 5);
858 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
859 strcpy(vcard_partnum, mime_partnum);
865 if (strlen(vcard_partnum) > 0) {
866 vcard_source = load_mimepart(msgnum, vcard_partnum);
867 if (vcard_source != NULL) {
869 /* Grab the name off the card */
870 display_vcard(vcard_source, 0, 0, namebuf);
876 lastfirst_firstlast(namebuf);
878 for (i=0; i<strlen(namebuf); ++i) {
879 if (namebuf[i] != ';') return;
881 strcpy(namebuf, "(no name)");
887 * Record compare function for sorting address book indices
889 int abcmp(const void *ab1, const void *ab2) {
891 (((const struct addrbookent *)ab1)->ab_name),
892 (((const struct addrbookent *)ab2)->ab_name)
898 * Helper function for do_addrbook_view()
899 * Converts a name into a three-letter tab label
901 void nametab(char *tabbuf, char *name) {
902 stresc(tabbuf, name, 0, 0);
903 tabbuf[0] = toupper(tabbuf[0]);
904 tabbuf[1] = tolower(tabbuf[1]);
905 tabbuf[2] = tolower(tabbuf[2]);
911 * Render the address book using info we gathered during the scan
913 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
917 static int NAMESPERPAGE = 60;
921 char tabfirst_label[SIZ];
923 char tablast_label[SIZ];
926 wprintf("<I>This address book is empty.</I>\n");
931 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
934 num_pages = num_ab / NAMESPERPAGE;
936 page = atoi(bstr("page"));
939 for (i=0; i<=num_pages; ++i) {
941 wprintf("<A HREF=\"/readfwd?page=%d\">", i);
946 tabfirst = i * NAMESPERPAGE;
947 tablast = tabfirst + NAMESPERPAGE - 1;
948 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
949 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
950 nametab(tablast_label, addrbook[tablast].ab_name);
951 wprintf("[%s - %s]",
952 tabfirst_label, tablast_label
963 wprintf("<TABLE border=0 cellspacing=0 "
964 "cellpadding=3 width=100%%>\n"
967 for (i=0; i<num_ab; ++i) {
969 if ((i / NAMESPERPAGE) == page) {
971 if ((displayed % 4) == 0) {
976 wprintf("<TR BGCOLOR=\"#%s\">",
977 (bg ? "DDDDDD" : "FFFFFF")
983 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
984 addrbook[i].ab_msgnum);
985 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
986 escputs(addrbook[i].ab_name);
987 wprintf("</A></TD>\n");
992 wprintf("</TR></TABLE>\n");
998 * load message pointers from the server
1000 int load_msg_ptrs(char *servcmd)
1007 maxload = sizeof(WC->msgarr) / sizeof(long) ;
1010 if (buf[0] != '1') {
1011 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1014 while (serv_gets(buf), strcmp(buf, "000")) {
1015 if (nummsgs < maxload) {
1016 WC->msgarr[nummsgs] = atol(buf);
1025 * command loop for reading messages
1027 void readloop(char *oper)
1037 int num_displayed = 0;
1039 int is_addressbook = 0;
1040 int is_singlecard = 0;
1041 int is_calendar = 0;
1044 int remaining_messages;
1046 int lowest_displayed = (-1);
1047 int highest_displayed = 0;
1048 long pn_previous = 0L;
1049 long pn_current = 0L;
1052 struct addrbookent *addrbook = NULL;
1055 startmsg = atol(bstr("startmsg"));
1056 maxmsgs = atoi(bstr("maxmsgs"));
1057 is_summary = atoi(bstr("summary"));
1058 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1060 output_headers(1, 1, 1, 0, 0, 0, 0);
1062 /* When in summary mode, always show ALL messages instead of just
1063 * new or old. Otherwise, show what the user asked for.
1065 if (!strcmp(oper, "readnew")) {
1066 strcpy(cmd, "MSGS NEW");
1068 else if (!strcmp(oper, "readold")) {
1069 strcpy(cmd, "MSGS OLD");
1072 strcpy(cmd, "MSGS ALL");
1075 if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1077 strcpy(cmd, "MSGS ALL");
1080 if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1082 strcpy(cmd, "MSGS ALL");
1087 strcpy(cmd, "MSGS ALL");
1090 /* Are we doing a summary view? If so, we need to know old messages
1091 * and new messages, so we can do that pretty boldface thing for the
1094 strcpy(old_msgs, "");
1098 if (buf[0] == '2') {
1099 strcpy(old_msgs, &buf[4]);
1103 is_singlecard = atoi(bstr("is_singlecard"));
1105 if (WC->wc_view == VIEW_CALENDAR) { /* calendar */
1107 strcpy(cmd, "MSGS ALL");
1110 if (WC->wc_view == VIEW_TASKS) { /* tasks */
1112 strcpy(cmd, "MSGS ALL");
1115 if (WC->wc_view == VIEW_NOTES) { /* notes */
1117 strcpy(cmd, "MSGS ALL");
1121 nummsgs = load_msg_ptrs(cmd);
1124 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1125 if (!strcmp(oper, "readnew")) {
1126 wprintf("<EM>No new messages.</EM>\n");
1127 } else if (!strcmp(oper, "readold")) {
1128 wprintf("<EM>No old messages.</EM>\n");
1130 wprintf("<EM>No messages here.</EM>\n");
1137 if (startmsg == 0L) startmsg = WC->msgarr[0];
1138 remaining_messages = 0;
1140 for (a = 0; a < nummsgs; ++a) {
1141 if (WC->msgarr[a] >= startmsg) {
1142 ++remaining_messages;
1146 wprintf("<FORM NAME=\"msgomatic\" "
1147 "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
1149 wprintf("<div style=\"margin-right:1px\">"
1150 "<table border=0 cellspacing=0 "
1151 "cellpadding=0 width=100%%>\n"
1153 "<TD><I>Subject</I></TD>"
1154 "<TD><I>Sender</I></TD>"
1155 "<TD><I>Date</I></TD>"
1161 for (a = 0; a < nummsgs; ++a) {
1162 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1164 /* Are you a new message, or an old message? */
1167 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1175 /* Learn which msgs "Prev" & "Next" buttons go to */
1176 pn_current = WC->msgarr[a];
1177 if (a > 0) pn_previous = WC->msgarr[a-1];
1178 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1180 /* If a tabular view, set up the line */
1183 wprintf("<TR BGCOLOR=\"#%s\">",
1184 (bg ? "DDDDDD" : "FFFFFF")
1188 /* Display the message */
1190 summarize_message(WC->msgarr[a], is_new);
1192 else if (is_addressbook) {
1193 fetch_ab_name(WC->msgarr[a], buf);
1195 addrbook = realloc(addrbook,
1196 (sizeof(struct addrbookent) * num_ab) );
1197 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1198 sizeof(addrbook[num_ab-1].ab_name));
1199 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1201 else if (is_calendar) {
1202 display_calendar(WC->msgarr[a]);
1204 else if (is_tasks) {
1205 display_task(WC->msgarr[a]);
1207 else if (is_notes) {
1208 display_note(WC->msgarr[a]);
1211 read_message(WC->msgarr[a]);
1214 /* If a tabular view, finish the line */
1219 if (lowest_displayed < 0) lowest_displayed = a;
1220 highest_displayed = a;
1223 --remaining_messages;
1228 wprintf("</table></div>\n");
1231 /* Bump these because although we're thinking in zero base, the user
1232 * is a drooling idiot and is thinking in one base.
1235 ++highest_displayed;
1237 /* If we're only looking at one message, do a prev/next thing */
1238 if (num_displayed == 1) {
1239 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1241 wprintf("<div style=\"margin-right:1px\">"
1242 "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>"
1243 "Reading #%d of %d messages.</TD>\n"
1244 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1245 lowest_displayed, nummsgs);
1248 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1249 "VALUE=\"Delete selected\">\n");
1252 if (pn_previous > 0L) {
1253 wprintf("<A HREF=\"/%s"
1263 wprintf("<A HREF=\"/%s"
1272 wprintf("<A HREF=\"/%s?startmsg=%ld"
1273 "&maxmsgs=999999&summary=1\">"
1279 wprintf("</td></tr></table></div>\n");
1284 * If we're not currently looking at ALL requested
1285 * messages, then display the selector bar
1287 if (num_displayed > 1) {
1288 if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1289 && (!is_notes) && (!is_singlecard)) {
1291 wprintf("Reading #", lowest_displayed, highest_displayed);
1293 wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
1294 "OnChange=\"location.href=msgomatic.whichones.options"
1295 "[selectedIndex].value\">\n");
1297 for (b=0; b<nummsgs; b = b + maxmsgs) {
1300 if (hi > nummsgs) hi = nummsgs;
1301 wprintf("<OPTION %s VALUE="
1306 "%d-%d</OPTION> \n",
1307 ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
1314 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1315 "&maxmsgs=999999&summary=%d\">"
1319 WC->msgarr[0], is_summary);
1321 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1322 "&maxmsgs=999999&summary=1\">"
1328 wprintf("</SELECT> of %d messages.", nummsgs);
1331 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1332 "VALUE=\"Delete selected\">\n");
1337 if (is_summary) wprintf("</FORM>\n");
1341 do_tasks_view(); /* Render the task list */
1345 do_calendar_view(); /* Render the calendar */
1348 if (is_addressbook) {
1349 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1353 if (addrbook != NULL) free(addrbook);
1358 * Back end for post_message() ... this is where the actual message
1359 * gets transmitted to the server.
1361 void post_mime_to_server(void) {
1363 int is_multipart = 0;
1365 struct wc_attachment *att;
1367 size_t encoded_length;
1369 /* If there are attachments, we have to do multipart/mixed */
1370 if (WC->first_attachment != NULL) {
1375 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1376 serv_info.serv_fqdn,
1381 /* Remember, serv_printf() appends an extra newline */
1382 serv_printf("Content-type: multipart/mixed; "
1383 "boundary=\"%s\"\n", boundary);
1384 serv_printf("This is a multipart message in MIME format.\n");
1385 serv_printf("--%s", boundary);
1388 serv_puts("Content-type: text/html");
1390 serv_puts("<HTML><BODY>\n");
1391 text_to_server(bstr("msgtext"), 0);
1392 serv_puts("</BODY></HTML>\n");
1397 /* Add in the attachments */
1398 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1400 encoded_length = ((att->length * 150) / 100);
1401 encoded = malloc(encoded_length);
1402 if (encoded == NULL) break;
1403 CtdlEncodeBase64(encoded, att->data, att->length);
1405 serv_printf("--%s", boundary);
1406 serv_printf("Content-type: %s", att->content_type);
1407 serv_printf("Content-disposition: attachment; "
1408 "filename=\"%s\"", att->filename);
1409 serv_puts("Content-transfer-encoding: base64");
1411 serv_write(encoded, strlen(encoded));
1416 serv_printf("--%s--", boundary);
1424 * Post message (or don't post message)
1426 * Note regarding the "dont_post" variable:
1427 * A random value (actually, it's just a timestamp) is inserted as a hidden
1428 * field called "postseq" when the display_enter page is generated. This
1429 * value is checked when posting, using the static variable dont_post. If a
1430 * user attempts to post twice using the same dont_post value, the message is
1431 * discarded. This prevents the accidental double-saving of the same message
1432 * if the user happens to click the browser "back" button.
1434 void post_message(void)
1437 static long dont_post = (-1L);
1438 struct wc_attachment *att, *aptr;
1440 if (WC->upload_length > 0) {
1442 /* There's an attachment. Save it to this struct... */
1443 att = malloc(sizeof(struct wc_attachment));
1444 memset(att, 0, sizeof(struct wc_attachment));
1445 att->length = WC->upload_length;
1446 strcpy(att->content_type, WC->upload_content_type);
1447 strcpy(att->filename, WC->upload_filename);
1450 /* And add it to the list. */
1451 if (WC->first_attachment == NULL) {
1452 WC->first_attachment = att;
1455 aptr = WC->first_attachment;
1456 while (aptr->next != NULL) aptr = aptr->next;
1460 /* Netscape sends a simple filename, which is what we want,
1461 * but Satan's browser sends an entire pathname. Reduce
1462 * the path to just a filename if we need to.
1464 while (num_tokens(att->filename, '/') > 1) {
1465 remove_token(att->filename, 0, '/');
1467 while (num_tokens(att->filename, '\\') > 1) {
1468 remove_token(att->filename, 0, '\\');
1471 /* Transfer control of this memory from the upload struct
1472 * to the attachment struct.
1474 att->data = WC->upload;
1475 WC->upload_length = 0;
1481 if (!strcasecmp(bstr("sc"), "Cancel")) {
1482 sprintf(WC->ImportantMessage,
1483 "Cancelled. Message was not posted.");
1484 } else if (!strcasecmp(bstr("attach"), "Add")) {
1487 } else if (atol(bstr("postseq")) == dont_post) {
1488 sprintf(WC->ImportantMessage,
1489 "Automatically cancelled because you have already "
1490 "saved this message.");
1492 sprintf(buf, "ENT0 1|%s|0|4|%s",
1497 if (buf[0] == '4') {
1498 post_mime_to_server();
1499 if (strlen(bstr("recp")) > 0) {
1500 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1503 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1505 dont_post = atol(bstr("postseq"));
1507 sprintf(WC->ImportantMessage,
1512 free_attachments(WC);
1513 readloop("readnew");
1520 * display the message entry screen
1522 void display_enter(void)
1526 struct wc_attachment *att;
1528 if (strlen(bstr("force_room")) > 0) {
1529 gotoroom(bstr("force_room"));
1532 /* Are we perhaps in an address book view? If so, then an "enter
1533 * message" command really means "add new entry."
1535 if (WC->wc_view == VIEW_ADDRESSBOOK) {
1536 do_edit_vcard(-1, "", "");
1540 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1541 /* Are we perhaps in a calendar view? If so, then an "enter
1542 * message" command really means "add new calendar item."
1544 if (WC->wc_view == VIEW_CALENDAR) {
1545 display_edit_event();
1549 /* Are we perhaps in a tasks view? If so, then an "enter
1550 * message" command really means "add new task."
1552 if (WC->wc_view == VIEW_TASKS) {
1553 display_edit_task();
1558 /* Otherwise proceed normally. Do a custom room banner with no navbar... */
1559 output_headers(1, 1, 2, 0, 0, 0, 0);
1560 wprintf("<div id=\"banner\">\n");
1561 embed_room_banner(NULL, navbar_none);
1562 wprintf("</div>\n");
1563 wprintf("<div id=\"content\">\n");
1565 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1569 if (!strncmp(buf, "570", 3)) {
1570 if (strlen(bstr("recp")) > 0) {
1571 svprintf("RECPERROR", WCS_STRING,
1572 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1576 do_template("prompt_for_recipient");
1579 if (buf[0] != '2') {
1580 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1586 strcat(&buf[strlen(buf)], " <I>from</I> ");
1587 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1588 if (strlen(bstr("recp")) > 0) {
1589 strcat(&buf[strlen(buf)], " <I>to</I> ");
1590 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1592 strcat(&buf[strlen(buf)], " <I>in</I> ");
1593 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1595 /* begin message entry screen */
1596 wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1598 wprintf("<form enctype=\"multipart/form-data\" "
1599 "method=\"POST\" action=\"/post\" "
1600 "name=\"enterform\""
1601 "onSubmit=\"return submitForm();\""
1603 wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1605 wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1608 wprintf("%s<br>\n", buf); /* header bar */
1609 wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1610 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1611 wprintf("<font size=-1>Subject (optional):</font>"
1612 "<input type=\"text\" name=\"subject\" value=\"");
1613 escputs(bstr("subject"));
1614 wprintf("\" size=40 maxlength=70>"
1618 wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1619 if (strlen(bstr("recp")) > 0) {
1620 wprintf("Send message");
1622 wprintf("Post message");
1625 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1627 /* begin richedit box */
1628 wprintf("<div style=\"position:absolute; left:0%%; width:100%%; "
1629 "top:15%; height:75%%\">\n");
1631 wprintf("<script type=\"text/javascript\" "
1632 "src=\"static/richtext.js\"></script>\n"
1633 "<script type=\"text/javascript\">\n"
1634 "function submitForm() { \n"
1635 " updateRTE('msgtext'); \n"
1639 "initRTE(\"static/\", \"static/\", \"\"); \n"
1641 "<noscript>JAVAscript MUST BE ENABLED.</noscript> \n"
1642 "<script type=\"text/javascript\"> \n"
1643 "writeRichText('msgtext', '");
1644 msgescputs(bstr("msgtext"));
1645 wprintf("', '100%%', '80%%', true, false); \n"
1647 wprintf("</div>\n"); /* end richedit box */
1649 /* Here comes the "do attachments" section on the bottom */
1650 wprintf("<div style=\"position:absolute; bottom:0px; left:0px; width:100%%\">\n");
1652 /* Enumerate any attachments which are already in place... */
1653 wprintf("<img src=\"/static/attachment.gif\" border=0 "
1654 "align=middle height=16 width=16> Attachments: ");
1655 wprintf("<select name=\"which_attachment\" size=1>");
1656 for (att = WC->first_attachment; att != NULL; att = att->next) {
1657 wprintf("<option value=\"");
1658 urlescputs(att->filename);
1660 escputs(att->filename);
1661 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
1662 wprintf("</option>\n");
1664 wprintf("</select>");
1666 /* Now offer the ability to attach additional files... */
1667 wprintf(" "
1668 "Attach file: <input NAME=\"attachfile\" "
1669 "SIZE=16 TYPE=\"file\">\n "
1670 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1672 wprintf("</div>\n"); /* end attachments section */
1674 wprintf("</FORM>\n");
1676 wprintf("</div>\n");
1677 DONE: wDumpContent(1);
1687 void delete_msg(void)
1692 msgid = atol(bstr("msgid"));
1694 output_headers(1, 1, 1, 0, 0, 0, 0);
1696 sprintf(buf, "DELE %ld", msgid);
1699 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1708 * Confirm move of a message
1710 void confirm_move_msg(void)
1716 msgid = atol(bstr("msgid"));
1718 output_headers(1, 1, 1, 0, 0, 0, 0);
1720 wprintf("<div style=\"margin-right:1px\">"
1721 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
1722 wprintf("<font size=+1 color=\"#ffffff\"");
1723 wprintf("<b>Confirm move of message</b>\n");
1724 wprintf("</font></td></tr></table></div>\n");
1726 wprintf("<CENTER>");
1728 wprintf("Move this message to:<br />\n");
1730 wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1731 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1735 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1738 if (buf[0] == '1') {
1739 while (serv_gets(buf), strcmp(buf, "000")) {
1740 extract(targ, buf, 0);
1741 wprintf("<OPTION>");
1746 wprintf("</SELECT>\n");
1747 wprintf("<br />\n");
1749 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1751 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1752 wprintf("</FORM></CENTER>\n");
1754 wprintf("</CENTER>\n");
1765 msgid = atol(bstr("msgid"));
1767 output_headers(1, 1, 1, 0, 0, 0, 0);
1769 if (!strcasecmp(bstr("yesno"), "Move")) {
1770 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1773 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1775 wprintf("<EM>Message not moved.</EM><br />\n");
1782 * This gets called when a user selects Reply/Move/Del etc. on *one* message.
1784 void do_stuff_to_one_msg(void) {
1787 msg_oper = bstr("msg_oper");
1789 if (!strcasecmp(msg_oper, "Delete")) {
1790 delete_msg(); /* It's already been confirmed using JS */
1793 if (!strcasecmp(msg_oper, "Move")) {
1797 if (!strcasecmp(msg_oper, "Reply")) {
1798 display_enter(); /* recp and subject already set */
1802 /* should never get here. FIXME: display an error */
1808 * This gets called when a user selects multiple messages in a summary
1809 * list and then clicks to perform a transformation of some sort on them
1810 * (such as deleting them).
1812 void do_stuff_to_msgs(void) {
1817 struct stuff_t *next;
1821 struct stuff_t *stuff = NULL;
1822 struct stuff_t *ptr;
1825 serv_puts("MSGS ALL");
1828 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1829 ptr = malloc(sizeof(struct stuff_t));
1830 ptr->msgnum = atol(buf);
1835 strcpy(sc, bstr("sc"));
1837 while (stuff != NULL) {
1839 sprintf(buf, "msg_%ld", stuff->msgnum);
1840 if (!strcasecmp(bstr(buf), "yes")) {
1842 if (!strcasecmp(sc, "Delete selected")) {
1843 serv_printf("DELE %ld", stuff->msgnum);
1854 readloop("readfwd");