From 936589301d8b26b5600b17323e7d5458531d9b8a Mon Sep 17 00:00:00 2001 From: xrogaan Date: Wed, 18 Jun 2008 13:43:27 +0200 Subject: [PATCH] cleaning --- sources/IRCMain.php | 96 ++++++++++++++++++++++++++------ sources/IRCMain/Adapter.php | 54 ------------------ sources/TextFormat-class.inc.php | 65 ---------------------- sources/irc-class.inc.php | 116 --------------------------------------- 4 files changed, 80 insertions(+), 251 deletions(-) delete mode 100644 sources/TextFormat-class.inc.php delete mode 100644 sources/irc-class.inc.php diff --git a/sources/IRCMain.php b/sources/IRCMain.php index c6dea34..41c01e5 100644 --- a/sources/IRCMain.php +++ b/sources/IRCMain.php @@ -22,10 +22,38 @@ require_once 'sources/IRCMain/Adapter.php'; class IRCMain extends IRCMain_Adapter { + public function __construct() { + $this->tick = tick::GetInstance(); + } + + private function _authentification(Event $event) { + // TODO : create a better login process + + if ($event->getDataFor() == self::getConfig('nick')) { + if (preg_match('`^connect ([^ ]+) ([^ ]+)`',trim($event->getDataMessage()),$m)) { + if ($m[1] == 'admin' && $m[2] == 'mypass') { + $this->auth = true; + self::notice($event->getDataSendBy(),'Vous êtes bien authentifié comme administrateur.'); + } else { + self::notice($event->getDataSendBy(),'Erreur dans votre login et / ou mot de passe.'); + echo debug() ? 'l: '.$m[1].' p: '.$m[2]."\n":''; + } + return true; + } + return false; + } + } public function launch() { parent::launch(); + $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(); + try { $this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($this->_socket === false) { @@ -96,27 +124,13 @@ class IRCMain extends IRCMain_Adapter { // ctcp if ($event->isCtcp()) { - $ctcp = new IRCMain_Ctcp($action,$this); $responce = $ctcp->getResponce(); self::notice($event->getDataSendBy(),$this->ctcp($responce)); - } else { - $dataInformation['message'] = trim($dataInformation['message']); - if ($dataInformation['to'] == self::$botName) { - // auth proccess - // TODO : create a better login process - if (preg_match('`^connect ([^ ]+) ([^ ]+)`',$dataInformation['message'],$m)) { - if ($m[1] == 'admin' && $m[2] == 'mypass') { - $this->auth = true; - self::notice($event->getDataSendBy(),'Vous êtes bien authentifié comme administrateur.'); - } else { - self::notice($event->getDataSendBy(),'Erreur dans votre login et / ou mot de passe.'); - echo debug() ? 'l: '.$m[1].' p: '.$m[2]."\n":''; - } - continue; - } + if (self::_authentification($event)) { + continue; } } break; @@ -143,6 +157,56 @@ 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 d759770..a083c76 100644 --- a/sources/IRCMain/Adapter.php +++ b/sources/IRCMain/Adapter.php @@ -45,54 +45,6 @@ class IRCMain_Adapter { ); abstract function __construct(array $options); - - 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; - } - } private function _checkTimeout() { $now = mktime(); @@ -107,12 +59,6 @@ class IRCMain_Adapter { public function launch() { $this->_pluginsInstance = new plugins(); - $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->_lastTimeData = mktime(); } diff --git a/sources/TextFormat-class.inc.php b/sources/TextFormat-class.inc.php deleted file mode 100644 index c03fe2c..0000000 --- a/sources/TextFormat-class.inc.php +++ /dev/null @@ -1,65 +0,0 @@ -. - */ - -class text_format { - static public $instance = FALSE; - - static function GetInstance() { - if (!text_format::$instance) { - text_format::$instance = new text_format(); - } - return text_format::$instance; - } - - /** - * Formate un texte pour la couleur - * - * @param unknown_type $msg Le message a formater - * @param unknown_type $text L'id de couleur du texte - * @param unknown_type $background L'id de couleur du background - * @return string - */ - function colors($msg,$text,$background=false) { - $color_tag = chr(3); - $first = ($background)?$color_tag.$text.','.$background:$color_tag.$text; - return $first.$msg.$color_tag; - } - - function bold($msg) { - return chr(2).$msg.chr(2); - } - - function ctcp($msg) { - return chr(1).$msg.chr(1); - } - - function reverse_color($msg) { - return chr(22).$msg.chr(22); - } - - 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/irc-class.inc.php b/sources/irc-class.inc.php deleted file mode 100644 index 6fe7705..0000000 --- a/sources/irc-class.inc.php +++ /dev/null @@ -1,116 +0,0 @@ -. - */ - -class irc { - - protected $current_msg_info = false; - private $ircmain; - private $msg, $msg_info; - - function __construct() { - $this->ircmain = bot::GetInstance(); - } - - function parse_get($msg) { - $this->msg = $msg; - - echo debug() ? "IRC:: -> ".$msg."\n" : ''; - // DECO - if (preg_match("`^ERROR :(Closing Link: )?(.*)$`i", $this->msg)) - { - $this->ircmain->disconnect('ERROR: Closing Link'); - sleep(3); - throw new Exception('Closing Link.',1); - } - // PONG - elseif (ereg("^PING ", $this->msg)) { - $pong = split(':',$this->msg); - $this->ircmain->put('PONG '.$pong[1]); - echo "PING :{$pong[1]}\nPONG {$pong[1]}\n\n"; - } - // Bot kicked, thanks kicker ! - elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.bot::$channel.' '.preg_quote(bot::$myBotName, '`').' :`', $this->msg, $T)) - { - sleep(1); - $this->ircmain->joinChannel(bot::$channel); - $this->ircmain->privmsg(bot::$channel,'Merci '.$T[1].' !'); - } - // OK : 001 - // Illegals characters in Nickname : 432 - // Nick already in use : 433 - elseif (preg_match('`^:[^ ]+ (001|432|433) (.*?)`', $this->msg,$T)) - { - switch ($T[1]) { - case 001: - $this->ircmain->joinChannel(bot::$channel); - break; - - case 432: - $this->ircmain->newNick('NoNameBot'.rand(0,9).rand(0,9)); - break; - case 433: - echo "Nick already in use\n\n"; - $this->ircmain->newNick(false); - default: - echo "Geeeh, I do not known what this mean ...\n"; - echo "DEBUG :>".$this->msg; - break; - } - } - - // : PRIVMSG : - if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.bot::$myBotName.'|'.bot::$channel.") :(.*)`",$this->msg,$T)) { - $this->msg_info = array ( - 'type' => 'PRIVMSG', - 'from' => $T[1], // owner - 'to' => $T[2], // message for bot or channel - 'message' => $T[3] - ); - } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.bot::$myBotName." :(.*)`",$this->msg,$T)) { - $this->msg_info = array ( - 'type' => 'NOTICE', - 'from' => $T[1], - 'to' => bot::$myBotName, - 'message' => $T[2] - ); - } - } - - /** - * return last privmsg / notice - * - * @return array - */ - public function get_msg_info() { - $temp = $this->msg_info; - $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 -- 2.11.4.GIT