3 // old file, will be deleted
7 public $IRCConn, $formater;
10 public $current_msg_info;
11 public $plugins = array();
14 private $core_commands;
17 // Constructeur de la classe
18 public function __construct($server, $port, $channel, $name, $myip, $mydomain) {
22 // Récupération d'une connexion unique
23 IRCConn
::Init($server, $port, $channel, $name, $myip, $mydomain);
24 $this->IRCConn
= IRCConn
::GetInstance();
26 $this->formater
= text_format
::GetInstance();
28 // core plugin is an exeption in do_command
29 self
::add_command('core','shownick',0,'Show the current nick used (debug)');
30 self
::add_command('core','nick',1,'Change the current nick');
31 self
::add_command('core','quit',0,'Disconnect and stop the process');
32 self
::add_command('core','restart',0,'Disconnect and restart the process');
33 self
::add_command('core','help',0,'Hmm, je dois recoder cette fonction');
36 public function shownick() {
37 $this->IRCConn
->privmsg(IRCConn
::$channel,IRCConn
::$myBotName."\n");
40 public function nick($newNick) {
41 $this->IRCConn
->newNick($newNick);
44 public function help() {
45 $this->IRCConn
->privmsg(IRCConn
::$channel,'On lui trouvera une utilité plus tard ...');
48 public function restart() {
49 if ($this->IRCConn
->auth
) {
50 echo 'Restart...'."\n";
51 $this->IRCConn
->disconnect();
55 $this->IRCConn
->privmsg($this->current_msg_info
['from'],"Vous n'êtes pas authentifié.");
59 public function quit() {
60 if ($this->IRCConn
->auth
) {
61 $this->IRCConn
->disconnect();
64 $this->IRCConn
->privmsg($this->current_msg_info
['from'],"Vous n'êtes pas authentifié.");
68 public function run() {
70 $this->msg
= $this->IRCConn
->get();
73 if (preg_match("`^ERROR :(Closing Link: )?(.*)\r?$`i", $this->msg
))
75 @fclose
($this->IRCConn
->C
);
77 echo 'Closing Link'."\n";
81 elseif (preg_match("`^PING :(.*)\r?\n`", $this->msg
, $T))
83 $this->IRCConn
->put('PONG '.$T[1]);
84 echo "PING :{$T[1]}\nPONG {$T[1]}\n\n";
86 // Bot kicked, thanks kicker !
87 elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.IRCConn
::$channel.' '.preg_quote(IRCConn
::$myBotName, '`').' :`', $this->msg
, $T))
90 $this->IRCConn
->joinChannel(IRCConn
::$channel);
91 $this->IRCConn
->privmsg(IRCConn
::$channel,'Merci '.$T[1].' !');
94 // Illegals characters in Nickname : 432
95 // Nick already in use : 433
96 elseif (preg_match('`^:[^ ]+ (001|432|433) (.*?)\r?\n`', $this->msg
,$T))
100 $this->IRCConn
->joinChannel(IRCConn
::$channel);
104 $this->IRCConn
->newNick('NoNameBot'.rand(0,9).rand(0,9));
107 echo "Nick already in use\n\n";
108 $this->IRCConn
->newNick(false);
110 echo "Geeeh, I do not known what this mean ...\n";
111 echo "DEBUG :>".$this->msg
;
116 $this->current_msg_info
= false;
118 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.IRCConn
::$myBotName.'|'.IRCConn
::$channel.") :(.*)\r`",$this->msg
,$T)) {
119 $this->current_msg_info
= array (
125 } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.IRCConn
::$myBotName." :(.*)\r`",$this->msg
,$T)) {
126 $this->current_msg_info
= array (
129 'to' => IRCConn
::$myBotName,
134 if ($this->current_msg_info
!== false) {
135 switch ($this->current_msg_info
['type']) {
137 if ($this->current_msg_info
['message'][0] == chr(001)) { // ctcp
138 list($cmd,$query) = explode(chr(032),$this->current_msg_info
['message']);
139 $cmd = str_replace(chr(001), "",$this->current_msg_info
['message']);
142 $this->IRCConn
->notice($this->current_msg_info
['from'],$this->formater
->ctcp('PING VERSION TIME USERINFO CLIENTINFO'));
145 $this->IRCConn
->notice($this->current_msg_info
['from'],$this->formater
->ctcp('RPGBot version '.IRCConn
::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed'));
148 $this->IRCConn
->notice($this->current_msg_info
['from'],$this->formater
->ctcp('RPGBot'));
151 $this->IRCConn
->notice($this->current_msg_info
['from'],$this->formater
->ctcp(date('Y-m-d H:i:s')));
154 $this->IRCConn
->notice($this->current_msg_info
['from'],$this->formater
->ctcp("PING ".$query));
158 if ($this->current_msg_info
['to'] == IRCConn
::$myBotName) {
160 if (preg_match('`^connect ([^ ]+) ([^ ]+)`',$this->current_msg_info
['message'],$m)) {
161 if ($m[1] == 'admin' && $m[2] == 'mypass') {
162 $this->IRCConn
->auth
= true;
163 $this->IRCConn
->notice($this->current_msg_info
['from'],'Vous êtes bien authentifié comme administrateur.');
165 $this->IRCConn
->notice($this->current_msg_info
['from'],'Erreur dans votre login et / ou mot de passe.');
171 if ($this->current_msg_info
['message'][0] == '!') {
172 $message = substr($this->current_msg_info
['message'],1);
173 $query = explode(' ',$message);
174 $query_count = count($query);
176 if (isset($this->commands
[$query[0]])) {
177 if ($query_count > 1) {
178 if ($this->commands
[$query[0]][$query[1]]['type'] == 'mixed' ||
179 $this->current_msg_info
['to'] == IRCConn
::$myBotName && $this->commands
[$query[0]][$query[1]]['type'] == 'private' ||
180 $this->current_msg_info
['to'] == IRCConn
::$channel && $this->commands
[$query[0]][$query[1]]['type'] == 'public') {
182 * $query = array( plugins, method[, arg[, ...]] )
183 * !plugin method[ arg[ ...]]
185 call_user_func_array(array($this,'do_command'),array_merge(array('msg_info'=>$this->current_msg_info
),$query));
188 // no method : need a list of commands
191 $commands = self
::list_command($plugin);
192 foreach ($commands as $command_info) {
193 if ($command_info['type'] == 'public') {
194 $get_plugin_method = '!'.$plugin;
196 $get_plugin_method = '/msg ' . IRCConn
::$myBotName . ' !' . $plugin;
199 $this->IRCConn
->privmsg($this->current_msg_info
['from'],$this->formater
->bold('Command :')." $get_plugin_method {$command_info['method']}".(($command_info['accepted_args']>0)?
' [some args...]':''));
201 $help = explode("\n",$command_info['help']);
202 foreach($help as $help_msg) {
203 $this->IRCConn
->privmsg($this->current_msg_info
['from'],$help_msg);
220 public function load_plugin($plugin_name) {
221 if ( !array_key_exists ( $plugin_name, $this->plugins
) ) {
222 if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) {
223 require_once './plugins/'.$plugin_name.'-plugin.inc.php';
224 $this->plugins
[$plugin_name] = new $plugin_name($this);
225 self
::plugin_register_cmd($plugin_name, $this->plugins
[$plugin_name]->commands_list());
227 trigger_error ("Class '$plugin_name' not found",E_USER_ERROR
);
232 protected function plugin_register_cmd($plugin,$commands) {
233 foreach ($commands as $method => $info) {
234 if (!method_exists($this->plugins
[$plugin],$method) ||
!is_callable(array($this->plugins
[$plugin],$method))) {
235 trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING
);
237 self
::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type']);
242 public function unload_plugin($plugin_name) {
243 if ( array_key_exists ( $plugin_name, $this->plugins
) ) {
244 unset( $this->plugins
[$plugin_name] );
248 private function do_command ($msg_info,$plugin,$method) {
249 $all_args = array_slice(func_get_args(), 3);
251 if (isset($this->commands
[$plugin])) {
252 foreach ($this->commands
[$plugin] as $method_name => $methods) {
253 if ($method_name == $method) {
254 $accepted_args = $methods['accepted_args'];
256 if ( $accepted_args >= 1 )
257 $the_args = array_slice($all_args, 0, $accepted_args);
258 elseif ( $accepted_args == 0 )
261 $the_args = $all_args;
264 // print_r($the_args);
267 if ($plugin == 'core') {
268 call_user_func_array(array($this,$method_name),$the_args);
270 $this->plugins
[$plugin]->current_message($msg_info);
271 call_user_func_array(array($this->plugins
[$plugin],$method_name),$the_args);
278 private function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public') {
279 if (isset($this->commands
[$plugin][$method_to_add])) {
283 $this->commands
[$plugin][$method_to_add] = array(
284 'accepted_args' => $accepted_args,
291 private function remove_command($plugin,$method_to_remove) {
292 if ($method_to_remove == '_all_method') {
293 unset($this->commands
[$plugin]);
294 } elseif (isset($this->commands
[$plugin][$method_to_remove])) {
295 unset($this->commands
[$plugin][$method_to_remove]);
297 $this->commands
[$plugin] = $new_method_list;
302 private function list_command($plugin) {
303 if (isset($this->commands
[$plugin])) {
304 foreach ($this->commands
[$plugin] as $method_name => $method_info) {
305 $command_list[] = array_merge(array('method'=>$method_name),$method_info);
307 return $command_list;
312 private function load_user($file='users.db') {
313 echo "Loading users ...";
314 $ulist = explode("\n",file_get_contents($file));
315 foreach ($ulist as $user) {
316 list($uname,$upass,$uright) = explode(':',$user);
317 $this->users_list
[$uname] = array($upass,$uright);
319 echo "\t\t\tdone.\n";
325 public function __construct($main);
326 public function commands_list();
327 public function current_message($message);