2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011 Philipp Kreil (pk910) *
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. *
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. *
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/>. *
18 ************************************************************************
20 * ModCMD/ModCMD.class.php
22 * shares the incoming events to all bot's that request them.
25 require_once("Binding.class.php");
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++);
49 class ModCMD implements EventHandler {
50 private static $eventHandler = null;
51 private static $bindings = array();
53 public static function getEventHandler() {
54 if(self::$eventHandler == null) {
55 self::$eventHandler = new ModCMD();
57 return self::$eventHandler;
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))
68 self::$bindings[$type] = array();
69 self::$bindings[$type][] = new Binding($bot, $method);
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]);
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]);
98 /********************************************************************************************
100 ********************************************************************************************/
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);
110 public function event_newserver($server, $isBurst) {
111 $this->event(BIND_NEWSERVER, array($server, $isBurst));
114 public function event_squit($server){
115 $this->event(BIND_SQUIT, array($server));
118 public function event_connect($user, $isBurst) {
119 $this->event(BIND_CONNECT, array($user, $isBurst));
122 public function event_nick($user, $newNick) {
123 $this->event(BIND_NICK, array($user, $newNick));
126 public function event_usermode($user, $modes) {
127 $this->event(BIND_USERMODE, array($user, $modes));
130 public function event_quit($user, $reason) {
131 $this->event(BIND_QUIT, array($user, $reason));
134 public function event_join($user, $channel, $isBurst) {
135 $this->event(BIND_JOIN, array($user, $channel, $isBurst));
138 public function event_part($user, $channel, $reason) {
139 $this->event(BIND_PART, array($user, $channel, $reason));
142 public function event_kick($user, $target, $channel, $reason) {
143 $this->event(BIND_KICK, array($user, $target, $channel, $reason));
146 public function event_chanmode($user, $channel, $modes) {
147 $this->event(BIND_CHANMODE, array($user, $channel, $modes));
150 public function event_chanmessage($user, $channel, $message) {
151 $this->event(BIND_CHANMSG, array($user, $channel, $message));
154 public function event_channotice($user, $channel, $message) {
155 $this->event(BIND_CHANNOTICE, array($user, $channel, $message));
158 public function event_privmessage($user, $target, $message) {
159 $this->event(BIND_PRIVMSG, array($user, $target, $message));
162 public function event_privnotice($user, $target, $message) {
163 $this->event(BIND_PRIVNOTICE, array($user, $target, $message));
166 public function event_preparse($from, $command, $arguments) {
167 $this->event(BIND_PREPARSE, array($from, $command, $arguments));
170 public function event_unknown_cmd($from, $command, $arguments) {
171 $this->event(BIND_UNKNOWNCMD, array($from, $command, $arguments));
174 public function event_chanctcp($user, $channel, $command, $text) {
175 $this->event(BIND_CTCP, array($user, $channel, $command, $text, true));
178 public function event_chanctcpreply($user, $target, $command, $text) {
179 $this->event(BIND_CTCPREPLY, array($user, $channel, $command, $text, true));
182 public function event_privctcp($user, $target, $command, $text) {
183 $this->event(BIND_CTCP, array($user, $channel, $command, $text, false));
186 public function event_privctcpreply($user, $target, $command, $text) {
187 $this->event(BIND_CTCPREPLY, array($user, $channel, $command, $text, false));
190 public function event_away($user, $away) {
191 $this->event(BIND_AWAY, array($user, $away));