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 )
95 * Turn a vCard "n" (name) field into something displayable.
97 void vcard_n_prettyize(char *name)
102 original_name = strdup(name);
103 for (i=0; i<5; ++i) {
104 if (strlen(original_name) > 0) {
105 if (original_name[strlen(original_name)-1] == ' ') {
106 original_name[strlen(original_name)-1] = 0;
108 if (original_name[strlen(original_name)-1] == ';') {
109 original_name[strlen(original_name)-1] = 0;
114 for (i=0; i<strlen(original_name); ++i) {
115 if (original_name[i] == ';') {
119 name[strlen(name)+1] = 0;
120 name[strlen(name)] = original_name[i];
129 /* display_vcard() calls this after parsing the textual vCard into
130 * our 'struct vCard' data object.
131 * This gets called instead of display_parsed_vcard() if we are only looking
132 * to extract the person's name instead of displaying the card.
134 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
137 strcpy(storename, "");
139 name = vcard_get_prop(v, "n", 1, 0, 0);
141 strcpy(storename, name);
142 /* vcard_n_prettyize(storename); */
149 /* display_vcard() calls this after parsing the textual vCard into
150 * our 'struct vCard' data object.
152 * Set 'full' to nonzero to display the full card, otherwise it will only
153 * show a summary line.
155 * This code is a bit ugly, so perhaps an explanation is due: we do this
156 * in two passes through the vCard fields. On the first pass, we process
157 * fields we understand, and then render them in a pretty fashion at the
158 * end. Then we make a second pass, outputting all the fields we don't
159 * understand in a simple two-column name/value format.
161 void display_parsed_vcard(struct vCard *v, int full) {
167 char *thisname, *thisvalue;
168 char firsttoken[SIZ];
171 char displayname[SIZ];
177 strcpy(displayname, "");
185 name = vcard_get_prop(v, "fn", 1, 0, 0);
189 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
190 strcpy(displayname, name);
191 vcard_n_prettyize(displayname);
192 escputs(displayname);
201 wprintf("<div align=center><table bgcolor=#aaaaaa width=50%%>");
202 for (pass=1; pass<=2; ++pass) {
204 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
206 thisname = strdup(v->prop[i].name);
207 extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
209 for (j=0; j<num_tokens(thisname, ';'); ++j) {
210 extract_token(buf, thisname, j, ';', sizeof buf);
211 if (!strcasecmp(buf, "encoding=quoted-printable")) {
213 remove_token(thisname, j, ';');
215 if (!strcasecmp(buf, "encoding=base64")) {
217 remove_token(thisname, j, ';');
222 thisvalue = malloc(strlen(v->prop[i].value) + 50);
223 j = CtdlDecodeQuotedPrintable(
224 thisvalue, v->prop[i].value,
225 strlen(v->prop[i].value) );
229 thisvalue = malloc(strlen(v->prop[i].value) + 50);
231 thisvalue, v->prop[i].value,
232 strlen(v->prop[i].value) );
235 thisvalue = strdup(v->prop[i].value);
238 /*** Various fields we may encounter ***/
240 /* N is name, but only if there's no FN already there */
241 if (!strcasecmp(firsttoken, "n")) {
242 if (strlen(displayname) == 0) {
243 strcpy(displayname, thisvalue);
244 vcard_n_prettyize(displayname);
248 /* FN (full name) is a true 'display name' field */
249 else if (!strcasecmp(firsttoken, "fn")) {
250 strcpy(displayname, thisvalue);
254 else if (!strcasecmp(firsttoken, "title")) {
255 strcpy(title, thisvalue);
259 else if (!strcasecmp(firsttoken, "org")) {
260 strcpy(org, thisvalue);
263 else if (!strcasecmp(firsttoken, "email")) {
264 if (strlen(mailto) > 0) strcat(mailto, "<br />");
266 "<A HREF=\"/display_enter"
267 "?force_room=_MAIL_&recp=");
268 urlesc(&mailto[strlen(mailto)], thisvalue);
269 strcat(mailto, "\">");
270 urlesc(&mailto[strlen(mailto)], thisvalue);
271 strcat(mailto, "</A>");
273 else if (!strcasecmp(firsttoken, "tel")) {
274 if (strlen(phone) > 0) strcat(phone, "<br />");
275 strcat(phone, thisvalue);
276 for (j=0; j<num_tokens(thisname, ';'); ++j) {
277 extract_token(buf, thisname, j, ';', sizeof buf);
278 if (!strcasecmp(buf, "tel"))
280 else if (!strcasecmp(buf, "work"))
281 strcat(phone, " (work)");
282 else if (!strcasecmp(buf, "home"))
283 strcat(phone, " (home)");
284 else if (!strcasecmp(buf, "cell"))
285 strcat(phone, " (cell)");
293 else if (!strcasecmp(firsttoken, "adr")) {
295 wprintf("<TR><TD>Address:</TD><TD>");
296 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
297 extract_token(buf, thisvalue, j, ';', sizeof buf);
298 if (strlen(buf) > 0) {
300 if (j<3) wprintf("<br />");
304 wprintf("</TD></TR>\n");
307 else if (!strcasecmp(firsttoken, "version")) {
310 else if (!strcasecmp(firsttoken, "rev")) {
313 else if (!strcasecmp(firsttoken, "label")) {
318 /*** Don't show extra fields. They're ugly.
322 wprintf("</TD><TD>");
324 wprintf("</TD></TR>\n");
334 wprintf("<TR BGCOLOR=\"#AAAAAA\">"
335 "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
336 "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
337 "<FONT SIZE=+1><B>");
338 escputs(displayname);
339 wprintf("</B></FONT>");
340 if (strlen(title) > 0) {
341 wprintf("<div align=right>");
345 if (strlen(org) > 0) {
346 wprintf("<div align=right>");
350 wprintf("</TD></TR>\n");
352 if (strlen(phone) > 0)
353 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
354 if (strlen(mailto) > 0)
355 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
360 wprintf("</table></div>\n");
366 * Display a textual vCard
367 * (Converts to a vCard object and then calls the actual display function)
368 * Set 'full' to nonzero to display the whole card instead of a one-liner.
369 * Or, if "storename" is non-NULL, just store the person's name in that
370 * buffer instead of displaying the card at all.
372 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
378 v = vcard_load(vcard_source);
379 if (v == NULL) return;
381 name = vcard_get_prop(v, "n", 1, 0, 0);
387 if (storename != NULL) {
388 fetchname_parsed_vcard(v, storename);
390 else if ( (alpha == 0)
391 || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
392 || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
394 display_parsed_vcard(v, full);
404 * I wanna SEE that message!
406 void read_message(long msgnum) {
408 char mime_partnum[SIZ];
409 char mime_filename[SIZ];
410 char mime_content_type[SIZ];
411 char mime_disposition[SIZ];
423 char vcard_partnum[SIZ];
424 char cal_partnum[SIZ];
425 char *part_source = NULL;
430 strcpy(reply_to, "");
431 strcpy(vcard_partnum, "");
432 strcpy(cal_partnum, "");
433 strcpy(mime_http, "");
435 serv_printf("MSG4 %ld", msgnum);
436 serv_getln(buf, sizeof buf);
438 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
442 /* begin everythingamundo table */
443 wprintf("<div id=\"fix_scrollbar_bug\">\n");
444 wprintf("<table width=100%% border=1 cellspacing=0 "
445 "cellpadding=0><TR><TD>\n");
447 /* begin message header table */
448 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
449 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
451 wprintf("<SPAN CLASS=\"message_header\">");
452 strcpy(m_subject, "");
454 while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
455 if (!strcmp(buf, "000")) {
456 wprintf("<I>unexpected end of message</I><br /><br />\n");
457 wprintf("</SPAN>\n");
460 if (!strncasecmp(buf, "nhdr=yes", 8))
464 if (!strncasecmp(buf, "type=", 5))
465 format_type = atoi(&buf[5]);
466 if (!strncasecmp(buf, "from=", 5)) {
467 strcpy(from, &buf[5]);
468 wprintf("from <A HREF=\"/showuser&who=");
474 if (!strncasecmp(buf, "subj=", 5))
475 strcpy(m_subject, &buf[5]);
476 if ((!strncasecmp(buf, "hnod=", 5))
477 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
478 wprintf("(%s) ", &buf[5]);
479 if ((!strncasecmp(buf, "room=", 5))
480 && (strcasecmp(&buf[5], WC->wc_roomname))
481 && (strlen(&buf[5])>0) )
482 wprintf("in %s> ", &buf[5]);
483 if (!strncasecmp(buf, "rfca=", 5)) {
484 strcpy(rfca, &buf[5]);
490 if (!strncasecmp(buf, "node=", 5)) {
491 strcpy(node, &buf[5]);
492 if ( ((WC->room_flags & QR_NETWORK)
493 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
494 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
497 wprintf("@%s ", &buf[5]);
500 if (!strncasecmp(buf, "rcpt=", 5))
501 wprintf("to %s ", &buf[5]);
502 if (!strncasecmp(buf, "time=", 5)) {
503 fmt_date(now, atol(&buf[5]), 0);
507 if (!strncasecmp(buf, "part=", 5)) {
508 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
509 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
510 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
511 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
512 mime_length = extract_int(&buf[5], 5);
514 if (!strcasecmp(mime_disposition, "attachment")) {
515 snprintf(&mime_http[strlen(mime_http)],
516 (sizeof(mime_http) - strlen(mime_http) - 1),
517 "<A HREF=\"/output_mimepart?"
518 "msgnum=%ld&partnum=%s\" "
519 "TARGET=\"wc.%ld.%s\">"
520 "<IMG SRC=\"/static/attachment.gif\" "
521 "BORDER=0 ALIGN=MIDDLE>\n"
522 "Part %s: %s (%s, %d bytes)</A><br />\n",
523 msgnum, mime_partnum,
524 msgnum, mime_partnum,
525 mime_partnum, mime_filename,
526 mime_content_type, mime_length);
529 if ((!strcasecmp(mime_disposition, "inline"))
530 && (!strncasecmp(mime_content_type, "image/", 6)) ){
531 snprintf(&mime_http[strlen(mime_http)],
532 (sizeof(mime_http) - strlen(mime_http) - 1),
533 "<IMG SRC=\"/output_mimepart?"
534 "msgnum=%ld&partnum=%s\">",
535 msgnum, mime_partnum);
538 /*** begin handler prep ***/
539 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
540 strcpy(vcard_partnum, mime_partnum);
543 if (!strcasecmp(mime_content_type, "text/calendar")) {
544 strcpy(cal_partnum, mime_partnum);
547 /*** end handler prep ***/
553 /* Generate a reply-to address */
554 if (strlen(rfca) > 0) {
555 strcpy(reply_to, rfca);
558 if ( (strlen(node) > 0)
559 && (strcasecmp(node, serv_info.serv_nodename))
560 && (strcasecmp(node, serv_info.serv_humannode)) ) {
561 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
565 snprintf(reply_to, sizeof(reply_to), "%s", from);
574 if (strlen(m_subject) > 0) {
576 "<SPAN CLASS=\"message_subject\">"
583 /* start msg buttons */
584 wprintf("<TD ALIGN=RIGHT>\n");
587 wprintf("<a href=\"/display_enter?recp=");
588 urlescputs(reply_to);
589 wprintf("&subject=");
590 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
591 urlescputs(m_subject);
592 wprintf("\">[Reply]</a> ");
594 if (WC->is_room_aide) {
597 wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
601 wprintf("<a href=\"/delete_msg?msgid=%ld\" "
602 "onClick=\"return confirm('Delete this message?');\">"
603 "[Delete]</a>", msgnum);
607 wprintf("</TD></TR></TABLE>\n");
610 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
611 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
614 * Learn the content type
616 strcpy(mime_content_type, "text/plain");
617 while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
618 if (!strcmp(buf, "000")) {
619 wprintf("<I>unexpected end of message</I><br /><br />\n");
622 if (!strncasecmp(buf, "Content-type: ", 14)) {
623 safestrncpy(mime_content_type, &buf[14],
624 sizeof(mime_content_type));
628 /* Messages in legacy Citadel variformat get handled thusly... */
629 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
630 fmout(NULL, "JUSTIFY");
633 /* Boring old 80-column fixed format text gets handled this way... */
634 else if (!strcasecmp(mime_content_type, "text/plain")) {
635 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
636 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
637 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
638 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
639 buf[strlen(buf) - 1] = 0;
641 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
642 wprintf("<BLOCKQUOTE>");
644 } else if ((bq == 1) &&
645 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
646 wprintf("</BLOCKQUOTE>");
652 wprintf("</TT><br />\n");
654 wprintf("</I><br />");
657 else /* HTML is fun, but we've got to strip it first */
658 if (!strcasecmp(mime_content_type, "text/html")) {
662 /* Unknown weirdness */
664 wprintf("I don't know how to display %s<br />\n",
666 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
670 /* Afterwards, offer links to download attachments 'n' such */
671 if (strlen(mime_http) > 0) {
672 wprintf("%s", mime_http);
675 /* Handler for vCard parts */
676 if (strlen(vcard_partnum) > 0) {
677 part_source = load_mimepart(msgnum, vcard_partnum);
678 if (part_source != NULL) {
680 /* If it's my vCard I can edit it */
681 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
682 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
683 || (WC->wc_view == VIEW_ADDRESSBOOK)
685 wprintf("<A HREF=\"/edit_vcard?"
686 "msgnum=%ld&partnum=%s\">",
687 msgnum, vcard_partnum);
688 wprintf("[edit]</A>");
691 /* In all cases, display the full card */
692 display_vcard(part_source, 0, 1, NULL);
696 /* Handler for calendar parts */
697 if (strlen(cal_partnum) > 0) {
698 part_source = load_mimepart(msgnum, cal_partnum);
699 if (part_source != NULL) {
700 cal_process_attachment(part_source,
701 msgnum, cal_partnum);
711 wprintf("</TD></TR></TABLE>\n");
713 /* end everythingamundo table */
714 wprintf("</TD></TR></TABLE>\n");
715 wprintf("</div><br />\n");
719 void summarize_message(long msgnum, int is_new) {
730 memset(&summ, 0, sizeof(summ));
731 strcpy(summ.subj, "(no subject)");
733 /* ask for headers only with no MIME */
734 sprintf(buf, "MSG0 %ld|3", msgnum);
736 serv_getln(buf, sizeof buf);
737 if (buf[0] != '1') return;
739 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
740 if (!strncasecmp(buf, "from=", 5)) {
741 strcpy(summ.from, &buf[5]);
743 if (!strncasecmp(buf, "subj=", 5)) {
744 strcpy(summ.subj, &buf[5]);
746 /* if (!strncasecmp(buf, "rfca=", 5)) {
747 strcat(summ.from, " <");
748 strcat(summ.from, &buf[5]);
749 strcat(summ.from, ">");
752 if (!strncasecmp(buf, "node=", 5)) {
753 if ( ((WC->room_flags & QR_NETWORK)
754 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
755 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
757 strcat(summ.from, " @ ");
758 strcat(summ.from, &buf[5]);
762 if (!strncasecmp(buf, "rcpt=", 5)) {
763 strcpy(summ.to, &buf[5]);
766 if (!strncasecmp(buf, "time=", 5)) {
767 fmt_date(summ.date, atol(&buf[5]), 1); /* brief */
772 if (is_new) wprintf("<B>");
773 wprintf("<A HREF=\"/readfwd?startmsg=%ld"
774 "&maxmsgs=1&summary=0\">",
778 if (is_new) wprintf("</B>");
779 wprintf("</TD><TD>");
780 if (is_new) wprintf("<B>");
782 if (is_new) wprintf("</B>");
783 wprintf(" </TD><TD>");
784 if (is_new) wprintf("<B>");
786 if (is_new) wprintf("</B>");
789 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
797 void display_addressbook(long msgnum, char alpha) {
799 char mime_partnum[SIZ];
800 char mime_filename[SIZ];
801 char mime_content_type[SIZ];
802 char mime_disposition[SIZ];
804 char vcard_partnum[SIZ];
805 char *vcard_source = NULL;
815 memset(&summ, 0, sizeof(summ));
816 strcpy(summ.subj, "(no subject)");
818 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
820 serv_getln(buf, sizeof buf);
821 if (buf[0] != '1') return;
823 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
824 if (!strncasecmp(buf, "part=", 5)) {
825 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
826 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
827 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
828 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
829 mime_length = extract_int(&buf[5], 5);
831 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
832 strcpy(vcard_partnum, mime_partnum);
838 if (strlen(vcard_partnum) > 0) {
839 vcard_source = load_mimepart(msgnum, vcard_partnum);
840 if (vcard_source != NULL) {
842 /* Display the summary line */
843 display_vcard(vcard_source, alpha, 0, NULL);
845 /* If it's my vCard I can edit it */
846 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
847 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
848 || (WC->wc_view == VIEW_ADDRESSBOOK)
850 wprintf("<A HREF=\"/edit_vcard?"
851 "msgnum=%ld&partnum=%s\">",
852 msgnum, vcard_partnum);
853 wprintf("[edit]</A>");
864 /* If it's an old "Firstname Lastname" style record, try to
867 void lastfirst_firstlast(char *namebuf) {
872 if (namebuf == NULL) return;
873 if (strchr(namebuf, ';') != NULL) return;
875 i = num_tokens(namebuf, ' ');
878 extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
879 remove_token(namebuf, i-1, ' ');
880 strcpy(firstname, namebuf);
881 sprintf(namebuf, "%s; %s", lastname, firstname);
885 void fetch_ab_name(long msgnum, char *namebuf) {
887 char mime_partnum[SIZ];
888 char mime_filename[SIZ];
889 char mime_content_type[SIZ];
890 char mime_disposition[SIZ];
892 char vcard_partnum[SIZ];
893 char *vcard_source = NULL;
904 if (namebuf == NULL) return;
907 memset(&summ, 0, sizeof(summ));
908 strcpy(summ.subj, "(no subject)");
910 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
912 serv_getln(buf, sizeof buf);
913 if (buf[0] != '1') return;
915 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
916 if (!strncasecmp(buf, "part=", 5)) {
917 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
918 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
919 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
920 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
921 mime_length = extract_int(&buf[5], 5);
923 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
924 strcpy(vcard_partnum, mime_partnum);
930 if (strlen(vcard_partnum) > 0) {
931 vcard_source = load_mimepart(msgnum, vcard_partnum);
932 if (vcard_source != NULL) {
934 /* Grab the name off the card */
935 display_vcard(vcard_source, 0, 0, namebuf);
941 lastfirst_firstlast(namebuf);
943 for (i=0; i<strlen(namebuf); ++i) {
944 if (namebuf[i] != ';') return;
946 strcpy(namebuf, "(no name)");
952 * Record compare function for sorting address book indices
954 int abcmp(const void *ab1, const void *ab2) {
956 (((const struct addrbookent *)ab1)->ab_name),
957 (((const struct addrbookent *)ab2)->ab_name)
963 * Helper function for do_addrbook_view()
964 * Converts a name into a three-letter tab label
966 void nametab(char *tabbuf, char *name) {
967 stresc(tabbuf, name, 0, 0);
968 tabbuf[0] = toupper(tabbuf[0]);
969 tabbuf[1] = tolower(tabbuf[1]);
970 tabbuf[2] = tolower(tabbuf[2]);
976 * Render the address book using info we gathered during the scan
978 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
982 static int NAMESPERPAGE = 60;
986 char tabfirst_label[SIZ];
988 char tablast_label[SIZ];
991 wprintf("<I>This address book is empty.</I>\n");
996 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
999 num_pages = num_ab / NAMESPERPAGE;
1001 page = atoi(bstr("page"));
1004 for (i=0; i<=num_pages; ++i) {
1006 wprintf("<A HREF=\"/readfwd?page=%d\">", i);
1011 tabfirst = i * NAMESPERPAGE;
1012 tablast = tabfirst + NAMESPERPAGE - 1;
1013 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1014 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1015 nametab(tablast_label, addrbook[tablast].ab_name);
1016 wprintf("[%s - %s]",
1017 tabfirst_label, tablast_label
1026 wprintf("<br />\n");
1028 wprintf("<TABLE border=0 cellspacing=0 "
1029 "cellpadding=3 width=100%%>\n"
1032 for (i=0; i<num_ab; ++i) {
1034 if ((i / NAMESPERPAGE) == page) {
1036 if ((displayed % 4) == 0) {
1037 if (displayed > 0) {
1041 wprintf("<TR BGCOLOR=\"#%s\">",
1042 (bg ? "DDDDDD" : "FFFFFF")
1048 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
1049 addrbook[i].ab_msgnum);
1050 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
1051 vcard_n_prettyize(addrbook[i].ab_name);
1052 escputs(addrbook[i].ab_name);
1053 wprintf("</A></TD>\n");
1058 wprintf("</TR></TABLE>\n");
1064 * load message pointers from the server
1066 int load_msg_ptrs(char *servcmd)
1073 maxload = sizeof(WC->msgarr) / sizeof(long) ;
1075 serv_getln(buf, sizeof buf);
1076 if (buf[0] != '1') {
1077 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1080 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1081 if (nummsgs < maxload) {
1082 WC->msgarr[nummsgs] = atol(buf);
1091 * command loop for reading messages
1093 void readloop(char *oper)
1103 int num_displayed = 0;
1105 int is_addressbook = 0;
1106 int is_singlecard = 0;
1107 int is_calendar = 0;
1110 int remaining_messages;
1112 int lowest_displayed = (-1);
1113 int highest_displayed = 0;
1114 long pn_previous = 0L;
1115 long pn_current = 0L;
1118 struct addrbookent *addrbook = NULL;
1121 startmsg = atol(bstr("startmsg"));
1122 maxmsgs = atoi(bstr("maxmsgs"));
1123 is_summary = atoi(bstr("summary"));
1124 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1126 output_headers(1, 1, 1, 0, 0, 0, 0);
1128 /* When in summary mode, always show ALL messages instead of just
1129 * new or old. Otherwise, show what the user asked for.
1131 if (!strcmp(oper, "readnew")) {
1132 strcpy(cmd, "MSGS NEW");
1134 else if (!strcmp(oper, "readold")) {
1135 strcpy(cmd, "MSGS OLD");
1138 strcpy(cmd, "MSGS ALL");
1141 if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1143 strcpy(cmd, "MSGS ALL");
1146 if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1148 strcpy(cmd, "MSGS ALL");
1153 strcpy(cmd, "MSGS ALL");
1156 /* Are we doing a summary view? If so, we need to know old messages
1157 * and new messages, so we can do that pretty boldface thing for the
1160 strcpy(old_msgs, "");
1163 serv_getln(buf, sizeof buf);
1164 if (buf[0] == '2') {
1165 strcpy(old_msgs, &buf[4]);
1169 is_singlecard = atoi(bstr("is_singlecard"));
1171 if (WC->wc_view == VIEW_CALENDAR) { /* calendar */
1173 strcpy(cmd, "MSGS ALL");
1176 if (WC->wc_view == VIEW_TASKS) { /* tasks */
1178 strcpy(cmd, "MSGS ALL");
1181 if (WC->wc_view == VIEW_NOTES) { /* notes */
1183 strcpy(cmd, "MSGS ALL");
1187 nummsgs = load_msg_ptrs(cmd);
1190 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1191 if (!strcmp(oper, "readnew")) {
1192 wprintf("<EM>No new messages.</EM>\n");
1193 } else if (!strcmp(oper, "readold")) {
1194 wprintf("<EM>No old messages.</EM>\n");
1196 wprintf("<EM>No messages here.</EM>\n");
1203 if (startmsg == 0L) startmsg = WC->msgarr[0];
1204 remaining_messages = 0;
1206 for (a = 0; a < nummsgs; ++a) {
1207 if (WC->msgarr[a] >= startmsg) {
1208 ++remaining_messages;
1212 wprintf("<form name=\"msgomatic\" "
1213 "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
1215 wprintf("<div id=\"fix_scrollbar_bug\">"
1216 "<table border=0 cellspacing=0 "
1217 "cellpadding=0 width=100%%>\n"
1219 "<TD><I>Subject</I></TD>"
1220 "<TD><I>Sender</I></TD>"
1221 "<TD><I>Date</I></TD>"
1227 for (a = 0; a < nummsgs; ++a) {
1228 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1230 /* Are you a new message, or an old message? */
1233 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1241 /* Learn which msgs "Prev" & "Next" buttons go to */
1242 pn_current = WC->msgarr[a];
1243 if (a > 0) pn_previous = WC->msgarr[a-1];
1244 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1246 /* If a tabular view, set up the line */
1249 wprintf("<TR BGCOLOR=\"#%s\">",
1250 (bg ? "DDDDDD" : "FFFFFF")
1254 /* Display the message */
1256 summarize_message(WC->msgarr[a], is_new);
1258 else if (is_addressbook) {
1259 fetch_ab_name(WC->msgarr[a], buf);
1261 addrbook = realloc(addrbook,
1262 (sizeof(struct addrbookent) * num_ab) );
1263 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1264 sizeof(addrbook[num_ab-1].ab_name));
1265 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1267 else if (is_calendar) {
1268 display_calendar(WC->msgarr[a]);
1270 else if (is_tasks) {
1271 display_task(WC->msgarr[a]);
1273 else if (is_notes) {
1274 display_note(WC->msgarr[a]);
1277 read_message(WC->msgarr[a]);
1280 /* If a tabular view, finish the line */
1285 if (lowest_displayed < 0) lowest_displayed = a;
1286 highest_displayed = a;
1289 --remaining_messages;
1294 wprintf("</table></div>\n");
1297 /* Bump these because although we're thinking in zero base, the user
1298 * is a drooling idiot and is thinking in one base.
1301 ++highest_displayed;
1303 /* If we're only looking at one message, do a prev/next thing */
1304 if (num_displayed == 1) {
1305 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1307 wprintf("<div id=\"fix_scrollbar_bug\">"
1308 "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>"
1309 "Reading #%d of %d messages.</TD>\n"
1310 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1311 lowest_displayed, nummsgs);
1314 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1315 "VALUE=\"Delete selected\">\n");
1318 if (pn_previous > 0L) {
1319 wprintf("<A HREF=\"/%s"
1329 wprintf("<A HREF=\"/%s"
1338 wprintf("<A HREF=\"/%s?startmsg=%ld"
1339 "&maxmsgs=999999&summary=1\">"
1345 wprintf("</td></tr></table></div>\n");
1350 * If we're not currently looking at ALL requested
1351 * messages, then display the selector bar
1353 if (num_displayed > 1) {
1354 if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1355 && (!is_notes) && (!is_singlecard)) {
1357 wprintf("Reading #", lowest_displayed, highest_displayed);
1359 wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
1360 "OnChange=\"location.href=msgomatic.whichones.options"
1361 "[selectedIndex].value\">\n");
1363 for (b=0; b<nummsgs; b = b + maxmsgs) {
1366 if (hi > nummsgs) hi = nummsgs;
1367 wprintf("<OPTION %s VALUE="
1372 "%d-%d</OPTION> \n",
1373 ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
1380 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1381 "&maxmsgs=999999&summary=%d\">"
1385 WC->msgarr[0], is_summary);
1387 wprintf("</SELECT> of %d messages.", nummsgs);
1390 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1391 "VALUE=\"Delete selected\">\n");
1396 wprintf("</form>\n");
1400 do_tasks_view(); /* Render the task list */
1404 do_calendar_view(); /* Render the calendar */
1407 if (is_addressbook) {
1408 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1412 if (addrbook != NULL) free(addrbook);
1417 * Back end for post_message() ... this is where the actual message
1418 * gets transmitted to the server.
1420 void post_mime_to_server(void) {
1422 int is_multipart = 0;
1424 struct wc_attachment *att;
1426 size_t encoded_length;
1428 /* If there are attachments, we have to do multipart/mixed */
1429 if (WC->first_attachment != NULL) {
1434 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1435 serv_info.serv_fqdn,
1440 /* Remember, serv_printf() appends an extra newline */
1441 serv_printf("Content-type: multipart/mixed; "
1442 "boundary=\"%s\"\n", boundary);
1443 serv_printf("This is a multipart message in MIME format.\n");
1444 serv_printf("--%s", boundary);
1447 serv_puts("Content-type: text/html");
1449 serv_puts("<HTML><BODY>\n");
1450 text_to_server(bstr("msgtext"), 0);
1451 serv_puts("</BODY></HTML>\n");
1456 /* Add in the attachments */
1457 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1459 encoded_length = ((att->length * 150) / 100);
1460 encoded = malloc(encoded_length);
1461 if (encoded == NULL) break;
1462 CtdlEncodeBase64(encoded, att->data, att->length);
1464 serv_printf("--%s", boundary);
1465 serv_printf("Content-type: %s", att->content_type);
1466 serv_printf("Content-disposition: attachment; "
1467 "filename=\"%s\"", att->filename);
1468 serv_puts("Content-transfer-encoding: base64");
1470 serv_write(encoded, strlen(encoded));
1475 serv_printf("--%s--", boundary);
1483 * Post message (or don't post message)
1485 * Note regarding the "dont_post" variable:
1486 * A random value (actually, it's just a timestamp) is inserted as a hidden
1487 * field called "postseq" when the display_enter page is generated. This
1488 * value is checked when posting, using the static variable dont_post. If a
1489 * user attempts to post twice using the same dont_post value, the message is
1490 * discarded. This prevents the accidental double-saving of the same message
1491 * if the user happens to click the browser "back" button.
1493 void post_message(void)
1496 static long dont_post = (-1L);
1497 struct wc_attachment *att, *aptr;
1499 if (WC->upload_length > 0) {
1501 /* There's an attachment. Save it to this struct... */
1502 att = malloc(sizeof(struct wc_attachment));
1503 memset(att, 0, sizeof(struct wc_attachment));
1504 att->length = WC->upload_length;
1505 strcpy(att->content_type, WC->upload_content_type);
1506 strcpy(att->filename, WC->upload_filename);
1509 /* And add it to the list. */
1510 if (WC->first_attachment == NULL) {
1511 WC->first_attachment = att;
1514 aptr = WC->first_attachment;
1515 while (aptr->next != NULL) aptr = aptr->next;
1519 /* Netscape sends a simple filename, which is what we want,
1520 * but Satan's browser sends an entire pathname. Reduce
1521 * the path to just a filename if we need to.
1523 while (num_tokens(att->filename, '/') > 1) {
1524 remove_token(att->filename, 0, '/');
1526 while (num_tokens(att->filename, '\\') > 1) {
1527 remove_token(att->filename, 0, '\\');
1530 /* Transfer control of this memory from the upload struct
1531 * to the attachment struct.
1533 att->data = WC->upload;
1534 WC->upload_length = 0;
1540 if (!strcasecmp(bstr("sc"), "Cancel")) {
1541 sprintf(WC->ImportantMessage,
1542 "Cancelled. Message was not posted.");
1543 } else if (!strcasecmp(bstr("attach"), "Add")) {
1546 } else if (atol(bstr("postseq")) == dont_post) {
1547 sprintf(WC->ImportantMessage,
1548 "Automatically cancelled because you have already "
1549 "saved this message.");
1551 sprintf(buf, "ENT0 1|%s|0|4|%s",
1555 serv_getln(buf, sizeof buf);
1556 if (buf[0] == '4') {
1557 post_mime_to_server();
1558 if (strlen(bstr("recp")) > 0) {
1559 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1562 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1564 dont_post = atol(bstr("postseq"));
1566 sprintf(WC->ImportantMessage,
1571 free_attachments(WC);
1572 readloop("readnew");
1579 * display the message entry screen
1581 void display_enter(void)
1585 struct wc_attachment *att;
1587 if (strlen(bstr("force_room")) > 0) {
1588 gotoroom(bstr("force_room"));
1591 /* Are we perhaps in an address book view? If so, then an "enter
1592 * message" command really means "add new entry."
1594 if (WC->wc_view == VIEW_ADDRESSBOOK) {
1595 do_edit_vcard(-1, "", "");
1599 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1600 /* Are we perhaps in a calendar view? If so, then an "enter
1601 * message" command really means "add new calendar item."
1603 if (WC->wc_view == VIEW_CALENDAR) {
1604 display_edit_event();
1608 /* Are we perhaps in a tasks view? If so, then an "enter
1609 * message" command really means "add new task."
1611 if (WC->wc_view == VIEW_TASKS) {
1612 display_edit_task();
1617 /* Otherwise proceed normally. Do a custom room banner with no navbar... */
1618 output_headers(1, 1, 2, 0, 0, 0, 0);
1619 wprintf("<div id=\"banner\">\n");
1620 embed_room_banner(NULL, navbar_none);
1621 wprintf("</div>\n");
1622 wprintf("<div id=\"content\">\n"
1623 "<div id=\"fix_scrollbar_bug\">"
1624 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1626 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1628 serv_getln(buf, sizeof buf);
1630 if (!strncmp(buf, "570", 3)) {
1631 if (strlen(bstr("recp")) > 0) {
1632 svprintf("RECPERROR", WCS_STRING,
1633 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1637 do_template("prompt_for_recipient");
1640 if (buf[0] != '2') {
1641 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1646 fmt_date(buf, now, 0);
1647 strcat(&buf[strlen(buf)], " <I>from</I> ");
1648 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1649 if (strlen(bstr("recp")) > 0) {
1650 strcat(&buf[strlen(buf)], " <I>to</I> ");
1651 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1653 strcat(&buf[strlen(buf)], " <I>in</I> ");
1654 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1656 /* begin message entry screen */
1657 // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1659 wprintf("<form enctype=\"multipart/form-data\" "
1660 "method=\"POST\" action=\"/post\" "
1661 "name=\"enterform\""
1662 "onSubmit=\"return submitForm();\""
1664 wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1666 wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1669 wprintf("%s<br>\n", buf); /* header bar */
1670 wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1671 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1672 wprintf("<font size=-1>Subject (optional):</font>"
1673 "<input type=\"text\" name=\"subject\" value=\"");
1674 escputs(bstr("subject"));
1675 wprintf("\" size=40 maxlength=70>"
1679 wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1680 if (strlen(bstr("recp")) > 0) {
1681 wprintf("Send message");
1683 wprintf("Post message");
1686 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1688 wprintf("<center><script type=\"text/javascript\" "
1689 "src=\"static/richtext.js\"></script>\n"
1690 "<script type=\"text/javascript\">\n"
1691 "function submitForm() { \n"
1692 " updateRTE('msgtext'); \n"
1696 "initRTE(\"static/\", \"static/\", \"\"); \n"
1698 "<noscript>JavaScript must be enabled.</noscript> \n"
1699 "<script type=\"text/javascript\"> \n"
1700 "writeRichText('msgtext', '");
1701 msgescputs(bstr("msgtext"));
1702 wprintf("', '96%%', '200', true, false); \n"
1703 "</script></center><br />\n");
1705 /* Enumerate any attachments which are already in place... */
1706 wprintf("<img src=\"/static/attachment.gif\" border=0 "
1707 "align=middle height=16 width=16> Attachments: ");
1708 wprintf("<select name=\"which_attachment\" size=1>");
1709 for (att = WC->first_attachment; att != NULL; att = att->next) {
1710 wprintf("<option value=\"");
1711 urlescputs(att->filename);
1713 escputs(att->filename);
1714 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
1715 wprintf("</option>\n");
1717 wprintf("</select>");
1719 /* Now offer the ability to attach additional files... */
1720 wprintf(" "
1721 "Attach file: <input NAME=\"attachfile\" "
1722 "SIZE=16 TYPE=\"file\">\n "
1723 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1725 wprintf("</form>\n");
1727 wprintf("</td></tr></table></div>\n");
1728 DONE: wDumpContent(1);
1738 void delete_msg(void)
1743 msgid = atol(bstr("msgid"));
1745 output_headers(1, 1, 1, 0, 0, 0, 0);
1747 sprintf(buf, "DELE %ld", msgid);
1749 serv_getln(buf, sizeof buf);
1750 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1759 * Confirm move of a message
1761 void confirm_move_msg(void)
1767 msgid = atol(bstr("msgid"));
1769 output_headers(1, 1, 1, 0, 0, 0, 0);
1771 wprintf("<div id=\"fix_scrollbar_bug\">"
1772 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
1773 wprintf("<font size=+1 color=\"#ffffff\"");
1774 wprintf("<b>Confirm move of message</b>\n");
1775 wprintf("</font></td></tr></table></div>\n");
1777 wprintf("<CENTER>");
1779 wprintf("Move this message to:<br />\n");
1781 wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1782 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1786 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1788 serv_getln(buf, sizeof buf);
1789 if (buf[0] == '1') {
1790 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1791 extract_token(targ, buf, 0, '|', sizeof targ);
1792 wprintf("<OPTION>");
1797 wprintf("</SELECT>\n");
1798 wprintf("<br />\n");
1800 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1802 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1803 wprintf("</form></CENTER>\n");
1805 wprintf("</CENTER>\n");
1816 msgid = atol(bstr("msgid"));
1818 output_headers(1, 1, 1, 0, 0, 0, 0);
1820 if (!strcasecmp(bstr("yesno"), "Move")) {
1821 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1823 serv_getln(buf, sizeof buf);
1824 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1826 wprintf("<EM>Message not moved.</EM><br />\n");
1833 * This gets called when a user selects multiple messages in a summary
1834 * list and then clicks to perform a transformation of some sort on them
1835 * (such as deleting them).
1837 void do_stuff_to_msgs(void) {
1842 struct stuff_t *next;
1846 struct stuff_t *stuff = NULL;
1847 struct stuff_t *ptr;
1850 serv_puts("MSGS ALL");
1851 serv_getln(buf, sizeof buf);
1853 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1854 ptr = malloc(sizeof(struct stuff_t));
1855 ptr->msgnum = atol(buf);
1860 strcpy(sc, bstr("sc"));
1862 while (stuff != NULL) {
1864 sprintf(buf, "msg_%ld", stuff->msgnum);
1865 if (!strcasecmp(bstr(buf), "yes")) {
1867 if (!strcasecmp(sc, "Delete selected")) {
1868 serv_printf("DELE %ld", stuff->msgnum);
1869 serv_getln(buf, sizeof buf);
1879 readloop("readfwd");