From 58d3b2e64be3a7cfbc83304f2d2fed77f393331d Mon Sep 17 00:00:00 2001 From: xrogaan Date: Wed, 19 Dec 2007 16:00:01 +0000 Subject: [PATCH] 13 "bot can run. there is some wrong things in the plugin feature. Now we use exception for all errors." --- main.php | 173 +++++++++++++++++------------------------- plugins/jet-plugin.inc.php | 4 +- plugins/sample-plugin.inc.php | 8 +- sources/functions.inc.php | 105 +++++++++++++++++++++++++ sources/irc-class.inc.php | 24 +++--- sources/main-class.inc.php | 96 +++++++++++++---------- sources/plugin-class.inc.php | 24 ++++-- 7 files changed, 266 insertions(+), 168 deletions(-) rewrite main.php (79%) create mode 100644 sources/functions.inc.php diff --git a/main.php b/main.php dissimilarity index 79% index b49baf8..170b5a3 100755 --- a/main.php +++ b/main.php @@ -1,102 +1,71 @@ -\n"; print_r($v); echo "\n"; -} - -/** - * var_dump avec un jolis formatage - * - */ -function xvdump() { - $args = func_get_args(); - echo "
\n"; call_user_func_array('var_dump',$args); echo "
\n"; -} - -function backtrace() { - $backtrace = debug_backtrace(); - $err_backtrace = " Backtrace :\n=============\n\r"; - foreach($backtrace as $t) { - $err_backtrace.= 'File : '.((isset($t['file'])) ? $t['file']:'[PHP KERNEL]').((isset($t['line'])) ? ':'.$t['line'] : '') . "\n". - 'Function : '.$t['function']."\n"; - if (isset($t['class'])) { - $err_backtrace.= 'Class : '.$t['class']."\n"; - } - if (isset($t['type'])) { - $err_backtrace.= 'Type : '.$t['type']."\n"; - } - if (isset($t['args']) && !empty($t['args'])) { - $err_backtrace.= '--== Args ==--'."\n"; - ob_start(); - var_dump($t['args']); - $err_backtrace.= "\t".str_replace("\n","\n\t",ob_get_contents()); - ob_end_clean(); - } - $err_backtrace.= "\n\n- - - - - - - - - - - - -\n\r"; - } - return $err_backtrace; -} - -// Arguments passés à la ligne de commande -if ( isset($argv[1], $argv[2], $argv[3], $argv[4]) ) { - $server = $argv[1] ; - $port = (int) $argv[2] ; - $chan = '#' . $argv[3] ; - $name = $argv[4] ; -} else { - $server = 'multimud.homeip.net'; - $port = 6667; - $chan = '#projet_mud'; - $name = 'RPGBot'; -} - -require_once('./sources/IRCMain-class.inc.php'); -require_once('./sources/IRCConn-class.inc.php'); -require_once('./sources/TextFormat-class.inc.php'); - -while (0) { - // Nouvelle instance de la classe IRCMain - $MainProc = new IRCMain($server, $port, $chan, $name, '127.0.0.1','xrogaan.be'); - - // On charge les plugins que l'on souhaite - $MainProc -> load_plugin('sample'); - $MainProc -> load_plugin('jet'); - - // On lance le bot - switch( $MainProc -> run() ) { - case 0: - die("Process terminating...\n"); - break; - case 1: // SIGHUP restart - echo "Processe restarting ..."; - continue 2; - case 3: // SIGQUIT - die("End from client...\n"); - break; - case 'TIMEOUT': - continue 2; - } -} - -?> \ No newline at end of file +plugins->load_plugin('sample'); + $MainProc->plugins->load_plugin('jet'); + + // On lance le bot + try { + $MainProc -> launch(); + } catch (Exception $e) { + switch($e->getCode()) { + case 0: + echo $e->getMessage()."\n"; + echo "Process terminating...\n"; + die("EOL from client\n"); + break 2; + case 1: // SIGHUP restart + echo $e->getMessage()."\n"; + echo "Process restarting ...\n\n"; + continue 2; + case 3: // SIGQUIT + die("EOL from user...\n"); + break 2; + case 'TIMEOUT': + continue 2; + } + } +} + +?> \ No newline at end of file diff --git a/plugins/jet-plugin.inc.php b/plugins/jet-plugin.inc.php index 615c174..197adfc 100644 --- a/plugins/jet-plugin.inc.php +++ b/plugins/jet-plugin.inc.php @@ -5,7 +5,7 @@ class jet implements plugin { private $message; public function __construct($main) { - $this->IRCConn = IRCConn::GetInstance(); + $this->IRCConn = bot::GetInstance(); $this->formater = text_format::GetInstance(); } @@ -108,7 +108,7 @@ class jet implements plugin { $r = eval('return abs(ceil($de'.$modifier_nature.'$modifier));'); } - if (IRCConn::$myBotName == $this->message['to']) { + if (bot::$myBotName == $this->message['to']) { $replyto = $this->message['from']; } else { $replyto = $this->message['to']; diff --git a/plugins/sample-plugin.inc.php b/plugins/sample-plugin.inc.php index 3ff73dc..6e577a0 100644 --- a/plugins/sample-plugin.inc.php +++ b/plugins/sample-plugin.inc.php @@ -4,7 +4,7 @@ class sample implements plugin { private $message; public function __construct($main) { - $this->IRCConn = IRCConn::GetInstance(); + $this->IRCConn = bot::GetInstance(); $this->formater = text_format::GetInstance(); } @@ -43,16 +43,16 @@ class sample implements plugin { } function method1 () { - $this->IRCConn->privmsg(IRCConn::$channel,'This is the method1'); + $this->IRCConn->privmsg(bot::$channel,'This is the method1'); } function method2 ($query) { - $this->IRCConn->privmsg(IRCConn::$channel,'This is the method2 : '.$query); + $this->IRCConn->privmsg(bot::$channel,'This is the method2 : '.$query); } function method3 ($query) { - if (IRCConn::$myBotName == $this->message['to']) { + if (bot::$myBotName == $this->message['to']) { $replyto = $this->message['from']; } else { $replyto = $this->message['to']; diff --git a/sources/functions.inc.php b/sources/functions.inc.php new file mode 100644 index 0000000..c9b804f --- /dev/null +++ b/sources/functions.inc.php @@ -0,0 +1,105 @@ +file = $file; + $this->line = $line; + $this->_level = $level; + $this->_context = $context; + } + +} + +/** + * Remplacant le gestionnaire d'erreur natif PHP. IL permet de lever une exception capturable dans un bloc catch + * pour les fonction/methode ne levant pas d'exception. + * @param int $level Niveau de l'erreur PHP + * @param str $string Description de l'erreur + * @param str $file Chemin d'accés au fichier dans lequel l'erreur s'est produite + * @param int $line Ligne de $file où l'erreur s'est produite + * @param array $context Un contexte d'éxécution fournit par PHP + * @access system + * @return void + * + */ +function myErrorHandler($level, $string, $file, $line, $context){ + + throw new myRuntimeException($level, $string, $file, $line, $context); + +} + +?> \ No newline at end of file diff --git a/sources/irc-class.inc.php b/sources/irc-class.inc.php index 0e2f899..ca0155f 100644 --- a/sources/irc-class.inc.php +++ b/sources/irc-class.inc.php @@ -4,12 +4,14 @@ class irc { protected $current_msg_info = false; private $ircmain; + private $msg, $msg_info; function __construct() { - $this->ircmain = main::GetInstance(); + $this->ircmain = bot::GetInstance(); } function parse_get($msg) { + $this->msg = $msg; // DECO if (preg_match("`^ERROR :(Closing Link: )?(.*)\r?$`i", $this->msg)) { @@ -24,11 +26,11 @@ class irc { echo "PING :{$T[1]}\nPONG {$T[1]}\n\n"; } // Bot kicked, thanks kicker ! - elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.main::$channel.' '.preg_quote(main::$myBotName, '`').' :`', $this->msg, $T)) + elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.bot::$channel.' '.preg_quote(bot::$myBotName, '`').' :`', $this->msg, $T)) { sleep(1); - $this->ircmain->joinChannel(main::$channel); - $this->ircmain->privmsg(main::$channel,'Merci '.$T[1].' !'); + $this->ircmain->joinChannel(bot::$channel); + $this->ircmain->privmsg(bot::$channel,'Merci '.$T[1].' !'); } // OK : 001 // Illegals characters in Nickname : 432 @@ -37,7 +39,7 @@ class irc { { switch ($T[1]) { case 001: - $this->ircmain->joinChannel(main::$channel); + $this->ircmain->joinChannel(bot::$channel); break; case 432: @@ -53,18 +55,18 @@ class irc { } } - if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.main::$myBotName.'|'.main::$channel.") :(.*)\r`",$msg,$T)) { - $msg_info = array ( + if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.bot::$myBotName.'|'.bot::$channel.") :(.*)\r`",$this->msg,$T)) { + $this->msg_info = array ( 'type' => 'PRIVMSG', 'from' => $T[1], 'to' => $T[2], 'message' => $T[3] ); - } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.main::$myBotName." :(.*)\r`",$msg,$T)) { - $msg_info = array ( + } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.bot::$myBotName." :(.*)\r`",$this->msg,$T)) { + $this->msg_info = array ( 'type' => 'NOTICE', 'from' => $T[1], - 'to' => main::$myBotName, + 'to' => bot::$myBotName, 'message' => $T[2] ); } @@ -76,7 +78,7 @@ class irc { * @return array */ public function get_msg_info() { - return $msg_info; + return $this->msg_info; } } diff --git a/sources/main-class.inc.php b/sources/main-class.inc.php index 28b4ebe..855dbd0 100644 --- a/sources/main-class.inc.php +++ b/sources/main-class.inc.php @@ -1,6 +1,6 @@ plugins = new plugins; + self::$instance->irc = new irc; } return self::$instance; } private function __construct() { - // load config and other things - - $this->irc = new irc; - $this->plugin = new plugin; + // load config and other things echo "oups main\n";die; $this->formater = text_format::GetInstance(); + } + private function load_core_plugin() { // core plugin is an exeption in do_command - $this->plugin->add_command('core','shownick',0,'Show the current nick used (debug)'); - $this->plugin->add_command('core','nick',1,'Change the current nick'); - $this->plugin->add_command('core','quit',0,'Disconnect and stop the process'); - $this->plugin->add_command('core','restart',0,'Disconnect and restart the process'); - $this->plugin->add_command('core','help',0,'Hmm, je dois recoder cette fonction'); + $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'); } public function launch() { + self::load_core_plugin(); try { $this->C = @fsockopen(self::$server, self::$port, $errno, $errstr, 10); if (!$this->C) { @@ -68,7 +69,7 @@ class main { while (1) { $this->msg = $this->get(); - $this->irc->parse_get($msg); + $this->irc->parse_get($this->msg); $msg_info = $this->irc->get_msg_info(); @@ -79,10 +80,16 @@ class main { // ctcp if ($msg_info['message'][0] == chr(001)) { - list($cmd,$query) = explode(chr(032),$msg_info['message']); + // we don't need this character + $msg_info['message'] = str_replace(chr(001), "", $msg_info['message']); + if (strstr($msg_info['message'],' ')===true) { + list($cmd,$query) = explode(" ",$msg_info['message']); + } else { + $cmd = $msg_info['message']; + } // we don't need this character - $cmd = str_replace(chr(001), "", $msg_info['message']); + //$cmd = str_replace(chr(001), "", $msg_info['message']); switch ($cmd) { case 'CLIENTINFO': @@ -90,7 +97,7 @@ class main { break; case 'VERSION': - self::notice($msg_info['from'],$this->formater->ctcp('RPGBot version '.main::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed')); + self::notice($msg_info['from'],$this->formater->ctcp('RPGBot version '.bot::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed')); break; case 'USERINFO': @@ -104,9 +111,12 @@ class main { case 'PING': self::notice($msg_info['from'],$this->formater->ctcp("PING ".$query)); break; + default: + self::notice($msg_info['from'],$this->formater->ctcp("UNKNOWN CTCP REQUEST : $cmd")); + break; } } else { - if ($msg_info['to'] == main::$myBotName) { + if ($msg_info['to'] == bot::$myBotName) { // auth proccess if (preg_match('`^connect ([^ ]+) ([^ ]+)`',$msg_info['message'],$m)) { if ($m[1] == 'admin' && $m[2] == 'mypass') { @@ -124,27 +134,27 @@ class main { $query = explode(' ',$message); $query_count = count($query); - if (isset($this->commands[$query[0]])) { + if (isset($this->plugins->commands[$query[0]])) { if ($query_count > 1) { - if ($this->commands[$query[0]][$query[1]]['type'] == 'mixed' || - $msg_info['to'] == main::$myBotName && $this->commands[$query[0]][$query[1]]['type'] == 'private' || - $msg_info['to'] == main::$channel && $this->commands[$query[0]][$query[1]]['type'] == 'public') { + 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') { /* * $query = array( plugins, method[, arg[, ...]] ) * !plugin method[ arg[ ...]] */ - call_user_func_array(array($this,'do_command'),array_merge(array('msg_info'=>$msg_info),$query)); + call_user_func_array(array($this->plugins,'do_command'),array_merge(array('msg_info'=>$msg_info),$query)); } } else { // no method : need a list of commands $plugin = $message; - $commands = self::list_command($plugin); + $commands = $this->plugins->list_command($plugin); foreach ($commands as $command_info) { if ($command_info['type'] == 'public') { $get_plugin_method = '!'.$plugin; } else { - $get_plugin_method = '/msg ' . main::$myBotName . ' !' . $plugin; + $get_plugin_method = '/msg ' . bot::$myBotName . ' !' . $plugin; } $this->privmsg($msg_info['from'],$this->formater->bold('Command :')." $get_plugin_method {$command_info['method']}".(($command_info['accepted_args']>0)?' [some args...]':'')); @@ -155,27 +165,30 @@ class main { } } } + } else { + var_dump($this->plugins->commands); } + } else { + echo 'debug: '.$msg_info['message'][0]."\n"; } } break; case 'NOTICE': break; default: + echo "Gee... I don't know what {$msg_info['type']} means..."; + var_dump($msg_info); break; } } + } catch (myRuntimeException $e) { + /*echo $e;*///* + echo $e->getMessage(); + echo backtrace($e->getTrace()); // */ + throw new Exception('Error occured',0); } catch (Exception $e) { - switch ($e->getCode()) { - case 0: // stop - echo "Processe restarting ..."; - return 0; - break; - - default: - break; - } + throw $e; } } @@ -229,7 +242,7 @@ class main { if (!is_resource($this->C)) { throw new Exception('Connection lost...',1); } - echo debug() ? '[->' . $command . "" : ''; + echo debug() ? '[->' . $command . "\n" : ''; fputs($this->C, $command . "\n"); return true; } @@ -258,6 +271,8 @@ class main { $this->put('QUIT :'.$msg); if (is_resource($this->C)) { @fclose($this->C); + } else { + echo "There is no link to close (->C is not a resource) !\n"; } return true; } @@ -271,13 +286,10 @@ class main { } echo "\t\t\tdone.\n"; } -} -interface plugin { - - public function __construct($main); - public function commands_list(); - public function current_message($message); + function __destruct() { + self::disconnect('Error occured'); + } } ?> \ No newline at end of file diff --git a/sources/plugin-class.inc.php b/sources/plugin-class.inc.php index a630e1d..5d6045a 100644 --- a/sources/plugin-class.inc.php +++ b/sources/plugin-class.inc.php @@ -1,12 +1,22 @@ ircmain = main::GetInstance(); + $this->ircmain = bot::GetInstance(); } public function load_plugin($plugin_name) { @@ -37,7 +47,7 @@ class plugin { } } - private function do_command ($msg_info,$plugin,$method) { + public function do_command ($msg_info,$plugin,$method) { $all_args = array_slice(func_get_args(), 3); if (isset($this->commands[$plugin])) { @@ -67,7 +77,7 @@ class plugin { } } - private 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') { if (isset($this->commands[$plugin][$method_to_add])) { return true; } @@ -80,7 +90,7 @@ class plugin { return true; } - private function remove_command($plugin,$method_to_remove) { + public function remove_command($plugin,$method_to_remove) { if ($method_to_remove == '_all_method') { unset($this->commands[$plugin]); } elseif (isset($this->commands[$plugin][$method_to_remove])) { @@ -91,7 +101,7 @@ class plugin { return false; } - private function list_command($plugin) { + public function list_command($plugin) { if (isset($this->commands[$plugin])) { foreach ($this->commands[$plugin] as $method_name => $method_info) { $command_list[] = array_merge(array('method'=>$method_name),$method_info); -- 2.11.4.GIT