diff --git a/BRAINSTORM b/BRAINSTORM index 6f7d7b9..2b310c8 100644 --- a/BRAINSTORM +++ b/BRAINSTORM @@ -12,4 +12,8 @@ - Logging mit Timestamp für: - Sollte das Teil mal abkacken, so will ich zumindest über Verbindungsabbrüche, Disconnects und Protokollseitige Quits, X-Lines und Errors einen Nachweis haben. -- Auch php-errors sollten in einem logfile landen. \ No newline at end of file +- Auch php-errors sollten in einem logfile landen. + +Brückenelement für den Clientmanager. +- Sollte in der Lage sein, eine Brücke zwischen mind. 2 Verbindungen aufzubauen. +- Anwendung: Ich baue mir nen Bouncer... diff --git a/Classes/Client/AbstractClient.php b/Classes/Client/AbstractClient.php index 8a80058..77e0976 100644 --- a/Classes/Client/AbstractClient.php +++ b/Classes/Client/AbstractClient.php @@ -74,7 +74,7 @@ abstract class Client_AbstractClient { /** * Will reset the connectionStatus of the client. - * Implementation depends on the client. + * Implementation is optional and depends on the client. * Should be used to reset internal variables. * @return void */ diff --git a/Classes/Client/IrcClient.php b/Classes/Client/IrcClient.php index 5b711e9..ce44341 100644 --- a/Classes/Client/IrcClient.php +++ b/Classes/Client/IrcClient.php @@ -42,14 +42,18 @@ class Client_IrcClient extends Client_AbstractClient { * @return void */ public function processContentObject($contentObject) { - $data = $contentObject->rawData; - echo "[RECV] ".$data; + $data = $contentObject->getRawData(); + //DEBUG + //var_dump($contentObject); + //echo "[RECV] ".$data; + + //respond to pings + if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams()); + $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++; diff --git a/Classes/Exception/ProtocolHandlerException.php b/Classes/Exception/ProtocolHandlerException.php new file mode 100644 index 0000000..896c533 --- /dev/null +++ b/Classes/Exception/ProtocolHandlerException.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/Classes/Protocol/AbstractProtocolContentObject.php b/Classes/Protocol/AbstractProtocolContentObject.php index a9b203d..519f2a2 100644 --- a/Classes/Protocol/AbstractProtocolContentObject.php +++ b/Classes/Protocol/AbstractProtocolContentObject.php @@ -10,17 +10,26 @@ abstract class Protocol_AbstractProtocolContentObject { /** * Contains the raw data. + * Standard attribute * @var string */ - public $rawData; + protected $rawData; /** * Default constructor. - * Sets the rawData field. + * Sets the raw data. * @return void */ function __construct($rawData) { $this->rawData = $rawData; } + + /** + * Returns raw data. + * @return string + */ + public function getRawData() { + return $this->rawData; + } } ?> \ No newline at end of file diff --git a/Classes/Protocol/BotProtocolHandler.php b/Classes/Protocol/BotProtocolHandler.php index 0bb827c..6227d70 100644 --- a/Classes/Protocol/BotProtocolHandler.php +++ b/Classes/Protocol/BotProtocolHandler.php @@ -37,13 +37,15 @@ class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler { } /** - * Will be replaced soon. Passes raw data into the outgoing buffer. + * Will put raw data into the outgoing buffer. + * This function will be removed soon. + * The ProtocolHandler shall take care of this directly. * @deprecated * @param string $data * @return void */ public function sendRaw($data) { - $this->buffer_incoming->addData($data); + $this->buffer_outgoing->addData($data); } } diff --git a/Classes/Protocol/IrcProtocolContentObject.php b/Classes/Protocol/IrcProtocolContentObject.php index e3d8335..dbcd844 100644 --- a/Classes/Protocol/IrcProtocolContentObject.php +++ b/Classes/Protocol/IrcProtocolContentObject.php @@ -6,5 +6,139 @@ */ 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; + } + } ?> \ No newline at end of file diff --git a/Classes/Protocol/IrcProtocolHandler.php b/Classes/Protocol/IrcProtocolHandler.php index 9fc1b73..747e85a 100644 --- a/Classes/Protocol/IrcProtocolHandler.php +++ b/Classes/Protocol/IrcProtocolHandler.php @@ -19,12 +19,143 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { /** * 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->buffer_incoming->getNextLine(); - return new Protocol_IrcProtocolContentObject($data); + //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 = ""; + list($command, $data) = explode(" ", $data, 2); + $contentObject->setCommand($command); + + //we'll write these values into the contentObject later! + //contains a user or the channel a command is addressed to. + $target = ""; + $params = ""; + $message = ""; + $subject = ""; + + //command-dependent parsing + switch($command) { + + case "PRIVMSG": + case "NOTICE": + list($target, $message) = 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": + //cut the colon + $message = substr($data, 1); + break; + + case "PING": + case "JOIN": + //cut the colon + $params = substr($data, 1); + break; + + //channel, + 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, + 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; + + //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": + case "372": + + //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; } /** @@ -40,7 +171,6 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { * Will put raw data into the outgoing buffer. * This function will be removed soon. * The ProtocolHandler shall take care of this directly. - * @deprecated * @param string $data * @return void */ @@ -48,5 +178,15 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { $this->buffer_outgoing->addData($data); } + /** + * Sends a pong. + * @param string $param + * @return void + */ + public function pong($param) { + $this->sendRaw("PONG :" . $param); + } + + } ?> \ No newline at end of file diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index 69838e9..5480541 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -15,9 +15,9 @@ $freenode->setReconnect(TRUE); $freenode = $clientManager->createTcpConnection("freenode", "irc"); $clientManager->attachConfig(array( - "nick" => "Testinstanz2", + "nick" => "Pb42", "userident" => "uzuguck", - "channels" => array("#mstasty") + "channels" => array("#mstasty", "#starsim") ), $freenode); $freenode->connect("irc.freenode.net", 6667); diff --git a/Testcode/Misc/IrcParser.php b/Testcode/Misc/IrcParser.php new file mode 100644 index 0000000..f2af15e --- /dev/null +++ b/Testcode/Misc/IrcParser.php @@ -0,0 +1,49 @@ + $from, "Command" => $command, "Msg" => $msg); +} + +$lines = array(); +$lines[] = ":kornbluth.freenode.net 376 JPT|test :End of /MOTD command."; +$lines[] = ":frigg!~frigg@freenode/utility-bot/frigg PRIVMSG JPT|test :VERSION"; +$lines[] = ":kornbluth.freenode.net 333 JPT|test #botted rotw!~rotw@p5B133ADF.dip.t-dialin.net 1291741910"; +$lines[] = ":ChanServ!ChanServ@services. NOTICE JPT|test :[#botted] Für Statistiken siehe !stats. Wenn du nicht in diesen Auftauchen willst, wende dich bitte an samohT"; +$lines[] = ":kornbluth.freenode.net NOTICE * :*** Found your hostname"; +$lines[] = ":thomasbot!~thomasbot@unaffiliated/thomas/x-476237/bot/thomasbot PRIVMSG #botted :*pong*"; +$lines[] = ":n|ki!~niki@star-sim.de MODE #botted -v JPT"; +$lines[] = "ERROR :Closing Link: dslb-088-070-011-158.pools.arcor-ip.net (Client Quit)"; + + +foreach($lines AS $line) { + var_dump(parse($line)); +} + +echo "\r\n\r\n"; +?> \ No newline at end of file