added ModCMD.class.php with simple binding principle
[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         /********************************************************************************************
64          *                                       EVENT HANDLER                                      *
65          ********************************************************************************************/
66         //All events trigger a binding. You may set the same binding for eg. CHANMODE and USERMODE (or some others)
67         //to get only one binding for both events... but i think that's senceless
68         const EVENT_NEWSERVER  = BIND_NEWSERVER;
69         const EVENT_SQUIT      = BIND_SQUIT;
70         const EVENT_CONNECT    = BIND_CONNECT;
71         const EVENT_NICK       = BIND_NICK;
72         const EVENT_USERMODE   = BIND_USERMODE;
73         const EVENT_QUIT       = BIND_QUIT;
74         const EVENT_JOIN       = BIND_JOIN;
75         const EVENT_PART       = BIND_PART;
76         const EVENT_KICK       = BIND_KICK;
77         const EVENT_CHANMODE   = BIND_CHANMODE;
78         const EVENT_CHANMSG    = BIND_CHANMSG;
79         const EVENT_PRIVMSG    = BIND_PRIVMSG;
80         const EVENT_CHANNOTICE = BIND_CHANNOTICE;
81         const EVENT_PRIVNOTICE = BIND_PRIVNOTICE;
82         const EVENT_PREPARSE   = BIND_PREPARSE;
83         const EVENT_UNKNOWNCMD = BIND_UNKNOWNCMD;
84         
85         private function event($type, $parameters) {
86                 if(array_key_exists($type, self::$bindings)) {
87                         foreach(self::$bindings as $binding) {
88                                 $binding->trigger();
89                         }
90                 }
91         }
92         
93         public function event_newserver($server, $isBurst) {
94                 $this->event(self::EVENT_NEWSERVER, array($server, $isBurst));
95         }
96         
97         public function event_squit($server){
98                 $this->event(self::EVENT_SQUIT, array($server));
99         }
100         
101         public function event_connect($user, $isBurst) {
102                 $this->event(self::EVENT_CONNECT, array($user, $isBurst));
103         }
104         
105         public function event_nick($user, $newNick) {
106                 $this->event(self::EVENT_NICK, array($user, $newNick));
107         }
108         
109         public function event_usermode($user, $modes) {
110                 $this->event(self::EVENT_USERMODE, array($user, $modes));
111         }
112         
113         public function event_quit($user, $reason) {
114                 $this->event(self::EVENT_QUIT, array($user, $reason));
115         }
116         
117         public function event_join($user, $channel, $isBurst) {
118                 $this->event(self::EVENT_JOIN, array($user, $channel, $isBurst));
119         }
120         
121         public function event_part($user, $channel, $reason) {
122                 $this->event(self::EVENT_PART, array($user, $channel, $reason));
123         }
124         
125         public function event_kick($user, $target, $channel, $reason) {
126                 $this->event(self::EVENT_KICK, array($user, $target, $channel, $reason));
127         }
128         
129         public function event_chanmode($user, $channel, $modes) {
130                 $this->event(self::EVENT_CHANMODE, array($user, $channel, $modes));
131         }
132         
133         public function event_chanmessage($user, $channel, $message) {
134                 $this->event(self::EVENT_CHANMSG, array($user, $channel, $message));
135         }
136         
137         public function event_channotice($user, $channel, $message) {
138                 $this->event(self::EVENT_CHANNOTICE, array($user, $channel, $message));
139         }
140         
141         public function event_privmessage($user, $channel, $message) {
142                 $this->event(self::EVENT_PRIVMSG, array($user, $channel, $message));
143         }
144         
145         public function event_privnotice($user, $channel, $message) {
146                 $this->event(self::EVENT_PRIVNOTICE, array($user, $channel, $message));
147         }
148         
149         public function event_preparse($from, $command, $arguments) {
150                 $this->event(self::EVENT_PREPARSE, array($from, $command, $arguments));
151         }
152         
153         public function event_unknown_cmd($from, $command, $arguments) {
154                 $this->event(self::EVENT_UNKNOWNCMD, array($from, $command, $arguments));
155         }
156         
157 }
158
159 ?>