IrBot can connect a server with the new code structure.
[irbot.git] / sources / IRCMain.php
blob782e9bc945e43984600acb280a0317a0d9edfe73
1 <?php
2 /**
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(array $options) {
26 self::setConfig($options);
27 $this->tick = tick::GetInstance($this);
30 private function _authentification(Event $event) {
31 // TODO : create a better login process
33 if ($event->getDataFor() == self::getConfig('nick')) {
34 if (preg_match('`^connect ([^ ]+) ([^ ]+)`',trim($event->getDataMessage()),$m)) {
35 if ($m[1] == 'admin' && $m[2] == 'mypass') {
36 $this->auth = true;
37 self::notice($event->getDataSendBy(),'Vous êtes bien authentifié comme administrateur.');
38 } else {
39 self::notice($event->getDataSendBy(),'Erreur dans votre login et / ou mot de passe.');
40 echo debug() ? 'l: '.$m[1].' p: '.$m[2]."\n":'';
42 return true;
44 return false;
48 public function launch() {
49 parent::launch();
51 /* $this->_pluginsInstance->add_command('core','shownick',0,'Show the current nick used (debug)','mixed');
52 $this->_pluginsInstance->add_command('core','nick',1,'Change the current nick','mixed');
53 $this->_pluginsInstance->add_command('core','quit',0,'Disconnect and stop the process','mixed');
54 $this->_pluginsInstance->add_command('core','restart',0,'Disconnect and restart the process','mixed');
55 $this->_pluginsInstance->add_command('core','help',0,'Hmm, je dois recoder cette fonction','mixed');
56 $this->_pluginsInstance->Init();*/
58 try {
59 $this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
60 if ($this->_socket === false) {
61 throw new Exception('Impossible de créer le socket IRC !',0);
64 if (self::getConfig('ip') != false && self::getConfig('ip') != "") {
65 socket_bind($this->_socket, self::getConfig('ip'));
68 $x = self::getConfig('server');
69 $socketUp = socket_connect($this->_socket, self::getConfig('server'), self::getConfig('port'));
70 if ($socketUp===false) {
71 throw new Exception('Impossible de se connecter au server IRC ! ('.socket_strerror ( socket_last_error() ).' (errno:'.socket_last_error().'))' ,0);
74 $this->connected = true;
76 if (self::getConfig('password') !== false) {
77 $this->put('PASS '.self::getConfig('password'));
79 // TODO : be sure for the validity of the connection password (what chain server return if fail ?)
81 $this->put('USER '.self::getConfig('nick').' '.self::getConfig('nick').'@'.self::getConfig('ip').' '.self::getConfig('server').' :'.self::getConfig('nick'));
82 $this->put('NICK '.self::getConfig('nick'));
84 /*$this->tick->setTick('all5sec',5);
85 $this->tick->addJob('all5sec','hello chan','privmsg',array(self::$channel,"I'm a tick. I show this msg all 5sec."),0,$this);
88 // Into the while !
89 while (1) {
91 // current data
92 $this->incomingData = $this->getIncomingData();
94 if (!is_array($this->incomingData)) {
95 $this->incomingData = array($this->incomingData);
98 foreach ($this->incomingData as $data) {
99 if (!empty($data)) {
100 $event = $this->event()->setIncoming($data);
102 $action = $event->getAction();
104 switch ($action) {
105 case Event::ACT_DISCONNECT:
106 $this->disconnect('ERROR: Closing Link');
107 sleep(3);
108 throw new Exception('Closing Link.',1);
109 break;
110 case Event::ACT_PING:
111 $pong = split(':',$data);
112 $this->put('PONG '.$pong[1]);
113 echo "PING :{$pong[1]}\nPONG {$pong[1]}\n\n";
114 break;
115 case Event::ACT_KICK:
116 sleep(1);
117 $this->joinChannel(self::getConfig('channel'));
118 break;
121 $this->dataInformation = $dataInformation = $event->getData();
123 switch ($dataInformation['type']) {
125 case Plugins_Command_Abstract::EVENT_PRIVMSG:
127 // ctcp
128 if ($event->isCtcp()) {
129 $ctcp = new IRCMain_Ctcp($action,$this);
130 $responce = $ctcp->getResponce();
132 self::notice($event->getDataSendBy(),$this->ctcp($responce));
133 } else {
134 if (self::_authentification($event)) {
135 continue;
138 break;
139 case 'NOTICE':
140 break;
142 //$this->plugins()->set_event($event);
146 } catch (myRuntimeException $e) {
147 $x = $e->getMessage();
148 echo $x;
149 $x.= backtrace($e->getTrace());
150 file_put_contents('errorlog',$x);
151 //if ($e->_level < 2 || ($e->_level >= 256 && $e->_level < 1024)) {
152 echo 'Error level : '.$e->_level."\n\n";
153 throw new Exception('Error occured',0);
154 /*} else {
155 echo 'Error level : '.$e->_level."\n\n\n\n";
156 throw new Exception("Error occured. Please see errorlog for details.",1);
158 } catch (Exception $e) {
159 throw $e;