Back to index

webcit  8.12-dfsg
roomchat.c
Go to the documentation of this file.
00001 /*
00002  * This module handles multiuser chat.
00003  *
00004  * Copyright (c) 1996-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License, version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "webcit.h"
00016 #include "webserver.h"
00017 
00018 /*
00019  * Display the screen containing multiuser chat for a room.
00020  */
00021 void do_chat(void)
00022 {
00023        char buf[256];
00024 
00025        WC->last_chat_seq = 0;
00026        WC->last_chat_user[0] = 0;
00027 
00028        output_headers(1, 1, 1, 0, 0, 0);
00029        do_template("roomchat");
00030 
00031        serv_puts("RCHT enter");
00032        serv_getln(buf, sizeof buf);
00033 
00034        wDumpContent(1);
00035 }
00036 
00037 
00038 /*
00039  * Receiving side of the chat window.  
00040  * This does JavaScript writes to
00041  * other divs whenever it refreshes and finds new data.
00042  */
00043 void chat_recv(void) {
00044        char buf[SIZ];
00045        char cl_user[SIZ];
00046 
00047        serv_printf("RCHT poll|%d", WC->last_chat_seq);
00048        serv_getln(buf, sizeof buf);
00049        if (buf[0] == '1') {
00050               WC->last_chat_seq = extract_int(&buf[4], 0);
00051               extract_token(cl_user, &buf[4], 2, '|', sizeof cl_user);
00052 
00053               /* who is speaking ... */
00054               if (strcasecmp(cl_user, WC->last_chat_user)) {
00055                      wc_printf("<br>\n");
00056                      if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
00057                             wc_printf("<span class=\"chat_myname_class\">");
00058                      }
00059                      else {
00060                             wc_printf("<span class=\"chat_notmyname_class\">");
00061                      }
00062                      escputs(cl_user);
00063                      strcpy(WC->last_chat_user, cl_user);
00064 
00065                      wc_printf(": </span>");
00066               }
00067               else {
00068                      wc_printf("&nbsp;&nbsp;&nbsp;");
00069               }
00070 
00071               /* what did they say ... */
00072               wc_printf("<span class=\"chat_text_class\">");
00073               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00074                      escputs(buf);
00075               }
00076               wc_printf("<br></span>\n");
00077        }
00078 }
00079 
00080 
00081 /*
00082  * This is the sending side of the chat window.  The form is designed to transmit asynchronously.
00083  */
00084 void chat_send(void) {
00085        char send_this[SIZ];
00086        char buf[SIZ];
00087 
00088        begin_ajax_response();
00089 
00090        if (havebstr("send_this")) {
00091               strcpy(send_this, bstr("send_this"));
00092        }
00093        else {
00094               strcpy(send_this, "");
00095        }
00096 
00097        if (havebstr("exit_button")) {
00098               strcpy(send_this, "/quit");
00099        }
00100 
00101        if (!IsEmptyStr(send_this)) {
00102               serv_puts("RCHT send");
00103               serv_getln(buf, sizeof buf);
00104               if (buf[0] == '4') {
00105                      text_to_server(send_this);
00106                      serv_puts("000");
00107               }
00108        }
00109        end_ajax_response();
00110 }
00111 
00112 
00113 /*
00114  * wholist for chat
00115  */
00116 void chat_rwho(void) {
00117        char buf[1024];
00118 
00119        serv_puts("RCHT rwho");
00120        serv_getln(buf, sizeof buf);
00121        if (buf[0] == '1') {
00122               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00123                      if (!strcasecmp(buf, ChrPtr(WC->wc_fullname))) {
00124                             wc_printf("<span class=\"chat_myname_class\">");
00125                      }
00126                      else {
00127                             wc_printf("<span class=\"chat_notmyname_class\">");
00128                      }
00129                      wc_printf("<img src=\"static/webcit_icons/essen/16x16/chat.png\">");
00130                      escputs(buf);
00131                      wc_printf("</span><br>\n");
00132               }
00133        }
00134 }
00135 
00136 
00137 /*
00138  * advise the Citadel server that the user is navigating away from the chat window
00139  */
00140 void chat_exit(void) {
00141        char buf[1024];
00142 
00143        serv_puts("RCHT exit");
00144        serv_getln(buf, sizeof buf);              /* Throw away the server reply */
00145 }
00146 
00147 
00148 
00149 void 
00150 InitModule_ROOMCHAT
00151 (void)
00152 {
00153        WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
00154        WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, AJAX);
00155        WebcitAddUrlHandler(HKEY("chat_rwho"), "", 0, chat_rwho, AJAX);
00156        WebcitAddUrlHandler(HKEY("chat_exit"), "", 0, chat_exit, AJAX);
00157        WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
00158 }
00159 
00160 
00161 void 
00162 SessionDestroyModule_ROOMCHAT
00163 (wcsession *sess)
00164 {
00165        /* nothing here anymore */
00166 }