15
[irbot.git] / sources / plugin-class.inc.php
blobebb16ba230a34ace82a8c81042e2148599ecfffc
1 <?php
4 interface plugin {
6 public function __construct($main);
7 public function commands_list();
8 public function current_message($message);
9 public function error($msg,$command);
12 class plugins {
14 public $plugin_list = array();
15 private $ircmain;
17 public $commands;
19 function __construct() {
20 $this->ircmain = bot::GetInstance();
23 public function load_plugin($plugin_name) {
24 if ( !array_key_exists ( $plugin_name, $this->plugin_list ) ) {
25 if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) {
26 require_once './plugins/'.$plugin_name.'-plugin.inc.php';
27 $this->plugin_list[$plugin_name] = new $plugin_name($this);
28 self::plugin_register_cmd($plugin_name, $this->plugin_list[$plugin_name]->commands_list());
29 } else {
30 throw new Exception("Class '$plugin_name' not found",0);
35 protected function plugin_register_cmd($plugin,$commands) {
36 foreach ($commands as $method => $info) {
37 if (!method_exists($this->plugin_list[$plugin],$method) || !is_callable(array($this->plugin_list[$plugin],$method))) {
38 trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING);
39 } else {
40 self::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type'],$info['requier_args']);
45 public function unload_plugin($plugin_name) {
46 if ($plugin_name == '_all') {
47 echo "Unload all plugins ...";
48 foreach($this->plugin_list as $pname => $tmp) {
49 unset ($this->plugin_list[$pname]);
51 echo "\t\t\tdone.\n";
53 if ( array_key_exists ( $plugin_name, $this->plugin_list ) ) {
54 unset( $this->plugin_list[$plugin_name] );
58 public function do_command ($msg_info,$plugin,$method) {
59 $all_args = array_slice(func_get_args(), 3);
61 if (isset($this->commands[$plugin])) {
62 foreach ($this->commands[$plugin] as $method_name => $methods) {
63 if ($method_name == $method) {
64 $accepted_args = $methods['accepted_args'];
66 if ( $accepted_args >= 1 )
67 $the_args = array_slice($all_args, 0, $accepted_args);
68 elseif ( $accepted_args == 0 )
69 $the_args = NULL;
70 else
71 $the_args = $all_args;
73 echo "accepted_args : $accepted_args, args : ".count($all_args)."\n\n\n";
75 if (count($all_args) < $accepted_args) {
76 print_r($msg_info);
77 $this->ircmain->privmsg($msg_info['from'],"Error with num args");
78 return;
81 // echo "Args :\n";
82 // print_r($the_args);
83 // echo "\n";
85 if ($plugin == 'core') {
86 print_r($method_name);
87 call_user_func_array(array($this->ircmain,$method_name),$the_args);
88 } else {
89 $this->plugin_list[$plugin]->current_message($msg_info);
90 call_user_func_array(array($this->plugin_list[$plugin],$method_name),$the_args);
97 public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public',$requier_args=0) {
98 if (isset($this->commands[$plugin][$method_to_add])) {
99 return true;
102 $this->commands[$plugin][$method_to_add] = array(
103 'accepted_args' => $accepted_args,
104 'help' => $help,
105 'type' => $type
107 return true;
110 public function remove_command($plugin,$method_to_remove) {
111 if ($method_to_remove == '_all_method') {
112 unset($this->commands[$plugin]);
113 } elseif (isset($this->commands[$plugin][$method_to_remove])) {
114 unset($this->commands[$plugin][$method_to_remove]);
116 $this->commands[$plugin] = $new_method_list;
118 return false;
121 public function list_command($plugin) {
122 if (isset($this->commands[$plugin])) {
123 foreach ($this->commands[$plugin] as $method_name => $method_info) {
124 $command_list[] = array_merge(array('method'=>$method_name),$method_info);
126 return $command_list;
128 return false;