added recv_nick and P10_User
authorpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 06:31:23 +0000 (08:31 +0200)
committerpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 06:31:23 +0000 (08:31 +0200)
Uplink/Numerics.class.php
Uplink/P10_ModeSets.class.php [new file with mode: 0644]
Uplink/P10_Server.class.php
Uplink/P10_User.class.php [new file with mode: 0644]
Uplink/Uplink.class.php

index 55db20977a6b0ecc3042f3def2753f4a984bb0b0..4727dcf2756ab75fbbd77e130f7efe68bee1de8f 100644 (file)
@@ -32,6 +32,9 @@
  *
  * static int numToInt(String $numeric)
  *     returns the integer value, the numeric represents
+ *
+ * static String parseIP(String $numeric)
+ *     parses an IP Address in numeric format
  */
 
 class Numerics {
@@ -78,6 +81,34 @@ class Numerics {
                return $int;
        }
        
+       public static function parseIP($numeric) {
+               if(strlen($numeric) == 6) { //IPv4
+                       $value = self::numToInt($numeric);
+                       $ip = array();
+                       $ip[0] = ($value & 0xff000000) >> 24;
+                       $ip[1] = ($value & 0x00ff0000) >> 16;
+                       $ip[2] = ($value & 0x0000ff00) >> 8;
+                       $ip[3] = ($value & 0x000000ff);
+                       return implode(".", $ip);
+               } else { //IPv6
+                       $ip = array();
+                       for($i = 0; $i < strlen($numeric);) {
+                               if($numeric[$i] == "_") {
+                                       $rightBlocks = (strlen($numeric) - ($i + 1)) / 3;
+                                       $skipCount = 8 - count($ip) - $rightBlocks;
+                                       for($j = 0; $j < $skipBlocks; $j++) {
+                                               $ip[] = "0";
+                                       }
+                               } else {
+                                       $value = self::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]);
+                                       $ip[] = dechex($value);
+                                       $i += 3;
+                               }
+                       }
+                       return implode(":", $ip);
+               }
+       }
+       
 }
 
 ?>
\ No newline at end of file
diff --git a/Uplink/P10_ModeSets.class.php b/Uplink/P10_ModeSets.class.php
new file mode 100644 (file)
index 0000000..4d0af7d
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+/********************************* PHP-P10 ******************************
+ *    P10 uplink class by pk910   (c)2011 pk910                         *
+ ************************************************************************
+ *                          Version 2 (OOP)                             *
+ *                                                                      *
+ * PHP-P10 is free software; you can redistribute it and/or modify      *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or    *
+ * (at your option) any later version.                                  *
+ *                                                                      *
+ * This program is distributed in the hope that it will be useful,      *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+ * GNU General Public License for more details.                         *
+ *                                                                      *
+ * You should have received a copy of the GNU General Public License    *
+ * along with PHP-P10; if not, write to the Free Software Foundation,   *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
+ *                                                                      *
+ ************************************************************************
+ * 
+ *  Uplink/P10_ModeSets.class.php
+ *
+ * classes to parse and store channel or user modes
+ *
+ */
+
+class P10_ChannelModeSet {
+
+}
+
+class P10_UserModeSet {
+       
+       public __construct($modes) {
+       
+       }
+       
+}
+
+?>
\ No newline at end of file
index 1603e4c84722a6455b7187e001a0e254addcab2f..41bf281d4c58ede41fd3fc2b90b397986a22a737 100644 (file)
@@ -76,6 +76,7 @@ class P10_Server {
        private $link_time;
        private $description;
        private $servers = array(); //all Servers connected to this Server
+       private $users = array(); //all Users connected to this Server
        
        public function __construct($name, $numeric, $parent_server, $start_time, $link_time, $description) {
                $this->name = $name;
@@ -103,6 +104,9 @@ class P10_Server {
        
        public function disconnectUsers() {
                //disconnect all Users connected to the actual Server
+               foreach($this->users as $user) {
+                       $user->quit("*.net *.split");
+               }
        }
        
        public function getNumeric() {
@@ -136,6 +140,18 @@ class P10_Server {
                        trigger_error("Tried to remove a Server, that does NOT exist.", E_USER_WARNING);
                }
        }
+       
+       public function addUser($user) {
+               $this->users[$user->getNumeric()] = $user;
+       }
+       
+       public function delUser($user) {
+               if(array_key_exists($user->getNumeric(), $this->users)) {
+                       unset($this->users[$user->getNumeric()]);
+               } else {
+                       trigger_error("Tried to remove a User, that does NOT exist.", E_USER_WARNING);
+               }
+       }
 }
 
 ?>
\ No newline at end of file
diff --git a/Uplink/P10_User.class.php b/Uplink/P10_User.class.php
new file mode 100644 (file)
index 0000000..bdc84e3
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+/********************************* PHP-P10 ******************************
+ *    P10 uplink class by pk910   (c)2011 pk910                         *
+ ************************************************************************
+ *                          Version 2 (OOP)                             *
+ *                                                                      *
+ * PHP-P10 is free software; you can redistribute it and/or modify      *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or    *
+ * (at your option) any later version.                                  *
+ *                                                                      *
+ * This program is distributed in the hope that it will be useful,      *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+ * GNU General Public License for more details.                         *
+ *                                                                      *
+ * You should have received a copy of the GNU General Public License    *
+ * along with PHP-P10; if not, write to the Free Software Foundation,   *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
+ *                                                                      *
+ ************************************************************************
+ * 
+ *  Uplink/P10_User.class.php
+ *
+ * This class represents a IRC User
+ *
+ ************************************************************************
+ * accessable methods:
+ *
+ * static P10_User getUserByNum(String $numeric) 
+ *     searches and returns the User with the provided Numeric
+ *
+ * __construct(String $nick, String $numeric, P10_Server $server, int $connect_time, String $ident, String $realname, P10_ModeSet $modes)
+ *     *** nothing to say here ***
+ *
+ */
+
+class P10_User {
+       private static $static_users = array();
+       
+       public static getUserByNum($numeric) {
+               if(array_key_exists($numeric, self::$static_servers)) {
+                       return self::$static_servers[$numeric];
+               }
+               return NULL;
+       }
+       
+       public static getUserByNick($nick) {
+               $nick = strtolower($nick);
+               foreach(self::$static_users as $user) {
+                       if(strtolower($user->getNick()) == $nick) {
+                               return $user;
+                       }
+               }
+               return NULL;
+       }
+       
+       
+       private $numeric;
+       private $nick;
+       private $ident;
+       private $host;
+       private $ip;
+       private $connect_time;
+       private $modes;
+       private $realname;
+       
+       public __construct($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes) {
+               $this->nick = $nick;
+               $this->numeric = $numeric;
+               $this->server = $server;
+               $this->connect_time = $connect_time;
+               $this->ident = $ident;
+               $this->host = $host;
+               $this->ip = $ip;
+               $this->realname = $realname;
+               $this->modes = $modes;
+               $server->addUser($this);
+               self::$static_users[$numeric] = $this;
+       }
+       
+       public function getNumeric() {
+               return $this->numeric;
+       }
+       
+       public function setNick($nick) {
+               $this->nick = $nick;
+       }
+       
+       public function getNick() {
+               return $this->nick;
+       }
+       
+       public function setIdent($ident) {
+               $this->ident = $ident;
+       }
+       
+       public function getIdent() {
+               return $this->ident;
+       }
+       
+       public function getHost() {
+               return $this->host;
+       }
+       
+       public function getIP() {
+               return $this->ip;
+       }
+       
+       public function getConnectTime() {
+               return $this->connect_time;
+       }
+       
+       public function getModes() {
+               return $this->modes;
+       }
+       
+       public function getRealname() {
+               return $this->realname;
+       }
+       
+       public function quit($reason) {
+               $this->server->delUser($this);
+       }
+}
+
+?>
\ No newline at end of file
index 2dee4da2f213caf4d93bc98d9c50bc7f1ba2f0d6..e7b8e9c9bc88fc9bf74a305169407fa01eac33f6 100644 (file)
@@ -50,6 +50,9 @@
 require_once("Client.class.php");
 require_once("Numerics.class.php");
 require_once("P10Formatter.class.php");
+require_once("P10_Server.class.php");
+require_once("P10_User.class.php");
+require_once("P10_ModeSets.class.php");
 
 class Uplink {
        private $client = new Client();
@@ -59,6 +62,7 @@ class Uplink {
        const FLAG_P10SESSION      = 0x0001; //connection is in P10 mode (server is connected)
        const FLAG_SECURITY_QUIT   = 0x0002; //local connection abort because of security issues
        const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
+       const FLAG_BURST_PENDING   = 0x0008; //we still have to burst
        private $flags = 0;
        
        public function __construct() {
@@ -185,6 +189,12 @@ class Uplink {
                        case "G":
                                $this->recv_ping($from, $arguments);
                                break;
+                       case "N":
+                               $this->recv_nick($from, $arguments);
+                               break;
+                       case "EB":
+                               $this->recv_end_of_burst($from, $arguments);
+                               break;
                //default
                        default:
                                //unknown cmd
@@ -234,7 +244,7 @@ class Uplink {
                        }
                        $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
                        $this->server->add_server($new_server);
-                       $this->flags |= self::FLAG_P10SESSION;
+                       $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
                } else {
                        //another server got a new slave server ^^
                        $server = P10_Server::getServerByNum($from);
@@ -251,6 +261,57 @@ class Uplink {
                $this->send("Z", $args[0]); //simply PONG
        }
        
+       private function recv_nick($from, $args) {
+               //[recv] AM N Zer0n|IRPG 2 1292194168 ~Zer0n c04D8C5.localIP +oiwgrftx Zer0n Zer0n.admin.WebGamesNet AKBNjF AMAAj :Zer0n IRPG - Will never answer here.
+               if(count($args) == 2) {
+                       //Nick change
+                       $user = P10_User::getUserByNum($from);
+                       if($user == null) {
+                               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]);
+               } else {
+                       //New User
+                       $numeric = $args[count($args)-2];
+                       $nick = $args[0];
+                       $server = P10_Server::getServerByNum($from);
+                       if($server == null) {
+                               trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
+                               return;
+                       }
+                       if(substr($numeric,0,2) != $from) {
+                               trigger_error("A Server (".$from.") tries to connect a User with an invalid User numeric ('".$numeric."' does not belong to the Server)", E_USER_WARNING);
+                       }
+                       $connect_time = $args[2];
+                       $ident = $args[3];
+                       $host = $args[4];
+                       $modes = implode(" ",array_slice($args, 5, count($args)-8));
+                       $modes = new 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);
+               }
+       }
+       
+       private function recv_end_of_burst($from, $args) {
+               if(($this->flags & self::FLAG_BURST_PENDING)) {
+                       $this->burst();
+                       $this->send("EA");
+                       $this->flags &= ~self::FLAG_BURST_PENDING;
+               }
+       }
+       
+       
+       
+       /********************************************************************************************
+        *                                     SERVER FUNCTIONS                                     *
+        ********************************************************************************************/
+       
+       private function burst() {
+               $this->send("EB");
+       }
+       
 }
 
 ?>
\ No newline at end of file