4 * Functions which implement the chat and paging facilities.
13 #include <sys/types.h>
15 #include <sys/socket.h>
19 #include <netinet/in.h>
31 * display the form for paging (x-messaging) another user
33 void display_page(void)
37 strcpy(recp, bstr("recp"));
39 output_headers(1, 1, 0, 0, 0, 0, 0);
41 svprintf("BOXTITLE", WCS_STRING, "Page: %s", recp);
42 do_template("beginbox");
44 wprintf("<FORM METHOD=\"POST\" ACTION=\"/page_user\">\n");
46 wprintf("<TABLE border=0 width=100%%><TR><TD>\n");
48 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
52 wprintf("<INPUT TYPE=\"hidden\" NAME=\"closewin\" VALUE=\"");
53 escputs(bstr("closewin"));
56 wprintf("Enter message text:<br />");
58 wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 "
59 "WIDTH=40></TEXTAREA>\n");
61 wprintf("</TD></TR></TABLE><br />\n");
63 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Send message\">");
64 wprintf("<br /><A HREF=\"javascript:window.close();\"Cancel</A>\n");
66 wprintf("</FORM></CENTER>\n");
67 do_template("endbox");
81 output_headers(1, 1, 0, 0, 0, 0, 0);
83 strcpy(recp, bstr("recp"));
84 strcpy(sc, bstr("sc"));
85 strcpy(closewin, bstr("closewin"));
87 if (strcmp(sc, "Send message")) {
88 wprintf("<EM>Message was not sent.</EM><br />\n");
90 serv_printf("SEXP %s|-", recp);
94 text_to_server(bstr("msgtext"), 0);
96 wprintf("<EM>Message has been sent to ");
98 wprintf(".</EM><br />\n");
101 wprintf("<EM>%s</EM><br />\n", &buf[4]);
105 if (!strcasecmp(closewin, "yes")) {
106 wprintf("<CENTER><A HREF=\"javascript:window.close();\">"
107 "[ close window ]</A></CENTER>\n");
122 /* First, check to make sure we're still allowed in this room. */
123 serv_printf("GOTO %s", WC->wc_roomname);
126 smart_goto("_BASEROOM_");
130 /* If the chat socket is still open from a previous chat,
131 * close it -- because it might be stale or in the wrong room.
133 if (WC->chat_sock < 0) {
134 close(WC->chat_sock);
135 WC->chat_sock = (-1);
138 /* WebCit Chat works by having transmit, receive, and refresh
139 * frames. Load the frameset.
141 do_template("chatframeset");
149 void page_popup(void)
154 while (serv_puts("GEXP"), serv_gets(buf), buf[0]=='1') {
156 extract(pagefrom, &buf[4], 3);
158 wprintf("<table border=1 bgcolor=\"#880000\"><tr><td>");
159 wprintf("<span class=\"titlebar\">Instant message from ");
161 wprintf("</span></td></tr><tr><td><font color=\"#FFFFFF\">");
163 wprintf("</font></td></tr>"
164 "<tr><td><div align=center><font color=\"#FFFFFF\">"
165 "<a href=\"javascript:hide_page_popup()\">[ close window ]</a>"
171 WC->HaveInstantMessages = 0;
177 * Support function for chat -- make sure the chat socket is connected
180 int setup_chat_socket(void) {
183 int good_chatmode = 0;
185 if (WC->chat_sock < 0) {
187 if (!strcasecmp(ctdlhost, "uds")) {
188 /* unix domain socket */
189 sprintf(buf, "%s/citadel.socket", ctdlport);
190 WC->chat_sock = uds_connectsock(buf);
194 WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
197 if (WC->chat_sock < 0) {
201 /* Temporarily swap the serv and chat sockets during chat talk */
203 WC->serv_sock = WC->chat_sock;
208 serv_printf("USER %s", WC->wc_username);
211 serv_printf("PASS %s", WC->wc_password);
214 serv_printf("GOTO %s", WC->wc_roomname);
227 /* Unswap the sockets. */
229 WC->serv_sock = WC->chat_sock;
232 if (!good_chatmode) close(WC->serv_sock);
241 * Receiving side of the chat window. This is implemented in a
242 * tiny hidden IFRAME that just does JavaScript writes to
243 * other frames whenever it refreshes and finds new data.
245 void chat_recv(void) {
249 int end_chat_now = 0;
253 char *output_data = NULL;
255 output_headers(0, 0, 0, 0, 0, 0, 0);
257 wprintf("Content-type: text/html\n");
261 "<meta http-equiv=\"refresh\" content=\"3\" />\n"
264 "<body bgcolor=\"#FFFFFF\">\n"
267 if (setup_chat_socket() != 0) {
268 wprintf("Error setting up chat socket</BODY></HTML>\n");
274 * See if there is any chat data waiting.
276 output_data = strdup("");
279 pf.fd = WC->chat_sock;
282 if (poll(&pf, 1, 1) > 0) if (pf.revents & POLLIN) {
285 /* Temporarily swap the serv and chat sockets during chat talk */
287 WC->serv_sock = WC->chat_sock;
292 if (!strcmp(buf, "000")) {
293 strcpy(buf, ":|exiting chat mode");
297 /* Unswap the sockets. */
299 WC->serv_sock = WC->chat_sock;
302 /* Append our output data */
303 output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
304 strcat(output_data, buf);
305 strcat(output_data, "\n");
308 } while ( (got_data) && (!end_chat_now) );
311 close(WC->chat_sock);
312 WC->chat_sock = (-1);
313 wprintf("<IMG SRC=\"/static/blank.gif\" onLoad=\"parent.window.close();\">\n");
316 if (strlen(output_data) > 0) {
318 if (output_data[strlen(output_data)-1] == '\n') {
319 output_data[strlen(output_data)-1] = 0;
322 /* Output our fun to the other frame. */
323 wprintf("<IMG SRC=\"/static/blank.gif\" WIDTH=1 HEIGHT=1\n"
327 for (i=0; i<num_tokens(output_data, '\n'); ++i) {
328 extract_token(buf, output_data, i, '\n');
329 extract_token(cl_user, buf, 0, '|');
330 extract_token(cl_text, buf, 1, '|');
332 if (strcasecmp(cl_text, "NOOP")) {
334 wprintf("parent.chat_transcript.document.write('");
336 if (strcasecmp(cl_user, WC->last_chat_user)) {
337 wprintf("<TABLE border=0 WIDTH=100%% "
338 "CELLSPACING=1 CELLPADDING=0 "
339 "BGCOLOR="#FFFFFF">"
340 "<TR><TD></TR></TD></TABLE>"
345 wprintf("<TABLE border=0 WIDTH=100%% "
346 "CELLSPACING=0 CELLPADDING=0 "
347 "BGCOLOR="#EEEEEE">");
351 if (!strcasecmp(cl_user, ":")) {
355 if (strcasecmp(cl_user, WC->last_chat_user)) {
358 if (!strcasecmp(cl_user, WC->wc_username)) {
359 wprintf("<FONT COLOR="#FF0000">");
362 wprintf("<FONT COLOR="#0000FF">");
366 wprintf("</FONT>: </B>");
369 wprintf(" ");
372 if (!strcasecmp(cl_user, ":")) {
376 wprintf("</TD></TR></TABLE>");
379 strcpy(WC->last_chat_user, cl_user);
383 wprintf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
388 wprintf("</BODY></HTML>\n");
394 * sending side of the chat window
396 void chat_send(void) {
401 output_headers(0, 0, 0, 0, 0, 0, 0);
402 wprintf("Content-type: text/html\n");
405 "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
408 if (bstr("send_this") != NULL) {
409 strcpy(send_this, bstr("send_this"));
412 strcpy(send_this, "");
415 if (bstr("sendbutton") != NULL) {
417 if (!strcasecmp(bstr("sendbutton"), "Help")) {
418 strcpy(send_this, "/help");
421 if (!strcasecmp(bstr("sendbutton"), "List Users")) {
422 strcpy(send_this, "/who");
425 if (!strcasecmp(bstr("sendbutton"), "Exit")) {
426 strcpy(send_this, "/quit");
429 if (setup_chat_socket() != 0) {
430 wprintf("Error setting up chat socket</BODY></HTML>\n");
435 /* Temporarily swap the serv and chat sockets during chat talk */
437 WC->serv_sock = WC->chat_sock;
440 while (strlen(send_this) > 0) {
441 if (strlen(send_this) < 67) {
442 serv_puts(send_this);
443 strcpy(send_this, "");
446 for (i=55; i<67; ++i) {
447 if (send_this[i] == ' ') break;
449 strncpy(buf, send_this, i);
451 strcpy(send_this, &send_this[i]);
456 /* Unswap the sockets. */
458 WC->serv_sock = WC->chat_sock;
463 wprintf("<FORM METHOD=\"POST\" ACTION=\"/chat_send\" NAME=\"chatsendform\">\n");
464 wprintf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
465 "NAME=\"send_this\">\n", SIZ-10);
467 wprintf("<INPUT TYPE=\"submit\" NAME=\"sendbutton\" VALUE=\"Send\">\n");
468 wprintf("<INPUT TYPE=\"submit\" NAME=\"sendbutton\" VALUE=\"Help\">\n");
469 wprintf("<INPUT TYPE=\"submit\" NAME=\"sendbutton\" VALUE=\"List Users\">\n");
470 wprintf("<INPUT TYPE=\"submit\" NAME=\"sendbutton\" VALUE=\"Exit\">\n");
471 wprintf("</FORM>\n");
473 wprintf("</BODY></HTML>\n");