Source code raw phps
<?php
require_once 'libraries/class.Link.php';
require_once 'libraries/iface.Observed.php';
require_once 'exceptions/expt.FatalError.php';
class Bot implements Observed{
static public $status = self::S_READY;
// internal status
const S_READY = 0;
const S_STOP = 1;
const S_ERROR = 2;
const S_RECONNECT = 3;
// execution status returned by modules
const E_CONTINUE = 0;
const E_BREAK = 1;
const E_STOP = 2;
const E_RECONNECT = 3;
// Update/notify status used by observer/observed
const U_INPUT = 1;
const U_OUTPUT = 2;
const U_ERROR = 4;
const U_DEBUG = 8;
const U_STATUS = 16;
const U_DB = 32;
protected $_reconnects = 0;
protected $_modules = array();
protected $_link = null;
public $configs = array( 'reconnects' => 10,
'sleepTime' => 20,
'linkType' => 'Link_IRC',
'host' => '195.54.159.109',
'port' => 6667,
'readLength' => 2048,
'username' => 'php-ref',
'hostname' => 'foo.bar.gee',
'servername' => 'colder.ch',
'realname' => 'php reference bot',
'nickname' => 'php-ref');
public function notify($message, $type)
{
// notify every module
foreach($this->_modules as $module) {
$r = $module->update($this, $message, $type);
switch($r) {
case self::E_CONTINUE:
continue 2;
case self::E_STOP:
self::$status = self::S_STOP;
break 2;
case self::E_RECONNECT:
self::$status = self::S_RECONNECT;
break 2;
default:
break 2;
}
}
return true;
}
public function send($message) {
if ($this->_link->write($message)) {
$this->notify($message, self::U_OUTPUT);
}
}
public function attach(Observer $module)
{
if (!isset($this->_modules[get_class($module)])) {
$this->_modules[get_class($module)] = $module;
return true;
} else {
return false;
}
}
public function detach(Observer $module)
{
if (isset($this->_modules[get_class($module)])) {
unset($this->_modules[get_class($module)]);
return true;
} else {
return false;
}
}
public function shutdownModule(Module $module)
{
if (isset($this->_modules[get_class($module)])) {
$this->_modules[get_class($module)] = null;
unset($this->_modules[get_class($module)]);
return true;
} else {
return false;
}
}
protected function _idle()
{
$this->_reconnects = $this->configs['reconnects'];
do {
self::$status = self::S_READY;
$this->_connect();
if(!$this->_link->isValid()){
$this->notify('Unable to connect : sleeping '.$this->configs['sleepTime'].' seconds', Observed::U_ERROR);
sleep($this->configs['sleepTime']);
continue;
}
while(!$this->_link->eof()) {
if (($raw = $this->_link->read($this->configs['readLength'])) !== false) {
self::notify($raw, self::U_INPUT);
}
// problems handling
if (self::$status == self::S_ERROR) {
$this->notify($raw, self::U_ERROR);
} else if (self::$status == self::S_STOP) {
$this->notify('Shutdown', self::U_STATUS);
break 2;
} else if (self::$status == self::S_RECONNECT) {
$this->notify('Reconnect', self::U_STATUS);
break 1;
}
}
} while ($this->_reconnects--);
}
protected function _connect()
{
if (!$this->_link instanceof Link) {
if (class_exists($this->configs['linkType']) && is_subclass_of($this->configs['linkType'], 'Link')) {
$this->_link = new $this->configs['linkType'];
} else {
self::$status = self::S_ERROR;
throw new FatalError('Invalid Link type');
}
}
return $this->_link->connect($this->configs['host'], $this->configs['port']);
}
public function start()
{
try {
$this->_idle();
} catch (FatalError $e) {
echo "<strong>Fatal Error:</strong> ".$e->getMessage()."\n";
}
}
public function shutdown()
{
foreach($this->_modules as $module) {
$this->shutdownModule($module);
}
return true;
}
}
Comments
There is currently no comment here.