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