e1b81cd87a5b16dc06c96a2430f976f8f6ac441e
[PHP-P10.git] / ModCMD / ModCMD.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  *  ModCMD/ModCMD.class.php
24  *
25  * shares the incoming events to all bot's that request them.
26  *
27  ************************************************************************
28  * 
29  * 
30  */
31 require_once("Binding.class.php");
32  
33 $bindid = 1;
34 define("BIND_NEWSERVER", $bindid++);
35 define("BIND_SQUIT", $bindid++);
36 define("BIND_CONNECT", $bindid++);
37 define("BIND_NICK", $bindid++);
38 define("BIND_USERMODE", $bindid++);
39 define("BIND_QUIT", $bindid++);
40 define("BIND_JOIN", $bindid++);
41 define("BIND_PART", $bindid++);
42 define("BIND_KICK", $bindid++);
43 define("BIND_AWAY", $bindid++);
44 define("BIND_CHANMODE", $bindid++);
45 define("BIND_CHANMSG", $bindid++);
46 define("BIND_PRIVMSG", $bindid++);
47 define("BIND_CHANNOTICE", $bindid++);
48 define("BIND_PRIVNOTICE", $bindid++);
49 define("BIND_CTCP", $bindid++);
50 define("BIND_CTCPREPLY", $bindid++);
51 define("BIND_PREPARSE", $bindid++);
52 define("BIND_UNKNOWNCMD", $bindid++);
53
54  
55 class ModCMD implements EventHandler {
56         private static $eventHandler = null;
57         private static $bindings = array();
58         
59         public static function getEventHandler() {
60                 if(self::$eventHandler == null) {
61                         self::$eventHandler = new ModCMD();
62                 }
63                 return self::$eventHandler;
64         }
65         
66         public static function bind($bot, $type, $method) {
67                 if(is_a($bot, "Bot") && method_exists($bot, $method)) {
68                         if(array_key_exists($type, self::$bindings)) {
69                                 foreach(self::$bindings[$type] as $binding) {
70                                         if($binding->match($bot, $method))
71                                                 return;
72                                 }
73                         } else 
74                                 self::$bindings[$type] = array();
75                         self::$bindings[$type][] = new Binding($bot, $method);
76                 }
77         }
78         
79         public static function unbind($bot, $type, $method) {
80                 if(is_a($bot, "Bot")) {
81                         if(array_key_exists($type, self::$bindings)) {
82                                 foreach(self::$bindings[$type] as $id => $binding) {
83                                         if($binding->match($bot, $method)) {
84                                                 unset(self::$bindings[$type][$id]);
85                                                 break;
86                                         }
87                                 }
88                         }
89                 }
90         }
91         
92         public static function unbindBot($bot) {
93                 if(is_a($bot, "Bot")) {
94                         foreach(self::$bindings as $type => $bindings) {
95                                 foreach($bindings as $id => $binding) {
96                                         if($binding->match($bot, null)) {
97                                                 unset(self::$bindings[$type][$id]);
98                                         }
99                                 }
100                         }
101                 }
102         }
103         
104         /********************************************************************************************
105          *                                       EVENT HANDLER                                      *
106          ********************************************************************************************/
107         
108         private function event($type, $parameters) {
109                 if(array_key_exists($type, self::$bindings)) {
110                         foreach(self::$bindings[$type] as $binding) {
111                                 $binding->trigger($parameters);
112                         }
113                 }
114         }
115         
116         public function event_newserver($server, $isBurst) {
117                 $this->event(BIND_NEWSERVER, array($server, $isBurst));
118         }
119         
120         public function event_squit($server){
121                 $this->event(BIND_SQUIT, array($server));
122         }
123         
124         public function event_connect($user, $isBurst) {
125                 $this->event(BIND_CONNECT, array($user, $isBurst));
126         }
127         
128         public function event_nick($user, $newNick) {
129                 $this->event(BIND_NICK, array($user, $newNick));
130         }
131         
132         public function event_usermode($user, $modes) {
133                 $this->event(BIND_USERMODE, array($user, $modes));
134         }
135         
136         public function event_quit($user, $reason) {
137                 $this->event(BIND_QUIT, array($user, $reason));
138         }
139         
140         public function event_join($user, $channel, $isBurst) {
141                 $this->event(BIND_JOIN, array($user, $channel, $isBurst));
142         }
143         
144         public function event_part($user, $channel, $reason) {
145                 $this->event(BIND_PART, array($user, $channel, $reason));
146         }
147         
148         public function event_kick($user, $target, $channel, $reason) {
149                 $this->event(BIND_KICK, array($user, $target, $channel, $reason));
150         }
151         
152         public function event_chanmode($user, $channel, $modes) {
153                 $this->event(BIND_CHANMODE, array($user, $channel, $modes));
154         }
155         
156         public function event_chanmessage($user, $channel, $message) {
157                 $this->event(BIND_CHANMSG, array($user, $channel, $message));
158         }
159         
160         public function event_channotice($user, $channel, $message) {
161                 $this->event(BIND_CHANNOTICE, array($user, $channel, $message));
162         }
163         
164         public function event_privmessage($user, $target, $message) {
165                 $this->event(BIND_PRIVMSG, array($user, $target, $message));
166         }
167         
168         public function event_privnotice($user, $target, $message) {
169                 $this->event(BIND_PRIVNOTICE, array($user, $target, $message));
170         }
171         
172         public function event_preparse($from, $command, $arguments) {
173                 $this->event(BIND_PREPARSE, array($from, $command, $arguments));
174         }
175         
176         public function event_unknown_cmd($from, $command, $arguments) {
177                 $this->event(BIND_UNKNOWNCMD, array($from, $command, $arguments));
178         }
179         
180         public function event_chanctcp($user, $channel, $command, $text) {
181                 $this->event(BIND_CTCP, array($user, $channel, $command, $text, true));
182         }
183         
184         public function event_chanctcpreply($user, $target, $command, $text) {
185                 $this->event(BIND_CTCPREPLY, array($user, $channel, $command, $text, true));
186         }
187         
188         public function event_privctcp($user, $target, $command, $text) {
189                 $this->event(BIND_CTCP, array($user, $channel, $command, $text, false));
190         }
191         
192         public function event_privctcpreply($user, $target, $command, $text) {
193                 $this->event(BIND_CTCPREPLY, array($user, $channel, $command, $text, false));
194         }
195         
196         public function event_away($user, $away) {
197                 $this->event(BIND_AWAY, array($user, $away));
198         }
199         
200 }
201
202 ?>