From 8e32f35edf2ae1c37a80aa08f4beb32d2be2e791 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 26 Jun 2011 14:03:32 +0000 Subject: [PATCH] [TASK] Introduced addLine() to the buffer class. [TASK] Made createBuffer function protected in ProtocolHandler classes. --- Classes/Client/IrcClient.php | 7 +++-- Classes/Connection/ConnectionHandler.php | 2 +- Classes/Connection/ConnectionPool.php | 4 +-- Classes/Misc/Buffer.php | 10 +++++++ Classes/Protocol/AbstractProtocolHandler.php | 2 +- Classes/Protocol/BotProtocolHandler.php | 4 +-- Classes/Protocol/IrcProtocolHandler.php | 30 +++++++++++++------- Testcode/Client/ClientManagerTest.php | 12 +++++++- 8 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Classes/Client/IrcClient.php b/Classes/Client/IrcClient.php index 8b5a52b..9433342 100644 --- a/Classes/Client/IrcClient.php +++ b/Classes/Client/IrcClient.php @@ -65,7 +65,7 @@ class Client_IrcClient extends Client_AbstractClient { */ public function initializeConnection() { if(!$this->authed) { - $data = "USER poweruser as as :JPTs Bot\r\nNICK :" . $this->nick . "\r\n"; + $data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n"; $this->authed = TRUE; } $this->protocolHandler->sendRaw($data); @@ -80,7 +80,8 @@ class Client_IrcClient extends Client_AbstractClient { protected function processContentObject($contentObject) { $data = $contentObject->getRawData(); //DEBUG - //var_dump($contentObject); + var_dump($contentObject); + var_dump($data); //respond to pings if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams()); @@ -98,7 +99,7 @@ class Client_IrcClient extends Client_AbstractClient { foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n"; } - if(strpos($data, "multivitamin") !== FALSE) { + if(strpos($data, "musdsdsafgagltivitamin") !== FALSE) { $return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n"; $return .= "QUIT :lol\r\n"; } diff --git a/Classes/Connection/ConnectionHandler.php b/Classes/Connection/ConnectionHandler.php index 0395382..6de0c23 100644 --- a/Classes/Connection/ConnectionHandler.php +++ b/Classes/Connection/ConnectionHandler.php @@ -370,7 +370,7 @@ class Connection_ConnectionHandler { * @param boolean $connected * @return void */ - private function setConnected($connected) { + protected function setConnected($connected) { return $this->socketHandler->setConnected($connected); } diff --git a/Classes/Connection/ConnectionPool.php b/Classes/Connection/ConnectionPool.php index 792c933..9276640 100644 --- a/Classes/Connection/ConnectionPool.php +++ b/Classes/Connection/ConnectionPool.php @@ -119,7 +119,7 @@ class Connection_ConnectionPool { if($connectionHandler->canWrite() === TRUE && $connectionHandler->isServer() === FALSE) { $write[] = $connectionSocket; //the line above does not work for freshly connected stuff. - //this is the fallback(?) - just write the stuff - no matter what happens. + //this is the fallback - just write the stuff - no matter what happens. if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler); } } @@ -143,7 +143,7 @@ class Connection_ConnectionPool { } break; case "write": - //this might still work on active connections that are "in use". + //this might still work on active connections that are "in use" and already received data. //however, it does not for freshly connected ones. if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler); break; diff --git a/Classes/Misc/Buffer.php b/Classes/Misc/Buffer.php index 3833172..5875d21 100644 --- a/Classes/Misc/Buffer.php +++ b/Classes/Misc/Buffer.php @@ -59,6 +59,16 @@ class Misc_Buffer { $this->pruneEmptyLines(); } + /** + * Appends data + linebreak to the buffer. + * @param string $data + * @return void + */ + public function addLine($data) { + $this->buffer .= $data . $this->linebreak; + $this->pruneEmptyLines(); + } + /** * Returns the next line in the buffer and removes it from the buffer. * @return string diff --git a/Classes/Protocol/AbstractProtocolHandler.php b/Classes/Protocol/AbstractProtocolHandler.php index 0a3aed8..0b5428f 100644 --- a/Classes/Protocol/AbstractProtocolHandler.php +++ b/Classes/Protocol/AbstractProtocolHandler.php @@ -29,7 +29,7 @@ abstract class Protocol_AbstractProtocolHandler { * put them in $bufferIncoming and $bufferOutgoing. * @return void */ - abstract public function createBuffers(); + abstract protected function createBuffers(); /** * General constructor. diff --git a/Classes/Protocol/BotProtocolHandler.php b/Classes/Protocol/BotProtocolHandler.php index 7cf7235..bcf2ada 100644 --- a/Classes/Protocol/BotProtocolHandler.php +++ b/Classes/Protocol/BotProtocolHandler.php @@ -11,7 +11,7 @@ class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler { * Shall create the two buffers and set them up. * @return void */ - public function createBuffers() { + protected function createBuffers() { $linebreak = "\r\n"; $this->bufferIncoming = new Misc_Buffer($linebreak); $this->bufferOutgoing = new Misc_Buffer($linebreak); @@ -45,7 +45,7 @@ class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler { * @return void */ public function sendRaw($data) { - $this->bufferOutgoing->addData($data); + $this->bufferOutgoing->addLine($data); } } diff --git a/Classes/Protocol/IrcProtocolHandler.php b/Classes/Protocol/IrcProtocolHandler.php index 5395642..780dfd4 100644 --- a/Classes/Protocol/IrcProtocolHandler.php +++ b/Classes/Protocol/IrcProtocolHandler.php @@ -16,7 +16,7 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { * Shall create the two buffers and set them up. * @return void */ - public function createBuffers() { + protected function createBuffers() { $this->linebreak = "\n"; $this->bufferIncoming = new Misc_Buffer($this->linebreak); $this->bufferOutgoing = new Misc_Buffer($this->linebreak); @@ -52,25 +52,29 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { //determine what command was sent $command = ""; - list($command, $data) = explode(" ", $data, 2); + 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 = ""; + $target = NULL; //parameters of the command - $params = ""; + $params = NULL; //content or message - $message = ""; - //affected nickname - $subject = ""; + $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, $message) = explode(" ", $data, 2); + list($target, $params) = explode(" ", $data, 2); //cut the colon $message = substr($params, 1); break; @@ -83,8 +87,12 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { case "ERROR": case "QUIT": - //cut the colon - $message = substr($data, 1); + if(strpos($data, " :") !== FALSE) { + list($params, $message) = explode(" :", $data); + $message = substr($message, 1); + } else { + $params = $data; + } break; case "PING": @@ -95,7 +103,7 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { //channel, case "PART": - if(strpos(" :", $data) !== FALSE) { + if(strpos($data, " :") !== FALSE) { list($params, $message) = explode(" :", $data); $message = substr($message, 1); } else { diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index 8cb5860..c7fa199 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -17,11 +17,21 @@ $euirc = $clientManager->createTcpConnection("euirc", "irc"); $clientManager->attachConfig(array( "nick" => "Pb42", "userident" => "Serena", - "channels" => array("#kuzuru-subs") + "channels" => array() ), $euirc); $euirc->connect("irc.euirc.net", 6667); $euirc->setReconnect(TRUE); +/*$osuIRC = $clientManager->createTcpConnection("osuIRC", "irc"); +$clientManager->attachConfig(array( + "nick" => "Pb42", + "userident" => "Serena", + "channels" => array("#osu", "#german") +), $osuIRC); +$osuIRC->connect("irc.ppy.sh", 6667); +$osuIRC->setReconnect(TRUE);*/ + + /*$config_eloxoph = array( "nick" => "Frischmilch",