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 wprintf("<div id=\"banner\">\n");
19 wprintf(_("Send instant message"));
23 wprintf("<div id=\"content\" class=\"service\">\n");
25 wprintf("<div class=\"fix_scrollbar_bug\">"
26 "<table class=\"paging_background\"><tr><td>\n");
28 wprintf(_("Send an instant message to: "));
32 wprintf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
33 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
34 wprintf("<input type=\"hidden\" name=\"template\" value=\"who\">\n");
36 wprintf("<TABLE border=0 width=100%%><TR><TD>\n");
38 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
42 wprintf(_("Enter message text:"));
45 wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 "
46 "WIDTH=40></TEXTAREA>\n");
48 wprintf("</TD></TR></TABLE><br />\n");
50 wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">", _("Send message"));
51 wprintf("<br /><a href=\"javascript:window.close();\"%s</A>\n", _("Cancel"));
53 wprintf("</FORM></CENTER>\n");
54 wprintf("</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 void page_popup(void)
147 /** JavaScript function to alert the user that popups are probably blocked */
148 wprintf("<script type=\"text/javascript\"> "
149 "function PopUpFailed() { "
153 _("You have one or more instant messages waiting, but the Citadel Instant Messenger "
154 "window failed to open. This is probably because you have a popup blocker "
155 "installed. Please configure your popup blocker to allow popups from this site "
156 "if you wish to receive instant messages.")
159 /** First, do the check as part of our page load. */
161 len = serv_getln(buf, sizeof buf);
162 if ((len >= 3) && (buf[3] == '*')) {
163 if ((time(NULL) - WC->last_pager_check) > 60) {
164 wprintf("<script type=\"text/javascript\">"
165 " var oWin = window.open('static/instant_messenger.html', "
166 " 'CTDL_MESSENGER', 'width=700,height=400'); "
167 " if (oWin==null || typeof(oWin)==\"undefined\") { "
175 /** Then schedule it to happen again a minute from now if the user is idle. */
176 wprintf("<script type=\"text/javascript\"> "
177 " function HandleSslp(sslg_xmlresponse) { "
178 " sslg_response = sslg_xmlresponse.responseText.substr(0, 1); "
179 " if (sslg_response == 'Y') { "
180 " var oWin = window.open('static/instant_messenger.html', 'CTDL_MESSENGER', "
181 " 'width=700,height=400'); "
182 " if (oWin==null || typeof(oWin)==\"undefined\") { "
187 " function CheckPager() { "
188 " new Ajax.Request('sslg', { method: 'get', parameters: CtdlRandomString(), "
189 " onSuccess: HandleSslp } ); "
191 " new PeriodicalExecuter(CheckPager, 30); "
199 * \brief Support function for chat
200 * make sure the chat socket is connected
203 int setup_chat_socket(void) {
206 int good_chatmode = 0;
208 if (WC->chat_sock < 0) {
210 if (!strcasecmp(ctdlhost, "uds")) {
211 /** unix domain socket */
212 sprintf(buf, "%s/citadel.socket", ctdlport);
213 WC->chat_sock = uds_connectsock(buf);
217 WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
220 if (WC->chat_sock < 0) {
224 /** Temporarily swap the serv and chat sockets during chat talk */
226 WC->serv_sock = WC->chat_sock;
229 serv_getln(buf, sizeof buf);
231 serv_printf("USER %s", ChrPtr(WC->wc_username));
232 serv_getln(buf, sizeof buf);
234 serv_printf("PASS %s", ChrPtr(WC->wc_password));
235 serv_getln(buf, sizeof buf);
237 serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
238 serv_getln(buf, sizeof buf);
241 serv_getln(buf, sizeof buf);
250 /** Unswap the sockets. */
252 WC->serv_sock = WC->chat_sock;
255 if (!good_chatmode) close(WC->serv_sock);
264 * \brief Receiving side of the chat window.
265 * This is implemented in a
266 * tiny hidden IFRAME that just does JavaScript writes to
267 * other frames whenever it refreshes and finds new data.
269 void chat_recv(void) {
273 int end_chat_now = 0;
277 char *output_data = NULL;
279 output_headers(0, 0, 0, 0, 0, 0);
281 hprintf("Content-type: text/html; charset=utf-8\r\n");
284 "<meta http-equiv=\"refresh\" content=\"3\" />\n"
287 "<body bgcolor=\"#FFFFFF\">\n"
290 if (setup_chat_socket() != 0) {
291 wprintf(_("An error occurred while setting up the chat socket."));
292 wprintf("</BODY></HTML>\n");
298 * See if there is any chat data waiting.
300 output_data = strdup("");
303 pf.fd = WC->chat_sock;
306 if ((poll(&pf, 1, 1) > 0) && (pf.revents & POLLIN)) {
309 /** Temporarily swap the serv and chat sockets during chat talk */
311 WC->serv_sock = WC->chat_sock;
314 serv_getln(buf, sizeof buf);
316 if (!strcmp(buf, "000")) {
318 strcat(buf, _("Now exiting chat mode."));
322 /** Unswap the sockets. */
324 WC->serv_sock = WC->chat_sock;
327 /** Append our output data */
328 output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
329 strcat(output_data, buf);
330 strcat(output_data, "\n");
333 } while ( (got_data) && (!end_chat_now) );
336 close(WC->chat_sock);
337 WC->chat_sock = (-1);
338 wprintf("<img src=\"static/blank.gif\" onLoad=\"parent.window.close();\">\n");
341 if (!IsEmptyStr(output_data)) {
343 len = strlen(output_data);
344 if (output_data[len-1] == '\n') {
345 output_data[len-1] = 0;
348 /** Output our fun to the other frame. */
349 wprintf("<img src=\"static/blank.gif\" WIDTH=1 HEIGHT=1\n"
353 for (i=0; i<num_tokens(output_data, '\n'); ++i) {
354 extract_token(buf, output_data, i, '\n', sizeof buf);
355 extract_token(cl_user, buf, 0, '|', sizeof cl_user);
356 extract_token(cl_text, buf, 1, '|', sizeof cl_text);
358 if (strcasecmp(cl_text, "NOOP")) {
360 wprintf("parent.chat_transcript.document.write('");
362 if (strcasecmp(cl_user, WC->last_chat_user)) {
363 wprintf("<TABLE border=0 WIDTH=100%% "
364 "CELLSPACING=1 CELLPADDING=0 "
365 "BGCOLOR="#FFFFFF">"
366 "<TR><TD></TR></TD></TABLE>"
371 wprintf("<TABLE border=0 WIDTH=100%% "
372 "CELLSPACING=0 CELLPADDING=0 "
373 "BGCOLOR="#EEEEEE">");
377 if (!strcasecmp(cl_user, ":")) {
381 if (strcasecmp(cl_user, WC->last_chat_user)) {
384 if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
385 wprintf("<FONT COLOR="#FF0000">");
388 wprintf("<FONT COLOR="#0000FF">");
392 wprintf("</FONT>: </B>");
395 wprintf(" ");
398 if (!strcasecmp(cl_user, ":")) {
402 wprintf("</TD></TR></TABLE>");
405 strcpy(WC->last_chat_user, cl_user);
409 wprintf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
414 wprintf("</BODY></HTML>\n");
420 * \brief sending side of the chat window
422 void chat_send(void) {
427 output_headers(0, 0, 0, 0, 0, 0);
428 hprintf("Content-type: text/html; charset=utf-8\r\n");
430 "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
433 if (havebstr("send_this")) {
434 strcpy(send_this, bstr("send_this"));
437 strcpy(send_this, "");
440 if (havebstr("help_button")) {
441 strcpy(send_this, "/help");
444 if (havebstr("list_button")) {
445 strcpy(send_this, "/who");
448 if (havebstr("exit_button")) {
449 strcpy(send_this, "/quit");
452 if (setup_chat_socket() != 0) {
453 wprintf(_("An error occurred while setting up the chat socket."));
454 wprintf("</BODY></HTML>\n");
459 /** Temporarily swap the serv and chat sockets during chat talk */
461 WC->serv_sock = WC->chat_sock;
464 while (!IsEmptyStr(send_this)) {
465 if (strlen(send_this) < 67) {
466 serv_puts(send_this);
467 strcpy(send_this, "");
470 for (i=55; i<67; ++i) {
471 if (send_this[i] == ' ') break;
473 strncpy(buf, send_this, i);
475 strcpy(send_this, &send_this[i]);
480 /** Unswap the sockets. */
482 WC->serv_sock = WC->chat_sock;
485 wprintf("<FORM METHOD=\"POST\" action=\"chat_send\" NAME=\"chatsendform\">\n");
486 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
487 wprintf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
488 "NAME=\"send_this\">\n", SIZ-10);
490 wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">\n", _("Send"));
491 wprintf("<INPUT TYPE=\"submit\" NAME=\"help_button\" VALUE=\"%s\">\n", _("Help"));
492 wprintf("<INPUT TYPE=\"submit\" NAME=\"list_button\" VALUE=\"%s\">\n", _("List users"));
493 wprintf("<INPUT TYPE=\"submit\" NAME=\"exit_button\" VALUE=\"%s\">\n", _("Exit"));
494 wprintf("</FORM>\n");
496 wprintf("</BODY></HTML>\n");
501 void ajax_send_instant_message(void) {
505 safestrncpy(recp, bstr("recp"), sizeof recp);
507 serv_printf("SEXP %s|-", recp);
508 serv_getln(buf, sizeof buf);
511 text_to_server(bstr("msg"));
515 escputs(buf); /* doesn't really matter what we return - the client ignores it */
523 WebcitAddUrlHandler(HKEY("display_page"), display_page, 0);
524 WebcitAddUrlHandler(HKEY("page_user"), page_user, 0);
525 WebcitAddUrlHandler(HKEY("chat"), do_chat, 0);
526 WebcitAddUrlHandler(HKEY("chat_recv"), chat_recv, 0);
527 WebcitAddUrlHandler(HKEY("chat_send"), chat_send, 0);
528 WebcitAddUrlHandler(HKEY("ajax_send_instant_message"), ajax_send_instant_message, AJAX);
533 SessionDestroyModule_CHAT
536 if (sess->chat_sock > 0)
537 close(sess->chat_sock);