Skip to content

Commit

Permalink
Some general tweaks/fixes/additions
Browse files Browse the repository at this point in the history
Started with the SA-MP Echo module
  • Loading branch information
pBlueG committed Mar 2, 2014
1 parent 92aa03a commit cee67ca
Show file tree
Hide file tree
Showing 27 changed files with 362 additions and 97 deletions.
26 changes: 21 additions & 5 deletions classes/bot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @version 2.0a
*/

Class Bot extends Main implements RawEvents, ColorCodes
Class Bot implements RawEvents, ColorCodes
{
/**
* This array stores the bot information
Expand Down Expand Up @@ -153,8 +153,6 @@ public function _EventHandler($in_buffer)
$sSplit = explode(' ', $in_buffer);
if(strtolower($sSplit[0]) == 'ping')
$this->_sendCommand('PONG :'.substr($sSplit[1], 1));
if($this->_isChild())
return; // childs shouldnt process data besides playing ping pong
$sIdent = substr($sSplit[0], 1);
$ptr = Plugins::getInstance();
switch(@strtolower($sSplit[1])) {
Expand All @@ -166,7 +164,6 @@ public function _EventHandler($in_buffer)
$this->m_aPing['LastHit'] = time();
continue;
}
//$ptr->_triggerEvent($this, "onNotice", $user, $msg)
$ptr->_triggerEvent($this, "onBotNotice", $sUser, $sMessage, $sIdent);
break;
}
Expand Down Expand Up @@ -198,7 +195,7 @@ public function _EventHandler($in_buffer)
$ptr->_triggerEvent($this, "onChannelMessage", $sChannel, $sUser, $sMessage, $sIdent);
} else {
$sCheck = explode(' ', $sMessage);
if(!strcasecmp($sCheck[0], 'login')) {
if(!strcasecmp($sCheck[0], 'login'))
if(!strcmp($this->m_aSettings['AdminPass'], implode(' ', array_slice($sCheck, 1))))
Privileges::AddBotAdmin($sIdent);
$ptr->_triggerEvent($this, "onPrivateMessage", $sUser, $sMessage, $sIdent);
Expand All @@ -213,13 +210,17 @@ public function _EventHandler($in_buffer)
break;
}
case 'join': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sChannel = substr($sSplit[2], 1);
Privileges::AddUser($sChannel, $sUser);
$ptr->_triggerEvent($this, "onChannelJoin", $sChannel, $sUser, $sIdent);
break;
}
case 'part': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sChannel = $sSplit[2];
if(count($sSplit) > 3)
Expand All @@ -231,6 +232,8 @@ public function _EventHandler($in_buffer)
break;
}
case 'kick': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sChannel = $sSplit[2];
$sVictim = $sSplit[3];
Expand All @@ -239,6 +242,8 @@ public function _EventHandler($in_buffer)
break;
}
case 'mode': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sChannel = $sSplit[2];
$sMode = $sSplit[3]; // +b/+l/+i etc
Expand All @@ -251,27 +256,35 @@ public function _EventHandler($in_buffer)
break;
}
case 'nick': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sNew = substr($sSplit[2], 1);
Privileges::RenameUser($sUser, $sNew);
$ptr->_triggerEvent($this, "onNickChange", $sUser, $sNew, $sIdent);
break;
}
case 'quit': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sMessage = substr(implode(' ', array_slice($sSplit, 2)), 1);
Privileges::RemoveUser(NULL, $sUser);
$ptr->_triggerEvent($this, "onUserQuit", $sUser, $sMessage, $sIdent);
break;
}
case 'topic': {
if($this->_isChild())
return;
$sUser = substr($sSplit[0], 1, strpos($sSplit[0], '!')-1);
$sChannel = $sSplit[2];
$sTopic = substr(implode(' ', array_slice($sSplit, 3)), 1);
$ptr->_triggerEvent($this, "onChannelTopic", $sChannel, $sUser, $sTopic, $sIdent);
break;
}
case self::NAMES_LIST: {
if($this->_isChild())
return;
$sChannel = $sSplit[4];
$sSplit[5] = substr($sSplit[5], 1);
$aUsers = array_slice($sSplit, 5);
Expand All @@ -295,10 +308,13 @@ public function _EventHandler($in_buffer)
foreach($this->m_aBotInfo['Channels'] as $Channel)
$this->_sendCommand(Commands::Join($Channel));
$this->m_bIsConnected = true;
$ptr->_triggerEvent($this, "onBotConnect");
}
break;
}
default: {
if($this->_isChild())
return;
if(is_numeric($sSplit[1])) {
$sSplit[3] = substr($sSplit[3], 1);
$ptr->_triggerEvent($this, "onRawEvent", $sSplit[1], array_slice($sSplit, 3), $sIdent);
Expand Down
40 changes: 29 additions & 11 deletions classes/commandhandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ public function __construct()
);
$this->_registerCommand(
'!!',
'if(is_array($aParams)) {
'if($bot->_isChild())
return;
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)));
$bot->Say($sRecipient, trim($sEcho));
} else {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !! (code)");
}',
Expand All @@ -85,7 +87,9 @@ public function __construct()
);
$this->_registerCommand(
'!addcmd',
'if(count($aParams) < 3) {
'if($bot->_isChild())
return;
if(count($aParams) < 3) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !addcmd (command) (privilege) (phpcode)");
} else {
if($this->_command_exists($aParams[0])) {
Expand All @@ -108,7 +112,9 @@ public function __construct()
);
$this->_registerCommand(
'!delcmd',
'if(count($aParams) < 1) {
'if($bot->_isChild())
return;
if(count($aParams) < 1) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !delcmd (command)");
} else {
$key = NULL;
Expand All @@ -129,7 +135,9 @@ public function __construct()
);
$this->_registerCommand(
'!cmds',
'$sCmds = NULL;
'if($bot->_isChild())
return;
$sCmds = NULL;
foreach($this->m_aCommands as $sCmd) {
$sCmds .= $sCmd["command"].", ";
}
Expand All @@ -140,7 +148,9 @@ public function __construct()
);
$this->_registerCommand(
'!load',
'if(count($aParams) < 1) {
'if($bot->_isChild())
return;
if(count($aParams) < 1) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !load (plugin name)");
} else {
$ptr = Plugins::getInstance();
Expand Down Expand Up @@ -173,7 +183,9 @@ public function __construct()
);
$this->_registerCommand(
'!unload',
'if(count($aParams) < 1) {
'if($bot->_isChild())
return;
if(count($aParams) < 1) {
$bot->Say($sRecipient, "[b][color=red]Syntax:[/color][/b] !unload (plugin name)");
} else {
$ptr = Plugins::getInstance();
Expand All @@ -189,7 +201,9 @@ public function __construct()
);
$this->_registerCommand(
'!plugins',
'$sPlugins = NULL;
'if($bot->_isChild())
return;
$sPlugins = NULL;
$ptr = Plugins::getInstance();
reset($ptr->m_aPlugins);
while(current($ptr->m_aPlugins)) {
Expand All @@ -207,7 +221,9 @@ public function __construct()
);
$this->_registerCommand(
'!ident',
'$bot->PM(
'if($bot->_isChild())
return;
$bot->PM(
$sUser,
"Your ident is [i]".$sIdent."[/i]"
);',
Expand All @@ -216,7 +232,9 @@ public function __construct()
);
$this->_registerCommand(
'!mem',
'$bot->Say(
'if($bot->_isChild())
return;
$bot->Say(
$sRecipient,
"Current memory usage is [b]".Misc::formatBytes(memory_get_usage(), "MB")."[/b]"
);',
Expand All @@ -226,7 +244,7 @@ public function __construct()
$this->_registerCommand(
'!uptime',
'$sUptime = time() - $bot->m_aPing["Uptime"];
$bot->Say(
$bot->PM(
$sRecipient,
"I have been up for ".Misc::SecondsToString($sUptime)
);',
Expand Down
14 changes: 14 additions & 0 deletions classes/commands.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public static function Quit()
$sCommand .= func_get_arg(0);
return $sCommand;
}

public static function Kick($channel, $user, $reason = NULL)
{
$sCommand = 'KICK '.$channel.' '.$user.' :';
if(!is_null($reason))
$sCommand .= $reason;
return $sCommand;
}

public static function Ban($channel, $user)
{
$sCommand = 'MODE '.$channel.' +b '.$user;
return $sCommand;
}
}

?>
7 changes: 3 additions & 4 deletions classes/log.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

public final function Error($log)
{
if($this->m_bLog)
return file_put_contents( 'logs/error.log', $log . PHP_EOL, FILE_APPEND );
return file_put_contents( 'logs/error.log', $log . PHP_EOL, FILE_APPEND );
}

public final function DebugHandler($errno, $errstr, $errfile, $errline, $errcontext)
Expand Down Expand Up @@ -49,9 +48,9 @@ public final function DebugHandler($errno, $errstr, $errfile, $errline, $errcont
}


public final function Log($filename, $data)
public final function _Log($filename, $data, $timestamp = NULL)
{
return file_put_contents('logs/'.$filename.'.log', $data . PHP_EOL, FILE_APPEND);
return file_put_contents($filename.'.log', (is_null($timestamp) ? $data : $timestamp.$data) . PHP_EOL, FILE_APPEND);
}
}

Expand Down
17 changes: 4 additions & 13 deletions classes/main.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* @version 2.0
*/

Class Main
Class Main extends Singleton
{
/**
* This array stores the bot instances
*
* @var array
* @access private
*/
private $m_aBots = array();
protected $m_aBots = array();

/**
* This array stores the network data
Expand Down Expand Up @@ -87,7 +87,6 @@ public function __construct($aConfig, $aNetworks)
$this->_initPlugins($this->m_aSettings['Plugins']);
$this->m_pTimer = Timer::getInstance();
$this->m_pTimer->_add($this, "_pingCheck", 0, $this->m_aSettings['Ping'], true);
Plugins::getInstance()->_triggerEvent($this, "onBotConnect");
}

/**
Expand Down Expand Up @@ -195,23 +194,15 @@ public function _pingCheck()
{
$iCurrent = time();
foreach($this->m_aBots as $pFamilyMember) {
echo "Hi Loop";
if($pFamilyMember->_isConnected()) {
if(($iCurrent-$pFamilyMember->_getLastPing()) > ($this->m_aSettings['Ping']+15)) {
// TODO
// bot has most likely timed out
echo "Bot has died. Goodbye my friend o/".PHP_EOL;
}
}
}
}

/**
* This function will be used to pass a bot object to the plugins
* @return object
*/
protected function _get_random_bot_obj($onlychilds = false)
{
// TODO
}
}


Expand Down
12 changes: 12 additions & 0 deletions classes/misc.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

Class Misc
{
// formats seconds to a readable string e.g. 5 days, 2 hours, 10 minutes and 10 seconds
public function SecondsToString($seconds)
{
$sFormat = NULL;
Expand Down Expand Up @@ -44,6 +45,16 @@ public function SecondsToString($seconds)
return $sFormat;
}

public function glueParams($string, $delimiter = ' ')
{
return explode($delimiter, $string);
}

public function isChannel($string)
{
return (substr($string, 0, 1) == '#' && strlen($string) > 1);
}

// (c) http://www.if-not-true-then-false.com/2009/format-bytes-with-php-b-kb-mb-gb-tb-pb-eb-zb-yb-converter/
public function formatBytes($bytes, $unit = "", $decimals = 2)
{
Expand All @@ -62,6 +73,7 @@ public function formatBytes($bytes, $unit = "", $decimals = 2)
return sprintf('%.' . $decimals . 'f '.$unit, $value);
}


}

?>
1 change: 1 addition & 0 deletions classes/plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function _load($plugin_name)
}
/*$szContent = preg_replace('/(\/\*)(.*?)(\*\/)/isU', '', $szContent);
$szContent = preg_replace('/^(\/\/)(.*?)$/m', '', $szContent);*/
file_put_contents('abc.txt', $szContent);
// This is a bit tricky, we will have to evaluate the class
eval(
$szContent . '
Expand Down
2 changes: 2 additions & 0 deletions classes/security.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @version 2.0a
*/

//TODO

Class Security
{

Expand Down
2 changes: 1 addition & 1 deletion configuration/networks/FoCoIRC.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Network]
Name = FoCoIRC
SSL = true
SSL_CRT = "ssl_cert/mYsTeRy.crt"
SSL_CRT = "ssl/mYsTeRy.crt"
Port = 6667
Servers[] = "foco.nl.irc.tl"
Servers[] = "foco2.us.irc.tl"
Expand Down
Loading

0 comments on commit cee67ca

Please sign in to comment.