finished first "alpha" version of PHP-P10 v2
[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_CHANMODE", $bindid++);
44 define("BIND_CHANMSG", $bindid++);
45 define("BIND_PRIVMSG", $bindid++);
46 define("BIND_CHANNOTICE", $bindid++);
47 define("BIND_PRIVNOTICE", $bindid++);
48 define("BIND_PREPARSE", $bindid++);
49 define("BIND_UNKNOWNCMD", $bindid++);
50
51  
52 class ModCMD implements EventHandler {
53         private static $eventHandler = null;
54         private static $bindings = array();
55         
56         public static function getEventHandler() {
57                 if(self::$eventHandler == null) {
58                         self::$eventHandler = new ModCMD();
59                 }
60                 return self::$eventHandler;
61         }
62         
63         public static function bind($bot, $type, $method) {
64                 if(is_a($bot, "Bot") && method_exists($bot, $method)) {
65                         if(array_key_exists($type, self::$bindings)) {
66                                 foreach(self::$bindings[$type] as $binding) {
67                                         if($binding->match($bot, $method))
68                                                 return;
69                                 }
70                         } else 
71                                 self::$bindings[$type] = array();
72                         self::$bindings[$type][] = new Binding($bot, $method);
73                 }
74         }
75         
76         public static function unbind($bot, $type, $method) {
77                 if(is_a($bot, "Bot")) {
78                         if(array_key_exists($type, self::$bindings)) {
79                                 foreach(self::$bindings[$type] as $id => $binding) {
80                                         if($binding->match($bot, $method)) {
81                                                 unset(self::$bindings[$type][$id]);
82                                                 break;
83                                         }
84                                 }
85                         }
86                 }
87         }
88         
89         public static function unbindBot($bot) {
90                 if(is_a($bot, "Bot")) {
91                         foreach(self::$bindings as $type => $bindings) {
92                                 foreach($bindings as $id => $binding) {
93                                         if($binding->match($bot, null)) {
94                                                 unset(self::$bindings[$type][$id]);
95                                         }
96                                 }
97                         }
98                 }
99         }
100         
101         /********************************************************************************************
102          *                                       EVENT HANDLER                                      *
103          ********************************************************************************************/
104         //All events trigger a binding. You may set the same binding for eg. CHANMODE and USERMODE (or some others)
105         //to get only one binding for both events... but i think that's senceless
106         const EVENT_NEWSERVER  = BIND_NEWSERVER;
107         const EVENT_SQUIT      = BIND_SQUIT;
108         const EVENT_CONNECT    = BIND_CONNECT;
109         const EVENT_NICK       = BIND_NICK;
110         const EVENT_USERMODE   = BIND_USERMODE;
111         const EVENT_QUIT       = BIND_QUIT;
112         const EVENT_JOIN       = BIND_JOIN;
113         const EVENT_PART       = BIND_PART;
114         const EVENT_KICK       = BIND_KICK;
115         const EVENT_CHANMODE   = BIND_CHANMODE;
116         const EVENT_CHANMSG    = BIND_CHANMSG;
117         const EVENT_PRIVMSG    = BIND_PRIVMSG;
118         const EVENT_CHANNOTICE = BIND_CHANNOTICE;
119         const EVENT_PRIVNOTICE = BIND_PRIVNOTICE;
120         const EVENT_PREPARSE   = BIND_PREPARSE;
121         const EVENT_UNKNOWNCMD = BIND_UNKNOWNCMD;
122         
123         private function event($type, $parameters) {
124                 if(array_key_exists($type, self::$bindings)) {
125                         foreach(self::$bindings[$type] as $binding) {
126                                 $binding->trigger($parameters);
127                         }
128                 }
129         }
130         
131         public function event_newserver($server, $isBurst) {
132                 $this->event(self::EVENT_NEWSERVER, array($server, $isBurst));
133         }
134         
135         public function event_squit($server){
136                 $this->event(self::EVENT_SQUIT, array($server));
137         }
138         
139         public function event_connect($user, $isBurst) {
140                 $this->event(self::EVENT_CONNECT, array($user, $isBurst));
141         }
142         
143         public function event_nick($user, $newNick) {
144                 $this->event(self::EVENT_NICK, array($user, $newNick));
145         }
146         
147         public function event_usermode($user, $modes) {
148                 $this->event(self::EVENT_USERMODE, array($user, $modes));
149         }
150         
151         public function event_quit($user, $reason) {
152                 $this->event(self::EVENT_QUIT, array($user, $reason));
153         }
154         
155         public function event_join($user, $channel, $isBurst) {
156                 $this->event(self::EVENT_JOIN, array($user, $channel, $isBurst));
157         }
158         
159         public function event_part($user, $channel, $reason) {
160                 $this->event(self::EVENT_PART, array($user, $channel, $reason));
161         }
162         
163         public function event_kick($user, $target, $channel, $reason) {
164                 $this->event(self::EVENT_KICK, array($user, $target, $channel, $reason));
165         }
166         
167         public function event_chanmode($user, $channel, $modes) {
168                 $this->event(self::EVENT_CHANMODE, array($user, $channel, $modes));
169         }
170         
171         public function event_chanmessage($user, $channel, $message) {
172                 $this->event(self::EVENT_CHANMSG, array($user, $channel, $message));
173         }
174         
175         public function event_channotice($user, $channel, $message) {
176                 $this->event(self::EVENT_CHANNOTICE, array($user, $channel, $message));
177         }
178         
179         public function event_privmessage($user, $channel, $message) {
180                 $this->event(self::EVENT_PRIVMSG, array($user, $channel, $message));
181         }
182         
183         public function event_privnotice($user, $channel, $message) {
184                 $this->event(self::EVENT_PRIVNOTICE, array($user, $channel, $message));
185         }
186         
187         public function event_preparse($from, $command, $arguments) {
188                 $this->event(self::EVENT_PREPARSE, array($from, $command, $arguments));
189         }
190         
191         public function event_unknown_cmd($from, $command, $arguments) {
192                 $this->event(self::EVENT_UNKNOWNCMD, array($from, $command, $arguments));
193         }
194         
195 }
196
197 ?>