[TASK] Moved the project to namespaces.

[TASK] Removed all the protocol handlers.
[TASK] Clients are no longer part of the SocketFramework itself.
[TASK] The Framework provides an "interface" to external clients - the ClientDispatcher.
[TASK] Added the Core package and added a better ClassLoader.
[TASK] Created a bootstrap module to include in other projects.
[TASK] Cleaned up comments, reformatted them.
This commit is contained in:
Jan Philipp Timme
2011-12-03 12:43:43 +01:00
parent 8de6bb29b6
commit 16ff75eb1d
32 changed files with 573 additions and 541 deletions
+58
View File
@@ -0,0 +1,58 @@
<?php
/**
* IrcClient class that contains all the Plugins
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_BotClient extends Client_AbstractClient {
/**
* Default constructor.
* @return void
*/
public function __construct() {
}
/**
* Does all the hard work.
* @param object $contentObject
* @return void
*/
public function processContentObject($contentObject) {
$return = "";
$rawData = $contentObject->getRawData();
if(trim($rawData) === "list") {
$return = print_r(array("IDs" => $this->clientManager->getIDs(), "Groups" => $this->clientManager->getGroups()), TRUE);
}
if(strpos($rawData, "-") !== FALSE ) {
$data = explode("-", $rawData, 2);
$this->clientManager->sendToID((int) $data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
if(strpos($rawData, "+") !== FALSE ) {
$data = explode("+", $rawData, 2);
$this->clientManager->sendToGroup($data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
//TODO: implement this correctly
$return = str_replace("\n", "\r\n", $return);
$this->protocolHandler->sendRaw($return);
}
/**
* Loads the given configuration.
* @param array $config
* @return void
*/
public function loadConfig($config) {
}
}
?>
+123
View File
@@ -0,0 +1,123 @@
<?php
/**
* IrcClient class that contains all the Plugins
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_IrcClient extends Client_AbstractClient {
/**
* @var boolean
*/
protected $joined;
/**
* @var boolean
*/
protected $got001;
/**
* @var boolean
*/
protected $authed;
/**
* @var string
*/
protected $nick;
/**
* @var array
*/
protected $channels;
/**
* @var int
*/
protected $lines;
/**
* Default constructor.
* @return void
*/
public function __construct() {
$this->nick = "Serena";
$this->channels = array();
$this->resetConnectionStatus();
}
/**
* Will reset the clients internal variables concerning the connection status.
* @return void
*/
public function resetConnectionStatus() {
$this->lines = 0;
$this->got001 = FALSE;
$this->joined = FALSE;
$this->authed = FALSE;
}
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
* @return void
*/
public function initializeConnection() {
if(!$this->authed) {
$data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n";
$this->authed = TRUE;
}
$this->protocolHandler->sendRaw($data);
}
/**
* Processes the resulting ContentObject from a ProtocolHandler.
* Does all the hard work.
* @param string $data
* @return void
*/
protected function processContentObject($contentObject) {
$data = $contentObject->getRawData();
//DEBUG
var_dump($contentObject);
var_dump($data);
//respond to pings
if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams());
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data);
if($contentObject->getCommand() === "001") $this->got001 = TRUE;
$return = "";
$this->lines++;
if(!$this->joined && $this->got001) {
$this->joined = TRUE;
foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n";
}
if(strpos($data, "musdsdsafgagltivitamin") !== FALSE) {
$return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n";
$return .= "QUIT :lol\r\n";
}
//workaround. will be removed soon
$this->protocolHandler->sendRaw($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"];
}
}
?>
@@ -0,0 +1,144 @@
<?php
/**
* ContentObject for IRC Protocol
* @author JPT
* @package Protocol
*/
class Protocol_IrcProtocolContentObject extends Protocol_AbstractProtocolContentObject {
/**
* @var string
*/
protected $sender;
/**
* @var string
*/
protected $command;
/**
* @var string
*/
protected $params;
/**
* Contains a user or the channel a command is addressed to.
* @var string
*/
protected $target;
/**
* @var string
*/
protected $message;
/**
* @var string
*/
protected $subject;
/**
* Sets sender.
* @param string $sender
* @return void
*/
public function setSender($sender) {
$this->sender = $sender;
}
/**
* Gets sender.
* @return string
*/
public function getSender() {
return $this->sender;
}
/**
* Sets command.
* @param string $command
* @return void
*/
public function setCommand($command) {
$this->command = $command;
}
/**
* Gets command.
* @return string
*/
public function getCommand() {
return $this->command;
}
/**
* Sets params.
* @param string $params
* @return void
*/
public function setParams($params) {
$this->params = $params;
}
/**
* Gets params.
* @return string
*/
public function getParams() {
return $this->params;
}
/**
* Sets target.
* @param string $target
* @return void
*/
public function setTarget($target) {
$this->target = $target;
}
/**
* Gets target.
* @return string
*/
public function getTarget() {
return $this->target;
}
/**
* Sets message.
* @param string $message
* @return void
*/
public function setMessage($message) {
$this->message = $message;
}
/**
* Gets message.
* @return string
*/
public function getMessage() {
return $this->message;
}
/**
* Sets subject.
* @param string $subject
* @return void
*/
public function setSubject($subject) {
$this->subject = $subject;
}
/**
* Gets subject.
* @return string
*/
public function getSubject() {
return $this->subject;
}
}
?>
@@ -0,0 +1,211 @@
<?php
/**
* The ProtocolHandler for the IRC Protocol.
* @author JPT
* @package Protocol
*/
class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler {
/**
* @var string
*/
protected $linebreak;
/**
* Is called by the constructor.
* Shall create the two buffers and set them up.
* @return void
*/
protected function createBuffers() {
$this->linebreak = "\n";
$this->bufferIncoming = new Misc_Buffer($this->linebreak);
$this->bufferOutgoing = new Misc_Buffer($this->linebreak);
}
/**
* Main worker function. It will be called in a loop.
* It does all the protocol-dependent parsing.
* The returned ContentObject will be passed to the client.
* @return Protocol_IrcProtocolContentObject
* @throws Exception_ProtocolHandlerException
* @throws Exception_GeneralException
*/
public function work() {
$data = $this->bufferIncoming->getNextLine();
//create contentObject and set the raw data.
$contentObject = new Protocol_IrcProtocolContentObject($data);
//trim whitespace
$data = trim($data);
//no data? complain!
if($data === "") throw new Exception_ProtocolHandlerException("No data to process! Where did it go?!", 1292082006);
//get the guy who sent the message
$sender = "";
if($data[0] === ":") {
list($sender, $data) = explode(" ", $data, 2);
//cut the colon
$sender = substr($sender, 1);
$contentObject->setSender($sender);
}
//determine what command was sent
$command = "";
if(strpos($data, " ") !== FALSE) {
list($command, $data) = explode(" ", $data, 2);
} else {
$command = $data;
}
$contentObject->setCommand($command);
//we'll write these values into the contentObject later!
//contains a user or the channel a command is addressed to.
$target = NULL;
//parameters of the command
$params = NULL;
//content or message
$message = NULL;
//affected nickname(s) (e.g. by a KICK or MODE +b)
$subject = NULL;
//command-dependent parsing
switch($command) {
case "PRIVMSG":
case "NOTICE":
list($target, $params) = explode(" ", $data, 2);
//cut the colon
$message = substr($params, 1);
break;
case "INVITE":
list($target, $params) = explode(" ", $data, 2);
//cut the colon
$params = substr($params, 1);
break;
case "ERROR":
case "QUIT":
if(strpos($data, " :") !== FALSE) {
list($params, $message) = explode(" :", $data);
$message = substr($message, 1);
} else {
$params = $data;
}
break;
case "PING":
case "JOIN":
//cut the colon
$params = substr($data, 1);
break;
//channel, <comment>
case "PART":
if(strpos($data, " :") !== FALSE) {
list($params, $message) = explode(" :", $data);
$message = substr($message, 1);
} else {
$params = $data;
}
break;
case "NICK":
//cut the colon
$params = substr($data, 1);
break;
//channel, user, <comment>
case "KICK":
$pieces = explode(" ", $data, 3);
list($target, $subject, $data) = explode(" ", $data, 3);
//get comment for kick and cut the colon
if(count($pieces === 3)) $message = substr($data, 1);
unset($pieces);
break;
//MOTD content
case "372":
list($target, $message) = explode(" :", $data);
break;
//user, comment
case "KILL":
case "MODE":
case "AWAY":
case "001":
case "002":
case "003":
case "004":
case "005":
case "250":
case "251":
case "252":
case "253":
case "254":
case "255":
case "265":
case "266":
case "332":
case "333":
case "353":
//NAMES end
case "366":
//MOTD start
case "375":
//MOTD end
case "376":
//break;
//tell when stuff is not implemented
default:
//echo "N.i.y.: " . $command . " [".$data."]\r\n";
break;
}
//we're done parsing, now write the stuff into the contentObject.
$contentObject->setTarget($target);
$contentObject->setParams($params);
$contentObject->setMessage($message);
return $contentObject;
}
/**
* Returns whether there is work to be done.
* Important in order to assure that a ContentObject is created and passed to the Client.
* @return boolean
*/
public function canWork() {
return $this->bufferIncoming->hasLines();
}
/**
* Will put raw data into the outgoing buffer.
* This function will be removed soon.
* The ProtocolHandler shall take care of this directly.
* @param string $data
* @return void
*/
public function sendRaw($data) {
$this->bufferOutgoing->addData($data . $this->linebreak);
}
/**
* Sends a pong.
* @param string $param
* @return void
*/
public function pong($param) {
$this->sendRaw("PONG :" . $param);
}
}
?>