From 8f1438943201127f1a65ad2f22b44829c8c15a74 Mon Sep 17 00:00:00 2001 From: xrogaan Date: Mon, 24 Dec 2007 02:46:57 +0000 Subject: [PATCH] 16 "delete old files adding copyright notice fix some bug" --- main.php | 33 +++- main.sh | 26 --- plugins/jet-plugin.inc.php | 18 +++ plugins/sample-plugin.inc.php | 18 +++ sources/IRCConn-class.inc.php | 145 ----------------- sources/IRCMain-class.inc.php | 331 --------------------------------------- sources/TextFormat-class.inc.php | 17 ++ sources/functions.inc.php | 17 ++ sources/irc-class.inc.php | 26 +++ sources/main-class.inc.php | 38 ++++- sources/plugin-class.inc.php | 18 ++- 11 files changed, 174 insertions(+), 513 deletions(-) delete mode 100755 main.sh delete mode 100644 sources/IRCConn-class.inc.php delete mode 100644 sources/IRCMain-class.inc.php diff --git a/main.php b/main.php index 0e8c8e0..a215449 100755 --- a/main.php +++ b/main.php @@ -1,5 +1,23 @@ #!/usr/bin/php . + */ + // This page need to be rewrited @@ -19,10 +37,10 @@ if ( isset($argv[1], $argv[2], $argv[3], $argv[4]) ) { $chan = '#' . $argv[3] ; $name = $argv[4] ; } else { - $server = 'irc.oh-my-songs.com'; + $server = 'irc.tty2.org'; $port = 6667; - $chan = '#irbot'; - $name = 'RPGBot'; + $chan = '#sharesource'; + $name = 'IrBot'; } require_once('./sources/main-class.inc.php'); @@ -39,6 +57,15 @@ bot::$ip = '127.0.0.1'; bot::$domain = 'localhost'; bot::$connection_password = false; +echo <<. + +EOF; + $MainProc = bot::GetInstance(); while (1) { diff --git a/main.sh b/main.sh deleted file mode 100755 index 55a911b..0000000 --- a/main.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -#echo -n "Entrez le serveur : " -#read server -#echo -n "Entrez le port : " -#read port -#echo -n "Entrez le canal à rejoindre : " -#read channel -#echo -n "Entrez le nom du bot : " -#read name - -server='multimud.homeip.net' -port=6667 -channel='projet_mud' -name='RPGBot' - -# boucle infinie qui relance le script si il est tué -while [ 1 ] ; do - php -f main.php $server $port $channel $name - if [ "$?" -eq "0" ]; then - echo - echo "End of live ..." - echo - exit 0 - fi -done diff --git a/plugins/jet-plugin.inc.php b/plugins/jet-plugin.inc.php index fe4917f..9043c8d 100644 --- a/plugins/jet-plugin.inc.php +++ b/plugins/jet-plugin.inc.php @@ -1,4 +1,22 @@ . + */ + class jet implements plugin { private $IRCConn,$formater; diff --git a/plugins/sample-plugin.inc.php b/plugins/sample-plugin.inc.php index 6af3700..12638f4 100644 --- a/plugins/sample-plugin.inc.php +++ b/plugins/sample-plugin.inc.php @@ -1,4 +1,22 @@ . + */ + class sample implements plugin { private $IRCConn,$formater; private $message; diff --git a/sources/IRCConn-class.inc.php b/sources/IRCConn-class.inc.php deleted file mode 100644 index 5895cbe..0000000 --- a/sources/IRCConn-class.inc.php +++ /dev/null @@ -1,145 +0,0 @@ -connect(self::$server, self::$port); - } - - static function Init($server, $port, $channel, $myBotName='XboT', $ip='127.0.0.1', $domain='localhost', $connection_password=false) { - self::$server = $server; - self::$port = $port; - self::$channel = $channel; - self::$myBotName = preg_replace('`[^_[:alnum:]\`\\\\[\]^-]`', '', $myBotName); - self::$ip = $ip; - self::$domain = $domain; - self::$connection_password = $connection_password; - } - - static function GetInstance() { - if (!IRCConn::$instance) { - IRCConn::$instance = new IRCConn(); - } - return IRCConn::$instance; - } - - public function disconnect($msg='EOL ;') { - echo 'Closing Link...'."\n"; - $this->put('QUIT :'.$msg); - if (is_resource($this->C)) { - @fclose($this->C); - } - return true; - } - - protected function connect() { - $this->C = @fsockopen(self::$server, self::$port, $errno, $errstr, 10); - if (!$this->C) { - echo "Impossible de se connecter au server IRC !\n"; - return 0; - } - if (self::$connection_password!==false) { - $this->put('PASS '.self::$connection_password); - } - // TODO : be sure for the validity of the connection password - // User - $this->put('USER '.self::$myBotName.' '.self::$myBotName.'@'.self::$ip.' '.self::$domain.' :XBOT'); - // Nick - $this->put('NICK '.self::$myBotName); - } - - public function joinChannel($channel) { - $this->put('JOIN '.$channel); - echo "Join channel $channel ...\n"; - } - - private function nick_change() { - echo "New nick : ".self::$myBotName."\n"; - $this->put('NICK :'.self::$myBotName); - } - - public function newNick($new=false) { - switch ($new) { - case self::$myBotName: - echo "New nick : [ERR] no changes :". self::$myBotName . ' == ' . $new ."\n"; - break; - case false: - self::$myBotName .= '_'; - self::nick_change(); - break; - default: - self::$myBotName = $new; - self::nick_change(); - break; - } - } - - /** - * Envoie une notice a un salon / utilisateur - * - * @param string $to - * @param string $message - */ - public function notice ($to,$message) { - echo "ERROR : deprecated use of notice function. Please use IRCMain::notice\n"; - return self::put('NOTICE '.$to.' :'.$message."\n"); - } - - /** - * Envoie un message (PRIVMSG) a un salon / utilisateur - * - * @param string $to - * @param string $message - */ - public function privmsg ($to,$message) { - echo "ERROR : deprecated use of privmsg function. Please use IRCMain::privmsg\n"; - return self::put('PRIVMSG '.$to.' :'.$message."\n"); - } - - public function put($command) { - if (!is_resource($this->C)) { - self::error(1,'Connection lost...'); - return false; - } - echo debug() ? '[->' . $command . "" : ''; - fputs($this->C, $command . "\n"); - return true; - } - - public function get() { - // Stream Timeout - stream_set_timeout($this->C, $this->socketTimeout); - $tmp1 = time(); - $content = fgets($this->C, 1024); - echo debug() ? "<-]$content" : ''; - if ($content != '') { - return $content; - } - // TIMEOUT - if (time()-$tmp1 >= $this->socketTimeout) { - self::error(0,'TIMEOUT'); - return false; - } - } - -} - -?> \ No newline at end of file diff --git a/sources/IRCMain-class.inc.php b/sources/IRCMain-class.inc.php deleted file mode 100644 index 8e71076..0000000 --- a/sources/IRCMain-class.inc.php +++ /dev/null @@ -1,331 +0,0 @@ -IRCConn = IRCConn::GetInstance(); - - $this->formater = text_format::GetInstance(); - - // core plugin is an exeption in do_command - self::add_command('core','shownick',0,'Show the current nick used (debug)'); - self::add_command('core','nick',1,'Change the current nick'); - self::add_command('core','quit',0,'Disconnect and stop the process'); - self::add_command('core','restart',0,'Disconnect and restart the process'); - self::add_command('core','help',0,'Hmm, je dois recoder cette fonction'); - } - - public function shownick() { - $this->IRCConn->privmsg(IRCConn::$channel,IRCConn::$myBotName."\n"); - } - - public function nick($newNick) { - $this->IRCConn->newNick($newNick); - } - - public function help() { - $this->IRCConn->privmsg(IRCConn::$channel,'On lui trouvera une utilité plus tard ...'); - } - - public function restart() { - if ($this->IRCConn->auth) { - echo 'Restart...'."\n"; - $this->IRCConn->disconnect(); - sleep(5); - return 1; - } else { - $this->IRCConn->privmsg($this->current_msg_info['from'],"Vous n'êtes pas authentifié."); - } - } - - public function quit() { - if ($this->IRCConn->auth) { - $this->IRCConn->disconnect(); - die(0); - } else { - $this->IRCConn->privmsg($this->current_msg_info['from'],"Vous n'êtes pas authentifié."); - } - } - - public function run() { - while (true) { - $this->msg = $this->IRCConn->get(); - - // DECO - if (preg_match("`^ERROR :(Closing Link: )?(.*)\r?$`i", $this->msg)) - { - @fclose($this->IRCConn->C); - sleep(3); - echo 'Closing Link'."\n"; - return 1; - } - // PONG - elseif (preg_match("`^PING :(.*)\r?\n`", $this->msg, $T)) - { - $this->IRCConn->put('PONG '.$T[1]); - echo "PING :{$T[1]}\nPONG {$T[1]}\n\n"; - } - // Bot kicked, thanks kicker ! - elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.IRCConn::$channel.' '.preg_quote(IRCConn::$myBotName, '`').' :`', $this->msg, $T)) - { - sleep(1); - $this->IRCConn->joinChannel(IRCConn::$channel); - $this->IRCConn->privmsg(IRCConn::$channel,'Merci '.$T[1].' !'); - } - // OK : 001 - // Illegals characters in Nickname : 432 - // Nick already in use : 433 - elseif (preg_match('`^:[^ ]+ (001|432|433) (.*?)\r?\n`', $this->msg,$T)) - { - switch ($T[1]) { - case 001: - $this->IRCConn->joinChannel(IRCConn::$channel); - break; - - case 432: - $this->IRCConn->newNick('NoNameBot'.rand(0,9).rand(0,9)); - break; - case 433: - echo "Nick already in use\n\n"; - $this->IRCConn->newNick(false); - default: - echo "Geeeh, I do not known what this mean ...\n"; - echo "DEBUG :>".$this->msg; - break; - } - } - - $this->current_msg_info = false; - - if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.IRCConn::$myBotName.'|'.IRCConn::$channel.") :(.*)\r`",$this->msg,$T)) { - $this->current_msg_info = array ( - 'type' => 'PRIVMSG', - 'from' => $T[1], - 'to' => $T[2], - 'message' => $T[3] - ); - } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.IRCConn::$myBotName." :(.*)\r`",$this->msg,$T)) { - $this->current_msg_info = array ( - 'type' => 'NOTICE', - 'from' => $T[1], - 'to' => IRCConn::$myBotName, - 'message' => $T[2] - ); - } - - if ($this->current_msg_info !== false) { - switch ($this->current_msg_info['type']) { - case 'PRIVMSG': - if ($this->current_msg_info['message'][0] == chr(001)) { // ctcp - list($cmd,$query) = explode(chr(032),$this->current_msg_info['message']); - $cmd = str_replace(chr(001), "",$this->current_msg_info['message']); - switch ($cmd) { - case 'CLIENTINFO': - $this->IRCConn->notice($this->current_msg_info['from'],$this->formater->ctcp('PING VERSION TIME USERINFO CLIENTINFO')); - break; - case 'VERSION': - $this->IRCConn->notice($this->current_msg_info['from'],$this->formater->ctcp('RPGBot version '.IRCConn::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed')); - break; - case 'USERINFO': - $this->IRCConn->notice($this->current_msg_info['from'],$this->formater->ctcp('RPGBot')); - break; - case 'TIME': - $this->IRCConn->notice($this->current_msg_info['from'],$this->formater->ctcp(date('Y-m-d H:i:s'))); - break; - case 'PING': - $this->IRCConn->notice($this->current_msg_info['from'],$this->formater->ctcp("PING ".$query)); - break; - } - } else { - if ($this->current_msg_info['to'] == IRCConn::$myBotName) { - // auth procedure - if (preg_match('`^connect ([^ ]+) ([^ ]+)`',$this->current_msg_info['message'],$m)) { - if ($m[1] == 'admin' && $m[2] == 'mypass') { - $this->IRCConn->auth = true; - $this->IRCConn->notice($this->current_msg_info['from'],'Vous êtes bien authentifié comme administrateur.'); - } else { - $this->IRCConn->notice($this->current_msg_info['from'],'Erreur dans votre login et / ou mot de passe.'); - } - continue; - } - } - - if ($this->current_msg_info['message'][0] == '!') { - $message = substr($this->current_msg_info['message'],1); - $query = explode(' ',$message); - $query_count = count($query); - - if (isset($this->commands[$query[0]])) { - if ($query_count > 1) { - if ($this->commands[$query[0]][$query[1]]['type'] == 'mixed' || - $this->current_msg_info['to'] == IRCConn::$myBotName && $this->commands[$query[0]][$query[1]]['type'] == 'private' || - $this->current_msg_info['to'] == IRCConn::$channel && $this->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'=>$this->current_msg_info),$query)); - } - } else { - // no method : need a list of commands - $plugin = $message; - - $commands = self::list_command($plugin); - foreach ($commands as $command_info) { - if ($command_info['type'] == 'public') { - $get_plugin_method = '!'.$plugin; - } else { - $get_plugin_method = '/msg ' . IRCConn::$myBotName . ' !' . $plugin; - } - - $this->IRCConn->privmsg($this->current_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->IRCConn->privmsg($this->current_msg_info['from'],$help_msg); - } - } - } - } - } - } - break; - case 'NOTICE': - break; - default: - break; - } - } - } - } - - public function load_plugin($plugin_name) { - if ( !array_key_exists ( $plugin_name, $this->plugins ) ) { - if ( is_readable('./plugins/'.$plugin_name.'-plugin.inc.php') ) { - require_once './plugins/'.$plugin_name.'-plugin.inc.php'; - $this->plugins[$plugin_name] = new $plugin_name($this); - self::plugin_register_cmd($plugin_name, $this->plugins[$plugin_name]->commands_list()); - } else { - trigger_error ("Class '$plugin_name' not found",E_USER_ERROR); - } - } - } - - protected function plugin_register_cmd($plugin,$commands) { - foreach ($commands as $method => $info) { - if (!method_exists($this->plugins[$plugin],$method) || !is_callable(array($this->plugins[$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']); - } - } - } - - public function unload_plugin($plugin_name) { - if ( array_key_exists ( $plugin_name, $this->plugins ) ) { - unset( $this->plugins[$plugin_name] ); - } - } - - private function do_command ($msg_info,$plugin,$method) { - $all_args = array_slice(func_get_args(), 3); - - if (isset($this->commands[$plugin])) { - foreach ($this->commands[$plugin] as $method_name => $methods) { - if ($method_name == $method) { - $accepted_args = $methods['accepted_args']; - - if ( $accepted_args >= 1 ) - $the_args = array_slice($all_args, 0, $accepted_args); - elseif ( $accepted_args == 0 ) - $the_args = NULL; - else - $the_args = $all_args; - - // echo "Args :\n"; - // print_r($the_args); - // echo "\n"; - - if ($plugin == 'core') { - call_user_func_array(array($this,$method_name),$the_args); - } else { - $this->plugins[$plugin]->current_message($msg_info); - call_user_func_array(array($this->plugins[$plugin],$method_name),$the_args); - } - } - } - } - } - - private function add_command($plugin,$method_to_add,$accepted_args=0,$help='',$type='public') { - if (isset($this->commands[$plugin][$method_to_add])) { - return true; - } - - $this->commands[$plugin][$method_to_add] = array( - 'accepted_args' => $accepted_args, - 'help' => $help, - 'type' => $type - ); - return true; - } - - private 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])) { - unset($this->commands[$plugin][$method_to_remove]); - - $this->commands[$plugin] = $new_method_list; - } - return false; - } - - private 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); - } - return $command_list; - } - return false; - } - - private function load_user($file='users.db') { - echo "Loading users ..."; - $ulist = explode("\n",file_get_contents($file)); - foreach ($ulist as $user) { - list($uname,$upass,$uright) = explode(':',$user); - $this->users_list[$uname] = array($upass,$uright); - } - echo "\t\t\tdone.\n"; - } -} - -interface plugin { - - public function __construct($main); - public function commands_list(); - public function current_message($message); - -} - -?> \ No newline at end of file diff --git a/sources/TextFormat-class.inc.php b/sources/TextFormat-class.inc.php index 2961fa6..8d0b664 100644 --- a/sources/TextFormat-class.inc.php +++ b/sources/TextFormat-class.inc.php @@ -1,4 +1,21 @@ . + */ class text_format { static public $instance = FALSE; diff --git a/sources/functions.inc.php b/sources/functions.inc.php index c0377b0..4760966 100644 --- a/sources/functions.inc.php +++ b/sources/functions.inc.php @@ -1,4 +1,21 @@ . + */ function debug($set=null) { static $debug; diff --git a/sources/irc-class.inc.php b/sources/irc-class.inc.php index 11ad51b..a3ac924 100644 --- a/sources/irc-class.inc.php +++ b/sources/irc-class.inc.php @@ -1,4 +1,21 @@ . + */ class irc { @@ -83,6 +100,15 @@ class irc { $this->msg_info = array('type'=>false); return $temp; } + + /** + * Return the current incoming line + * + * @return string + */ + public function get_msg() { + return $this->msg; + } } ?> \ No newline at end of file diff --git a/sources/main-class.inc.php b/sources/main-class.inc.php index 0cf45cf..8d5f789 100644 --- a/sources/main-class.inc.php +++ b/sources/main-class.inc.php @@ -1,4 +1,21 @@ . + */ final class bot { @@ -284,7 +301,11 @@ final class bot { } public function help() { - $this->privmsg(bot::$channel,'On lui trouvera une utilité plus tard ...'); + $this->privmsg($this->msg_info['from'],"List of plugins :"); + foreach($this->plugins->plugin_list as $pluginName => $object) { + $this->privmsg($this->msg_info['from'],"!$pluginName"); + usleep(500000); + } } public function restart() { @@ -299,21 +320,21 @@ final class bot { } public function quit() { - if ($this->IRCConn->auth) { - $this->IRCConn->disconnect(); + if ($this->auth) { + $this->disconnect(); throw new Exception('Quit from',3); } else { - $this->IRCConn->privmsg($this->msg_info['from'],"Vous n'êtes pas authentifié."); + $this->privmsg($this->msg_info['from'],"Vous n'êtes pas authentifié."); } } public function disconnect($msg='EOL ;') { - echo 'Closing Link...'; $this->plugins->unload_plugin('_all'); + echo "Closing Link..."; if (is_resource($this->C)) { $this->put('QUIT :'.$msg); @fclose($this->C); - echo "\t\t\tdone."; + echo "\t\t\tdone.\n"; } else { echo "\t\t\tError.\nConnection already breack down. ".'bot::disconnect'."\n"; } @@ -331,7 +352,10 @@ final class bot { } function __destruct() { - self::disconnect('Error occured'); + if (is_resource($this->C)) { + $this->put('QUIT :Error occured'); + @fclose($this->C); + } } } diff --git a/sources/plugin-class.inc.php b/sources/plugin-class.inc.php index ebb16ba..5d072a9 100644 --- a/sources/plugin-class.inc.php +++ b/sources/plugin-class.inc.php @@ -1,5 +1,21 @@ . + */ interface plugin { -- 2.11.4.GIT