3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007-2008 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/>.
23 public function __construct($main);
24 public function commands_list();
25 public function current_message($message);
28 class plugins
extends RecursiveDirectoryIterator
{
29 const EVENT_PRIVMSG
= "privmsg";
30 const EVENT_NOTICE
= "notice";
33 protected $text_format;
35 protected $_events = array(
36 self
::EVENT_NOTICE
=> true,
37 self
::EVENT_PRIVMSG
=> true
40 protected $commands = array();
42 public function __construct(IRCMain
$ircmain) {
43 $this->ircmain
= $ircmain;
44 parent
::__construct(BASE_DIR
.'plugins/');
46 while(self
::valid()) {
51 if (self
::isReadable()) {
52 $pluginFileName = self
::getFilename();
60 public function addPlugin(Plugins_Abstract
$plugin) {
63 public function add_command($plugin_name,Plugins_Command_Abstract
$method_to_add,$do_on) {
64 if (isset($this->commands
[$plugin][$method_to_add])) {
65 echo debug() ?
'plugins::add_command() -> ' . "$plugin -> $method_to_add\t\t\ALREADY LOADED\n" : '';
69 if (!in_array($do_on,$this->accepted_event
)) {
70 echo debug() ?
'plugins::add_command() -> ' . "$plugin -> $method_to_add\t\t\tFAILED\n" : '';
74 $this->commands
[$plugin][$method_to_add] = array(
75 'requier_args' => $method_to_add->requieredArgs
,
76 'accepted_args' => $method_to_add->acceptedArgs
,
77 'help' => $method_to_add->helpMessage(),
78 'type' => $method_to_add->commandVisibility
,
82 echo debug() ?
'plugins::add_command() -> ' . "$plugin -> $method_to_add\t\t\LOADED\n" : '';
91 public $plugin_list = array();
96 public $method_on = array();
97 public $accepted_event = array();
99 function __construct() {
100 $this->ircmain = bot::GetInstance();
101 $this->text_format = text_format::GetInstance();
102 $this->accepted_event = array('PRIVMSG','NOTICE');
105 public function Init() {
106 self::build_command_list();
107 self::build_buffer();
110 public function load_plugin($plugin_name) {
111 if ( !array_key_exists ( $plugin_name, $this->plugin_list ) ) {
112 if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) {
114 echo "plugins::load_plugin()[$plugin_name]\n";
116 require_once './plugins/'.$plugin_name.'-plugin.inc.php';
117 $this->plugin_list[$plugin_name] = new $plugin_name($this);
118 self::plugin_register_cmd($plugin_name, $this->plugin_list[$plugin_name]->commands_list());
120 throw new Exception("Class '$plugin_name' not found",0);
125 protected function plugin_register_cmd($plugin,$commands) {
126 foreach ($commands as $method => $info) {
127 if (!method_exists($this->plugin_list[$plugin],$method) || !is_callable(array($this->plugin_list[$plugin],$method))) {
128 trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING);
130 self::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type'],$info['requier_args'],$info['do_on']);
135 public function unload_plugin($plugin_name) {
136 if ($plugin_name == '_all') {
137 echo "Unload all plugins ...";
138 foreach($this->plugin_list as $pname => $tmp) {
139 unset ($this->plugin_list[$pname]);
141 echo "\t\t\tdone.\n";
143 if ( array_key_exists ( $plugin_name, $this->plugin_list ) ) {
144 unset( $this->plugin_list[$plugin_name] );
148 public function do_command ($msg_info,$plugin,$method) {
149 $all_args = array_slice(func_get_args(), 3);
151 if (isset($this->commands[$plugin])) {
152 foreach ($this->commands[$plugin] as $method_name => $methods) {
153 if ($method_name == $method) {
154 $accepted_args = $methods['accepted_args'];
156 if ( $accepted_args >= 1 )
157 $the_args = array_slice($all_args, 0, $accepted_args);
158 elseif ( $accepted_args == 0 )
161 $the_args = $all_args;
163 $call_args = count($all_args);
166 echo "plugins::do_command()[$plugin::$method_name] -> type : {$methods['type']}\n";
167 echo "plugins::do_command()[$plugin::$method_name] -> accepted_args : $accepted_args, requier_args : {$methods['requier_args']}, args : ".$call_args."\n\n\n";
170 $dest_error_prototype = "This command must be called in %s mode. Try /msg %s !$plugin $method_name";
173 switch ($methods['type']) {
175 if ($msg_info['to'] != bot::$myBotName) {
176 $msg = sprintf($dest_error_prototype,'private',bot::$myBotName);
181 if ($msg_info['to'] != bot::$channel) {
182 $msg = sprintf($dest_error_prototype,'public', bot::$channel);
187 // no reason to match this...
192 if ($methods['requier_args'] > 1) {
194 while ($i<$methods['requier_args']) {
198 $msg = substr($msg,0,-1);
199 $msg.= "\n".text_format::bold('You must enter '.$methods['requier_args'].' arguments.');
201 $this->ircmain->mprivmsg($msg_info['from'],text_format::paragraphe($msg));
205 if ($call_args < $methods['requier_args']) {
206 $msg = "There is an error with the number of arguments. You need to enter on minimum {$methods['requier_args']} arguments for this command.";
207 $this->ircmain->privmsg($msg_info['from'],$msg);
208 //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name));
210 } elseif ($call_args > $accepted_args) {
211 $msg = "There is an error with the number of arguments. You need to enter on maximum {$methods['requier_args']} arguments for this command.";
212 $this->ircmain->privmsg($msg_info['from'],$msg);
213 //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name));
214 //$this->ircmain->privmsg($msg_info['from'],"Error with num args");
219 // print_r($the_args);
222 if ($plugin == 'core') {
223 call_user_func_array(array($this->ircmain,$method_name),$the_args);
225 $this->plugin_list[$plugin]->current_message($msg_info);
226 call_user_func_array(array($this->plugin_list[$plugin],$method_name),$the_args);
234 public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public',$requier_args=0,$do_on) {
235 if (isset($this->commands[$plugin][$method_to_add])) {
236 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\ALREADY LOADED\n" : '';
240 if (!in_array($do_on,$this->accepted_event)) {
241 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\tFAILED\n" : '';
245 $this->commands[$plugin][$method_to_add] = array(
246 'requier_args' => $requier_args,
247 'accepted_args' => $accepted_args,
253 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\LOADED\n" : '';
257 public function remove_command($plugin,$method_to_remove) {
258 if ($method_to_remove == '_all_method') {
259 unset($this->commands[$plugin]);
260 } elseif (isset($this->commands[$plugin][$method_to_remove])) {
261 unset($this->commands[$plugin][$method_to_remove]);
263 $this->commands[$plugin] = $new_method_list;
265 self::build_command_list();
269 public function list_command($plugin) {
270 if (isset($this->commands[$plugin])) {
271 foreach ($this->commands[$plugin] as $method_name => $method_info) {
272 $command_list[] = array_merge(array('method'=>$method_name),$method_info);
274 return $command_list;
279 public function command_exist($command) {
280 if (isset($this->command_list[$command])) {
287 public function set_event($data) {
288 switch ($data['type']) {
290 self::do_on_privmsg($data);
293 self::do_on_notice($data);
299 if ($data['message'][0] == '!') {
300 $message = substr($data['message'],1);
301 $query = explode(' ',$message);
302 $query_count = count($query);
304 if (isset($this->plugins->commands[$query[0]])) {
305 if ($query_count > 1) {
306 if (isset($this->plugins->commands[$query[0]][$query[1]])) {
307 call_user_func_array(array($this->plugins,'do_command'),array_merge(array('msg_info'=>$msg_info),$query));
309 bot::GetInstance()->privmsg($data['from'],"This command do not exist. Try !{$query[0]} for the list of commands avaiable with this plugin.");
312 // no method : need a list of commands
315 $commands = $this->plugins->list_command($plugin);
316 foreach ($commands as $command_info) {
317 if ($command_info['type'] == 'public') {
318 $get_plugin_method = '!'.$plugin;
320 $get_plugin_method = '/msg ' . bot::$myBotName . ' !' . $plugin;
323 $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']));
324 bot::GetInstance()->mprivmsg($msg_info['from'],$msg);
328 //var_dump($this->plugins->commands);
333 private function do_on_privmsg() {
334 foreach ($this->method_on['privmsg'] as $method_name) {
338 private function do_on_notice() {
339 foreach ($this->method_on['notice'] as $method_name) {
344 private function build_command_list() {
345 foreach ($this->commands as $plugin => $method_data) {
346 foreach ($method_data as $method_name => $method_info) {
347 $this->command_list = array(
348 $method_name => array(
350 'method_info' => $method_info,
353 switch($method_info['do_on']) {
355 $this->method_on['privmsg'] = $method_name;
358 $this->method_on['privmsg'] = $method_name;