From 68cdf673d8f3a410ec9004d4e696dcac705e8bc6 Mon Sep 17 00:00:00 2001 From: xrogaan Date: Thu, 3 Jan 2008 03:19:35 +0000 Subject: [PATCH] 25 "plugin system updated : * requier_args is now supported * all errors is showed debug mode updated text_format can split a strong into an array for multi-privmsg at once bot::mprivmsg added : send multiple privmsg at once with anti-flood feature." --- main.php | 4 +-- plugins/jet-plugin.inc.php | 11 ------- plugins/sample-plugin.inc.php | 11 ++++--- sources/TextFormat-class.inc.php | 4 +++ sources/main-class.inc.php | 67 ++++++++++++++-------------------------- sources/plugin-class.inc.php | 65 +++++++++++++++++++++++++++++++++----- 6 files changed, 94 insertions(+), 68 deletions(-) diff --git a/main.php b/main.php index ffce771..f63375b 100755 --- a/main.php +++ b/main.php @@ -17,7 +17,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * - * $Id: main.php 24 2007-12-30 12:43:56Z xrogaan $ + * $Id: main.php 25 2008-01-03 03:19:35Z xrogaan $ */ require_once('./config.php'); @@ -125,8 +125,6 @@ while (1) { case 3: // SIGQUIT die("EOL from user...\n"); break 2; - case 'TIMEOUT': - continue 2; } } } diff --git a/plugins/jet-plugin.inc.php b/plugins/jet-plugin.inc.php index 9043c8d..41f68f9 100644 --- a/plugins/jet-plugin.inc.php +++ b/plugins/jet-plugin.inc.php @@ -49,17 +49,6 @@ class jet implements plugin { return $this->commands; } - public function error($msg,$command) { - switch ($this->commands[$command]) { - case 'private': - $this->IRCConn->privmsg($this->message['from'],$msg); - break; - - default: - break; - } - } - public function current_message ($message) { /* gived by irc::parse_get $message = array ( diff --git a/plugins/sample-plugin.inc.php b/plugins/sample-plugin.inc.php index 12638f4..5d743da 100644 --- a/plugins/sample-plugin.inc.php +++ b/plugins/sample-plugin.inc.php @@ -35,7 +35,7 @@ class sample implements plugin { 'type' => 'public', ), 'method2' => array( // !sample method2 arg - 'requier_args' => 0, + 'requier_args' => 1, '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', @@ -45,7 +45,7 @@ class sample implements plugin { 'requier_args' => 0, 'accepted_args' => 1, 'help' => 'some help for this method3', - 'type' => 'private', + 'type' => 'mixed', ), ); return $commands; @@ -62,7 +62,6 @@ class sample implements plugin { */ $this->message = $message; } - public function error($msg,$command) {} function method1 () { $this->IRCConn->privmsg(bot::$channel,'This is the method1'); @@ -72,7 +71,11 @@ class sample implements plugin { $this->IRCConn->privmsg(bot::$channel,'This is the method2 : '.$query); } - function method3 ($query) { + function method3 ($query='') { + + if ($query=='') { + $query = 'no args given. All works !'; + } if (bot::$myBotName == $this->message['to']) { $replyto = $this->message['from']; diff --git a/sources/TextFormat-class.inc.php b/sources/TextFormat-class.inc.php index 8d0b664..c03fe2c 100644 --- a/sources/TextFormat-class.inc.php +++ b/sources/TextFormat-class.inc.php @@ -56,6 +56,10 @@ class text_format { function underline($msg) { return chr(31).$msg.chr(31); } + + function paragraphe($msg) { + return explode("\n",$msg); + } } ?> \ No newline at end of file diff --git a/sources/main-class.inc.php b/sources/main-class.inc.php index feaf0e3..db782d0 100644 --- a/sources/main-class.inc.php +++ b/sources/main-class.inc.php @@ -20,7 +20,7 @@ final class bot { // Timeout avant une reconnexion - public $socketTimeout = 280; + public $socketTimeout = 180; // Variables privées static public $instance = FALSE; @@ -47,6 +47,7 @@ final class bot { private $core_commands; private $users_list; private $msg_info; + private $lastData; public static function GetInstance() { if (!self::$instance) { @@ -77,6 +78,7 @@ final class bot { self::load_core_plugin(); try { //$this->C = @fsockopen(self::$server, self::$port, $errno, $errstr, 10); + $this->lastData = mktime(); $this->C = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($this->C === false) { throw new Exception('Impossible de créer le socket IRC !',0); @@ -202,19 +204,10 @@ 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')) { - /* - * $query = array( plugins, method[, arg[, ...]] ) - * !plugin method[ arg[ ...]] - */ + if (isset($this->plugins->commands[$query[0]][$query[1]])) { 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."); + $this->privmsg($msg_info['from'],"This command do not exist. Try !{$query[0]} for the list of commands avaiable with this plugin."); } } else { // no method : need a list of commands @@ -228,12 +221,8 @@ final class bot { $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...]':'')); - - $help = explode("\n",$command_info['help']); - foreach($help as $help_msg) { - $this->privmsg($msg_info['from'],$help_msg); - } + $msg = array_merge(array($this->formater->bold('Command :')." $get_plugin_method {$command_info['method']}".(($command_info['accepted_args']>0)?' [some args...]':'')),text_format::paragraphe($command_info['help'])); + $this->mprivmsg($msg_info['from'],$msg); } } } else { @@ -288,12 +277,22 @@ final class bot { self::put('PRIVMSG '.$to.' :'.$message."\n"); } + public function mprivmsg($to,$messages,$interval=750000) { + if (is_array($messages)) { + foreach ($messages as $msg) { + $this->privmsg($to,$msg); + usleep($interval); + } + } + } + public function put($data) { if (!is_resource($this->C)) { throw new Exception('Connection lost...',1); + return; } - echo debug() ? '[->' . $data . "\n" : ''; + echo debug() ? 'bot::put() -> ' . $data . "\n" : ''; $ok = socket_write($this->C, $data ."\n"); if ($ok) { @@ -314,15 +313,16 @@ final class bot { while (1) { //echo "TOC - ",time(),"\n"; $this->tick->doAllTicks(); - $time1 = time(); $buf = @socket_read($this->C, 4096); if (empty($buf)) { - $this->CheckTimeout($time1); + $this->CheckTimeout(); sleep(1); continue; } + $this->lastData = mktime(); + if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise $buffer = $buffer.$buf; $data = ""; //rien à envoyer @@ -350,25 +350,6 @@ final class bot { } continue; - -/* - echo "TOC - ",time(),"\n"; - $this->tick->doAllTicks(); - // Stream Timeout - stream_set_timeout($this->C, $this->socketTimeout); - $tmp1 = time(); - $content = fgets($this->C, 1024); - - echo debug() ? "<-]$content" : ''; - - if (empty($content)) { - $this->CheckTimeout($tmp1); - sleep(1); - continue; - } else { - return $content; - } -*/ } } @@ -432,9 +413,9 @@ final class bot { return false; } - private function CheckTimeout($time1) { - if (time()-$time1 >= $this->socketTimeout) { - throw new Exception('TIMEOUT',0); + private function CheckTimeout() { + if ($this->lastData+$this->socketTimeout < mktime()) { + throw new Exception('Connection lost (Timeout).',1); } } diff --git a/sources/plugin-class.inc.php b/sources/plugin-class.inc.php index 5d072a9..7f6fde9 100644 --- a/sources/plugin-class.inc.php +++ b/sources/plugin-class.inc.php @@ -22,7 +22,6 @@ interface plugin { public function __construct($main); public function commands_list(); public function current_message($message); - public function error($msg,$command); } class plugins { @@ -34,11 +33,15 @@ class plugins { function __construct() { $this->ircmain = bot::GetInstance(); + $this->text_format = text_format::GetInstance(); } public function load_plugin($plugin_name) { if ( !array_key_exists ( $plugin_name, $this->plugin_list ) ) { if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) { + if (debug()) { + echo "plugins::load_plugin()[$plugin_name]\n"; + } require_once './plugins/'.$plugin_name.'-plugin.inc.php'; $this->plugin_list[$plugin_name] = new $plugin_name($this); self::plugin_register_cmd($plugin_name, $this->plugin_list[$plugin_name]->commands_list()); @@ -86,12 +89,59 @@ class plugins { else $the_args = $all_args; - echo "accepted_args : $accepted_args, args : ".count($all_args)."\n\n\n"; + $call_args = count($all_args); + + if (debug()) { + echo "plugins::do_command()[$plugin::$method_name] -> type : {$methods['type']}\n"; + echo "plugins::do_command()[$plugin::$method_name] -> accepted_args : $accepted_args, requier_args : {$methods['requier_args']}, args : ".$call_args."\n\n\n"; + } + + $dest_error_prototype = "This command must be called in %s mode. Try /msg %s !$plugin $method_name"; + $fail = false; + + switch ($methods['type']) { + case 'private': + if ($msg_info['to'] != bot::$myBotName) { + $msg = sprintf($dest_error_prototype,'private',bot::$myBotName); + $fail = true; + } + break; + case 'public': + if ($msg_info['to'] != bot::$channel) { + $msg = sprintf($dest_error_prototype,'public', bot::$channel); + $fail = true; + } + break; + case 'mixed': + // no reason to match this... + break; + } + + if ($fail == true) { + if ($methods['requier_args'] > 1) { + $i=0; + while ($i<$methods['requier_args']) { + $msg.= " arg,"; + $i++; + } + $msg = substr($msg,0,-1); + $msg.= "\n".text_format::bold('You must enter '.$methods['requier_args'].' arguments.'); + } + $this->ircmain->mprivmsg($msg_info['from'],text_format::paragraphe($msg)); + return false; + } - if (count($all_args) < $accepted_args) { - print_r($msg_info); - $this->ircmain->privmsg($msg_info['from'],"Error with num args"); - return; + if ($call_args < $methods['requier_args']) { + $msg = "There is an error with the number of arguments. You need to enter on minimum {$methods['requier_args']} arguments for this command."; + $this->ircmain->privmsg($msg_info['from'],$msg); + //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name)); + return false; + } elseif ($call_args > $accepted_args) { + $msg = "There is an error with the number of arguments. You need to enter on maximum {$methods['requier_args']} arguments for this command."; + $this->ircmain->privmsg($msg_info['from'],$msg); + //call_user_func_array(array($this->plugin_list[$plugin],'error'),array($msg,$method_name)); + //$this->ircmain->privmsg($msg_info['from'],"Error with num args"); + return false; } // echo "Args :\n"; @@ -99,12 +149,12 @@ class plugins { // echo "\n"; if ($plugin == 'core') { - print_r($method_name); 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); } + return true; } } } @@ -116,6 +166,7 @@ class plugins { } $this->commands[$plugin][$method_to_add] = array( + 'requier_args' => $requier_args, 'accepted_args' => $accepted_args, 'help' => $help, 'type' => $type -- 2.11.4.GIT