From c6e702f20a30960f03d70dfa4ed4f7bfeeaf53e4 Mon Sep 17 00:00:00 2001 From: xrogaan Date: Thu, 26 Jun 2008 03:44:02 +0200 Subject: [PATCH] IrBot can connect a server with the new code structure. --- config.php | 2 +- main.php | 56 ++++++++------- sources/Event.php | 20 ++++-- sources/IRCMain.php | 70 +++--------------- sources/IRCMain/Adapter.php | 72 +++++++++++++++++-- sources/Plugins/Command/Abstract.php | 6 +- sources/Plugins/Command/Command_Abstract.php | 103 --------------------------- sources/tick-class.inc.php | 31 ++++++-- 8 files changed, 147 insertions(+), 213 deletions(-) delete mode 100644 sources/Plugins/Command/Command_Abstract.php diff --git a/config.php b/config.php index 50ef5bc..567fba2 100644 --- a/config.php +++ b/config.php @@ -26,7 +26,7 @@ date_default_timezone_set('Europe/Brussels'); define('IRC_SERVER','irc.oh-my-songs.com'); define('IRC_PORT',6667); -define('IRC_CHANNEL','#irbot'); +define('IRC_CHANNEL','#oms-network'); define('IRC_PSEUDO','irbot'); define('IRC_IP',''); define('IRC_DOMAIN',''); diff --git a/main.php b/main.php index 63c3346..3835d7a 100755 --- a/main.php +++ b/main.php @@ -2,7 +2,7 @@ . * - * $Id: main.php 28 2008-01-19 17:17:01Z xrogaan $ */ define('BASE_DIR',dirname(__FILE__).'/'); @@ -28,18 +27,16 @@ require_once('./sources/functions.inc.php'); debug(true); //set_error_handler('myErrorHandler'); -require_once('source/Registry.php'); +require_once('sources/Registry.php'); Zend_Registry::registerAutoload(); -require_once('./sources/main-class.inc.php'); -require_once('./sources/irc-class.inc.php'); -require_once('./sources/Plugins.php'); +require_once('./sources/IRCMain.php'); +//require_once('./sources/Plugins.php'); require_once('./sources/tick-class.inc.php'); -require_once('./sources/TextFormat-class.inc.php'); echo <<. @@ -56,7 +53,8 @@ if (isset($argv[1])) { if (substr_count($option,'=') === 1) { list($option,$val) = explode('=',$option); } - + + // short option used if (!$val && ($option != '-h' && $option != '--help')) { $i++; $val = $argv[$i]; @@ -93,29 +91,30 @@ Options : $stop = true; break; case '-s':case '--server': - bot::$server = $val; + $setServer = $val; break; case '-p':case '--port': - bot::$port = (int) $val; + $setPort = (int) $val; break; case '-c':case '--channel': - bot::$channel = $val; + $setChannel = $val; break; case '-n':case '--nick': - bot::$myBotName = $val; + // TODO check for alphanumeric nick name. No utf8 allowed. + $setNick = $val; break; case '-i':case '--ip': if (ereg("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",$val)) { - bot::$ip = $val; + $setIp = $val; } else { echo "The ip adress entered is wrong.\nUsing default ip instead : ".IRC_IP."\n"; } break; case '-d':case '--domain': - bot::$domain = $val; + $setDomain = $val; break; case '-a':case '--password': - bot::$connection_password = $val; + $setPassword = $val; break; default: break; @@ -127,24 +126,27 @@ Options : } } -if (!isset(bot::$server)) bot::$server = IRC_SERVER; -if (!isset(bot::$port)) bot::$port = (int) IRC_PORT; -if (!isset(bot::$channel)) bot::$channel = IRC_CHANNEL; -if (!isset(bot::$myBotName)) bot::$myBotName = IRC_PSEUDO; -if (!isset(bot::$ip)) bot::$ip = IRC_IP; -if (!isset(bot::$domain)) bot::$domain = IRC_DOMAIN; -if (!isset(bot::$connection_password)) bot::$connection_password = IRC_PASSWORD; +$options = array( + 'server' => (isset($setServer)) ? $setServer : IRC_SERVER, + 'port' => (isset($setPort)) ? $setPort : IRC_PORT, + 'channel' => (isset($setChannel)) ? $setChannel : IRC_CHANNEL, + 'nick' => (isset($setNick)) ? $setNick : IRC_PSEUDO, + 'ip' => (isset($setIp)) ? $setIp : IRC_IP, + 'domain' => (isset($setDomain)) ? $setDomain : IRC_DOMAIN, + 'password' => (isset($setDomain)) ? $setDomain : IRC_PASSWORD, +); + +$ircMain = new IRCMain($options); -$MainProc = bot::GetInstance(); while (1) { // On charge les plugins que l'on souhaite - $MainProc->plugins->load_plugin('sample'); - $MainProc->plugins->load_plugin('jet'); +// $ircMain->plugins()->load_plugin('sample'); +// $ircMain->plugins()->load_plugin('jet'); // On lance le bot try { - $MainProc -> launch(); + $ircMain -> launch(); } catch (Exception $e) { switch($e->getCode()) { case 0: diff --git a/sources/Event.php b/sources/Event.php index 408ebbb..0865394 100644 --- a/sources/Event.php +++ b/sources/Event.php @@ -31,6 +31,9 @@ class Event { const ACT_318 = 'RPL_ENDOFWHOIS'; const ACT_319 = 'RPL_WHOISCHANNELS'; const ACT_322 = 'RPL_LIST'; + const ACT_375 = 'RPL_MOTDSTART'; + const ACT_372 = 'RPL_MOTD'; + const ACT_376 = 'RPL_ENDOFMOTD'; const ACT_401 = 'ERR_NOSUCHNICK'; const ACT_404 = 'ERR_NOSUCHSERVER'; @@ -74,6 +77,7 @@ class Event { */ public function setIncoming($message) { $this->incoming = $message; + return $this; } /** @@ -92,7 +96,7 @@ class Event { */ function getAction() { - echo debug() ? "IRC:: -> ".$this->incoming."\n" : ''; + echo debug() ? "Event:: -> ".$this->incoming."\n" : ''; if (preg_match("`^ERROR :(Closing Link: )?(.*)$`i", $this->incoming)) { return self::ACT_DISCONNECT; @@ -103,7 +107,7 @@ class Event { } elseif (preg_match('`^:[^ ]+ ([0-9]{3}) (.*?)`', $this->incoming,$T)) { switch ($T[1]) { case 001: - $this->ircmain->joinChannel($this->_ircmain->getConfig('channel')); + $this->_ircmain->joinChannel($this->_ircmain->getConfig('channel')); return self::ACT_001; break; case 311: @@ -113,24 +117,26 @@ class Event { case 313: return self::ACT_313; break; + case 372: case 375: case 376: // motd + echo $this->incoming; case 432: - $this->ircmain->newNick('NoNameBot'.rand(0,9).rand(0,9)); + $this->_ircmain->newNick('NoNameBot'.rand(0,9).rand(0,9)); return self::ACT_432; break; case 433: echo "Nick already in use\n\n"; - $this->ircmain->newNick(false); + $this->_ircmain->newNick(false); return self::ACT_433; break; default: echo "I got a responce from the server, but the code was not grabbed :\n"; - echo "DEBUG :> code : $T[1]"; - echo "DEBUG :>".$this->incoming; + echo "Event::DEBUG :> code : $T[1]\n"; + echo "Event::DEBUG :> raw : ".$this->incoming; break; } } else { echo "Geeeh, I do not known what this mean ...\n"; - echo "DEBUG :>".$this->incoming; + echo "Event::DEBUG :>".$this->incoming; } // : PRIVMSG : diff --git a/sources/IRCMain.php b/sources/IRCMain.php index 41c01e5..782e9bc 100644 --- a/sources/IRCMain.php +++ b/sources/IRCMain.php @@ -22,8 +22,9 @@ require_once 'sources/IRCMain/Adapter.php'; class IRCMain extends IRCMain_Adapter { - public function __construct() { - $this->tick = tick::GetInstance(); + public function __construct(array $options) { + self::setConfig($options); + $this->tick = tick::GetInstance($this); } private function _authentification(Event $event) { @@ -47,12 +48,12 @@ class IRCMain extends IRCMain_Adapter { public function launch() { parent::launch(); - $this->_pluginsInstance->add_command('core','shownick',0,'Show the current nick used (debug)','mixed'); +/* $this->_pluginsInstance->add_command('core','shownick',0,'Show the current nick used (debug)','mixed'); $this->_pluginsInstance->add_command('core','nick',1,'Change the current nick','mixed'); $this->_pluginsInstance->add_command('core','quit',0,'Disconnect and stop the process','mixed'); $this->_pluginsInstance->add_command('core','restart',0,'Disconnect and restart the process','mixed'); $this->_pluginsInstance->add_command('core','help',0,'Hmm, je dois recoder cette fonction','mixed'); - $this->_pluginsInstance->Init(); + $this->_pluginsInstance->Init();*/ try { $this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); @@ -60,18 +61,19 @@ class IRCMain extends IRCMain_Adapter { throw new Exception('Impossible de créer le socket IRC !',0); } - if (self::$ip != "") { + if (self::getConfig('ip') != false && self::getConfig('ip') != "") { socket_bind($this->_socket, self::getConfig('ip')); } - + + $x = self::getConfig('server'); $socketUp = socket_connect($this->_socket, self::getConfig('server'), self::getConfig('port')); if ($socketUp===false) { - throw new Exception('Impossible de se connecter au server IRC ! ('.socket_last_error().')' ,0); + throw new Exception('Impossible de se connecter au server IRC ! ('.socket_strerror ( socket_last_error() ).' (errno:'.socket_last_error().'))' ,0); } $this->connected = true; - if (self::$connection_password !== false) { + if (self::getConfig('password') !== false) { $this->put('PASS '.self::getConfig('password')); } // TODO : be sure for the validity of the connection password (what chain server return if fail ?) @@ -137,7 +139,7 @@ class IRCMain extends IRCMain_Adapter { case 'NOTICE': break; } - self::$plugins->set_event($event); + //$this->plugins()->set_event($event); } } } @@ -157,56 +159,6 @@ class IRCMain extends IRCMain_Adapter { throw $e; } } - - - - public function getIncomingData() { - - $buffer = ''; - socket_set_nonblock($this->C); - - while (1) { - //echo "TOC - ",time(),"\n"; - //$this->tick->doAllTicks(); - $buf = @socket_read($this->_socket, 4096); - - if (empty($buf)) { - $this->_checkTimeout(); - sleep(1); - continue; - } - - $this->_lastTimeData = mktime(); - - if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise - $buffer = $buffer.$buf; - $data = ""; //rien è envoyer - } else { - //Si contient au moins un retour, - //on vèrifie que le dernier caractère en est un - if (substr($buf, -1, 1) == "\n") { - //alors on additionne ces donnèes au buffer - $data = $buffer.$buf; - $buffer = ""; //on vide le buffer - } else { - //si le dernier caractère n'est pas un retour è la - //ligne, alors on envoit tout jusqu'au dernier retour - //puis on bufferise le reste - $buffer = $buffer.substr($buf, strrchr($buf, "\n")); - $data = substr($buf, 0, strrchr($buf, "\n")); - $data = $buffer.$data; - $buffer = ""; //on vide le buffer - } - } - - if ($data != '') { - $data = split("\n", $data); - return $data; - } - - continue; - } - } } ?> \ No newline at end of file diff --git a/sources/IRCMain/Adapter.php b/sources/IRCMain/Adapter.php index a083c76..98f6f85 100644 --- a/sources/IRCMain/Adapter.php +++ b/sources/IRCMain/Adapter.php @@ -21,9 +21,9 @@ /** * @see Zend_Registry */ -require "sources/Registry.php"; +require_once "sources/Registry.php"; -class IRCMain_Adapter { +abstract class IRCMain_Adapter { static public $_instance = false; static public $_pluginsInstance = false; @@ -46,7 +46,7 @@ class IRCMain_Adapter { abstract function __construct(array $options); - private function _checkTimeout() { + protected function _checkTimeout() { $now = mktime(); if ($this->_lastTimeData+self::getConfig('socketTimeout') < mktime()) { throw new Exception('Connection lost (Timeout).',1); @@ -56,14 +56,74 @@ class IRCMain_Adapter { echo "Lag: ".$lag." seconds\n"; } } + + protected function getIncomingData() { + + $buffer = ''; + socket_set_nonblock($this->_socket); + + while (1) { + //echo "TOC - ",time(),"\n"; + //$this->tick->doAllTicks(); + $buf = @socket_read($this->_socket, 4096); + + if (empty($buf)) { + $this->_checkTimeout(); + sleep(1); + continue; + } + + $this->_lastTimeData = mktime(); + + if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise + $buffer = $buffer.$buf; + $data = ""; //rien è envoyer + } else { + //Si contient au moins un retour, + //on vèrifie que le dernier caractère en est un + if (substr($buf, -1, 1) == "\n") { + //alors on additionne ces donnèes au buffer + $data = $buffer.$buf; + $buffer = ""; //on vide le buffer + } else { + //si le dernier caractère n'est pas un retour è la + //ligne, alors on envoit tout jusqu'au dernier retour + //puis on bufferise le reste + $buffer = $buffer.substr($buf, strrchr($buf, "\n")); + $data = substr($buf, 0, strrchr($buf, "\n")); + $data = $buffer.$data; + $buffer = ""; //on vide le buffer + } + } + + if ($data != '') { + $data = split("\n", $data); + return $data; + } + + continue; + } + } public function launch() { - $this->_pluginsInstance = new plugins(); + //$this->_pluginsInstance = new plugins($this); $this->_lastTimeData = mktime(); } + public function plugins() { + if ($this->_pluginsInstance instanceof plugins) { + return $this->_pluginsInstance; + } else { + throw new Exception('Plugins is not initialized'); + } + } + + protected function event() { + return new Event($this); + } + public function getConfig($name) { - return $this->_config[$name]; + return array_key_exists($name, $this->_config) ? $this->_config[$name] : false ; } public function setConfig(array $options) { @@ -143,7 +203,7 @@ class IRCMain_Adapter { return; } - echo debug() ? 'bot::put() -> ' . $data . "\n" : ''; + echo debug() ? 'core::put() -> ' . $data . "\n" : ''; $ok = socket_write($this->_socket, $data ."\n"); if ($ok) { diff --git a/sources/Plugins/Command/Abstract.php b/sources/Plugins/Command/Abstract.php index fd99233..b70ede3 100644 --- a/sources/Plugins/Command/Abstract.php +++ b/sources/Plugins/Command/Abstract.php @@ -15,11 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * - * @category IrBot - * @package IrBot_Plugins - * @copyright Copyright (c) 2008 Bellière Ludovic - * @license http://www.gnu.org/licenses/gpl-3.0.html + * */ abstract class Plugins_Command_Abstract { diff --git a/sources/Plugins/Command/Command_Abstract.php b/sources/Plugins/Command/Command_Abstract.php deleted file mode 100644 index b70ede3..0000000 --- a/sources/Plugins/Command/Command_Abstract.php +++ /dev/null @@ -1,103 +0,0 @@ -. - * - */ - -abstract class Plugins_Command_Abstract { - - const REQUIERED_ARGS = 'requieredArgs'; - const ACCEPTED_ARGS = 'acceptedArgs'; - const COMMAND_VISIBILITY = 'commandVisibility'; - const TYPE_PUBLIC = 'public'; - const TYPE_PRIVATE = 'private'; - const TYPE_BOTH = 'both'; - const NOT_PUBLIC_MESSAGE = 'notPublicMessage'; - const NOT_PRIVATE_MESSAGE = 'notPublicMessage'; - const ALLOW_EMPTY = 'allowEmpty'; - const NOT_EMPTY_MESSAGE = 'notEmptyMessage'; - const ON_EVENT = 'onEvent'; - const EVENT_PRIVMSG = "privmsg"; - const EVENT_NOTICE = "notice"; - - - - /** - * Parent object - * - * @var Plugins_Abstract - */ - protected $_plugin; - - protected $_helpMessage = ''; - protected $type; - - protected $_defaults = array( - self::REQUIERED_ARGS => 0, - self::ACCEPTED_ARGS => 0, - self::COMMAND_VISIBILITY => self::TYPE_BOTH, - self::NOT_PUBLIC_MESSAGE => 'Sorry, but the command is not for public channel.', - self::NOT_PRIVATE_MESSAGE => 'Sorry, but the command is not for private query.', - ); - - protected $_defaultsArgs = array( - self::ALLOW_EMPTY => true, - - ); - - protected $_events = array( - self::EVENT_NOTICE => true, - self::EVENT_PRIVMSG => true - ); - - /** - * Event object - * - * @var Event - */ - protected $_event; - - public function __construct(Plugins_Abstract $plugin) { - $this->_plugin = $plugin; - } - - public function setOptions(array $options) { - foreach ($options as $option => $value) { - switch ($option) { - case self::ACCEPTED_ARGS: - case self::REQUIERED_ARGS: - $this->_default[$option] = $value; - break; - default: - throw new Exception ("Unkown option '$option'"); - } - } - } - - public function setCurrentEvent (Event $event) { - $this->_event = $event; - } - - public function helpMessage() { - return $this->_helpMessage; - } - - public function __get($option) { - return $this->_default[$option]; - } - abstract public function __toString(); -} diff --git a/sources/tick-class.inc.php b/sources/tick-class.inc.php index cb86881..f8775e0 100644 --- a/sources/tick-class.inc.php +++ b/sources/tick-class.inc.php @@ -1,8 +1,10 @@ . + * + * @category IrBot + * @package IrBot_Tick + * @copyright Copyright (c) 2007-2008 Bellière Ludovic + * @license http://www.gnu.org/licenses/gpl-3.0.html + * */ +/** + * Cron event emulation + * + * @category IrBot + * @package IrBot_Tick + * @copyright Copyright (c) 2007-2008 Bellière Ludovic + * @license http://www.gnu.org/licenses/gpl-3.0.html + */ class tick { private $ticks = array(); + /** + * IRCMain object + * + * @var IRCMain + */ private $ircmain; static public $instance = false; - public static function GetInstance() { + public static function GetInstance(IRCMain $ircmain) { if (!self::$instance) { - self::$instance = new tick(); + self::$instance = new tick($ircmain); } return self::$instance; } - private function __construct() { - $this->ircmain = bot::GetInstance(); + private function __construct(IRCMain $ircmain) { + $this->ircmain = $ircmain; } /** -- 2.11.4.GIT