3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007-2008 Bellière Ludovic
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 require_once 'sources/IRCMain/Adapter.php';
23 class IRCMain
extends IRCMain_Adapter
{
25 public function __construct() {
26 $this->tick
= tick
::GetInstance();
29 private function _authentification(Event
$event) {
30 // TODO : create a better login process
32 if ($event->getDataFor() == self
::getConfig('nick')) {
33 if (preg_match('`^connect ([^ ]+) ([^ ]+)`',trim($event->getDataMessage()),$m)) {
34 if ($m[1] == 'admin' && $m[2] == 'mypass') {
36 self
::notice($event->getDataSendBy(),'Vous êtes bien authentifié comme administrateur.');
38 self
::notice($event->getDataSendBy(),'Erreur dans votre login et / ou mot de passe.');
39 echo debug() ?
'l: '.$m[1].' p: '.$m[2]."\n":'';
47 public function launch() {
50 $this->_pluginsInstance
->add_command('core','shownick',0,'Show the current nick used (debug)','mixed');
51 $this->_pluginsInstance
->add_command('core','nick',1,'Change the current nick','mixed');
52 $this->_pluginsInstance
->add_command('core','quit',0,'Disconnect and stop the process','mixed');
53 $this->_pluginsInstance
->add_command('core','restart',0,'Disconnect and restart the process','mixed');
54 $this->_pluginsInstance
->add_command('core','help',0,'Hmm, je dois recoder cette fonction','mixed');
55 $this->_pluginsInstance
->Init();
58 $this->_socket
= socket_create(AF_INET
, SOCK_STREAM
, SOL_TCP
);
59 if ($this->_socket
=== false) {
60 throw new Exception('Impossible de créer le socket IRC !',0);
63 if (self
::$ip != "") {
64 socket_bind($this->_socket
, self
::getConfig('ip'));
67 $socketUp = socket_connect($this->_socket
, self
::getConfig('server'), self
::getConfig('port'));
68 if ($socketUp===false) {
69 throw new Exception('Impossible de se connecter au server IRC ! ('.socket_last_error().')' ,0);
72 $this->connected
= true;
74 if (self
::$connection_password !== false) {
75 $this->put('PASS '.self
::getConfig('password'));
77 // TODO : be sure for the validity of the connection password (what chain server return if fail ?)
79 $this->put('USER '.self
::getConfig('nick').' '.self
::getConfig('nick').'@'.self
::getConfig('ip').' '.self
::getConfig('server').' :'.self
::getConfig('nick'));
80 $this->put('NICK '.self
::getConfig('nick'));
82 /*$this->tick->setTick('all5sec',5);
83 $this->tick->addJob('all5sec','hello chan','privmsg',array(self::$channel,"I'm a tick. I show this msg all 5sec."),0,$this);
90 $this->incomingData
= $this->getIncomingData();
92 if (!is_array($this->incomingData
)) {
93 $this->incomingData
= array($this->incomingData
);
96 foreach ($this->incomingData
as $data) {
98 $event = $this->event()->setIncoming($data);
100 $action = $event->getAction();
103 case Event
::ACT_DISCONNECT
:
104 $this->disconnect('ERROR: Closing Link');
106 throw new Exception('Closing Link.',1);
108 case Event
::ACT_PING
:
109 $pong = split(':',$data);
110 $this->put('PONG '.$pong[1]);
111 echo "PING :{$pong[1]}\nPONG {$pong[1]}\n\n";
113 case Event
::ACT_KICK
:
115 $this->joinChannel(self
::getConfig('channel'));
119 $this->dataInformation
= $dataInformation = $event->getData();
121 switch ($dataInformation['type']) {
123 case Plugins_Command_Abstract
::EVENT_PRIVMSG
:
126 if ($event->isCtcp()) {
127 $ctcp = new IRCMain_Ctcp($action,$this);
128 $responce = $ctcp->getResponce();
130 self
::notice($event->getDataSendBy(),$this->ctcp($responce));
132 if (self
::_authentification($event)) {
140 self
::$plugins->set_event($event);
144 } catch (myRuntimeException
$e) {
145 $x = $e->getMessage();
147 $x.= backtrace($e->getTrace());
148 file_put_contents('errorlog',$x);
149 //if ($e->_level < 2 || ($e->_level >= 256 && $e->_level < 1024)) {
150 echo 'Error level : '.$e->_level
."\n\n";
151 throw new Exception('Error occured',0);
153 echo 'Error level : '.$e->_level."\n\n\n\n";
154 throw new Exception("Error occured. Please see errorlog for details.",1);
156 } catch (Exception
$e) {
163 public function getIncomingData() {
166 socket_set_nonblock($this->C
);
169 //echo "TOC - ",time(),"\n";
170 //$this->tick->doAllTicks();
171 $buf = @socket_read
($this->_socket
, 4096);
174 $this->_checkTimeout();
179 $this->_lastTimeData
= mktime();
181 if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise
182 $buffer = $buffer.$buf;
183 $data = ""; //rien è envoyer
185 //Si contient au moins un retour,
186 //on vèrifie que le dernier caractère en est un
187 if (substr($buf, -1, 1) == "\n") {
188 //alors on additionne ces donnèes au buffer
189 $data = $buffer.$buf;
190 $buffer = ""; //on vide le buffer
192 //si le dernier caractère n'est pas un retour è la
193 //ligne, alors on envoit tout jusqu'au dernier retour
194 //puis on bufferise le reste
195 $buffer = $buffer.substr($buf, strrchr($buf, "\n"));
196 $data = substr($buf, 0, strrchr($buf, "\n"));
197 $data = $buffer.$data;
198 $buffer = ""; //on vide le buffer
203 $data = split("\n", $data);