From 1c27964c16ec7c01bf9ef377500d9a4afdd7d507 Mon Sep 17 00:00:00 2001 From: pBlueG Date: Fri, 2 May 2014 14:35:18 +0200 Subject: [PATCH] several fixes and code improvements --- classes/bot.class.php | 2 + classes/channel_priv.class.php | 2 +- classes/commandhandler.class.php | 127 ++++++++++++++++++++--------- configuration/networks/FoCoIRC.ini | 1 + plugins/auto_perform.plugin.php | 2 +- start.php | 24 +++--- 6 files changed, 106 insertions(+), 52 deletions(-) diff --git a/classes/bot.class.php b/classes/bot.class.php index b93d15f..933bb65 100644 --- a/classes/bot.class.php +++ b/classes/bot.class.php @@ -155,6 +155,8 @@ public function _EventHandler($in_buffer) $this->_sendCommand('PONG :'.substr($sSplit[1], 1)); $sIdent = substr($sSplit[0], 1); $ptr = Plugins::getInstance(); + if(is_null($sSplit[1])) + return; switch(@strtolower($sSplit[1])) { // let the events begin :o case 'notice': { diff --git a/classes/channel_priv.class.php b/classes/channel_priv.class.php index 3201015..0836437 100644 --- a/classes/channel_priv.class.php +++ b/classes/channel_priv.class.php @@ -115,7 +115,7 @@ public static function GetUserPrivilege($sUser, $sChannel) if(isset(self::$m_aChannels[$sChannel][$sUser])) return self::$m_aChannels[$sChannel][$sUser]; else - return false; + return LEVEL_NONE; } private static function _privilege_bit($offset = 0) diff --git a/classes/commandhandler.class.php b/classes/commandhandler.class.php index fcc5292..94cd05e 100644 --- a/classes/commandhandler.class.php +++ b/classes/commandhandler.class.php @@ -1,3 +1,4 @@ + Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !addcmd (command) (privilege) (phpcode)"); } else { - if($this->_command_exists($aParams[0])) { + $cmd = CommandHandler::getInstance(); + if($cmd->_commandExists($aParams[0])) { $bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] This command already exists."); } else { $code = trim(implode(" ", array_slice($aParams, 2))); $aError = array(); - if($this->_validateSyntax($code, $aError)) { - $this->_registerCommand($aParams[0], $code, $aParams[1]); + if($cmd->_validateSyntax($code, $aError)) { + $cmd->_registerCommand($aParams[0], $code, $aParams[1]); $bot->Say($sRecipient, "[b][color=green]Success:[/color][/b] The command [b]".$aParams[0]."[/b] has been succesfully added."); } else { $bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] You have an error in your php syntax:"); @@ -117,33 +119,27 @@ public function __construct() if(count($aParams) < 1) { $bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !delcmd (command)"); } else { - $key = NULL; - $key = (object)$key; - if(!$this->_command_exists($aParams[0], $key)) { - $bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] This command does not exists."); - } else { - unset($this->m_aCommands[$key->index]); - $aCmd = array("command" => $aParams[0]); - Database::getInstance()->_delete($this->m_sTable, $aCmd); - $bot->Say($sRecipient, "[b][color=green]Success:[/color][/b] The command has been successfully removed."); - $this->m_aCommands = array_values($this->m_aCommands); - } - unset($key); + $cmd = CommandHandler::getInstance(); + if($cmd->_unregisterCommand($aParams[0], true)) + $bot->Say($sRecipient, "[b][color=green]Success:[/color][/b] The command has been successfully removed."); + else + $bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] This command does not exists."); + unset($cmd); }', Privileges::LEVEL_BOT_ADMIN, 'Deletes a command from the database.' ); $this->_registerCommand( '!cmds', - 'if($bot->_isChild()) + 'if($bot->_isChild() || !Misc::isChannel($sRecipient)) return; - $sCmds = NULL; - foreach($this->m_aCommands as $sCmd) { - $sCmds .= $sCmd["command"].", "; - } - $sCmds = substr($sCmds, 0, -2); + if(Privileges::IsBotAdmin($sIdent)) + $priv = Privileges::LEVEL_BOT_ADMIN; + else + $priv = Privileges::GetUserPrivilege($sUser, $sRecipient); + $sCmds = CommandHandler::getInstance()->_listCommands($priv); $bot->Notice($sUser, "Commands: ".$sCmds);', - Privileges::LEVEL_BOT_ADMIN, + Privileges::LEVEL_NONE, 'Displays all registered commands.' ); $this->_registerCommand( @@ -257,9 +253,7 @@ public function __construct() unset($pDB); } - //public function _call_command($sCommand, $aParams, - - public function _command_exists($sCommand) + public function _commandExists($sCommand) { while($aCommand = current($this->m_aCommands)) { if(!strcasecmp($aCommand['command'], $sCommand)) { @@ -276,27 +270,80 @@ public function _command_exists($sCommand) return false; } - public function _registerCommand($sCommand, $sCode, $privilege, $description = NULL, $save_to_db = true) + public function _listCommands($privilege) + { + $iPermission = $this->_getPermission($privilege); + $sRet = NULL; + foreach($this->m_aCommands as $aCmd) { + if($aCmd['privilege'] <= $iPermission) + $sRet .= $aCmd['command'].', '; + } + if(strlen($sRet) > 0) + $sRet = substr($sRet, 0, -2); + return $sRet; + } + + private function _getPermission($privilege) { - if(!$this->_command_exists($sCommand)) { - if(!is_numeric($privilege)) { - $aPrivileges = array('0' => 0, '+' => 1, '%' => 2, '@' => 4, '&' => 8, '~' => 16, '*' => 1337); + $iPermission = Privileges::LEVEL_NONE; + if(!is_numeric($privilege)) { + $aPrivileges = array('0' => 0, '+' => 1, '%' => 2, '@' => 4, '&' => 8, '~' => 16, '*' => 1337); + if(!is_null($aPrivileges[$privilege])) $iPermission = $aPrivileges[$privilege]; - } else - $iPermission = $privilege; + } else + $iPermission = $privilege; + return $iPermission; + } + + public function _registerCallback($sCommand, $oClass, $sCallback, $privilege) + { + if(!$this->_commandExists($sCommand)) { + if(method_exists($oClass, $sCallback) && is_callable(array($oClass, $sCallback))) { + $this->m_aCommands[] = array( + 'command' => $sCommand, + 'code' => NULL, + 'class' => $oClass, + 'callback' => $sCallback, + 'privilege' => $this->_getPermission($privilege) + ); + return true; + } + } + return false; + } + + public function _registerCommand($sCommand, $sCode, $privilege, $description = NULL, $save_to_db = true) + { + if(!$this->_commandExists($sCommand)) { $finalCode = str_replace(array("\r", "\n", "\t"), "", $sCode); + $closure = create_function('$bot, $sCommand, $aParams, $sUser, $sRecipient, $sIdent', $finalCode); $this->m_aCommands[] = array( 'command' => $sCommand, 'code' => $finalCode, 'description' => $description, - 'privilege' => $iPermission + 'privilege' => $this->_getPermission($privilege) ); + end($this->m_aCommands); + $ret = key($this->m_aCommands); if($save_to_db) - Database::getInstance()->_insert($this->m_sTable, end($this->m_aCommands)); + Database::getInstance()->_insert($this->m_sTable, $this->m_aCommands[$ret]); + $this->m_aCommands[$ret]['closure'] = $closure; return true; } return false; } + + public function _unregisterCommand($sCommand, $db_reset = false) + { + $key = NULL; + $key = (object)$key; + if(!$this->_commandExists($sCommand, $key)) + return false; + unset($this->m_aCommands[$key->index]); + if($db_reset) + Database::getInstance()->_delete($this->m_sTable, array('command' => $sCommand)); + return true; + } public function _parse($bot, $sCommand, $aParams, $sUser, $sRecipient, $sIdent) { @@ -306,7 +353,7 @@ public function _parse($bot, $sCommand, $aParams, $sUser, $sRecipient, $sIdent) $RequiredPrivilege = $aCommand['privilege']; $bIsAdmin = Privileges::IsBotAdmin($sIdent); $bExecute = false; - if($RequiredPrivilege > 0 && !$bIsAdmin && $sRecipient[0] == '#') { + if($RequiredPrivilege > 0 && !$bIsAdmin && Misc::isChannel($sRecipient)) { //$UserPrivilege = Privileges::GetUserPrivilege($sUser, $sRecipient); switch($RequiredPrivilege) { case Privileges::LEVEL_VOICE: @@ -329,8 +376,14 @@ public function _parse($bot, $sCommand, $aParams, $sUser, $sRecipient, $sIdent) } } $bExists = true; - if($bExecute || !$RequiredPrivilege || $bIsAdmin) - eval($aCommand['code']); + if($bExecute || !$RequiredPrivilege || $bIsAdmin) { + if(array_key_exists('callback', $aCommand)) + call_user_func_array(array($aCommand['class'], $aCommand['callback']), array($bot, $sUser, $sRecipient, $aParams)); + else + //$aCommand['closure']($bot, $sCommand, $aParams, $sUser, $sRecipient, $sIdent); + call_user_func($aCommand['closure'], $bot, $sCommand, $aParams, $sUser, $sRecipient, $sIdent); + //eval($aCommand['code']); // evil eval! + } break; } } @@ -350,7 +403,7 @@ private function _loadCommands() return true; } - protected function _validateSyntax($sCode, &$errorReturn) + public function _validateSyntax($sCode, &$errorReturn) { $tempFile = time().".temp.php"; file_put_contents($tempFile, ""); diff --git a/configuration/networks/FoCoIRC.ini b/configuration/networks/FoCoIRC.ini index 6d77e7c..d095861 100644 --- a/configuration/networks/FoCoIRC.ini +++ b/configuration/networks/FoCoIRC.ini @@ -5,4 +5,5 @@ SSL_CRT = "ssl/mYsTeRy.crt" ; location of the ssl certificate Port = 6667 ; port to connect Servers[] = "foco.nl.irc.tl" ; server hosts Servers[] = "foco2.us.irc.tl" +Servers[] = "exnet.se.irc.tl" ;Servers[] = "irc.tl" \ No newline at end of file diff --git a/plugins/auto_perform.plugin.php b/plugins/auto_perform.plugin.php index 990c44d..d20aa3d 100644 --- a/plugins/auto_perform.plugin.php +++ b/plugins/auto_perform.plugin.php @@ -14,7 +14,7 @@ { public function __construct() { - echo ">> Auto Perform plugin called.".PHP_EOL; + echo ">> Auto Perform has been loaded.".PHP_EOL; } public function onBotConnect($bot) diff --git a/start.php b/start.php index 0bbb11c..fd6f502 100644 --- a/start.php +++ b/start.php @@ -28,23 +28,17 @@ require_once('classes/timer.class.php'); require_once('classes/commandhandler.class.php'); -/* -// soon to uncomment -foreach(glob('classes/*.class.php') as $filename) { - require_once($filename); -} -*/ +echo "Initializing mYsTeRy ".REVISION." ...". PHP_EOL; -$pIni = Ini::getInstance(); +$INI = Ini::getInstance(); +$g_aConfig = array( + 'General' => $INI->_getConfig('configuration/general.ini', 'General'), + 'Bots' => $INI->_getArrayConfig('bots', 'Bot'), + 'Networks' => $INI->_getArrayConfig('networks', 'Network'), +); CommandHandler::getInstance(); Database::getInstance(); Timer::getInstance(); - -$g_aConfig = array( - 'General' => $pIni->_getConfig('configuration/general.ini', 'General'), - 'Bots' => $pIni->_getArrayConfig('bots', 'Bot'), - 'Networks' => $pIni->_getArrayConfig('networks', 'Network'), -); $g_aNetworks = $g_aConfig['Networks']; unset($g_aConfig['Networks']); @@ -58,9 +52,13 @@ Log::Error('>> Invalid ident format -> '.$sAdmin); } +echo "General & Bot configuration has been loaded.". PHP_EOL; + $gHandler = Main::getInstance($g_aConfig, $g_aNetworks); $iSleep = $g_aConfig['General']['Sleep']; +echo "All required modules have been successfully registered.". PHP_EOL; + while(true) { $gHandler->_Run(); usleep($iSleep);