implemented EventHandler
authorpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 18:57:05 +0000 (20:57 +0200)
committerpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 18:57:05 +0000 (20:57 +0200)
Uplink/EventHandler.interface.php
Uplink/P10_Server.class.php
Uplink/Uplink.class.php

index a7665f6ebca02f2d2f2b6f5bf88e5d39b0013229..185ee97041d5b6fb5c9aa3b462e57fbc7d28f3c0 100644 (file)
@@ -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);
index 1b65ed95e79e2731ca448ee04dc14c289a5270ff..465fa3c00d97a830779a26981724ee3dda3f7b2d 100644 (file)
@@ -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);
                }
        }
        
index cf9b34fab94a767b8dd228802eaa59b9d27cf49f..7cbc2f3a12dba8b51b83c52765f5a09caa975e54 100644 (file)
@@ -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);
        }
        
        /********************************************************************************************