[TASK] Introduced addLine() to the buffer class.

[TASK] Made createBuffer function protected in ProtocolHandler classes.
This commit is contained in:
Jan Philipp Timme 2011-06-26 14:03:32 +00:00
parent df030e453d
commit 8e32f35edf
8 changed files with 50 additions and 21 deletions

View File

@ -65,7 +65,7 @@ class Client_IrcClient extends Client_AbstractClient {
*/ */
public function initializeConnection() { public function initializeConnection() {
if(!$this->authed) { 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->authed = TRUE;
} }
$this->protocolHandler->sendRaw($data); $this->protocolHandler->sendRaw($data);
@ -80,7 +80,8 @@ class Client_IrcClient extends Client_AbstractClient {
protected function processContentObject($contentObject) { protected function processContentObject($contentObject) {
$data = $contentObject->getRawData(); $data = $contentObject->getRawData();
//DEBUG //DEBUG
//var_dump($contentObject); var_dump($contentObject);
var_dump($data);
//respond to pings //respond to pings
if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams()); 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"; 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 .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n";
$return .= "QUIT :lol\r\n"; $return .= "QUIT :lol\r\n";
} }

View File

@ -370,7 +370,7 @@ class Connection_ConnectionHandler {
* @param boolean $connected * @param boolean $connected
* @return void * @return void
*/ */
private function setConnected($connected) { protected function setConnected($connected) {
return $this->socketHandler->setConnected($connected); return $this->socketHandler->setConnected($connected);
} }

View File

@ -119,7 +119,7 @@ class Connection_ConnectionPool {
if($connectionHandler->canWrite() === TRUE && $connectionHandler->isServer() === FALSE) { if($connectionHandler->canWrite() === TRUE && $connectionHandler->isServer() === FALSE) {
$write[] = $connectionSocket; $write[] = $connectionSocket;
//the line above does not work for freshly connected stuff. //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); if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler);
} }
} }
@ -143,7 +143,7 @@ class Connection_ConnectionPool {
} }
break; break;
case "write": 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. //however, it does not for freshly connected ones.
if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler); if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler);
break; break;

View File

@ -59,6 +59,16 @@ class Misc_Buffer {
$this->pruneEmptyLines(); $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. * Returns the next line in the buffer and removes it from the buffer.
* @return string * @return string

View File

@ -29,7 +29,7 @@ abstract class Protocol_AbstractProtocolHandler {
* put them in $bufferIncoming and $bufferOutgoing. * put them in $bufferIncoming and $bufferOutgoing.
* @return void * @return void
*/ */
abstract public function createBuffers(); abstract protected function createBuffers();
/** /**
* General constructor. * General constructor.

View File

@ -11,7 +11,7 @@ class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler {
* Shall create the two buffers and set them up. * Shall create the two buffers and set them up.
* @return void * @return void
*/ */
public function createBuffers() { protected function createBuffers() {
$linebreak = "\r\n"; $linebreak = "\r\n";
$this->bufferIncoming = new Misc_Buffer($linebreak); $this->bufferIncoming = new Misc_Buffer($linebreak);
$this->bufferOutgoing = new Misc_Buffer($linebreak); $this->bufferOutgoing = new Misc_Buffer($linebreak);
@ -45,7 +45,7 @@ class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler {
* @return void * @return void
*/ */
public function sendRaw($data) { public function sendRaw($data) {
$this->bufferOutgoing->addData($data); $this->bufferOutgoing->addLine($data);
} }
} }

View File

@ -16,7 +16,7 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler {
* Shall create the two buffers and set them up. * Shall create the two buffers and set them up.
* @return void * @return void
*/ */
public function createBuffers() { protected function createBuffers() {
$this->linebreak = "\n"; $this->linebreak = "\n";
$this->bufferIncoming = new Misc_Buffer($this->linebreak); $this->bufferIncoming = new Misc_Buffer($this->linebreak);
$this->bufferOutgoing = 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 //determine what command was sent
$command = ""; $command = "";
if(strpos($data, " ") !== FALSE) {
list($command, $data) = explode(" ", $data, 2); list($command, $data) = explode(" ", $data, 2);
} else {
$command = $data;
}
$contentObject->setCommand($command); $contentObject->setCommand($command);
//we'll write these values into the contentObject later! //we'll write these values into the contentObject later!
//contains a user or the channel a command is addressed to. //contains a user or the channel a command is addressed to.
$target = ""; $target = NULL;
//parameters of the command //parameters of the command
$params = ""; $params = NULL;
//content or message //content or message
$message = ""; $message = NULL;
//affected nickname //affected nickname(s) (e.g. by a KICK or MODE +b)
$subject = ""; $subject = NULL;
//command-dependent parsing //command-dependent parsing
switch($command) { switch($command) {
case "PRIVMSG": case "PRIVMSG":
case "NOTICE": case "NOTICE":
list($target, $message) = explode(" ", $data, 2); list($target, $params) = explode(" ", $data, 2);
//cut the colon //cut the colon
$message = substr($params, 1); $message = substr($params, 1);
break; break;
@ -83,8 +87,12 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler {
case "ERROR": case "ERROR":
case "QUIT": case "QUIT":
//cut the colon if(strpos($data, " :") !== FALSE) {
$message = substr($data, 1); list($params, $message) = explode(" :", $data);
$message = substr($message, 1);
} else {
$params = $data;
}
break; break;
case "PING": case "PING":
@ -95,7 +103,7 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler {
//channel, <comment> //channel, <comment>
case "PART": case "PART":
if(strpos(" :", $data) !== FALSE) { if(strpos($data, " :") !== FALSE) {
list($params, $message) = explode(" :", $data); list($params, $message) = explode(" :", $data);
$message = substr($message, 1); $message = substr($message, 1);
} else { } else {

View File

@ -17,11 +17,21 @@ $euirc = $clientManager->createTcpConnection("euirc", "irc");
$clientManager->attachConfig(array( $clientManager->attachConfig(array(
"nick" => "Pb42", "nick" => "Pb42",
"userident" => "Serena", "userident" => "Serena",
"channels" => array("#kuzuru-subs") "channels" => array()
), $euirc); ), $euirc);
$euirc->connect("irc.euirc.net", 6667); $euirc->connect("irc.euirc.net", 6667);
$euirc->setReconnect(TRUE); $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( /*$config_eloxoph = array(
"nick" => "Frischmilch", "nick" => "Frischmilch",