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)))
330 display_parsed_vcard(v, full);
340 * I wanna SEE that message!
342 void read_message(long msgnum) {
344 char mime_partnum[SIZ];
345 char mime_filename[SIZ];
346 char mime_content_type[SIZ];
347 char mime_disposition[SIZ];
359 char vcard_partnum[SIZ];
360 char cal_partnum[SIZ];
361 char *part_source = NULL;
366 strcpy(reply_to, "");
367 strcpy(vcard_partnum, "");
368 strcpy(cal_partnum, "");
369 strcpy(mime_http, "");
371 serv_printf("MSG4 %ld", msgnum);
374 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
378 /* begin everythingamundo table */
379 wprintf("<div id=\"fix_scrollbar_bug\">\n");
380 wprintf("<table width=100%% border=1 cellspacing=0 "
381 "cellpadding=0><TR><TD>\n");
383 /* begin message header table */
384 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
385 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
387 wprintf("<SPAN CLASS=\"message_header\">");
388 strcpy(m_subject, "");
390 while (serv_gets(buf), strcasecmp(buf, "text")) {
391 if (!strcmp(buf, "000")) {
392 wprintf("<I>unexpected end of message</I><br /><br />\n");
393 wprintf("</SPAN>\n");
396 if (!strncasecmp(buf, "nhdr=yes", 8))
400 if (!strncasecmp(buf, "type=", 5))
401 format_type = atoi(&buf[5]);
402 if (!strncasecmp(buf, "from=", 5)) {
403 strcpy(from, &buf[5]);
404 wprintf("from <A HREF=\"/showuser&who=");
410 if (!strncasecmp(buf, "subj=", 5))
411 strcpy(m_subject, &buf[5]);
412 if ((!strncasecmp(buf, "hnod=", 5))
413 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
414 wprintf("(%s) ", &buf[5]);
415 if ((!strncasecmp(buf, "room=", 5))
416 && (strcasecmp(&buf[5], WC->wc_roomname))
417 && (strlen(&buf[5])>0) )
418 wprintf("in %s> ", &buf[5]);
419 if (!strncasecmp(buf, "rfca=", 5)) {
420 strcpy(rfca, &buf[5]);
426 if (!strncasecmp(buf, "node=", 5)) {
427 strcpy(node, &buf[5]);
428 if ( ((WC->room_flags & QR_NETWORK)
429 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
430 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
433 wprintf("@%s ", &buf[5]);
436 if (!strncasecmp(buf, "rcpt=", 5))
437 wprintf("to %s ", &buf[5]);
438 if (!strncasecmp(buf, "time=", 5)) {
439 fmt_date(now, atol(&buf[5]));
443 if (!strncasecmp(buf, "part=", 5)) {
444 extract(mime_filename, &buf[5], 1);
445 extract(mime_partnum, &buf[5], 2);
446 extract(mime_disposition, &buf[5], 3);
447 extract(mime_content_type, &buf[5], 4);
448 mime_length = extract_int(&buf[5], 5);
450 if (!strcasecmp(mime_disposition, "attachment")) {
451 snprintf(&mime_http[strlen(mime_http)],
452 (sizeof(mime_http) - strlen(mime_http) - 1),
453 "<A HREF=\"/output_mimepart?"
454 "msgnum=%ld&partnum=%s\" "
455 "TARGET=\"wc.%ld.%s\">"
456 "<IMG SRC=\"/static/attachment.gif\" "
457 "BORDER=0 ALIGN=MIDDLE>\n"
458 "Part %s: %s (%s, %d bytes)</A><br />\n",
459 msgnum, mime_partnum,
460 msgnum, mime_partnum,
461 mime_partnum, mime_filename,
462 mime_content_type, mime_length);
465 if ((!strcasecmp(mime_disposition, "inline"))
466 && (!strncasecmp(mime_content_type, "image/", 6)) ){
467 snprintf(&mime_http[strlen(mime_http)],
468 (sizeof(mime_http) - strlen(mime_http) - 1),
469 "<IMG SRC=\"/output_mimepart?"
470 "msgnum=%ld&partnum=%s\">",
471 msgnum, mime_partnum);
474 /*** begin handler prep ***/
475 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
476 strcpy(vcard_partnum, mime_partnum);
479 if (!strcasecmp(mime_content_type, "text/calendar")) {
480 strcpy(cal_partnum, mime_partnum);
483 /*** end handler prep ***/
489 /* Generate a reply-to address */
490 if (strlen(rfca) > 0) {
491 strcpy(reply_to, rfca);
494 if ( (strlen(node) > 0)
495 && (strcasecmp(node, serv_info.serv_nodename))
496 && (strcasecmp(node, serv_info.serv_humannode)) ) {
497 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
501 snprintf(reply_to, sizeof(reply_to), "%s", from);
510 if (strlen(m_subject) > 0) {
512 "<SPAN CLASS=\"message_subject\">"
519 /* start msg buttons */
520 wprintf("<TD ALIGN=RIGHT>\n");
523 wprintf("<a href=\"/display_enter?recp=");
524 urlescputs(reply_to);
525 wprintf("&subject=");
526 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
527 urlescputs(m_subject);
528 wprintf("\">Reply</a> ");
530 if (WC->is_room_aide) {
533 wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">Move </a>",
537 wprintf("<a href=\"/delete_msg?msgid=%ld\" "
538 "onClick=\"return confirm('Delete this message?');\">"
539 "Delete</a>", msgnum);
543 wprintf("</TD></TR></TABLE>\n");
546 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
547 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
550 * Learn the content type
552 strcpy(mime_content_type, "text/plain");
553 while (serv_gets(buf), (strlen(buf) > 0)) {
554 if (!strcmp(buf, "000")) {
555 wprintf("<I>unexpected end of message</I><br /><br />\n");
558 if (!strncasecmp(buf, "Content-type: ", 14)) {
559 safestrncpy(mime_content_type, &buf[14],
560 sizeof(mime_content_type));
564 /* Messages in legacy Citadel variformat get handled thusly... */
565 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
566 fmout(NULL, "JUSTIFY");
569 /* Boring old 80-column fixed format text gets handled this way... */
570 else if (!strcasecmp(mime_content_type, "text/plain")) {
571 while (serv_gets(buf), strcmp(buf, "000")) {
572 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
573 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
574 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
575 buf[strlen(buf) - 1] = 0;
577 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
578 wprintf("<BLOCKQUOTE>");
580 } else if ((bq == 1) &&
581 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
582 wprintf("</BLOCKQUOTE>");
588 wprintf("</TT><br />\n");
590 wprintf("</I><br />");
593 else /* HTML is fun, but we've got to strip it first */
594 if (!strcasecmp(mime_content_type, "text/html")) {
598 /* Unknown weirdness */
600 wprintf("I don't know how to display %s<br />\n",
602 while (serv_gets(buf), strcmp(buf, "000")) { }
606 /* Afterwards, offer links to download attachments 'n' such */
607 if (strlen(mime_http) > 0) {
608 wprintf("%s", mime_http);
611 /* Handler for vCard parts */
612 if (strlen(vcard_partnum) > 0) {
613 part_source = load_mimepart(msgnum, vcard_partnum);
614 if (part_source != NULL) {
616 /* If it's my vCard I can edit it */
617 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
618 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
619 || (WC->wc_view == VIEW_ADDRESSBOOK)
621 wprintf("<A HREF=\"/edit_vcard?"
622 "msgnum=%ld&partnum=%s\">",
623 msgnum, vcard_partnum);
624 wprintf("[edit]</A>");
627 /* In all cases, display the full card */
628 display_vcard(part_source, 0, 1, NULL);
632 /* Handler for calendar parts */
633 if (strlen(cal_partnum) > 0) {
634 part_source = load_mimepart(msgnum, cal_partnum);
635 if (part_source != NULL) {
636 cal_process_attachment(part_source,
637 msgnum, cal_partnum);
647 wprintf("</TD></TR></TABLE>\n");
649 /* end everythingamundo table */
650 wprintf("</TD></TR></TABLE>\n");
651 wprintf("</div><br />\n");
655 void summarize_message(long msgnum, int is_new) {
666 memset(&summ, 0, sizeof(summ));
667 strcpy(summ.subj, "(no subject)");
669 sprintf(buf, "MSG0 %ld|3", msgnum); /* ask for headers only with no MIME */
672 if (buf[0] != '1') return;
674 while (serv_gets(buf), strcmp(buf, "000")) {
675 if (!strncasecmp(buf, "from=", 5)) {
676 strcpy(summ.from, &buf[5]);
678 if (!strncasecmp(buf, "subj=", 5)) {
679 strcpy(summ.subj, &buf[5]);
681 if (!strncasecmp(buf, "rfca=", 5)) {
682 strcat(summ.from, " <");
683 strcat(summ.from, &buf[5]);
684 strcat(summ.from, ">");
687 if (!strncasecmp(buf, "node=", 5)) {
688 if ( ((WC->room_flags & QR_NETWORK)
689 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
690 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
692 strcat(summ.from, " @ ");
693 strcat(summ.from, &buf[5]);
697 if (!strncasecmp(buf, "rcpt=", 5)) {
698 strcpy(summ.to, &buf[5]);
701 if (!strncasecmp(buf, "time=", 5)) {
702 fmt_date(summ.date, atol(&buf[5]));
707 if (is_new) wprintf("<B>");
708 wprintf("<A HREF=\"/readfwd?startmsg=%ld"
709 "&maxmsgs=1&summary=0\">",
713 if (is_new) wprintf("</B>");
714 wprintf("</TD><TD>");
715 if (is_new) wprintf("<B>");
717 if (is_new) wprintf("</B>");
718 wprintf(" </TD><TD>");
719 if (is_new) wprintf("<B>");
721 if (is_new) wprintf("</B>");
724 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
732 void display_addressbook(long msgnum, char alpha) {
734 char mime_partnum[SIZ];
735 char mime_filename[SIZ];
736 char mime_content_type[SIZ];
737 char mime_disposition[SIZ];
739 char vcard_partnum[SIZ];
740 char *vcard_source = NULL;
750 memset(&summ, 0, sizeof(summ));
751 strcpy(summ.subj, "(no subject)");
753 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
756 if (buf[0] != '1') return;
758 while (serv_gets(buf), strcmp(buf, "000")) {
759 if (!strncasecmp(buf, "part=", 5)) {
760 extract(mime_filename, &buf[5], 1);
761 extract(mime_partnum, &buf[5], 2);
762 extract(mime_disposition, &buf[5], 3);
763 extract(mime_content_type, &buf[5], 4);
764 mime_length = extract_int(&buf[5], 5);
766 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
767 strcpy(vcard_partnum, mime_partnum);
773 if (strlen(vcard_partnum) > 0) {
774 vcard_source = load_mimepart(msgnum, vcard_partnum);
775 if (vcard_source != NULL) {
777 /* Display the summary line */
778 display_vcard(vcard_source, alpha, 0, NULL);
780 /* If it's my vCard I can edit it */
781 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
782 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
783 || (WC->wc_view == VIEW_ADDRESSBOOK)
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 id=\"fix_scrollbar_bug\">"
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 id=\"fix_scrollbar_bug\">"
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("</SELECT> of %d messages.", nummsgs);
1324 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1325 "VALUE=\"Delete selected\">\n");
1330 wprintf("</form>\n");
1334 do_tasks_view(); /* Render the task list */
1338 do_calendar_view(); /* Render the calendar */
1341 if (is_addressbook) {
1342 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1346 if (addrbook != NULL) free(addrbook);
1351 * Back end for post_message() ... this is where the actual message
1352 * gets transmitted to the server.
1354 void post_mime_to_server(void) {
1356 int is_multipart = 0;
1358 struct wc_attachment *att;
1360 size_t encoded_length;
1362 /* If there are attachments, we have to do multipart/mixed */
1363 if (WC->first_attachment != NULL) {
1368 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1369 serv_info.serv_fqdn,
1374 /* Remember, serv_printf() appends an extra newline */
1375 serv_printf("Content-type: multipart/mixed; "
1376 "boundary=\"%s\"\n", boundary);
1377 serv_printf("This is a multipart message in MIME format.\n");
1378 serv_printf("--%s", boundary);
1381 serv_puts("Content-type: text/html");
1383 serv_puts("<HTML><BODY>\n");
1384 text_to_server(bstr("msgtext"), 0);
1385 serv_puts("</BODY></HTML>\n");
1390 /* Add in the attachments */
1391 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1393 encoded_length = ((att->length * 150) / 100);
1394 encoded = malloc(encoded_length);
1395 if (encoded == NULL) break;
1396 CtdlEncodeBase64(encoded, att->data, att->length);
1398 serv_printf("--%s", boundary);
1399 serv_printf("Content-type: %s", att->content_type);
1400 serv_printf("Content-disposition: attachment; "
1401 "filename=\"%s\"", att->filename);
1402 serv_puts("Content-transfer-encoding: base64");
1404 serv_write(encoded, strlen(encoded));
1409 serv_printf("--%s--", boundary);
1417 * Post message (or don't post message)
1419 * Note regarding the "dont_post" variable:
1420 * A random value (actually, it's just a timestamp) is inserted as a hidden
1421 * field called "postseq" when the display_enter page is generated. This
1422 * value is checked when posting, using the static variable dont_post. If a
1423 * user attempts to post twice using the same dont_post value, the message is
1424 * discarded. This prevents the accidental double-saving of the same message
1425 * if the user happens to click the browser "back" button.
1427 void post_message(void)
1430 static long dont_post = (-1L);
1431 struct wc_attachment *att, *aptr;
1433 if (WC->upload_length > 0) {
1435 /* There's an attachment. Save it to this struct... */
1436 att = malloc(sizeof(struct wc_attachment));
1437 memset(att, 0, sizeof(struct wc_attachment));
1438 att->length = WC->upload_length;
1439 strcpy(att->content_type, WC->upload_content_type);
1440 strcpy(att->filename, WC->upload_filename);
1443 /* And add it to the list. */
1444 if (WC->first_attachment == NULL) {
1445 WC->first_attachment = att;
1448 aptr = WC->first_attachment;
1449 while (aptr->next != NULL) aptr = aptr->next;
1453 /* Netscape sends a simple filename, which is what we want,
1454 * but Satan's browser sends an entire pathname. Reduce
1455 * the path to just a filename if we need to.
1457 while (num_tokens(att->filename, '/') > 1) {
1458 remove_token(att->filename, 0, '/');
1460 while (num_tokens(att->filename, '\\') > 1) {
1461 remove_token(att->filename, 0, '\\');
1464 /* Transfer control of this memory from the upload struct
1465 * to the attachment struct.
1467 att->data = WC->upload;
1468 WC->upload_length = 0;
1474 if (!strcasecmp(bstr("sc"), "Cancel")) {
1475 sprintf(WC->ImportantMessage,
1476 "Cancelled. Message was not posted.");
1477 } else if (!strcasecmp(bstr("attach"), "Add")) {
1480 } else if (atol(bstr("postseq")) == dont_post) {
1481 sprintf(WC->ImportantMessage,
1482 "Automatically cancelled because you have already "
1483 "saved this message.");
1485 sprintf(buf, "ENT0 1|%s|0|4|%s",
1490 if (buf[0] == '4') {
1491 post_mime_to_server();
1492 if (strlen(bstr("recp")) > 0) {
1493 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1496 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1498 dont_post = atol(bstr("postseq"));
1500 sprintf(WC->ImportantMessage,
1505 free_attachments(WC);
1506 readloop("readnew");
1513 * display the message entry screen
1515 void display_enter(void)
1519 struct wc_attachment *att;
1521 if (strlen(bstr("force_room")) > 0) {
1522 gotoroom(bstr("force_room"));
1525 /* Are we perhaps in an address book view? If so, then an "enter
1526 * message" command really means "add new entry."
1528 if (WC->wc_view == VIEW_ADDRESSBOOK) {
1529 do_edit_vcard(-1, "", "");
1533 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1534 /* Are we perhaps in a calendar view? If so, then an "enter
1535 * message" command really means "add new calendar item."
1537 if (WC->wc_view == VIEW_CALENDAR) {
1538 display_edit_event();
1542 /* Are we perhaps in a tasks view? If so, then an "enter
1543 * message" command really means "add new task."
1545 if (WC->wc_view == VIEW_TASKS) {
1546 display_edit_task();
1551 /* Otherwise proceed normally. Do a custom room banner with no navbar... */
1552 output_headers(1, 1, 2, 0, 0, 0, 0);
1553 wprintf("<div id=\"banner\">\n");
1554 embed_room_banner(NULL, navbar_none);
1555 wprintf("</div>\n");
1556 wprintf("<div id=\"content\">\n"
1557 "<div id=\"fix_scrollbar_bug\">"
1558 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1560 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1564 if (!strncmp(buf, "570", 3)) {
1565 if (strlen(bstr("recp")) > 0) {
1566 svprintf("RECPERROR", WCS_STRING,
1567 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1571 do_template("prompt_for_recipient");
1574 if (buf[0] != '2') {
1575 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1581 strcat(&buf[strlen(buf)], " <I>from</I> ");
1582 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1583 if (strlen(bstr("recp")) > 0) {
1584 strcat(&buf[strlen(buf)], " <I>to</I> ");
1585 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1587 strcat(&buf[strlen(buf)], " <I>in</I> ");
1588 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1590 /* begin message entry screen */
1591 // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1593 wprintf("<form enctype=\"multipart/form-data\" "
1594 "method=\"POST\" action=\"/post\" "
1595 "name=\"enterform\""
1596 "onSubmit=\"return submitForm();\""
1598 wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1600 wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1603 wprintf("%s<br>\n", buf); /* header bar */
1604 wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1605 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1606 wprintf("<font size=-1>Subject (optional):</font>"
1607 "<input type=\"text\" name=\"subject\" value=\"");
1608 escputs(bstr("subject"));
1609 wprintf("\" size=40 maxlength=70>"
1613 wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1614 if (strlen(bstr("recp")) > 0) {
1615 wprintf("Send message");
1617 wprintf("Post message");
1620 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1622 wprintf("<center><script type=\"text/javascript\" "
1623 "src=\"static/richtext.js\"></script>\n"
1624 "<script type=\"text/javascript\">\n"
1625 "function submitForm() { \n"
1626 " updateRTE('msgtext'); \n"
1630 "initRTE(\"static/\", \"static/\", \"\"); \n"
1632 "<noscript>JavaScript must be enabled.</noscript> \n"
1633 "<script type=\"text/javascript\"> \n"
1634 "writeRichText('msgtext', '");
1635 msgescputs(bstr("msgtext"));
1636 wprintf("', '96%%', '200', true, false); \n"
1637 "</script></center><br />\n");
1639 /* Enumerate any attachments which are already in place... */
1640 wprintf("<img src=\"/static/attachment.gif\" border=0 "
1641 "align=middle height=16 width=16> Attachments: ");
1642 wprintf("<select name=\"which_attachment\" size=1>");
1643 for (att = WC->first_attachment; att != NULL; att = att->next) {
1644 wprintf("<option value=\"");
1645 urlescputs(att->filename);
1647 escputs(att->filename);
1648 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
1649 wprintf("</option>\n");
1651 wprintf("</select>");
1653 /* Now offer the ability to attach additional files... */
1654 wprintf(" "
1655 "Attach file: <input NAME=\"attachfile\" "
1656 "SIZE=16 TYPE=\"file\">\n "
1657 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1659 wprintf("</form>\n");
1661 wprintf("</td></tr></table></div>\n");
1662 DONE: wDumpContent(1);
1672 void delete_msg(void)
1677 msgid = atol(bstr("msgid"));
1679 output_headers(1, 1, 1, 0, 0, 0, 0);
1681 sprintf(buf, "DELE %ld", msgid);
1684 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1693 * Confirm move of a message
1695 void confirm_move_msg(void)
1701 msgid = atol(bstr("msgid"));
1703 output_headers(1, 1, 1, 0, 0, 0, 0);
1705 wprintf("<div id=\"fix_scrollbar_bug\">"
1706 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
1707 wprintf("<font size=+1 color=\"#ffffff\"");
1708 wprintf("<b>Confirm move of message</b>\n");
1709 wprintf("</font></td></tr></table></div>\n");
1711 wprintf("<CENTER>");
1713 wprintf("Move this message to:<br />\n");
1715 wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1716 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1720 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1723 if (buf[0] == '1') {
1724 while (serv_gets(buf), strcmp(buf, "000")) {
1725 extract(targ, buf, 0);
1726 wprintf("<OPTION>");
1731 wprintf("</SELECT>\n");
1732 wprintf("<br />\n");
1734 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1736 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1737 wprintf("</form></CENTER>\n");
1739 wprintf("</CENTER>\n");
1750 msgid = atol(bstr("msgid"));
1752 output_headers(1, 1, 1, 0, 0, 0, 0);
1754 if (!strcasecmp(bstr("yesno"), "Move")) {
1755 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1758 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1760 wprintf("<EM>Message not moved.</EM><br />\n");
1767 * This gets called when a user selects multiple messages in a summary
1768 * list and then clicks to perform a transformation of some sort on them
1769 * (such as deleting them).
1771 void do_stuff_to_msgs(void) {
1776 struct stuff_t *next;
1780 struct stuff_t *stuff = NULL;
1781 struct stuff_t *ptr;
1784 serv_puts("MSGS ALL");
1787 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1788 ptr = malloc(sizeof(struct stuff_t));
1789 ptr->msgnum = atol(buf);
1794 strcpy(sc, bstr("sc"));
1796 while (stuff != NULL) {
1798 sprintf(buf, "msg_%ld", stuff->msgnum);
1799 if (!strcasecmp(bstr(buf), "yes")) {
1801 if (!strcasecmp(sc, "Delete selected")) {
1802 serv_printf("DELE %ld", stuff->msgnum);
1813 readloop("readfwd");