3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007 Bellière Ludovic
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * $Id: Controller.php 28 2008-01-19 17:17:01Z xrogaan $
24 public function __construct($main);
25 public function commands_list();
26 public function current_message($message);
31 public $plugin_list = array();
36 public $method_on = array();
37 public $accepted_event = array();
39 function __construct() {
40 $this->ircmain
= bot
::GetInstance();
41 $this->text_format
= text_format
::GetInstance();
42 $this->accepted_event
= array('PRIVMSG','NOTICE');
45 public function Init() {
46 self
::build_command_list();
50 public function load_plugin($plugin_name) {
51 if ( !array_key_exists ( $plugin_name, $this->plugin_list
) ) {
52 if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) {
54 echo "plugins::load_plugin()[$plugin_name]\n";
56 require_once './plugins/'.$plugin_name.'-plugin.inc.php';
57 $this->plugin_list
[$plugin_name] = new $plugin_name($this);
58 self
::plugin_register_cmd($plugin_name, $this->plugin_list
[$plugin_name]->commands_list());
60 throw new Exception("Class '$plugin_name' not found",0);
65 protected function plugin_register_cmd($plugin,$commands) {
66 foreach ($commands as $method => $info) {
67 if (!method_exists($this->plugin_list
[$plugin],$method) ||
!is_callable(array($this->plugin_list
[$plugin],$method))) {
68 trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING
);
70 self
::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type'],$info['requier_args'],$info['do_on']));
75 public function unload_plugin($plugin_name) {
76 if ($plugin_name == '_all') {
77 echo "Unload all plugins ...";
78 foreach($this->plugin_list
as $pname => $tmp) {
79 unset ($this->plugin_list
[$pname]);
83 if ( array_key_exists ( $plugin_name, $this->plugin_list
) ) {
84 unset( $this->plugin_list
[$plugin_name] );
88 public function do_command ($msg_info,$plugin,$method) {
89 $all_args = array_slice(func_get_args(), 3);
91 if (isset($this->commands
[$plugin])) {
92 foreach ($this->commands
[$plugin] as $method_name => $methods) {
93 if ($method_name == $method) {
94 $accepted_args = $methods['accepted_args'];
96 if ( $accepted_args >= 1 )
97 $the_args = array_slice($all_args, 0, $accepted_args);
98 elseif ( $accepted_args == 0 )
101 $the_args = $all_args;
103 $call_args = count($all_args);
106 echo "plugins::do_command()[$plugin::$method_name] -> type : {$methods['type']}\n";
107 echo "plugins::do_command()[$plugin::$method_name] -> accepted_args : $accepted_args, requier_args : {$methods['requier_args']}, args : ".$call_args."\n\n\n";
110 $dest_error_prototype = "This command must be called in %s mode. Try /msg %s !$plugin $method_name";
113 switch ($methods['type']) {
115 if ($msg_info['to'] != bot
::$myBotName) {
116 $msg = sprintf($dest_error_prototype,'private',bot
::$myBotName);
121 if ($msg_info['to'] != bot
::$channel) {
122 $msg = sprintf($dest_error_prototype,'public', bot
::$channel);
127 // no reason to match this...
132 if ($methods['requier_args'] > 1) {
134 while ($i<$methods['requier_args']) {
138 $msg = substr($msg,0,-1);
139 $msg.= "\n".text_format
::bold('You must enter '.$methods['requier_args'].' arguments.');
141 $this->ircmain
->mprivmsg($msg_info['from'],text_format
::paragraphe($msg));
145 if ($call_args < $methods['requier_args']) {
146 $msg = "There is an error with the number of arguments. You need to enter on minimum {$methods['requier_args']} arguments for this command.";
147 $this->ircmain
->privmsg($msg_info['from'],$msg);
148 //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name));
150 } elseif ($call_args > $accepted_args) {
151 $msg = "There is an error with the number of arguments. You need to enter on maximum {$methods['requier_args']} arguments for this command.";
152 $this->ircmain
->privmsg($msg_info['from'],$msg);
153 //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name));
154 //$this->ircmain->privmsg($msg_info['from'],"Error with num args");
159 // print_r($the_args);
162 if ($plugin == 'core') {
163 call_user_func_array(array($this->ircmain
,$method_name),$the_args);
165 $this->plugin_list
[$plugin]->current_message($msg_info);
166 call_user_func_array(array($this->plugin_list
[$plugin],$method_name),$the_args);
174 public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public',$requier_args=0,$do_on) {
175 if (isset($this->commands
[$plugin][$method_to_add])) {
176 echo debug() ?
'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\ALREADY LOADED\n" : '';
180 if (!in_array($do_on,$this->accepted_event
)) {
181 echo debug() ?
'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\tFAILED\n" : '';
185 $this->commands
[$plugin][$method_to_add] = array(
186 'requier_args' => $requier_args,
187 'accepted_args' => $accepted_args,
193 echo debug() ?
'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\LOADED\n" : '';
197 public function remove_command($plugin,$method_to_remove) {
198 if ($method_to_remove == '_all_method') {
199 unset($this->commands
[$plugin]);
200 } elseif (isset($this->commands
[$plugin][$method_to_remove])) {
201 unset($this->commands
[$plugin][$method_to_remove]);
203 $this->commands
[$plugin] = $new_method_list;
205 self
::build_command_list();
209 public function list_command($plugin) {
210 if (isset($this->commands
[$plugin])) {
211 foreach ($this->commands
[$plugin] as $method_name => $method_info) {
212 $command_list[] = array_merge(array('method'=>$method_name),$method_info);
214 return $command_list;
219 public function command_exist($command) {
220 if (isset($this->command_list
[$command]) {
227 public function set_event($data) {
228 switch ($data['type'])
230 self
::do_on_privmsg($data);
233 self
::do_on_notice($data);
237 if ($data['message'][0] == '!') {
238 $message = substr($data['message'],1);
239 $query = explode(' ',$message);
240 $query_count = count($query);
242 if (isset($this->plugins
->commands
[$query[0]])) {
243 if ($query_count > 1) {
244 if (isset($this->plugins
->commands
[$query[0]][$query[1]])) {
245 call_user_func_array(array($this->plugins
,'do_command'),array_merge(array('msg_info'=>$msg_info),$query));
247 bot
::GetInstance()->privmsg($data['from'],"This command do not exist. Try !{$query[0]} for the list of commands avaiable with this plugin.");
250 // no method : need a list of commands
253 $commands = $this->plugins
->list_command($plugin);
254 foreach ($commands as $command_info) {
255 if ($command_info['type'] == 'public') {
256 $get_plugin_method = '!'.$plugin;
258 $get_plugin_method = '/msg ' . bot
::$myBotName . ' !' . $plugin;
261 $msg = array_merge(array(bot
::GetInstance()->formater
->bold('Command :')." $get_plugin_method {$command_info['method']}".(($command_info['accepted_args']>0)?
' [some args...]':'')),text_format
::paragraphe($command_info['help']));
262 bot
::GetInstance()mprivmsg($msg_info['from'],$msg);
266 //var_dump($this->plugins->commands);
271 private function do_on_privmsg() {
272 foreach ($this->method_on
['privmsg'] as $method_name) {
276 private function do_on_notice() {
277 foreach ($this->method_on
['notice'] as $method_name) {
282 private function build_command_list() {
283 foreach ($this->commands
as $plugin => $method_data) {
284 foreach ($method_data as $method_name => $method_info) {
285 $this->command_list
= array(
286 $method_name => array(
288 'method_info' => $method_info;
291 switch($method_info['do_on']) {
293 $this->method_on
['privmsg'] = $method_name;
296 $this->method_on
['privmsg'] = $method_name;