Source code raw phps
<?php
require_once 'modules/class.Module.php';
class Module_Auth extends Module {
protected $_restrictedCommands = array('!right' => 4,
'!dumpRights' => 4,
'!flushRights' => 4,
'!saveRights' => 4,
'!talk' => 2,
'!+' => 2,
'!search' => 2,
'!learn' => 8,
'!control' => 4);
protected $_grantedUsers = array();
public function __construct()
{
$this->_loadPrivileges();
}
public function __destruct()
{
$this->_savePrivileges();
}
protected function _loadPrivileges()
{
$this->_grantedUsers = array();
// load granted users
$db = DB::instance();
$results = $db->execute('SELECT `mask`, `rights` FROM users');
if ($results instanceof DB_Common_Results) {
foreach($results as $result) {
$this->_grantedUsers[$result['mask']] = (int)$result['rights'];
}
}
}
protected function _savePrivileges()
{
$db = DB::instance();
$db->execute('TRUNCATE TABLE users');
$users = array();
foreach ($this->_grantedUsers as $mask => $rights) {
$users[] = "('$mask',$rights)";
}
$db->execute('INSERT INTO users (`mask`, `rights`) VALUES '.implode(', ', $users));
}
public function update(Observed $bot, $raw, $type)
{
if (($type & Bot::U_INPUT) != Bot::U_INPUT) {
return Bot::E_CONTINUE;
}
$raw_pieces = explode(' ', $raw, 4);
if (isset($raw_pieces[1]) && $raw_pieces[1] == 'PRIVMSG' && $raw_pieces[2][0] == '#' && $raw_pieces[2] != '#php.bottest') {
$message = substr($raw_pieces[3],1);
foreach ($this->_restrictedCommands as $command => $karma) {
if (substr($message, 0, strlen($command)) == $command) {
// handle the command
$mask = substr(strstr($raw_pieces[0], '!'), 1);
if (isset($this->_grantedUsers[$mask]) && ($this->_grantedUsers[$mask] & $karma) == $karma) {
// allowed
switch($command) {
case '!right':
return $this->_commandRight($raw_pieces, $bot);
case '!dumpRights':
return $this->_commandDumpRights($raw_pieces, $bot);
case '!flushRights':
$this->_loadPrivileges();
return Bot::E_CONTINUE;
case '!saveRights':
$this->_savePrivileges();
return Bot::E_CONTINUE;
}
return Bot::E_CONTINUE;
} else {
$bot->notify('Unauthorized command call', Bot::U_ERROR);
return Bot::E_BREAK;
}
}
}
}
return Bot::E_CONTINUE;
}
protected function _commandRight($raw_pieces, $bot)
{
$message = substr($raw_pieces[3],1);
$message_pieces = explode(' ', $message);
list($nick) = explode('!', substr($raw_pieces[0], 1), 2);
if (!empty($message_pieces[1]) && !empty($message_pieces[2])) {
$mask = $message_pieces[1];
$level = $message_pieces[2];
if ($level[1] == '+' && is_numeric(substr($level,1))) {
if (!isset($this->_grantedUsers[$mask])) {
$this->_grantedUsers[$mask] = (int)substr($level,1);
} else {
$this->_grantedUsers[$mask] |= (int)substr($level,1);
}
}
else if ($level[1] == '-' && is_numeric(substr($level,1))) {
if (!isset($this->_grantedUsers[$mask])){
$this->_grantedUsers[$mask] = 0;
} else {
$this->_grantedUsers[$mask] &= ~(int)substr($level,1);
}
}
else if (is_numeric($level)) {
$this->_grantedUsers[$mask] = (int)$level;
} else {
return Bot::E_CONTINUE;
}
$bot->send('PRIVMSG '.$raw_pieces[2].' :Understood.');
}
return Bot::E_CONTINUE;
}
protected function _commandDumpRights($raw_pieces, $bot)
{
list($nick) = explode('!', substr($raw_pieces[0], 1), 2);
arsort($this->_grantedUsers);
$bot->send('PRIVMSG '.$nick.' :Dumping rights:');
foreach ($this->_grantedUsers as $mask => $right) {
$bot->send('PRIVMSG '.$nick.' :'.str_pad($right, 5, ' ', STR_PAD_LEFT).' => '.$mask);
}
$bot->send('PRIVMSG '.$nick.' :End of dump.');
return Bot::E_CONTINUE;
}
}
Comments
There is currently no comment here.