From e59e78694524537f0bd03782c777cc097b435e3d Mon Sep 17 00:00:00 2001 From: pk910 Date: Tue, 26 Jul 2011 20:57:05 +0200 Subject: [PATCH] implemented EventHandler --- Uplink/EventHandler.interface.php | 1 + Uplink/P10_Server.class.php | 6 ++++-- Uplink/Uplink.class.php | 36 ++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Uplink/EventHandler.interface.php b/Uplink/EventHandler.interface.php index a7665f6..185ee97 100644 --- a/Uplink/EventHandler.interface.php +++ b/Uplink/EventHandler.interface.php @@ -32,6 +32,7 @@ interface EventHandler { public function event_squit($server); public function event_connect($user, $isBurst); + public function event_nick($user, $newNick); public function event_quit($user, $reason); public function event_join($user, $channel, $isBurst); diff --git a/Uplink/P10_Server.class.php b/Uplink/P10_Server.class.php index 1b65ed9..465fa3c 100644 --- a/Uplink/P10_Server.class.php +++ b/Uplink/P10_Server.class.php @@ -108,8 +108,10 @@ class P10_Server { self::$static_servers[$numeric] = $this; } - public function disconnectServer($linked_only = false) { + public function disconnectServer($eventHandler, $linked_only = false) { if(!$linked_only) { + if($eventHandler) + $eventHandler->event_squit($this); if($this->parent_server) { $this->parent_server->delServer($this); } @@ -117,7 +119,7 @@ class P10_Server { unset(self::$static_servers[$this->numeric]); } foreach($this->servers as $server) { - $server->disconnectServer(); + $server->disconnectServer($eventHandler); } } diff --git a/Uplink/Uplink.class.php b/Uplink/Uplink.class.php index cf9b34f..7cbc2f3 100644 --- a/Uplink/Uplink.class.php +++ b/Uplink/Uplink.class.php @@ -57,11 +57,13 @@ require_once("P10_Server.class.php"); require_once("P10_User.class.php"); require_once("P10_Channel.class.php"); require_once("P10_ModeSets.class.php"); +require_once("EventHandler.interface.php"); class Uplink { private $client; private $settings = array(); private $server; + private $eventHandler = null; const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected) const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues @@ -99,7 +101,7 @@ class Uplink { if(!$this->client->connected()) { if(($this->flags & self::FLAG_P10SESSION)) { //Server got disconnected - $this->server->disconnectServer(true); + $this->server->disconnectServer($this->eventHandler, true); $this->flags &= ~self::FLAG_P10SESSION; } $host = $this->getSetting("host"); @@ -151,6 +153,14 @@ class Uplink { $this->setSetting("their_password", $password); } + public function setEventHandler($event_handler) { + if(!is_a($event_handler, "EventHandler")) { + trigger_error((is_object($event_handler) ? get_class($event_handler) : gettype($event_handler))." is NOT a valid EventHandler.", E_USER_ERROR); + return; + } + $this->eventHandler = $event_handler; + } + private function setSetting($setting, $value) { $this->settings[$setting] = $value; } @@ -281,6 +291,8 @@ class Uplink { $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]); $this->server->addServer($new_server); $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING; + if($this->eventHandler) + $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED)); } else { //another server got a new slave server ^^ $server = P10_Server::getServerByNum($from); @@ -290,6 +302,8 @@ class Uplink { } $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]); $server->addServer($new_server); + if($this->eventHandler) + $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED)); } } @@ -305,7 +319,9 @@ class Uplink { trigger_error("Server tries to change the nick of an user that does not exist or was not found on recv_nick.", E_USER_ERROR); return; } - $nick->setNick($args[0]); + if($this->eventHandler) + $this->eventHandler->event_nick($user, $args[0]); + $user->setNick($args[0]); } else { //New User $numeric = $args[count($args)-2]; @@ -325,7 +341,9 @@ class Uplink { $modes = new P10_UserModeSet($modes); $ip = Numerics::parseIP($args[count($args)-3]); $realname = $args[count($args)-1]; - new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes); + $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes); + if($this->eventHandler) + $this->eventHandler->event_connect($user, !($this->flags & self::FLAG_CONNECTED)); } } @@ -347,7 +365,7 @@ class Uplink { trigger_error("Server (".$args[0].") not found.", E_USER_ERROR); return; } - $server->disconnectServer(); + $server->disconnectServer($this->eventHandler); } private function recv_quit($from, $args) { @@ -356,6 +374,8 @@ class Uplink { trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR); return; } + if($this->eventHandler) + $this->eventHandler->event_quit($user, $args[1]); $user->quit($args[0]); } @@ -404,6 +424,8 @@ class Uplink { return; } $channel->burstUser($user, $isop, $isvoice); + if($this->eventHandler) + $this->eventHandler->event_join($user, $channel, true); } $modes->parseModes(implode(array_slice($args, 2, $modeparamcount))); } @@ -418,6 +440,8 @@ class Uplink { if($channel == null) $channel = new P10_Channel($args[0]); $channel->joinUser($user); + if($this->eventHandler) + $this->eventHandler->event_join($user, $channel, false); } private function recv_part($from, $args) { @@ -429,7 +453,9 @@ class Uplink { $channel = P10_Channel::getChannelByName($args[0]); if($channel == null) $channel = new P10_Channel($args[0]); - $channel->partUser($user, $args[1]); + if($this->eventHandler) + $this->eventHandler->event_part($user, $channel, $args[1]); + $channel->partUser($user); } /******************************************************************************************** -- 2.20.1