added Bot class (simple method dummy) & fixed some bugs
[PHP-P10.git] / Uplink / Uplink.class.php
index 0585595fdce6a576cfff90eb9370b0c6131581ac..524d392c1084db3e72e6e8cb632c43d34e807a98 100644 (file)
@@ -47,6 +47,9 @@
  * void setValidateServer(String $name, String $password)
  *     sets additional security relevant information about the remote server.
  *
+ * void setHISOptions(String $serverName, String $serverDescription String $usermask)
+ *     sets the default Account fakehost
+ *
  * void setEventHandler(EventHandler $event_handler)
  *     sets the EventHandlder
  */
@@ -84,6 +87,7 @@ class Uplink {
        public function __construct() {
                $this->client = new Client();
                $this->setSetting("recv_timeout", 1000);
+               $this->setSetting("his_usermask", "user.NoMask");
        }
        
        public function initialize() {
@@ -161,6 +165,12 @@ class Uplink {
                $this->setSetting("their_password", $password);
        }
        
+       public function setHISOptions($servername, $serverdesc, $usermask) {
+               $this->setSetting("his_name", $servername);
+               $this->setSetting("his_desc", $serverdesc);
+               $this->setSetting("his_usermask", $usermask);
+       }
+       
        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);
@@ -254,6 +264,9 @@ class Uplink {
                        case "O":
                                $this->recv_notice($from, $arguments);
                                break;
+                       case "W":
+                               $this->recv_whois($from, $arguments);
+                               break;
                //default
                        default:
                                //unknown cmd
@@ -317,7 +330,7 @@ class Uplink {
                        $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));
+                               $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
                } else {
                        //another server got a new slave server ^^
                        $server = P10_Server::getServerByNum($from);
@@ -328,7 +341,7 @@ 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));
+                               $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
                }
        }
        
@@ -559,6 +572,78 @@ class Uplink {
                }
        }
        
+       private function recv_whois($from, $args) {
+               /* [get] ACAAF W AX :NetworkServ */
+               $fromUser = P10_User::getUserByNum($from);
+               if($fromUser == null) {
+                       trigger_error("Server tries to send a whois from an user that does not exist or was not found on recv_whois.", E_USER_ERROR);
+                       return;
+               }
+               $users=explode(",",$args[1]);
+               foreach($users as $nick) {
+                       $user = P10_User::getUserByNick($nick);
+                       if(!$user) {
+                               $this->send("401", $from, $nick);
+                               continue;
+                       }
+                       $nick = $user->getNick();
+                       $ident = $user->getIdent();
+                       $hostmask = $user->getHost();
+                       $modes = $user->getModes();
+                       if($modes->hasMode('x')) {
+                               if(($fakehost = $modes->hasMode('f'))) {
+                                       $hostmask = $fakehost;
+                               } elseif(($account = $modes->hasMode('r'))) {
+                                       $hostmask = $account.".".$this->getSetting("his_usermask");
+                               }
+                       }
+                       $realname = $user->getRealname();
+                       $this->send("311", $from , $nick, $ident, $hostmask, $realname);
+                       if(((!$modes->hasMode('n') && !$modes->hasMode('k')) || $from == $user->getNumeric()) && count($user->getChannels()) != 0) {
+                               $channels = "";
+                               foreach($user->getChannels() as $channel) {
+                                       $cmodes = $channel->getModes();
+                                       $privs = $channel->getUserPrivs($user);
+                                       if($cmodes->hasMode("s") && !$fromUser->isOnChannel($channel) && $from != $user->getNumeric()) continue;
+                                       if($cmodes->hasMode("u") && ($privs & (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE)) == 0 && $from != $user->getNumeric()) continue;
+                                       $chanstr = ($channels == "" ? "" : " ");
+                                       $prefix = "";
+                                       if(($privs & P10_Channel::USERPRIV_OPED)) {
+                                               $prefix = "@";
+                                       } else if(($privs & P10_Channel::USERPRIV_VOICE)) {
+                                               $prefix = "+";
+                                       }
+                                       $chanstr .= $prefix.$channel->getName();
+                                       if(strlen($channels) + strlen($chanstr) > 450) {
+                                               $this->send("319", $from, $nick, $channels);
+                                               $channels = $prefix.$channel->getName();
+                                       }
+                               }
+                               if($channels != "") {
+                                       $this->send("319", $from, $nick, $channels);
+                               }
+                               if($fromUser->getModes()->hasMode("o") || $from == $user->getNumeric() || !$this->getSetting("his_name")) {
+                                       $this->send("312", $from, $nick, $user->getServer()->getName(), $user->getServer()->getDescription());
+                               } else {
+                                       $this->send("312", $from, $nick, $this->getSetting("his_name"), $this->getSetting("his_desc"));
+                               }
+                               if($modes->hasMode("o") && (!$modes->hasMode("H") || $fromUser->getModes()->hasMode("o"))) {
+                                       if($modes->hasMode("S")) {
+                                               if($modes->hasMode("D"))
+                                                       $this->send("313", $from, $nick, "is a Network Service");
+                                               else
+                                                       $this->send("313", $from, $nick, "is an illegal Network Service - HACKER!");
+                                       } else
+                                               $this->send("313", $from, $nick, "is an IRC Operator");
+                               }
+                               if(($auth = $modes->hasMode("r"))) {
+                                       $this->send("330", $from, $nick, $auth);
+                               }
+                       }
+               }
+               $this->send("318", $from, $args[1]);
+       }
+       
        /********************************************************************************************
         *                                     SERVER FUNCTIONS                                     *
         ********************************************************************************************/