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);
30 const EVENT_PRIVMSG
= "privmsg";
31 const EVENT_NOTICE
= "notice";
34 protected $text_format;
36 protected $_events = array(
37 self
::EVENT_NOTICE
=> true,
38 self
::EVENT_PRIVMSG
=> true
41 public function __construct(bot
$ircmain) {
42 $this->ircmain
= $ircmain;
43 $this->text_format
= $this->ircmain
->formater
;
46 public function add_command(Plugins_Abstract
$plugin,Plugins_Command_Abstract
$method_to_add,$do_on) {
47 if (isset($this->commands
[$plugin][$method_to_add])) {
48 echo debug() ?
'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\ALREADY LOADED\n" : '';
52 if (!in_array($do_on,$this->accepted_event
)) {
53 echo debug() ?
'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\tFAILED\n" : '';
57 $this->commands
[$plugin][$method_to_add] = array(
58 'requier_args' => $method_to_add->requieredArgs
,
59 'accepted_args' => $method_to_add->acceptedArgs
,
60 'help' => $method_to_add->helpMessage(),
61 'type' => $method_to_add->commandVisibility
,
65 echo debug() ?
'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\LOADED\n" : '';
74 public $plugin_list = array();
79 public $method_on = array();
80 public $accepted_event = array();
82 function __construct() {
83 $this->ircmain = bot::GetInstance();
84 $this->text_format = text_format::GetInstance();
85 $this->accepted_event = array('PRIVMSG','NOTICE');
88 public function Init() {
89 self::build_command_list();
93 public function load_plugin($plugin_name) {
94 if ( !array_key_exists ( $plugin_name, $this->plugin_list ) ) {
95 if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) {
97 echo "plugins::load_plugin()[$plugin_name]\n";
99 require_once './plugins/'.$plugin_name.'-plugin.inc.php';
100 $this->plugin_list[$plugin_name] = new $plugin_name($this);
101 self::plugin_register_cmd($plugin_name, $this->plugin_list[$plugin_name]->commands_list());
103 throw new Exception("Class '$plugin_name' not found",0);
108 protected function plugin_register_cmd($plugin,$commands) {
109 foreach ($commands as $method => $info) {
110 if (!method_exists($this->plugin_list[$plugin],$method) || !is_callable(array($this->plugin_list[$plugin],$method))) {
111 trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING);
113 self::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type'],$info['requier_args'],$info['do_on']);
118 public function unload_plugin($plugin_name) {
119 if ($plugin_name == '_all') {
120 echo "Unload all plugins ...";
121 foreach($this->plugin_list as $pname => $tmp) {
122 unset ($this->plugin_list[$pname]);
124 echo "\t\t\tdone.\n";
126 if ( array_key_exists ( $plugin_name, $this->plugin_list ) ) {
127 unset( $this->plugin_list[$plugin_name] );
131 public function do_command ($msg_info,$plugin,$method) {
132 $all_args = array_slice(func_get_args(), 3);
134 if (isset($this->commands[$plugin])) {
135 foreach ($this->commands[$plugin] as $method_name => $methods) {
136 if ($method_name == $method) {
137 $accepted_args = $methods['accepted_args'];
139 if ( $accepted_args >= 1 )
140 $the_args = array_slice($all_args, 0, $accepted_args);
141 elseif ( $accepted_args == 0 )
144 $the_args = $all_args;
146 $call_args = count($all_args);
149 echo "plugins::do_command()[$plugin::$method_name] -> type : {$methods['type']}\n";
150 echo "plugins::do_command()[$plugin::$method_name] -> accepted_args : $accepted_args, requier_args : {$methods['requier_args']}, args : ".$call_args."\n\n\n";
153 $dest_error_prototype = "This command must be called in %s mode. Try /msg %s !$plugin $method_name";
156 switch ($methods['type']) {
158 if ($msg_info['to'] != bot::$myBotName) {
159 $msg = sprintf($dest_error_prototype,'private',bot::$myBotName);
164 if ($msg_info['to'] != bot::$channel) {
165 $msg = sprintf($dest_error_prototype,'public', bot::$channel);
170 // no reason to match this...
175 if ($methods['requier_args'] > 1) {
177 while ($i<$methods['requier_args']) {
181 $msg = substr($msg,0,-1);
182 $msg.= "\n".text_format::bold('You must enter '.$methods['requier_args'].' arguments.');
184 $this->ircmain->mprivmsg($msg_info['from'],text_format::paragraphe($msg));
188 if ($call_args < $methods['requier_args']) {
189 $msg = "There is an error with the number of arguments. You need to enter on minimum {$methods['requier_args']} arguments for this command.";
190 $this->ircmain->privmsg($msg_info['from'],$msg);
191 //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name));
193 } elseif ($call_args > $accepted_args) {
194 $msg = "There is an error with the number of arguments. You need to enter on maximum {$methods['requier_args']} arguments for this command.";
195 $this->ircmain->privmsg($msg_info['from'],$msg);
196 //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name));
197 //$this->ircmain->privmsg($msg_info['from'],"Error with num args");
202 // print_r($the_args);
205 if ($plugin == 'core') {
206 call_user_func_array(array($this->ircmain,$method_name),$the_args);
208 $this->plugin_list[$plugin]->current_message($msg_info);
209 call_user_func_array(array($this->plugin_list[$plugin],$method_name),$the_args);
217 public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public',$requier_args=0,$do_on) {
218 if (isset($this->commands[$plugin][$method_to_add])) {
219 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\ALREADY LOADED\n" : '';
223 if (!in_array($do_on,$this->accepted_event)) {
224 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\tFAILED\n" : '';
228 $this->commands[$plugin][$method_to_add] = array(
229 'requier_args' => $requier_args,
230 'accepted_args' => $accepted_args,
236 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\LOADED\n" : '';
240 public function remove_command($plugin,$method_to_remove) {
241 if ($method_to_remove == '_all_method') {
242 unset($this->commands[$plugin]);
243 } elseif (isset($this->commands[$plugin][$method_to_remove])) {
244 unset($this->commands[$plugin][$method_to_remove]);
246 $this->commands[$plugin] = $new_method_list;
248 self::build_command_list();
252 public function list_command($plugin) {
253 if (isset($this->commands[$plugin])) {
254 foreach ($this->commands[$plugin] as $method_name => $method_info) {
255 $command_list[] = array_merge(array('method'=>$method_name),$method_info);
257 return $command_list;
262 public function command_exist($command) {
263 if (isset($this->command_list[$command])) {
270 public function set_event($data) {
271 switch ($data['type']) {
273 self::do_on_privmsg($data);
276 self::do_on_notice($data);
282 if ($data['message'][0] == '!') {
283 $message = substr($data['message'],1);
284 $query = explode(' ',$message);
285 $query_count = count($query);
287 if (isset($this->plugins->commands[$query[0]])) {
288 if ($query_count > 1) {
289 if (isset($this->plugins->commands[$query[0]][$query[1]])) {
290 call_user_func_array(array($this->plugins,'do_command'),array_merge(array('msg_info'=>$msg_info),$query));
292 bot::GetInstance()->privmsg($data['from'],"This command do not exist. Try !{$query[0]} for the list of commands avaiable with this plugin.");
295 // no method : need a list of commands
298 $commands = $this->plugins->list_command($plugin);
299 foreach ($commands as $command_info) {
300 if ($command_info['type'] == 'public') {
301 $get_plugin_method = '!'.$plugin;
303 $get_plugin_method = '/msg ' . bot::$myBotName . ' !' . $plugin;
306 $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']));
307 bot::GetInstance()->mprivmsg($msg_info['from'],$msg);
311 //var_dump($this->plugins->commands);
316 private function do_on_privmsg() {
317 foreach ($this->method_on['privmsg'] as $method_name) {
321 private function do_on_notice() {
322 foreach ($this->method_on['notice'] as $method_name) {
327 private function build_command_list() {
328 foreach ($this->commands as $plugin => $method_data) {
329 foreach ($method_data as $method_name => $method_info) {
330 $this->command_list = array(
331 $method_name => array(
333 'method_info' => $method_info,
336 switch($method_info['do_on']) {
338 $this->method_on['privmsg'] = $method_name;
341 $this->method_on['privmsg'] = $method_name;