Documentation is available at class.Commands.inc
- <?php //-*-php-*-
- /* ******************************************************************** **
- ** Copyright notice **
- ** **
- ** (c) 1995-2003 PHPOpenChat Development Team **
- ** http://phpopenchat.sourceforge.net/ **
- ** **
- ** All rights reserved **
- ** **
- ** This script is part of the PHPOpenChat project. The PHPOpenChat **
- ** project is free software; you can redistribute it and/or modify **
- ** it under the terms of the GNU General Public License as published by **
- ** the Free Software Foundation; either version 2 of the License, or **
- ** (at your option) any later version. **
- ** **
- ** The GNU General Public License can be found at **
- ** http://www.gnu.org/copyleft/gpl.html. **
- ** A copy is found in the textfile GPL and important notices to the **
- ** license from the team is found in the textfile LICENSE distributed **
- ** with these scripts. **
- ** **
- ** This script is distributed in the hope that it will be useful, **
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
- ** GNU General Public License for more details. **
- ** **
- ** This copyright notice MUST APPEAR in all copies of the script! **
- ** ******************************************************************** */
- require_once('config.inc.php');
- require_once(POC_INCLUDE_PATH.'/definitions.inc');
- require_once(POC_INCLUDE_PATH.'/functions.inc');
- require_once(POC_INCLUDE_PATH.'/class.Response.inc');
- require_once(POC_INCLUDE_PATH.'/class.Line.inc');
- function _chatter_is_away($rec) {
- // $rec == string for now
- // returns false or away msg (maybe empty)
- if( $rec == '###EVERYBODY###') {
- return(false);
- }
- else {
- $_SESSION['chat']->connect();
- $away_msg = $_SESSION['chat']->is_away( $rec );
- $_SESSION['chat']->disconnect();
- return ($away_msg);
- }
- }
- /**
- * Checks the posted string and replaces the forbidden words with 'Ooops'
- *
- * @package phpopenchat
- * @param string $string
- * @param array $forbidden_strings
- * @return string
- */
- function check_chat( $string, $forbidden_strings ) {
- if(! is_array($forbidden_strings) ) die('forbidden_strings: Array expected!');
- if( count($forbidden_strings) == 0 ) return $string;
- $chars= '[\.\-\'\(\=\?\/\|)_,;:!"`§$%& ]';
- do{
- $search_string = chunk_split(current($forbidden_strings), 1, $chars.'*');
- $string = preg_replace('/'.$search_string.'/','<acronym title="'.$_SESSION['translator']->out('FORBIDDEN_WORD').'">...Ooops...</acronym>',$string);
- }while( next($forbidden_strings) );
- unset($search_string);
- unset($chars);
- return $string;
- }
- function NewLineFromBot () {
- $line =& new Line( null , "" );
- $bot =& new Chatter(strval(STATUS_BOT_NAME),'');
- $line->set_sender($bot);
- $line->set_channel( $_SESSION['channel']->get_name() );
- unset($bot);
- return ($line);
- }
- function _response_to_me ($msg = '' ) {
- $res=& new Response();
- $res->line = NewLineFromBot();
- $res->line->set_said($msg);
- $res->line->set_recipient($_SESSION['chatter']);
- // $res->line->set_approved();
- $res->show_local = true;
- return($res);
- }
- function _handle_away() {
- deb("entering handle_away", 'dataflow');
- if ( ! ( _chatter_is_away($_SESSION['chatter']->get_nick() ) === false ) ) {
- $res =& new Response();
- $res->line =& NewLineFromBot();
- $res->line->set_said( $_SESSION['translator']->out('HINT_YOU_ARE_AFK') );
- $res->line->set_recipient($_SESSION['chatter']);
- $res->line->set_approved();
- $res->comment='msg to '.$this->PlainSender.' that (s)he is afk';
- $res->show_local = true;
- return($res);
- }
- return(null);
- }
- function _channel_exists($channel) {
- /* // test if channel exists
- $channels = array();
- $_SESSION['chat']->connect();
- $channels = $_SESSION['chat']->get_channels();
- $_SESSION['chat']->disconnect();
- return ( in_array(strval($channel), $channels) );
- */
- $_SESSION['chat']->connect();
- $exist = $_SESSION['chat']->channel_exists($channel);
- $_SESSION['chat']->disconnect();
- return($exist);
- }
- /**
- * Implements a irc-like command within the chat
- *
- * @package phpopenchat
- */
- class Command {
- var $error = '';
- function Command () {
- }
- function get ($key) {
- if ( isset($this->$key) ) {
- return ($this->$key);
- }
- else {
- // return (undef);
- deb("key $key undefined");
- die("key $key undefined");
- }
- }
- function set($key, $val) {
- if (isset($this->$key)) {
- $this->$key = $val;
- }
- else {
- deb("key $key undefined");
- die("key $key undefined");
- // return(undef);
- }
- }
- }
- /**
- * Implements a irc-like command line within the chat
- *
- * @package phpopenchat
- */
- class _ChatlineCommand extends Command {
- var $from = '';
- var $to = '';
- var $channel = '';
- var $whispered = '';
- var $what = '';
- function _ChatlineCommand ( $from, $to, $channel, $what, $whispered ) {
- $this->from = $from;
- $this->to = $to;
- $this->channel = $channel;
- $this->what = $what;
- $this->whispered = $whispered;
- }
- function run () {
- deb("running _ChatlineCommand");
- $allres = array();
- // if line is empty do nothing.
- if (! $this->what) {
- $res =& _response_to_me('');
- $res->show_local = false;
- $res->show_remote = false;
- $res->comment='empty line';
- deb("empty line");
- return($res);
- }
- // Bug: for now "from" has to be the same as $_SESSION['chatter']->get_nick()
- // test if from and to are online
- $_SESSION['chat']->connect();
- if ( ! $_SESSION['chat']->is_online($this->from) || ! $this->from == $_SESSION['chatter']->get_nick() ) {
- // should not happen, but handle anyway
- deb("chatter is not online");
- die ("you are not online!");
- }
- if ( ! $_SESSION['chat']->is_online($this->to) ) {
- deb("to is not online");
- $res =& _response_to_me($this->to." ".$_SESSION['translator']->out('IS_NOT_ONLINE') );
- return($res);
- }
- $_SESSION['chat']->disconnect();
- // test if from is afk
- if ($res = _handle_away() ) {
- deb("got a response from handle_away");
- return($res);
- }
- // test if "to" is afk
- if (! ( $msg = _chatter_is_away($this->to) === false ) ) {
- deb("recipient is away");
- $res =& _response_to_me( $_SESSION['translator']->out('CHATTER_IS_AWAY'));
- $res->line->set_whispered();
- $res->comment='msg to '.$this->PlainSender.' that '.$this->PlainRecipient.' is away';
- $res->show_local = true;
- return($res);
- }
- // test if channel exists
- if (! _channel_exists($this->channel) ) {
- deb("channel does not exists");
- $res =& _response_to_me ( $_SESSION['translator']->out(CHANNEL) . ' '.
- $_SESSION['translator']->out(NOT_FOUND) );
- $res->comment="$comment channel does not exist, sending errormsg";
- return($res);
- }
- // authorized to speak in channel ?
- if( ! $_SESSION['chatter']->is_authorized_for( $this->channel )) {
- deb("chatter not authorized to speak");
- $res =& _response_to_me($_SESSION['translator']->out(NOT). ' '.
- $_SESSION['translator']->out(AUTHORIZED));
- $res->line->set_whispered();
- $res->comment='msg to '.$this->from.' that he is not authorized';
- $res->show_local = true;
- return($res);
- }
- deb("starting normal processing");
- $res =& new Response();
- $res->line =& new Line ( $_SESSION['chatter'], '' );
- $res->line->set_channel($this->channel);
- $recipient =& new Recipient( $this->to );
- $res->line->set_recipient( $recipient );
- unset ($recipient);
- $res->line->set_whispered( $this->whispered );
- $mytmp = $this->what;
- $mytmp = escape_html($mytmp);
- global $NONO_WORDS;
- $mytmp = check_chat( $mytmp, $NONO_WORDS);
- $mytmp = html_breaklines($mytmp);
- $mytmp = handle_adodb($mytmp);
- $res->line->set_said($mytmp);
- unset($mytmp);
- $res->line->filter_buffer_input();
- $res->line->set_accepted_mime_types($ACCEPTED_MIME_TYPES);
- // check if moderated
- $chnl = new Channel($this->channel);
- if (! $chnl->is_moderated() ) {
- // print line locally and in buffer
- $res->show_local = true;
- $res->show_remote = true;
- $res->comment = "normal line: ".$this->what;
- $allres[] = $res;
- }
- else {
- if ( $_SESSION['chatter']->is_vip() || $_SESSION['chatter']->is_moderator() ) {
- $res->line->set_approved();
- $res->comment = "line from moderator or vip: ".$this->what ;
- $res->show_local = true;
- $res->show_remote = true;
- $allres[] = $res;
- }
- else {
- // we are not moderator nore vip
- // first of all, look if a moderator is already present in the channel
- $_SESSION['chat']->connect();
- $moderators = $_SESSION['chat']->get_group_members("moderator", $this->channel );
- $chatters = $_SESSION['chat']->get_chatters($this->channel);
- $_SESSION['chat']->disconnect();
- $moderators_online = array();
- foreach ( $moderators as $mod ) {
- if ( in_array($mod, $chatters ) ) {
- $moderators_online[] = $mod;
- }
- }
- deb("moderators online: ".print_r($moderators_online,1));
- if ( empty($moderators_online ) ) {
- // send info msg that no moderator available
- unset ($res);
- $res =& _response_to_me($_SESSION['translator']->out('MODERATOR_NOT_HERE') );
- $res->show_local=true;
- $allres[] = $res;
- }
- else {
- // send line to moderator, send infomsg to chatter
- $res->comment = "line in moderated channel: ".$this->what;
- $res->show_remote = true;
- $res->show_local = false;
- $allres[] = $res;
- unset($res);
- $res =& _response_to_me( $_SESSION['translator']->out('MESSAGE_FORWARDED_TO_MODERATOR') );
- $res->comment='msg to '.$this->from.' that line is forwarded to moderator';
- $res->show_local = true;
- $res->show_remote = false;
- $res->line->set_whispered();
- $allres[] = $res;
- }
- }
- }
- unset($chnl);
- return($allres);
- }
- }
- /**
- * Implements the irc-like command "join"
- *
- * @package phpopenchat
- */
- class JoinCommand extends Command {
- var $channel = '';
- var $password = '';
- function JoinCommand ($channel = '', $password = '') {
- if ($channel == '') {
- $this->error=NO_CHANNEL_SELECTED;
- }
- $this->password = $password;
- $this->channel=$channel;
- }
- function run () {
- if ( $res = _handle_away() ) {
- return($res);
- }
- deb("entering run join");
- $comment = "/join ".$this->channel.":";
- if ($this->error || $_SESSION['channel']->get_name() == $this->channel ) {
- deb("channel is the same");
- // simply return an empty response that does nothing
- $res=& _response_to_me('');
- $res->comment="$comment no channel selected or channel is the same as before. Do nothing.";
- $res->show_local=false;
- return($res);
- }
- if ( ! _channel_exists($this->channel) ) {
- deb("channel does not exists");
- $res =& _response_to_me ( $_SESSION['translator']->out(ERROR_CHANNEL_UPDATE) );
- $res->comment="$comment channel does not exist, sending errormsg";
- return($res);
- }
- // maybe password is sent ?
- if ($this->password) {
- deb("password found:".$this->password);
- $chnl =& new Channel($this->channel);
- if (! $chnl ) {
- die ("new Channel failed. this should never happen!");
- }
- if ( $chnl->get_password() == $this->password ) {
- deb("joning channel with password");
- $_SESSION['chatter']->unlock_channel($this->channel);
- }
- else {
- deb("implement me: wrong password:".$this->password." for channel ".$this->channel);
- }
- unset($chnl);
- }
- if( $_SESSION['chatter']->is_authorized_for( $this->channel )) {
- // maybe we require to unlock channel every time user is joining
- // then this would lock the channel we are leaving
- // /*
- deb("yes, im authorized");
- if ( $_SESSION['channel']->is_password_protected() ) {
- $_SESSION['chatter']->lock_channel($_SESSION['channel']->get_name());
- }
- // */
- $_SESSION['reload_count'] = 0; //reset chat session expiration time
- $_SESSION['chatter']->join_channel( $this->channel );
- $res =& _response_to_me('');
- $res->comment="$comment succeeded. Channel switched";
- $res->show_local=true;
- $res->show_remote = false;
- }
- else {
- deb("not authorized");
- $res =& _response_to_me('');
- $res->show_local=false;
- $res->show_remote=false;
- $res->set_channel_login(true);
- $res->line->set_channel($this->channel);
- $res->comment="$comment is locked, display login form";
- return($res);
- }
- return($res);
- }
- }
- /**
- * Implements the irc-like command "roll"
- *
- * Roll means the is a dies game where a user can roll dies
- *
- * @package phpopenchat
- */
- class RollCommand extends Command {
- var $num;
- function RollCommand ( $num = 1 ) {
- if ( $num == "" ) $num = 1;
- if (! is_numeric($num) ) {
- $this->error = WRONG_PARAMETER;
- }
- $num=(integer) $num;
- if ($num > 6) $num=6;
- $this->num = $num;
- }
- function run () {
- if ( $res = _handle_away() ) {
- return($res);
- }
- $htmlhead = '<img src="'.$_SESSION['template']->get_tmpl_web_path().'/images/icons/dice_';
- $htmltail = '.png" align="middle" alt="'.$_SESSION['translator']->out('DIE').'" width="16" height="16" border="0" /> ';
- for ($i=1;$i<=$this->num;$i++) {
- $html.=$htmlhead.rand(1,6).$htmltail;
- }
- $response =& new Response();
- $response->comment="showing ".$this->num." dices";
- $response->show_remote = true;
- $response->line =& NewLineFromBot();
- $response->line->set_said($html);
- $response->line->set_recipient($_SESSION['chatter']);
- return($response);
- }
- }
- /**
- * Implements the irc-like command "show buddy"
- *
- * @package phpopenchat
- */
- class ShowbuddyCommand extends Command {
- function ShowbuddyCommand () {
- }
- function run() {
- if ( $res = _handle_away() ) {
- return($res);
- }
- $friends = $_SESSION['chatter']->get_friends();
- $friends_list = '';
- foreach ($friends as $friend) {
- $friends_list .= ', '.$friend;
- }
- unset($friends);
- unset($friend);
- define('_CHATTER_BUDDY_LIST', substr($friends_list, 2));
- unset($friends_list);
- $res =& new Response();
- $res->comment='showing buddies of '.$_SESSION['chatter']->get_nick();
- $res->line =& NewLineFromBot();
- $res->line->set_whispered();
- $res->line->set_recipient($_SESSION['chatter']);
- //get template of inline-help
- $content = $_SESSION['chat']->get_template('inline_boddies', true);
- $res->line->set_said($content);
- unset($content);
- $res->show_local = true;
- $res->show_remote = false;
- return($res);
- }
- }
- /**
- * Implements the irc-like command "msg"
- *
- * @package phpopenchat
- */
- class MsgCommand extends Command {
- /* IRC-command
- * /msg <nickname> <line content>
- * to whisper to a chatter
- */
- var $nickname = '';
- var $content = '';
- function MsgCommand ( $nickname = '' , $irc = array() ) {
- if ( ! $nickname or ! is_array($irc) ) {
- $this->error = WRONG_PARAMETER;
- }
- $this->nickname = $nickname;
- $this->content = implode (" ", $irc);
- $this->content = preg_replace('#msg *'.$nickname.' *#', '', $this->content );
- }
- function run () {
- deb("running msg command with params: ".$this->nickname." ".$this->content);
- if ( $res = _handle_away() ) {
- return($res);
- }
- $_SESSION['chat']->connect();
- $recipient_channel = $_SESSION['chat']->get_channel_of($this->nickname);
- $_SESSION['chat']->disconnect();
- if( ! $recipient_channel ) {
- $res =& _response_to_me($this->nickname.' '.$_SESSION['translator']->out('IS_NOT_ONLINE'));
- $res->comment='displaying a message that chatter is not online';
- return($res);
- }
- $allres=array();
- // create msg to chatter
- $res = _response_to_me($_SESSION['translator']->out('LINE_HAS_BEEN_SENT_TO').' '.
- $this->nickname.' '.
- '['.$recipient_channel.']'
- );
- $res->comment="displaying line that line is sent";
- $allres[] = $res;
- unset($res);
- $cl =& new _ChatlineCommand ( $_SESSION['chatter']->get_nick(),
- $this->nickname,
- $recipient_channel,
- $this->content,
- true ) ;
- $res = $cl->run();
- if (is_array($res))
- $allres = array_merge($allres, $res );
- else
- $allres[] = $res;
- return($allres);
- }
- }
- /**
- * Implements the irc-like command "plain line"
- *
- * @package phpopenchat
- */
- class PlainlineCommand extends Command {
- // var $line=null;
- var $PlainSender = '';
- var $PlainRecipient = '';
- var $PlainChannel = '';
- var $PlainLine = '';
- var $PlainWhispered = 'off';
- var $channel_password = '';
- function PlainlineCommand($icl) {
- /* set the recipient. we have two (or three) possibilities:
- 1. recipient is set in InputChatline (as part of $_POST)
- 2. recipient is set on the textline like "nickname: line" (overrides POST if nick is in chann
- el )
- 3. recipient is not set at all
- */
- if (! $icl->recipient ) $icl->recipient = '###EVERYBODY###';
- preg_match ( '#^([[:alpha:]]+[[:alnum:]]*[^:]*)(: ?)(.*)$#i', $icl->line, $recipient_from_line )
- ;
- if( count($recipient_from_line) > 0 ) {
- if( $line_recipient = $_SESSION['channel']->is_attendant($recipient_from_line[1],true) ) {
- $icl->line = str_replace($recipient_from_line[1].$recipient_from_line[2],'',$icl->line );
- $icl->recipient = $line_recipient;
- }
- }
- $this->PlainRecipient = $icl->recipient;
- $this->PlainSender = $_SESSION['chatter']->get_nick();
- $this->PlainLine = $icl->line;
- $this->PlainChannel = $icl->channel ? $icl->channel : CHANNEL_SELECTED ;
- $this->PlainWhispered = $icl->whispered ? $icl->whispered : 'off';
- $this->channel_password = $icl->channel_password;
- }
- function run() {
- deb("running PlainlineCommand", 'dataflow');
- $allres = array(); // array of responses if set
- if ( $_SESSION['channel']->get_name() != $this->PlainChannel ) {
- $join =& new JoinCommand($this->PlainChannel, $this->channel_password);
- $allres[] = $join->run();
- unset ($join);
- }
- $cmd =& new _ChatlineCommand($this->PlainSender, $this->PlainRecipient, $this->PlainChannel,
- $this->PlainLine,
- $this->PlainWhispered == 'on' ?