changed file headers & added AUTHORS file
[PHP-P10.git] / ModCMD / ModCMD.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  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) {
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))
65                                                 return;
66                                 }
67                         } else 
68                                 self::$bindings[$type] = array();
69                         self::$bindings[$type][] = new Binding($bot, $method);
70                 }
71         }
72         
73         public static function unbind($bot, $type, $method) {
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)) {
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)) {
91                                                 unset(self::$bindings[$type][$id]);
92                                         }
93                                 }
94                         }
95                 }
96         }
97         
98         /********************************************************************************************
99          *                                       EVENT HANDLER                                      *
100          ********************************************************************************************/
101         
102         private function event($type, $parameters) {
103                 if(array_key_exists($type, self::$bindings)) {
104                         foreach(self::$bindings[$type] as $binding) {
105                                 $binding->trigger($parameters);
106                         }
107                 }
108         }
109         
110         public function event_newserver($server, $isBurst) {
111                 $this->event(BIND_NEWSERVER, array($server, $isBurst));
112         }
113         
114         public function event_squit($server){
115                 $this->event(BIND_SQUIT, array($server));
116         }
117         
118         public function event_connect($user, $isBurst) {
119                 $this->event(BIND_CONNECT, array($user, $isBurst));
120         }
121         
122         public function event_nick($user, $newNick) {
123                 $this->event(BIND_NICK, array($user, $newNick));
124         }
125         
126         public function event_usermode($user, $modes) {
127                 $this->event(BIND_USERMODE, array($user, $modes));
128         }
129         
130         public function event_quit($user, $reason) {
131                 $this->event(BIND_QUIT, array($user, $reason));
132         }
133         
134         public function event_join($user, $channel, $isBurst) {
135                 $this->event(BIND_JOIN, array($user, $channel, $isBurst));
136         }
137         
138         public function event_part($user, $channel, $reason) {
139                 $this->event(BIND_PART, array($user, $channel, $reason));
140         }
141         
142         public function event_kick($user, $target, $channel, $reason) {
143                 $this->event(BIND_KICK, array($user, $target, $channel, $reason));
144         }
145         
146         public function event_chanmode($user, $channel, $modes) {
147                 $this->event(BIND_CHANMODE, array($user, $channel, $modes));
148         }
149         
150         public function event_chanmessage($user, $channel, $message) {
151                 $this->event(BIND_CHANMSG, array($user, $channel, $message));
152         }
153         
154         public function event_channotice($user, $channel, $message) {
155                 $this->event(BIND_CHANNOTICE, array($user, $channel, $message));
156         }
157         
158         public function event_privmessage($user, $target, $message) {
159                 $this->event(BIND_PRIVMSG, array($user, $target, $message));
160         }
161         
162         public function event_privnotice($user, $target, $message) {
163                 $this->event(BIND_PRIVNOTICE, array($user, $target, $message));
164         }
165         
166         public function event_preparse($from, $command, $arguments) {
167                 $this->event(BIND_PREPARSE, array($from, $command, $arguments));
168         }
169         
170         public function event_unknown_cmd($from, $command, $arguments) {
171                 $this->event(BIND_UNKNOWNCMD, array($from, $command, $arguments));
172         }
173         
174         public function event_chanctcp($user, $channel, $command, $text) {
175                 $this->event(BIND_CTCP, array($user, $channel, $command, $text, true));
176         }
177         
178         public function event_chanctcpreply($user, $target, $command, $text) {
179                 $this->event(BIND_CTCPREPLY, array($user, $channel, $command, $text, true));
180         }
181         
182         public function event_privctcp($user, $target, $command, $text) {
183                 $this->event(BIND_CTCP, array($user, $channel, $command, $text, false));
184         }
185         
186         public function event_privctcpreply($user, $target, $command, $text) {
187                 $this->event(BIND_CTCPREPLY, array($user, $channel, $command, $text, false));
188         }
189         
190         public function event_away($user, $away) {
191                 $this->event(BIND_AWAY, array($user, $away));
192         }
193         
194 }
195
196 ?>