workaround for broken ipv6 numeric builder
[PHP-P10.git] / Bots / IPv6.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Bots/ModManager.class.php
24  *
25  * module manager bot...
26  *
27  */
28
29 class {$_NAME} extends Bot {
30         private $uplink;
31         private $ipv6;
32         private $cache = array(
33                                                 0 => array(),
34                                                 1 => array()
35                                         );
36         
37         public function load($uplink, $old = false) {
38                 $this->uplink = $uplink;
39                 if(!$old) {
40                         $nick = "IPv6";
41                         $ident = "ipv6";
42                         $ip = "0::0";
43                         $host = "fd00::C0CA:C01A:ADD5:11FE"; //coca cola adds life   maybe someone undestands it :D
44                         $realname = "IPv6";
45                         $modes = "ioknISD";
46                         $this->ipv6 = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
47                         if(is_a($this->ipv6, "P10_User")) {
48                                 $this->uplink->join($this->ipv6, "#IPv6", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
49                                 $this->uplink->join($this->ipv6, "#dev", P10_Channel::USERPRIV_VOICE);
50                         }
51                 } else {
52                         $this->ipv6 = $old;
53                 }
54                 
55                 ModCMD::bind($this, BIND_JOIN, "recive_join");
56                 ModCMD::bind($this, BIND_CHANMODE, "recive_mode");
57                 ModCMD::bind($this, BIND_KICK, "recive_kick");
58                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
59         }
60         
61         public function unload($rehash = false) {
62                 if($rehash) {
63                         return $this->ipv6;
64                 } else {
65                         $this->uplink->delUser($this->ipv6, "Bye.");
66                 }
67         }
68         
69         public function loop() {
70                 for($i = 0; $i < 2; $i++) {
71                         foreach($this->cache[$i] as $id => $cache) {
72                                 if($cache < time()) unset($this->cache[$i][$id]);
73                         }
74                 }
75         }
76         
77         private function botOppedOnChannel($channel) {
78                 $privs = $channel->getUserPrivs($this->ipv6);
79                 return ($privs & P10_Channel::USERPRIV_OPED);
80         }
81         
82         public function recive_join($user, $channel, $isBurst) {
83                 if(!$this->botOppedOnChannel($channel)) return false;
84                 $ipv6 = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))(|\/[0-9]{1,3})$/';
85                 if(preg_match($ipv6, $user->getIP())) {
86                         $privs = $channel->getUserPrivs($user);
87                         $privs |= (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE);
88                         $channel->setUserPrivs($user, $privs);
89                         $this->uplink->mode($this->ipv6, $channel, "+ov ".$user->getNumeric()." ".$user->getNumeric());
90                 } else {
91                         $privs = $channel->getUserPrivs($user);
92                         $privs |= (P10_Channel::USERPRIV_VOICE);
93                         $channel->setUserPrivs($user, $privs);
94                         $this->uplink->mode($this->ipv6, $channel, "+v ".$user->getNumeric());
95                 }
96         }
97         
98         public function recive_mode($user, $channel, $modes) {
99                 if($user->getModes()->hasMode('o') || !$this->botOppedOnChannel($channel)) return false;
100                 if(array_key_exists($user->getNumeric(), $this->cache[0]) && $this->cache[0][$user->getNumeric()]['time'] > time()) {
101                         $this->uplink->kick($this->ipv6, $user, $channel, "you're not allowed to change modes in this channel!");
102                 } else {
103                         $this->uplink->notice($this->ipv6, $user, "you're not allowed to change modes in this channel!");
104                         $this->cache[0][$user->getNumeric()] = time() + 600;
105                 }
106                 $mode = explode(" ",$modes);
107                 $modestr="";
108                 $params="";
109                 $p=0;
110                 for($i=0;$i<strlen($mode[0]);$i++) {
111                         switch ($mode[0][$i]) {
112                                 case "+":
113                                         $mode[0][$i] = "-";
114                                         break;
115                                 case "-":
116                                         $mode[0][$i] = "+";
117                                         break;
118                         }
119                 }
120                 $modes = implode(" ",$mode);
121                 $this->uplink->mode($this->ipv6, $channel, $modes);
122         }
123         
124         public function recive_kick($user, $target, $channel, $modes) {
125                 if($user->getModes()->hasMode('o') || !$this->botOppedOnChannel($channel)) return false;
126                 if(array_key_exists($user->getNumeric(), $this->cache[1]) && $this->cache[1][$user->getNumeric()]['time'] > time()) {
127                         $this->uplink->delUser($user, "You have been warned not to kick users from this Channel!");
128                 } else {
129                         $this->uplink->kick($this->ipv6, $user, $channel, "you're not allowed to kick users from this channel!");
130                         $this->cache[1][$user->getNumeric()] = time() + 600;
131                 }
132         }
133         
134         public function recive_quit($user, $reason) {
135                 if($user === $this->ipv6) {
136                         $this->load($this->uplink);
137                 }
138                 for($i = 0; $i < 2; $i++) {
139                         if(array_key_exists($user->getNumeric(), $this->cache[$i])) {
140                                 unset($this->cache[$i][$user->getNumeric()]);
141                         }
142                 }
143         }
144 }
145
146 ?>