From d7444fdaef0c1b3f24c9c1e131be2498af780763 Mon Sep 17 00:00:00 2001 From: xrogaan Date: Sun, 23 Dec 2007 19:44:05 +0000 Subject: [PATCH] 15 "All primary bugs fixed." --- main.php | 22 +++++++-------- plugins/jet-plugin.inc.php | 6 ++-- plugins/sample-plugin.inc.php | 5 +++- sources/main-class.inc.php | 64 +++++++++++++++++++++++++++++++++++-------- sources/plugin-class.inc.php | 17 ++++++++---- 5 files changed, 83 insertions(+), 31 deletions(-) diff --git a/main.php b/main.php index 3dfa26d..0e8c8e0 100755 --- a/main.php +++ b/main.php @@ -3,14 +3,14 @@ // This page need to be rewrited -error_reporting(0); +error_reporting(E_ALL); date_default_timezone_set('Europe/Brussels'); require_once('./sources/functions.inc.php'); debug(true); -set_error_handler('myErrorHandler'); +//set_error_handler('myErrorHandler'); // Arguments passés à la ligne de commande if ( isset($argv[1], $argv[2], $argv[3], $argv[4]) ) { @@ -21,7 +21,7 @@ if ( isset($argv[1], $argv[2], $argv[3], $argv[4]) ) { } else { $server = 'irc.oh-my-songs.com'; $port = 6667; - $chan = '#oms-network'; + $chan = '#irbot'; $name = 'RPGBot'; } @@ -31,16 +31,16 @@ require_once('./sources/plugin-class.inc.php'); require_once('./sources/TextFormat-class.inc.php'); // Nouvelle instance de la classe IRCMain - bot::$server = $server; - bot::$port = $port; - bot::$channel = $chan; - bot::$myBotName = $name; - bot::$ip = '127.0.0.1'; - bot::$domain = 'localhost'; - bot::$connection_password = false; +bot::$server = $server; +bot::$port = $port; +bot::$channel = $chan; +bot::$myBotName = $name; +bot::$ip = '127.0.0.1'; +bot::$domain = 'localhost'; +bot::$connection_password = false; +$MainProc = bot::GetInstance(); while (1) { - $MainProc = bot::GetInstance(); // On charge les plugins que l'on souhaite $MainProc->plugins->load_plugin('sample'); diff --git a/plugins/jet-plugin.inc.php b/plugins/jet-plugin.inc.php index 3e26834..fe4917f 100644 --- a/plugins/jet-plugin.inc.php +++ b/plugins/jet-plugin.inc.php @@ -9,13 +9,15 @@ class jet implements plugin { $this->IRCConn = bot::GetInstance(); $this->formater = text_format::GetInstance(); $this->commands = array( - 'string2dice' => array( // !sample method1 + 'string2dice' => array( // !jet string2dice 'accepted_args' => 1, + 'requier_args' => 1, 'help' => 'some help for this method1', 'type' => 'private', ), 'caract' => array ( 'accepted_args' => 3, + 'requier_args' => 1, 'help' => "Jet basé sur une caractéristique.\n". "Argument premier : Valeur de la caractéristique\n". "Argument second : Base du jet, par défaut une base 20 (peut être mis a 100 pour calculer le poid transportable par exemple)\n". @@ -41,7 +43,7 @@ class jet implements plugin { } public function current_message ($message) { - /* + /* gived by irc::parse_get $message = array ( 'type' => PRIVMSG|NOTICE, 'from' => Nick (reply to), diff --git a/plugins/sample-plugin.inc.php b/plugins/sample-plugin.inc.php index 61b70bf..6af3700 100644 --- a/plugins/sample-plugin.inc.php +++ b/plugins/sample-plugin.inc.php @@ -11,17 +11,20 @@ class sample implements plugin { public function commands_list() { $commands = array( 'method1' => array( // !sample method1 + 'requier_args' => 0, 'accepted_args' => 0, 'help' => 'some help for this method1', 'type' => 'public', ), 'method2' => array( // !sample method2 arg + 'requier_args' => 0, 'accepted_args' => 1, // the \n chr in a double quote string create a new line (2 PRIVMSG) 'help' => "some help for this method2\n".$this->formater->bold('one arg').' requier', 'type' => 'public', ), 'method3' => array( // /msg BotName sample method3 arg + 'requier_args' => 0, 'accepted_args' => 1, 'help' => 'some help for this method3', 'type' => 'private', @@ -31,7 +34,7 @@ class sample implements plugin { } public function current_message ($message) { - /* + /* gived by irc::parse_get $message = array ( 'type' => PRIVMSG|NOTICE, 'from' => Nick (reply to), diff --git a/sources/main-class.inc.php b/sources/main-class.inc.php index afb2fdd..0cf45cf 100644 --- a/sources/main-class.inc.php +++ b/sources/main-class.inc.php @@ -27,11 +27,12 @@ final class bot { private $core_commands; private $users_list; + private $msg_info; public static function GetInstance() { if (!self::$instance) { self::$instance = new bot(); - self::$instance->plugins = new plugins; + self::$instance->plugins = new plugins(); self::$instance->irc = new irc; } return self::$instance; @@ -44,11 +45,11 @@ final class bot { private function load_core_plugin() { // core plugin is an exeption in do_command - $this->plugins->add_command('core','shownick',0,'Show the current nick used (debug)'); - $this->plugins->add_command('core','nick',1,'Change the current nick'); - $this->plugins->add_command('core','quit',0,'Disconnect and stop the process'); - $this->plugins->add_command('core','restart',0,'Disconnect and restart the process'); - $this->plugins->add_command('core','help',0,'Hmm, je dois recoder cette fonction'); + $this->plugins->add_command('core','shownick',0,'Show the current nick used (debug)','mixed'); + $this->plugins->add_command('core','nick',1,'Change the current nick','mixed'); + $this->plugins->add_command('core','quit',0,'Disconnect and stop the process','mixed'); + $this->plugins->add_command('core','restart',0,'Disconnect and restart the process','mixed'); + $this->plugins->add_command('core','help',0,'Hmm, je dois recoder cette fonction','mixed'); } public function launch() { @@ -71,7 +72,7 @@ final class bot { $this->msg = $this->get(); $this->irc->parse_get($this->msg); - $msg_info = $this->irc->get_msg_info(); + $this->msg_info = $msg_info = $this->irc->get_msg_info(); switch ($msg_info['type']) { @@ -136,14 +137,19 @@ final class bot { if (isset($this->plugins->commands[$query[0]])) { if ($query_count > 1) { + if ($this->plugins->commands[$query[0]][$query[1]]['type'] == 'mixed' || - $msg_info['to'] == bot::$myBotName && $this->plugins->commands[$query[0]][$query[1]]['type'] == 'private' || - $msg_info['to'] == bot::$channel && $this->plugins->commands[$query[0]][$query[1]]['type'] == 'public') { + ($msg_info['to'] == bot::$myBotName && $this->plugins->commands[$query[0]][$query[1]]['type'] == 'private') || + ($msg_info['to'] == bot::$channel && $this->plugins->commands[$query[0]][$query[1]]['type'] == 'public')) { /* * $query = array( plugins, method[, arg[, ...]] ) * !plugin method[ arg[ ...]] */ call_user_func_array(array($this->plugins,'do_command'),array_merge(array('msg_info'=>$msg_info),$query)); + } else { + print_r($msg_info); + echo $this->plugins->commands[$query[0]][$query[1]]['type']."\n"; + $this->privmsg($msg_info['from'],"Error with the request."); } } else { // no method : need a list of commands @@ -269,13 +275,47 @@ final class bot { } } + public function shownick() { + $this->privmsg(bot::$channel,bot::$myBotName."\n"); + } + + public function nick($newNick) { + $this->newNick($newNick); + } + + public function help() { + $this->privmsg(bot::$channel,'On lui trouvera une utilité plus tard ...'); + } + + public function restart() { + if ($this->auth) { + echo 'Restart...'."\n"; + $this->disconnect(); + sleep(5); + throw new Exception('Restart...',1); + } else { + $this->privmsg($this->msg_info['from'],"Vous n'êtes pas authentifié."); + } + } + + public function quit() { + if ($this->IRCConn->auth) { + $this->IRCConn->disconnect(); + throw new Exception('Quit from',3); + } else { + $this->IRCConn->privmsg($this->msg_info['from'],"Vous n'êtes pas authentifié."); + } + } + public function disconnect($msg='EOL ;') { - echo 'Closing Link...'."\n"; - $this->put('QUIT :'.$msg); + echo 'Closing Link...'; + $this->plugins->unload_plugin('_all'); if (is_resource($this->C)) { + $this->put('QUIT :'.$msg); @fclose($this->C); + echo "\t\t\tdone."; } else { - echo "There is no link to close (->C is not a resource) !\n"; + echo "\t\t\tError.\nConnection already breack down. ".'bot::disconnect'."\n"; } return true; } diff --git a/sources/plugin-class.inc.php b/sources/plugin-class.inc.php index efa6040..ebb16ba 100644 --- a/sources/plugin-class.inc.php +++ b/sources/plugin-class.inc.php @@ -37,12 +37,19 @@ class plugins { if (!method_exists($this->plugin_list[$plugin],$method) || !is_callable(array($this->plugin_list[$plugin],$method))) { trigger_error("method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING); } else { - self::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type']); + self::add_command($plugin,$method,$info['accepted_args'],$info['help'],$info['type'],$info['requier_args']); } } } public function unload_plugin($plugin_name) { + if ($plugin_name == '_all') { + echo "Unload all plugins ..."; + foreach($this->plugin_list as $pname => $tmp) { + unset ($this->plugin_list[$pname]); + } + echo "\t\t\tdone.\n"; + } if ( array_key_exists ( $plugin_name, $this->plugin_list ) ) { unset( $this->plugin_list[$plugin_name] ); } @@ -65,10 +72,10 @@ class plugins { echo "accepted_args : $accepted_args, args : ".count($all_args)."\n\n\n"; - if (count($all_args)<$accepted_args) { + if (count($all_args) < $accepted_args) { print_r($msg_info); + $this->ircmain->privmsg($msg_info['from'],"Error with num args"); return; - $this->ircmain->privmsg(); } // echo "Args :\n"; @@ -77,7 +84,7 @@ class plugins { if ($plugin == 'core') { print_r($method_name); - call_user_func_array(array(self::ircmain,$method_name),$the_args); + call_user_func_array(array($this->ircmain,$method_name),$the_args); } else { $this->plugin_list[$plugin]->current_message($msg_info); call_user_func_array(array($this->plugin_list[$plugin],$method_name),$the_args); @@ -87,7 +94,7 @@ class plugins { } } - public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public') { + public function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public',$requier_args=0) { if (isset($this->commands[$plugin][$method_to_add])) { return true; } -- 2.11.4.GIT