new way for dev
[irbot.git] / sources / plugins / Controller.php
bloba40f632d5668cb2e0fd07184c77a1a963dfe7925
1 <?php
2 /**
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 $
22 interface plugin {
24 public function __construct($main);
25 public function commands_list();
26 public function current_message($message);
29 class plugins {
31 public $plugin_list = array();
32 private $ircmain;
34 public $commands;
35 public $command_list;
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();
47 self::build_buffer();
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') ) {
53 if (debug()) {
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());
59 } else {
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);
69 } else {
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]);
81 echo "\t\t\tdone.\n";
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 )
99 $the_args = NULL;
100 else
101 $the_args = $all_args;
103 $call_args = count($all_args);
105 if (debug()) {
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";
111 $fail = false;
113 switch ($methods['type']) {
114 case 'private':
115 if ($msg_info['to'] != bot::$myBotName) {
116 $msg = sprintf($dest_error_prototype,'private',bot::$myBotName);
117 $fail = true;
119 break;
120 case 'public':
121 if ($msg_info['to'] != bot::$channel) {
122 $msg = sprintf($dest_error_prototype,'public', bot::$channel);
123 $fail = true;
125 break;
126 case 'mixed':
127 // no reason to match this...
128 break;
131 if ($fail == true) {
132 if ($methods['requier_args'] > 1) {
133 $i=0;
134 while ($i<$methods['requier_args']) {
135 $msg.= " arg,";
136 $i++;
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));
142 return false;
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));
149 return false;
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");
155 return false;
158 // echo "Args :\n";
159 // print_r($the_args);
160 // echo "\n";
162 if ($plugin == 'core') {
163 call_user_func_array(array($this->ircmain,$method_name),$the_args);
164 } else {
165 $this->plugin_list[$plugin]->current_message($msg_info);
166 call_user_func_array(array($this->plugin_list[$plugin],$method_name),$the_args);
168 return true;
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" : '';
177 return true;
180 if (!in_array($do_on,$this->accepted_event)) {
181 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\tFAILED\n" : '';
182 return false;
185 $this->commands[$plugin][$method_to_add] = array(
186 'requier_args' => $requier_args,
187 'accepted_args' => $accepted_args,
188 'help' => $help,
189 'type' => $type,
190 'do_on' => $do_on,
193 echo debug() ? 'plugins_controller::add_command() -> ' . "$plugin -> $method_to_add\t\t\LOADED\n" : '';
194 return true;
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();
206 return true;
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;
216 return false;
219 public function command_exist($command) {
220 if (isset($this->command_list[$command]) {
221 return true;
222 } else {
223 return false;
227 public function set_event($data) {
228 switch ($data['type'])
229 case 'PRIVMSG':
230 self::do_on_privmsg($data);
231 break;
232 case 'NOTICE':
233 self::do_on_notice($data);
234 break
236 return;
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));
246 } else {
247 bot::GetInstance()->privmsg($data['from'],"This command do not exist. Try !{$query[0]} for the list of commands avaiable with this plugin.");
249 } else {
250 // no method : need a list of commands
251 $plugin = $message;
253 $commands = $this->plugins->list_command($plugin);
254 foreach ($commands as $command_info) {
255 if ($command_info['type'] == 'public') {
256 $get_plugin_method = '!'.$plugin;
257 } else {
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);
265 } else {
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(
287 'plugin' => $plugin,
288 'method_info' => $method_info;
291 switch($method_info['do_on']) {
292 case 'PRIVMSG':
293 $this->method_on['privmsg'] = $method_name;
294 break;
295 case 'NOTICE':
296 $this->method_on['privmsg'] = $method_name;
297 break;
301 return true;