17
[irbot.git] / sources / plugin-class.inc.php
blob5d072a9531c93a99bdd9e96034e0513d4f4dd5ca
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/>.
20 interface plugin {
22 public function __construct($main);
23 public function commands_list();
24 public function current_message($message);
25 public function error($msg,$command);
28 class plugins {
30 public $plugin_list = array();
31 private $ircmain;
33 public $commands;
35 function __construct() {
36 $this->ircmain = bot::GetInstance();
39 public function load_plugin($plugin_name) {
40 if ( !array_key_exists ( $plugin_name, $this->plugin_list ) ) {
41 if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) {
42 require_once './plugins/'.$plugin_name.'-plugin.inc.php';
43 $this->plugin_list[$plugin_name] = new $plugin_name($this);
44 self::plugin_register_cmd($plugin_name, $this->plugin_list[$plugin_name]->commands_list());
45 } else {
46 throw new Exception("Class '$plugin_name' not found",0);
51 protected function plugin_register_cmd($plugin,$commands) {
52 foreach ($commands as $method => $info) {
53 if (!method_exists($this->plugin_list[$plugin],$method) || !is_callable(array($this->plugin_list[$plugin],$method))) {
54 trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING);
55 } else {
56 self::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type'],$info['requier_args']);
61 public function unload_plugin($plugin_name) {
62 if ($plugin_name == '_all') {
63 echo "Unload all plugins ...";
64 foreach($this->plugin_list as $pname => $tmp) {
65 unset ($this->plugin_list[$pname]);
67 echo "\t\t\tdone.\n";
69 if ( array_key_exists ( $plugin_name, $this->plugin_list ) ) {
70 unset( $this->plugin_list[$plugin_name] );
74 public function do_command ($msg_info,$plugin,$method) {
75 $all_args = array_slice(func_get_args(), 3);
77 if (isset($this->commands[$plugin])) {
78 foreach ($this->commands[$plugin] as $method_name => $methods) {
79 if ($method_name == $method) {
80 $accepted_args = $methods['accepted_args'];
82 if ( $accepted_args >= 1 )
83 $the_args = array_slice($all_args, 0, $accepted_args);
84 elseif ( $accepted_args == 0 )
85 $the_args = NULL;
86 else
87 $the_args = $all_args;
89 echo "accepted_args : $accepted_args, args : ".count($all_args)."\n\n\n";
91 if (count($all_args) < $accepted_args) {
92 print_r($msg_info);
93 $this->ircmain->privmsg($msg_info['from'],"Error with num args");
94 return;
97 // echo "Args :\n";
98 // print_r($the_args);
99 // echo "\n";
101 if ($plugin == 'core') {
102 print_r($method_name);
103 call_user_func_array(array($this->ircmain,$method_name),$the_args);
104 } else {
105 $this->plugin_list[$plugin]->current_message($msg_info);
106 call_user_func_array(array($this->plugin_list[$plugin],$method_name),$the_args);
113 public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public',$requier_args=0) {
114 if (isset($this->commands[$plugin][$method_to_add])) {
115 return true;
118 $this->commands[$plugin][$method_to_add] = array(
119 'accepted_args' => $accepted_args,
120 'help' => $help,
121 'type' => $type
123 return true;
126 public function remove_command($plugin,$method_to_remove) {
127 if ($method_to_remove == '_all_method') {
128 unset($this->commands[$plugin]);
129 } elseif (isset($this->commands[$plugin][$method_to_remove])) {
130 unset($this->commands[$plugin][$method_to_remove]);
132 $this->commands[$plugin] = $new_method_list;
134 return false;
137 public function list_command($plugin) {
138 if (isset($this->commands[$plugin])) {
139 foreach ($this->commands[$plugin] as $method_name => $method_info) {
140 $command_list[] = array_merge(array('method'=>$method_name),$method_info);
142 return $command_list;
144 return false;