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