8 * display the form for paging (x-messaging) another user
10 void display_page(void)
14 strcpy(recp, bstr("recp"));
16 output_headers(1, 1, 2, 0, 0, 0);
17 wc_printf("<div id=\"banner\">\n");
19 wc_printf(_("Send instant message"));
21 wc_printf("</div>\n");
23 wc_printf("<div id=\"content\" class=\"service\">\n");
25 wc_printf("<div class=\"fix_scrollbar_bug\">"
26 "<table class=\"paging_background\"><tr><td>\n");
28 wc_printf(_("Send an instant message to: "));
32 wc_printf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
33 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
34 wc_printf("<input type=\"hidden\" name=\"template\" value=\"who\">\n");
36 wc_printf("<TABLE border=0 width=100%%><TR><TD>\n");
38 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
42 wc_printf(_("Enter message text:"));
45 wc_printf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 "
46 "WIDTH=40></TEXTAREA>\n");
48 wc_printf("</TD></TR></TABLE><br />\n");
50 wc_printf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">", _("Send message"));
51 wc_printf("<br /><a href=\"javascript:window.close();\"%s</A>\n", _("Cancel"));
53 wc_printf("</FORM></CENTER>\n");
54 wc_printf("</td></tr></table></div>\n");
59 * \brief page another user
66 safestrncpy(recp, bstr("recp"), sizeof recp);
68 if (!havebstr("send_button")) {
69 safestrncpy(WC->ImportantMessage,
70 _("Message was not sent."),
71 sizeof WC->ImportantMessage
74 serv_printf("SEXP %s|-", recp);
75 serv_getln(buf, sizeof buf);
78 text_to_server(bstr("msgtext"));
80 stresc(buf, 256, recp, 0, 0);
81 snprintf(WC->ImportantMessage,
82 sizeof WC->ImportantMessage,
84 _("Message has been sent to "),
89 safestrncpy(WC->ImportantMessage, &buf[4], sizeof WC->ImportantMessage);
99 * \brief multiuser chat
105 /** First, check to make sure we're still allowed in this room. */
106 serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
107 serv_getln(buf, sizeof buf);
110 Buf = NewStrBufPlain(HKEY("_BASEROOM_"));
117 * If the chat socket is still open from a previous chat,
118 * close it -- because it might be stale or in the wrong room.
120 if (WC->chat_sock < 0) {
121 close(WC->chat_sock);
122 WC->chat_sock = (-1);
126 * WebCit Chat works by having transmit, receive, and refresh
127 * frames. Load the frameset. (This isn't AJAX but the headers
128 * output by begin_ajax_response() happen to be the ones we need.)
130 begin_ajax_response();
131 do_template("chatframeset", NULL);
138 * \brief display page popup
139 * If there are instant messages waiting, and we notice that we haven't checked them in
140 * a while, it probably means that we need to open the instant messenger window.
142 int Conditional_PAGE_WAITING(StrBuf *Target, WCTemplputParams *TP)
147 /** JavaScript function to alert the user that popups are probably blocked */
148 /** First, do the check as part of our page load. */
150 len = serv_getln(buf, sizeof buf);
151 if ((len >= 3) && (buf[3] == '*')) {
152 if ((time(NULL) - WC->last_pager_check) > 60) {
157 /** Then schedule it to happen again a minute from now if the user is idle. */
163 * \brief Support function for chat
164 * make sure the chat socket is connected
167 int setup_chat_socket(void) {
170 int good_chatmode = 0;
172 if (WC->chat_sock < 0) {
174 if (!strcasecmp(ctdlhost, "uds")) {
175 /** unix domain socket */
176 sprintf(buf, "%s/citadel.socket", ctdlport);
177 WC->chat_sock = uds_connectsock(buf);
181 WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
184 if (WC->chat_sock < 0) {
188 /** Temporarily swap the serv and chat sockets during chat talk */
190 WC->serv_sock = WC->chat_sock;
193 serv_getln(buf, sizeof buf);
195 serv_printf("USER %s", ChrPtr(WC->wc_username));
196 serv_getln(buf, sizeof buf);
198 serv_printf("PASS %s", ChrPtr(WC->wc_password));
199 serv_getln(buf, sizeof buf);
201 serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
202 serv_getln(buf, sizeof buf);
205 serv_getln(buf, sizeof buf);
214 /** Unswap the sockets. */
216 WC->serv_sock = WC->chat_sock;
219 if (!good_chatmode) close(WC->serv_sock);
228 * \brief Receiving side of the chat window.
229 * This is implemented in a
230 * tiny hidden IFRAME that just does JavaScript writes to
231 * other frames whenever it refreshes and finds new data.
233 void chat_recv(void) {
237 int end_chat_now = 0;
241 char *output_data = NULL;
243 output_headers(0, 0, 0, 0, 0, 0);
245 hprintf("Content-type: text/html; charset=utf-8\r\n");
249 "<meta http-equiv=\"refresh\" content=\"3\" />\n"
252 "<body bgcolor=\"#FFFFFF\">\n"
255 if (setup_chat_socket() != 0) {
256 wc_printf(_("An error occurred while setting up the chat socket."));
257 wc_printf("</BODY></HTML>\n");
263 * See if there is any chat data waiting.
265 output_data = strdup("");
268 pf.fd = WC->chat_sock;
271 if ((poll(&pf, 1, 1) > 0) && (pf.revents & POLLIN)) {
274 /** Temporarily swap the serv and chat sockets during chat talk */
276 WC->serv_sock = WC->chat_sock;
279 serv_getln(buf, sizeof buf);
281 if (!strcmp(buf, "000")) {
283 strcat(buf, _("Now exiting chat mode."));
287 /** Unswap the sockets. */
289 WC->serv_sock = WC->chat_sock;
292 /** Append our output data */
293 output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
294 strcat(output_data, buf);
295 strcat(output_data, "\n");
298 } while ( (got_data) && (!end_chat_now) );
301 close(WC->chat_sock);
302 WC->chat_sock = (-1);
303 wc_printf("<img src=\"static/blank.gif\" onLoad=\"parent.window.close();\">\n");
306 if (!IsEmptyStr(output_data)) {
308 len = strlen(output_data);
309 if (output_data[len-1] == '\n') {
310 output_data[len-1] = 0;
313 /** Output our fun to the other frame. */
314 wc_printf("<img src=\"static/blank.gif\" WIDTH=1 HEIGHT=1\n"
318 for (i=0; i<num_tokens(output_data, '\n'); ++i) {
319 extract_token(buf, output_data, i, '\n', sizeof buf);
320 extract_token(cl_user, buf, 0, '|', sizeof cl_user);
321 extract_token(cl_text, buf, 1, '|', sizeof cl_text);
323 if (strcasecmp(cl_text, "NOOP")) {
325 wc_printf("parent.chat_transcript.document.write('");
327 if (strcasecmp(cl_user, WC->last_chat_user)) {
328 wc_printf("<TABLE border=0 WIDTH=100%% "
329 "CELLSPACING=1 CELLPADDING=0 "
330 "BGCOLOR="#FFFFFF">"
331 "<TR><TD></TR></TD></TABLE>"
336 wc_printf("<TABLE border=0 WIDTH=100%% "
337 "CELLSPACING=0 CELLPADDING=0 "
338 "BGCOLOR="#EEEEEE">");
340 wc_printf("<TR><TD>");
342 if (!strcasecmp(cl_user, ":")) {
346 if (strcasecmp(cl_user, WC->last_chat_user)) {
349 if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
350 wc_printf("<FONT COLOR="#FF0000">");
353 wc_printf("<FONT COLOR="#0000FF">");
357 wc_printf("</FONT>: </B>");
360 wc_printf(" ");
363 if (!strcasecmp(cl_user, ":")) {
367 wc_printf("</TD></TR></TABLE>");
370 strcpy(WC->last_chat_user, cl_user);
374 wc_printf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
379 wc_printf("</BODY></HTML>\n");
385 * \brief sending side of the chat window
387 void chat_send(void) {
392 output_headers(0, 0, 0, 0, 0, 0);
393 hprintf("Content-type: text/html; charset=utf-8\r\n");
396 "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
399 if (havebstr("send_this")) {
400 strcpy(send_this, bstr("send_this"));
403 strcpy(send_this, "");
406 if (havebstr("help_button")) {
407 strcpy(send_this, "/help");
410 if (havebstr("list_button")) {
411 strcpy(send_this, "/who");
414 if (havebstr("exit_button")) {
415 strcpy(send_this, "/quit");
418 if (setup_chat_socket() != 0) {
419 wc_printf(_("An error occurred while setting up the chat socket."));
420 wc_printf("</BODY></HTML>\n");
425 /** Temporarily swap the serv and chat sockets during chat talk */
427 WC->serv_sock = WC->chat_sock;
430 while (!IsEmptyStr(send_this)) {
431 if (strlen(send_this) < 67) {
432 serv_puts(send_this);
433 strcpy(send_this, "");
436 for (i=55; i<67; ++i) {
437 if (send_this[i] == ' ') break;
439 strncpy(buf, send_this, i);
441 strcpy(send_this, &send_this[i]);
446 /** Unswap the sockets. */
448 WC->serv_sock = WC->chat_sock;
451 wc_printf("<FORM METHOD=\"POST\" action=\"chat_send\" NAME=\"chatsendform\">\n");
452 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
453 wc_printf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
454 "NAME=\"send_this\">\n", SIZ-10);
456 wc_printf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">\n", _("Send"));
457 wc_printf("<INPUT TYPE=\"submit\" NAME=\"help_button\" VALUE=\"%s\">\n", _("Help"));
458 wc_printf("<INPUT TYPE=\"submit\" NAME=\"list_button\" VALUE=\"%s\">\n", _("List users"));
459 wc_printf("<INPUT TYPE=\"submit\" NAME=\"exit_button\" VALUE=\"%s\">\n", _("Exit"));
460 wc_printf("</FORM>\n");
462 wc_printf("</BODY></HTML>\n");
467 void ajax_send_instant_message(void) {
471 safestrncpy(recp, bstr("recp"), sizeof recp);
473 serv_printf("SEXP %s|-", recp);
474 serv_getln(buf, sizeof buf);
477 text_to_server(bstr("msg"));
481 escputs(buf); /* doesn't really matter what we return - the client ignores it */
489 WebcitAddUrlHandler(HKEY("display_page"), "", 0, display_page, 0);
490 WebcitAddUrlHandler(HKEY("page_user"), "", 0, page_user, 0);
491 WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
492 WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, 0);
493 WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
494 WebcitAddUrlHandler(HKEY("ajax_send_instant_message"), "", 0, ajax_send_instant_message, AJAX);
495 RegisterConditional(HKEY("COND:PAGE:WAITING"), 0, Conditional_PAGE_WAITING, CTX_NONE);
500 SessionDestroyModule_CHAT
503 if (sess->chat_sock > 0)
504 close(sess->chat_sock);