From 8b4c19792d7ebac850f7e70565c86c8ee51fbdf1 Mon Sep 17 00:00:00 2001 From: xrogaan Date: Mon, 26 Nov 2007 23:00:30 +0000 Subject: [PATCH] 6 "good way for new plugin system" --- main.php | 10 -- plugins/sample-plugin.inc.php | 28 +++++ sources/IRCConn-class.inc.php | 2 +- sources/IRCMain-class.inc.php | 228 ++++++++++++++++++++++++++++++++-------- sources/plug_base-class.inc.php | 8 +- 5 files changed, 217 insertions(+), 59 deletions(-) create mode 100644 plugins/sample-plugin.inc.php rewrite sources/IRCMain-class.inc.php (80%) diff --git a/main.php b/main.php index 2adcc02..c2c4abf 100755 --- a/main.php +++ b/main.php @@ -32,16 +32,6 @@ function xvdump() { echo "
\n"; call_user_func_array('var_dump',$args); echo "
\n"; } -// Mécanisme de chargement automatique des classes selon leur nom -function __autoload($class) { - if (is_readable('./sources/'.$class.'-class.inc.php')) { - require_once './sources/'.$class.'-class.inc.php'; - } else { - trigger_error ("Class '$class' not found",E_USER_ERROR); - } -} - - function backtrace() { $backtrace = debug_backtrace(); $err_backtrace = " Backtrace :\n=============\n\r"; diff --git a/plugins/sample-plugin.inc.php b/plugins/sample-plugin.inc.php new file mode 100644 index 0000000..bfd7357 --- /dev/null +++ b/plugins/sample-plugin.inc.php @@ -0,0 +1,28 @@ +IRCConn = IRCConn::GetInstance(); + } + function commands_list() { + $commands = array('method1','method2','method3'); + return $commands; + } + function help() { + $msg['method1'] = 'some help for this method'; + $msg['method2'] = 'some help for this method'; + $msg['method3'] = 'some help for this method'; + return $msg; + } + function method1() { + $this->IRCConn->privmsg(IRCConn::$channel,'This is the method1'); + } + function method2() { + $this->IRCConn->privmsg(IRCConn::$channel,'This is the method2'); + } + function method3() { + $this->IRCConn->privmsg(IRCConn::$channel,'This is the method2'); + } +} +?> \ No newline at end of file diff --git a/sources/IRCConn-class.inc.php b/sources/IRCConn-class.inc.php index d51db0a..0852080 100644 --- a/sources/IRCConn-class.inc.php +++ b/sources/IRCConn-class.inc.php @@ -8,7 +8,7 @@ Class IRCConn { // Variables privées static public $instance = FALSE; protected $C; - static $botVersion = 1.0; + static $botVersion = '1.0'; static $server; static $port; static $channel; diff --git a/sources/IRCMain-class.inc.php b/sources/IRCMain-class.inc.php dissimilarity index 80% index ae65b6c..d720214 100644 --- a/sources/IRCMain-class.inc.php +++ b/sources/IRCMain-class.inc.php @@ -1,41 +1,187 @@ -MyConn = IRCConn::GetInstance(); - // On charge le plugin de base indispensable - $this->AddPlug('plug_base'); - } - - public function run() { - while (true) { - $this->msg = $this->MyConn->get(); - foreach ($this->Plist as $Plug) { - $Plug->start($this->msg); - } - } - } - - public function AddPlug($Pname) { - if ( !array_key_exists ( $Pname, $this->Plist ) ) { - $this->Plist[$Pname] = new $Pname($this); - } - } - - public function UnloadPlug($Pname) { - if ( array_key_exists ( $Pname, $this->Plist ) ) { - unset( $this->Plist[$Pname] ); - } - } - -} - -?> \ No newline at end of file +IRCConn = IRCConn::GetInstance(); + // On charge le plugin de base indispensable + require_once('./sources/plug_base-class.inc.php'); + $this->Plist['plug_base'] = new plug_base(); + + self::register_command('core','!shownick'); + self::register_command('core','!nick'); + } + + public function run() { + while (true) { + $this->msg = $this->IRCConn->get(); + + $msg = false; + + if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.IRCConn::$myBotName.'|'.IRCConn::$channel.") :(.*)\r`",$this->msg,$T)) { + $msg = array ( + 'type' => 'PRIVMSG', + 'from' => $T[1], + 'to' => $T[2], + 'message' => $T[3] + ); + } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.IRCConn::$myBotName." :(.*)\r`",$this->msg,$T)) { + $msg = array ( + 'type' => 'NOTICE', + 'from' => $T[1], + 'to' => IRCConn::$myBotName, + 'message' => $T[2] + ); + } + + if ($msg !== false) { + switch ($msg['type']) { + case 'PRIVMSG': + if ($msg['message'][0] == chr(001)) { // ctcp + list($cmd,$query) = explode(chr(032),$msg['message']); + $cmd = str_replace(chr(001), "",$msg['message']); + switch ($cmd) { + case 'CLIENTINFO': + $this->IRCConn->notice($msg['from'],chr(001).'PING VERSION TIME USERINFO CLIENTINFO'.chr(001)); + break; + case 'VERSION': + $this->IRCConn->notice($msg['from'],chr(001).'RPGBot version '.IRCConn::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed'.chr(001)); + break; + case 'USERINFO': + $this->IRCConn->notice($msg['from'],chr(001).'RPGBot'.chr(001)); + break; + case 'TIME': + $this->IRCConn->notice($msg['from'],chr(001).date('Y-m-d H:i:s').chr(001)); + break; + case 'PING': + $this->IRCConn->notice($msg['from'],chr(001)."PING ".$query.chr(001)); + break; + } + } else { + if ($to == IRCConn::$myBotName) { + if (preg_match('^connect ([^ ]+) ([^ ]+)',$msg['message'],$m)) { + if ($m[1] == 'admin' && $m[2] == 'mypass') { + $this->IRCConn->notice($msg['from'],'Vous êtes bien authentifié comme administrateur.'); + } else { + $this->IRCConn->notice($msg['from'],'Erreur dans votre login et / ou mot de passe.'); + } + } + } else { + if ($msg['message'][0] == '!') { + $pos_space = strpos($msg['message'],' '); + if ($pos_space !== false) { + $command = substr($msg['message'],0,$pos_space); + $query = substr($msg['message'],$pos_space+1); + } else { + $command = $msg['message']; + } + // + } + } + } + break; + + default: + break; + } + } + + foreach ($this->plugins['_run'] as $plugin => $methods) { + foreach ($methods as $method) { + $this->plugins[$plugin]->$method($msg); + } + } + } + } + + public function AddPlug($Pname) { + if ( !array_key_exists ( $Pname, $this->Plist ) ) { + $this->Plist[$Pname] = new $Pname($this); + } + } + + public function load_plugin($pname) { + if ( !array_key_exists ( $pname, $this->plugins ) ) { + if ( is_readable('./plugins/'.$class.'-plugin.inc.php') ) { + require_once './plugins/'.$class.'-plugin.inc.php'; + $this->plugins[$pname] = new $pname($this); + self::plugin_register_cmd($pname, $this->plugins[$pname]->commands_list()); + } else { + trigger_error ("Class '$class' not found",E_USER_ERROR); + } + } + } + + public function unload_plugin($plugin_name) { + if ( array_key_exists ( $plugin_name, $this->plugins ) ) { + unset( $this->plugins[$plugin_name] ); + } + } + + protected function plugin_register_cmd($plugin,$commands) { + foreach ($commands as $method => $accepted_args) { + if (!method_exists($this->plugins[$plugin],$method) || !is_callable(array($this->plugins[$plugin],$method))) { + trigger_error("ERROR : method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING); + } else { + self::add_command($plugin,$method,$accepted_args); + } + } + } + + public function UnloadPlug($Pname) { + if ( array_key_exists ( $Pname, $this->Plist ) ) { + unset( $this->Plist[$Pname] ); + } + } + + private function add_command($plugin,$method_to_add,$accepted_args=0) { + if (isset($this->commands[$plugin])) { + foreach ($this->commands[$plugin] as $method) { + if (isset($method[$method_to_add])) + return true; + } + } + + $this->commands[$plugin][] = array( + 'method'=>$method_to_add, + 'accepted_args'=>$accepted_args, + ); + return true; + } + + private function remove_command($plugin,$method_to_remove, $accepted_args = 0) { + if (isset($this->commands[$plugin])) { + if ($method_to_remove != '_all_method') { + foreach($this->commands[$plugin] as $method) { + if ($method['method'] != $method_to_remove) + $new_method_list[] = $method; + } + + $this->commands[$plugin] = $new_method_list; + } else { + unset($this->commands[$plugin]); + } + } + return false; + } +} + +interface plugin { + public function __construct($main); + public function start($IRCtxt); + public function help(); +} + +?> \ No newline at end of file diff --git a/sources/plug_base-class.inc.php b/sources/plug_base-class.inc.php index 6a7b4a1..7d24229 100644 --- a/sources/plug_base-class.inc.php +++ b/sources/plug_base-class.inc.php @@ -1,11 +1,5 @@ main->MyConn->notice($T[1],'RPGBot version '.IRCConn::$botVersion.' - PHP '.phpversion().' -- par Tornald'); } if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.preg_quote(IRCConn::$myBotName, '`').'(.*?)PING (.*?)\r?\n`', $IRCtext, $T)) { - $this->main->MyConn->notice($T[1],"PING\1".time()."\1"); + $this->main->MyConn->notice($T[1],"PONG\1".time()."\1"); } if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.preg_quote(IRCConn::$myBotName,'`').'(.*?)(TIME)`', $IRCtext, $T)) { $this->main->MyConn->notice($T[1],date('Y-m-d H:i:s')); -- 2.11.4.GIT