Skip to content

Commit

Permalink
several fixes and code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pBlueG committed May 2, 2014
1 parent d82472b commit 1c27964
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 52 deletions.
2 changes: 2 additions & 0 deletions classes/bot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down
2 changes: 1 addition & 1 deletion classes/channel_priv.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
127 changes: 90 additions & 37 deletions classes/commandhandler.class.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<?php

/**
Expand Down Expand Up @@ -92,13 +93,14 @@ public function __construct()
if(count($aParams) < 3) {
$bot->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:");
Expand All @@ -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(
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)
{
Expand All @@ -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:
Expand All @@ -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;
}
}
Expand All @@ -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, "<?php ".$sCode." ?>");
Expand Down
1 change: 1 addition & 0 deletions configuration/networks/FoCoIRC.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion plugins/auto_perform.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 11 additions & 13 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand All @@ -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);
Expand Down

0 comments on commit 1c27964

Please sign in to comment.