This commit is contained in:
Jan Philipp Timme
2010-11-21 22:48:11 +00:00
parent 01018862f7
commit 952af43b97
23 changed files with 3314 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
<?php
/**
* Abstract class that implements the basic api for any client.
* @author jpt
* @abstract
* @package Client
*/
abstract class Client_AbstractClient {
/**
* @var int
*/
protected $ID;
/**
* @var string
*/
protected $group;
/**
* Contains a reference to the ClientManager in order to change stuff on the fly.
* @var Client_ClientManager
*/
protected $clientManager;
/**
* This function will be the main entry point of any client.
* @param string $data
* @return string
*/
abstract public function processData($data);
/**
* This function will load the given config.
* @param array $config
*/
abstract public function loadConfig($config);
/**
* @param int $id
*/
public function setID($id) {
$this->ID = $id;
}
/**
* @param string $group
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* Injects ClientManager
* @param Client_ClientManager $clientManager
*/
public function injectClientManager($clientManager) {
$this->clientManager = $clientManager;
}
}
?>
+48
View File
@@ -0,0 +1,48 @@
<?php
/**
* IrcClient class that contains all the Plugins
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_BotClient extends Client_AbstractClient {
/**
* Default constructor.
*/
function __construct() {
}
/**
* Does all the hard work.
* @param string $data
* @return string
*/
public function processData($data) {
if(strpos($data, "-") !== FALSE ) {
$data = explode("-", $data);
$this->clientManager->sendToID((int) $data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
if(strpos($data, "+") !== FALSE ) {
$data = explode("+", $data);
$this->clientManager->sendToGroup($data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
return $return;
}
/**
* Loads the given configuration.
* @param array $config
* @return void
*/
public function loadConfig($config) {
}
}
?>
+242
View File
@@ -0,0 +1,242 @@
<?php
/**
* This class contains the connection handler.
* It handles all the incoming and outgoing data by forwarding it
* to the clients for the specific connection.
* @author jpt
* @package Client
* @depends Connection
*/
class Client_ClientManager {
/**
* @var Connection_ConnectionPool
*/
protected $connectionPool;
/**
* An array that contains all client instances for each connection.
* @var array
*/
protected $clientPool;
/**
* @var array
*/
protected $rawRoutingRules;
/**
* An array that contains all protocols we have clients for.
* @var array
*/
protected $registeredProtocols;
/**
* Contains Connection-attached configurations.
* @var array
*/
protected $configPool;
/**
* Default constructor.
* @param string $linebreak
* @return void
*/
function __construct($linebreak = "\r\n") {
$this->connectionPool = new Connection_ConnectionPool($linebreak);
$this->clientPool = array();
$this->registeredProtocols = array();
$this->rawRoutingRules = array();
$this->configPool = array();
}
/**
* Default destructor. Closes connections before being killed.
* @return void
*/
function __destruct() {
unset($this->clientPool);
unset($this->connectionPool);
}
/**
* @see Connection_ConnectionPool
* @return int
*/
public function countConnections() {
return $this->connectionPool->countConnections();
}
/**
* Main worker function.
* Processes incoming data, calls clients and much more.
* @return void
*/
public function work() {
$connectionHandlers = $this->connectionPool->select();
if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return;
foreach($connectionHandlers["read"] AS $connectionHandler) {
//handle disconnects
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
$this->removeClientForConnectionHandler($connectionHandler);
}
//ne warte mal, das geht nicht...oder so
if($connectionHandler->isListening() === TRUE) {
$this->addClientForConnectionHandler($connectionHandler);
}
//create a client for a connection without one.
if(!isset($this->clientPool[$connectionHandler->getID()])) {
$this->addClientForConnectionHandler($connectionHandler);
}
if(isset($this->clientPool[$connectionHandler->getID()])) {
//let the client process the data, send the results back.
while($data = $connectionHandler->read()) {
$result = $this->clientPool[$connectionHandler->getID()]->processData($data);
if($result !== "") {
$connectionHandler->write($result);
}
}
}
}
}
/**
* TODO: implement this!
* @param unknown_type $from
* @param unknown_type $to
*/
public function addRawRouting($from, $to) {
$this->rawRoutingRules[] = array("");
}
/**
* Calls ConnectionPool in order to create a simple connection.
* @see Connection_ConnectionPool
* @param string $group
* @param boolean $IPv6
* @return Connection_ConnectionHandler
*/
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6);
}
/**
* Closes and removes the connection.
* @param Connection_ConnectionHandler $connectionHandler
*/
public function removeConnection($connectionHandler) {
unset($this->configPool[$connectionHandler->getID()]);
unset($this->clientPool[$connectionHandler->getID()]);
$this->connectionPool->removeConnectionHandler($connectionHandler);
}
/**
* Registers a protocol with a specific client.
* @param string $protocol
* @param string $className
* @return void
*/
public function registerProtocol($protocol, $className) {
$this->registeredProtocols[$protocol] = $className;
}
/**
* Unregisters a protocol.
* @param string $protocol
* @return void
*/
public function unregisterProtocol($protocol) {
unset($this->registeredProtocols[$protocol]);
}
/**
* Searches for the registered client for the given protocol.
* Returns a client instance or void.
* @param string $protocol
* @return mixed
* @throws Exception_GeneralException
*/
protected function createClientForProtocol($protocol) {
//look for the protocol
if(!isset($this->registeredProtocols[$protocol])) {
throw new Exception_GeneralException("No client registered for protocol: '" . $protocol . "'!", 1290271651);
}
$className = $this->registeredProtocols[$protocol];
//look for the class
if(class_exists($className)) {
return new $className();
} else {
throw new Exception_GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773);
}
}
/**
* Adds a client to our client pool.
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
protected function addClientForConnectionHandler($connectionHandler) {
$protocol = $connectionHandler->getProtocol();
//do not try this for raw connections
if($protocol === "RAW") return;
$client = $this->createClientForProtocol($protocol);
if(isset($this->configPool[$connectionHandler->getID()])) {
$client->loadConfig($this->configPool[$connectionHandler->getID()]);
}
$client->injectClientManager($this);
$client->setID($connectionHandler->getID());
$client->setGroup($connectionHandler->getGroup());
$this->clientPool[$connectionHandler->getID()] = $client;
}
/**
* Removes a client from our client pool.
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
protected function removeClientForConnectionHandler($connectionHandler) {
unset($this->clientPool[$connectionHandler->getID()]);
$this->connectionPool->removeConnectionHandler($connectionHandler);
}
/**
* Attaches a configuration to a connection.
* Will overwrite the existing configuration for the connection.
* @param array $config
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
public function attachConfig($config, $connectionHandler) {
$this->configPool[$connectionHandler->getID()] = $config;
}
/**
* Calls Connection_Pool
* @see Connection_ConnectionPool
* @param int $id
* @param string $data
* @return void
*/
public function sendToID($id, $data) {
return $this->connectionPool->writeToID($id, $data);
}
/**
* Calls Connection_Pool
* @see Connection_ConnectionPool
* @param string $group
* @param string $data
* @return void
*/
public function sendToGroup($group, $data) {
return $this->connectionPool->writeToGroup($group, $data);
}
}
?>
+77
View File
@@ -0,0 +1,77 @@
<?php
/**
* IrcClient class that contains all the Plugins
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_IrcClient extends Client_AbstractClient {
protected $got_001;
protected $joined;
protected $authed;
protected $nick;
protected $channels;
protected $lines;
/**
* Default constructor.
*/
function __construct() {
$this->nick = "Serena";
$this->channels = array();
$this->lines = 0;
$this->got_001 = FALSE;
$this->joined = FALSE;
$this->authed = FALSE;
}
/**
* Does all the hard work.
* @param string $data
* @return string
*/
public function processData($data) {
//echo "[RECV] ".$data;
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data."\r\n\r\n");
$return = "";
if(strpos($data, "PING :") !== FALSE) $return .= str_replace("PING", "PONG", $data);
if(preg_match("/001/", $data)) $this->got_001 = TRUE;
$this->lines++;
if(!$this->authed && $this->lines > 1) {
$return .= "USER as as as :Asdfg\r\nNICK :" . $this->nick . "\r\n";
$this->authed = TRUE;
}
if($this->got_001 && !$this->joined) {
$this->joined = TRUE;
foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n";
}
if(strpos($data, "hau ab") !== FALSE) {
$return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n";
$return .= "QUIT :lol\r\n";
}
//if($return !== "") echo "[SEND] ".$return;
return $return;
}
/**
* Loads the given configuration.
* @param array $config
* @return void
*/
public function loadConfig($config) {
$this->nick = $config["nick"];
$this->channels = $config["channels"];
$this->userident = $config["userident"];
}
}
?>