Skip to content

Commit

Permalink
Command update
Browse files Browse the repository at this point in the history
- all commands have been moved to a custom plugin
/plugins/commands.plugin.class
  • Loading branch information
pBlueG committed May 21, 2016
1 parent 9ae65e4 commit 1583a7b
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 85 deletions.
4 changes: 2 additions & 2 deletions classes/bot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ public function _update()

public function __call($method, $args)
{
if(method_exists('Commands', $method)) {
if(method_exists('RawCommands', $method)) {
try {
// allows us to call non-existing methods: $bot->Say() $bot->Notice() etc.
$pMethod = new ReflectionMethod('Commands', $method);
$pMethod = new ReflectionMethod('RawCommands', $method);
$sCommand = $pMethod->invokeArgs(NULL, $args);
$this->_sendCommand($sCommand);
unset($pMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @version 2.0a
*/

Class Commands implements ColorCodes
Class RawCommands implements ColorCodes
{
private static $m_aReplaceBB =
array (
Expand Down
211 changes: 211 additions & 0 deletions plugins/commands.plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

/**
* Administration commands
*/

Class Commands
{
private $Handler;

public function __construct()
{
$this->Handler = CommandHandler::getInstance();
$this->Handler
->_registerCommand('!cmds', $this, 'cmds', Privileges::LEVEL_NONE, 'Displays a list of all commands unlocked for you.')
->_registerCommand('!join', $this, 'join', Privileges::LEVEL_BOT_ADMIN, 'Joins the given channel.')
->_registerCommand('!part', $this, 'part', Privileges::LEVEL_BOT_ADMIN, 'Leaves the given channel.')
->_registerCommand('!quit', $this, 'quit', Privileges::LEVEL_BOT_ADMIN, 'Disconnects the bot from the network.')
->_registerCommand('!!', $this, 'boteval', Privileges::LEVEL_BOT_ADMIN, 'Evaluates PHP code.')
->_registerCommand('!addcmd', $this, 'addcmd', Privileges::LEVEL_BOT_ADMIN, 'Adds a custom command.')
->_registerCommand('!delcmd', $this, 'delcmd', Privileges::LEVEL_BOT_ADMIN, 'Deletes a custom made command.')
->_registerCommand('!load', $this, 'load', Privileges::LEVEL_BOT_ADMIN, 'Loads a plugin.')
->_registerCommand('!unload', $this, 'unload', Privileges::LEVEL_BOT_ADMIN, 'Unloads a plugin.')
->_registerCommand('!plugins', $this, 'plugins', Privileges::LEVEL_BOT_ADMIN, 'Displays a list of all active plugin instances.')
->_registerCommand('!ident', $this, 'ident', Privileges::LEVEL_NONE, 'Shows your current ident.')
->_registerCommand('!mem', $this, 'mem', Privileges::LEVEL_NONE, 'Shows the bots current memory usage.')
->_registerCommand('!uptime', $this, 'uptime', Privileges::LEVEL_NONE, 'Shows how long the bot has been up.');
echo '>> Commands plugin has been loaded.' . PHP_EOL;
}

public function cmds($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(Privileges::IsBotAdmin($sIdent))
$priv = Privileges::LEVEL_BOT_ADMIN;
else
$priv = Privileges::GetUserPrivilege($sUser, $sRecipient);
$bot->Notice($sUser, 'Commands: '.$this->Handler->_listCommands($priv));
}

public function join($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(count($aParams) < 1)
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !join (#channel) [key]");
else
if(isset($aParams[1]))
$bot->Join($aParams[0], $aParams[1]);
else
$bot->Join($aParams[0]);
}

public function part($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(count($aParams) < 1)
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !part (#channel) [part message]");
else
if(isset($aParams[1]))
$bot->Part($aParams[0], $aParams[1]);
else
$bot->Part($aParams[0]);
}

public function quit($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(is_array($aParams))
$bot->Quit(implode(" ", $aParams));
else
$bot->Quit();
}

public function boteval($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(is_array($aParams)) {
$sEval = implode(" ", $aParams);
ob_start();
eval($sEval);
$sReturn = ob_get_contents();
ob_end_flush();
$aReturn = array_filter(explode("\n", $sReturn));
foreach($aReturn as $sEcho)
$bot->Say($sRecipient, trim($sEcho));
} else {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !! (code)");
}
}

public function addcmd($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(count($aParams) < 3) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !addcmd (command) (privilege) (phpcode)");
} else {
if($this->Handler->_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->Handler->_validateSyntax($code, $aError)) {
$this->Handler->_saveCommand($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:");
foreach($aError as $sError)
$bot->Say($sRecipient, trim($sError));
}
}
}
}

public function delcmd($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(count($aParams) < 1) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !delcmd (command)");
} else {
if($this->Handler->_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.");
}
}

public function load($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(count($aParams) < 1) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !load (plugin name)");
} else {
$ptr = Plugins::getInstance();
$sPlugin = $aParams[0];
if($ptr->_plugin_exists($sPlugin)) {
if(!$ptr->_isLoaded($sPlugin)) {
$sError = NULL;
try {
$ptr->_load($sPlugin);
} catch (Exception $e) {
$sError = $e->getMessage();
$sError = str_replace(array("\n", "\r", "\t"), "", $sError);
}
if(is_null($sError))
$bot->Say($sRecipient, "[b][color=green]Success:[/color][/b] The plugin `".$sPlugin."` has been successfully loaded.");
else {
$bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] An error has occured:");
$bot->Say($sRecipient, $sError);
}
} else {
$bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] This plugin is already loaded. Use !unload ".$sPlugin.".");
}
} else {
$bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] `".$sPlugin."` does not exist.");
}
}
unset($ptr);
}

public function unload($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
if(count($aParams) < 1) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !unload (plugin name)");
} else {
$ptr = Plugins::getInstance();
$sPlugin = $aParams[0];
if($ptr->_unload($sPlugin))
$bot->Say($sRecipient, "[b][color=green]Success:[/color][/b] `".$sPlugin."` has been successfully unloaded.");
else
$bot->Say($sRecipient, "[b][color=red]Error:[/color][/b] `".$sPlugin."` is not loaded.");
unset($ptr);
}
}

public function plugins($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
$sPlugins = NULL;
$ptr = Plugins::getInstance();
reset($ptr->m_aPlugins);
while(current($ptr->m_aPlugins)) {
$sPlugins .= key($ptr->m_aPlugins). ".class.php, ";
next($ptr->m_aPlugins);
}
reset($ptr->m_aPlugins);
$sPlugins = substr($sPlugins, 0, -2);
$bot->Say(
$sRecipient,
"[b]Active plugin instances:[/b] ".$sPlugins
);
}

public function ident($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
$bot->Notice(
$sUser,
"Your ident is ".$sIdent
);
}

public function mem($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
$bot->Say(
$sRecipient,
"Current memory usage is [b]".Misc::formatBytes(memory_get_usage(), "MB")."[/b]"
);
}

public function uptime($bot, $sUser, $sRecipient, $aParams, $sIdent)
{
$sUptime = time() - $bot->m_aPing["Uptime"];
$bot->PM(
$sRecipient,
"I have been up for ".Misc::SecondsToString($sUptime)
);
}

}

?>
81 changes: 0 additions & 81 deletions plugins/test.plugin.php

This file was deleted.

2 changes: 1 addition & 1 deletion start.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
require_once('classes/log.class.php');
set_error_handler('Log::DebugHandler', E_ALL);
require_once('classes/privileges.class.php');
require_once('classes/commands.class.php');
require_once('classes/rawcommands.class.php');
require_once('classes/misc.class.php');
require_once('classes/main.class.php');
require_once('classes/bot.class.php');
Expand Down

0 comments on commit 1583a7b

Please sign in to comment.