added P10_Channel.class.php and recv_burst
authorpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 10:02:50 +0000 (12:02 +0200)
committerpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 10:02:50 +0000 (12:02 +0200)
Uplink/P10_Channel.class.php [new file with mode: 0644]
Uplink/P10_ModeSets.class.php
Uplink/P10_User.class.php
Uplink/Uplink.class.php

diff --git a/Uplink/P10_Channel.class.php b/Uplink/P10_Channel.class.php
new file mode 100644 (file)
index 0000000..efe960d
--- /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_Channel.class.php
+ *
+ * This class represents a IRC Channel
+ *
+ ************************************************************************
+ * accessable methods:
+ *
+ * static P10_User getChannelByName(String $name) 
+ *     searches and returns the Channel with the provided name
+ *
+ * __construct(String $name)
+ *     *** nothing to say here ***
+ *
+ */
+
+class P10_Channel {
+       private static $static_channels = array();
+       
+       public static function getChannelByName($name) {
+               $name = strtolower($name);
+               if(array_key_exists($name, self::$static_channels)) {
+                       return self::$static_channels[$name];
+               }
+               return NULL;
+       }
+       
+       
+       private $name;
+       private $topic;
+       private $modes;
+       private $users = array();
+       const USERPRIV_OPED  = 0x0001;
+       const USERPRIV_VOICE = 0x0002;
+       private $userPrivs = array();
+       
+       public function __construct($name) {
+               $this->name = $name;
+               $this->modes = new P10_ChannelModeSet($this);
+               self::$static_channels[strtolower($name)] = $this;
+       }
+       
+       public function getName() {
+               return $this->name;
+       }
+       
+       public function getModes() {
+               return $this->modes;
+       }
+       
+       public function setTopic($topic) {
+               $this->topic = $topic;
+       }
+       
+       public function getTopic() {
+               return $this->topic;
+       }
+       
+       public function joinUser($user) {
+               $this->users[$user->getNumeric()] = $user;
+               $this->userPrivs[$user->getNumeric()] = 0;
+               $user->addChannel($this);
+       }
+       
+       public function burstUser($user, $opped, $voiced) {
+               $this->users[$user->getNumeric()] = $user;
+               $this->userPrivs[$user->getNumeric()] = ($opped ? self::USERPRIV_OPED : 0) | ($voiced ? self::USERPRIV_VOICE : 0);
+               $user->addChannel($this);
+       }
+       
+       public function quitUser($user) {
+               if(array_key_exists($user->getNumeric(), $this->users)) {
+                       unset($this->users[$user->getNumeric()]);
+                       unset($this->userPrivs[$user->getNumeric()]);
+                       //$user->delChannel($this)  is not necessary because the user quits (the whole Object gets removed later)
+               } else {
+                       trigger_error("Tried to quit a User from a Channel it is not joined.", E_USER_WARNING);
+               }
+       }
+       
+       public function partUser($user) {
+               if(array_key_exists($user->getNumeric(), $this->users)) {
+                       unset($this->users[$user->getNumeric()]);
+                       unset($this->userPrivs[$user->getNumeric()]);
+                       $user->delChannel($this);
+               } else {
+                       trigger_error("Tried to part a User from a Channel it is not joined.", E_USER_WARNING);
+               }
+       }
+       
+       public function getUserPrivs($user) {
+               if(array_key_exists($user->getNumeric(), $this->users)) {
+                       return $this->userPrivs[$user->getNumeric()];
+               } else 
+                       return 0;
+       }
+       
+       public function setUserPrivs($user, $privs) {
+               if(array_key_exists($user->getNumeric(), $this->users)) {
+                       $this->userPrivs[$user->getNumeric()] = $privs;
+               }
+       }
+       
+}
+
+?>
\ No newline at end of file
index 83611b8df4fb1039156292acf415dba4b866ea08..264a53f94d7c86f711b25f6be3f80a37810937fa 100644 (file)
@@ -69,13 +69,18 @@ class P10_ChannelModeSet {
                "D" => self::MODE_TYPE_D,
                "d" => self::MODE_TYPE_D,
                "R" => self::MODE_TYPE_D,
-               "z" => self::MODE_TYPE_D
+               "z" => self::MODE_TYPE_D,
+               
+               //special behavior
+               "o" => self::MODE_TYPE_B,
+               "v" => self::MODE_TYPE_B
        );
        private static $modevalues = null;
        private $modeflags = 0;
        private $modeparams = array();
+       private $channel;
        
-       public function __construct($modes) {
+       public function __construct($channel) {
                if(self::$modevalues == null) {
                        //build modevalues array
                        $flag = 1;
@@ -85,7 +90,7 @@ class P10_ChannelModeSet {
                                $flag <<= 1;
                        }
                }
-               $this->parseModes($modes);
+               $this->channel = $channel;
        }
        
        public function parseModes($modes) {
@@ -109,6 +114,7 @@ class P10_ChannelModeSet {
                                $this->modeparams[$mode] = $args[$c++];
                        }
                }
+               return $c-1;
        }
        
        public function setModes($modes, $returndiff = false) {
@@ -133,6 +139,18 @@ class P10_ChannelModeSet {
                                trigger_error("unknown mode (".$mode.") on setModes (".$modes.").", E_USER_WARNING);
                                continue;
                        }
+                       if($mode == "o" || $mode == "v") {
+                               if($this->setPrivs($add, $mode, $args[$c++])) {
+                                       if($returndiff && $add) {
+                                               $modestradd .= $mode;
+                                               $paramstradd .= " ".$args[$c-1];
+                                       } else if($returndiff && !$add) {
+                                               $modestrdel .= $mode;
+                                               $paramstrdel .= " ".$args[$c-1];
+                                       }
+                               }
+                               continue;
+                       }
                        $flag = self::$modevalues[$mode];
                        if($add) {
                                if($returndiff && !($this->modeflags & $flag)) {
@@ -167,6 +185,25 @@ class P10_ChannelModeSet {
                }
        }
        
+       private function setPrivs($add, $mode, $user) {
+               $user = P10_User::getUserByNum($user);
+               if($user == null) {
+                       trigger_error("Tried to set privs on a User that does not exist.", E_USER_ERROR);
+                       return;
+               }
+               $privs = $this->channel->getUserPrivs($user);
+               $privFlag = 0;
+               if($mode == "o") $privFlag = P10_Channel::USERPRIV_OPED;
+               if($mode == "v") $privFlag = P10_Channel::USERPRIV_VOICE;
+               if(!($add xor ($privs & $privFlag)))
+                       return false;
+               if($add) $privs |= $privFlag;
+               else $privs &= ~$privFlag;
+               $this->channel->setUserPrivs($user, $privs);
+               return true;
+               
+       }
+       
        public function getModeString() {
                $modestr = "+";
                $paramstr = "";
index a05f40bb664dded9c99a15c0e02b7ce08db4053d..83169bfe4ada43fa419ec21df9d51a8cdc92e0dc 100644 (file)
@@ -64,6 +64,7 @@ class P10_User {
        private $connect_time;
        private $modes;
        private $realname;
+       private $channels;
        
        public function __construct($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes) {
                $this->nick = $nick;
@@ -121,6 +122,21 @@ class P10_User {
        
        public function quit($reason) {
                $this->server->delUser($this);
+               foreach($this->channels as $channel) {
+                       $channel->quitUser($this);
+               }
+       }
+       
+       public function addChannel($channel) {
+               $this->channels[strtolower($channel->getName())] = $channel;
+       }
+       
+       public function delChannel($channel) {
+               if(array_key_exists(strtolower($channel->getName()), $this->channels)) {
+                       unset($this->channels[strtolower($channel->getName())]);
+               } else {
+                       trigger_error("Tried to remove a Channel, that does NOT exist.", E_USER_WARNING);
+               }
        }
 }
 
index 7cd1d77b601a70a77abdb22ce5bc359c45f7e50e..5c86c95b6afb0d6e183892e85b9d190b63a9e9e9 100644 (file)
@@ -52,6 +52,7 @@ 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_Channel.class.php");
 require_once("P10_ModeSets.class.php");
 
 class Uplink {
@@ -205,6 +206,9 @@ class Uplink {
                        case "Q":
                                $this->recv_quit($from, $arguments);
                                break;
+                       case "B":
+                               $this->recv_burst($from, $arguments);
+                               break;
                //default
                        default:
                                //unknown cmd
@@ -335,6 +339,51 @@ class Uplink {
                $user->quit($args[0]);
        }
        
+       private function recv_burst($from, $args) {
+               //AK B #aide 1292621006 +tn ALAMH:o
+               $name = $args[0];
+               $create_time = $args[1];
+               $channel = P10_Channel::getChannelByName($name);
+               if($name == null)
+                       $channel = new P10_Channel($name);
+               $modes = $channel->getModes();
+               $userstr = $args[count($args)-1];
+               $modeparamcount = count($args)-3;
+               if($userstr[0] == "%") {
+                       //ban list
+                       $banlist = explode(" ", substr($userstr, 1));
+                       foreach($banlist as $ban) {
+                               //TODO: save bans
+                       }
+                       $userstr = $args[count($args)-2];
+                       $modeparamcount--;
+               }
+               $users = explode(":",$userstr);
+               $isop = false; $isvoice = false;
+               foreach($users as $user) {
+                       $uexp = explode(":", $user);
+                       if(strlen($uexp[0]) != 6) {
+                               trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
+                               return;
+                       }
+                       if(count($uexp) > 1) {
+                               $isop = false;
+                               $isvoice = false;
+                               for($i = 0; $i < strlen($uexp[1]); $i++) {
+                                       if($uexp[1][0] == "@") $isop = true;
+                                       if($uexp[1][0] == "+") $isvoice = true;
+                               }
+                       }
+                       $user = P10_User::getUserByNum($uexp[0]);
+                       if($user == null) {
+                               trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
+                               return;
+                       }
+                       $channel->burstUser($user, $isop, $isvoice);
+               }
+               $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
+       }
+       
        /********************************************************************************************
         *                                     SERVER FUNCTIONS                                     *
         ********************************************************************************************/