Verify the existence of the command file before loading in bot
[irbot.git] / sources / IRCMain.php
blob0410da91848e969aab83d95200d4f35733528a57
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/>.
19 * @category IrBot
20 * @package IrBot_IRCMain
21 * @copyright Copyright (c) 2008 Bellière Ludovic
22 * @license http://www.gnu.org/licenses/gpl-3.0.html
25 require_once 'sources/IRCMain/Adapter.php';
27 class IRCMain extends IRCMain_Adapter {
29 public function __construct(array $options) {
30 self::setConfig($options);
31 $this->tick = tick::GetInstance($this);
34 private function _authentification(Event $event) {
35 // TODO : create a better login process
37 if ($event->getDataFor() == self::getConfig('nick')) {
38 if (preg_match('`^connect ([^ ]+) ([^ ]+)`',trim($event->getDataMessage()),$m)) {
39 if ($m[1] == 'admin' && $m[2] == 'mypass') {
40 $this->auth = true;
41 self::notice($event->getDataSendBy(),'Vous êtes bien authentifié comme administrateur.');
42 } else {
43 self::notice($event->getDataSendBy(),'Erreur dans votre login et / ou mot de passe.');
44 echo debug() ? 'l: '.$m[1].' p: '.$m[2]."\n":'';
46 return true;
48 return false;
52 public function launch() {
53 parent::launch();
55 /* $this->_pluginsInstance->add_command('core','shownick',0,'Show the current nick used (debug)','mixed');
56 $this->_pluginsInstance->add_command('core','nick',1,'Change the current nick','mixed');
57 $this->_pluginsInstance->add_command('core','quit',0,'Disconnect and stop the process','mixed');
58 $this->_pluginsInstance->add_command('core','restart',0,'Disconnect and restart the process','mixed');
59 $this->_pluginsInstance->add_command('core','help',0,'Hmm, je dois recoder cette fonction','mixed');
60 $this->_pluginsInstance->Init();*/
62 try {
63 $this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
64 if ($this->_socket === false) {
65 throw new Exception('Impossible de créer le socket IRC !',0);
68 if (self::getConfig('ip') != false && self::getConfig('ip') != "") {
69 socket_bind($this->_socket, self::getConfig('ip'));
72 $x = self::getConfig('server');
73 $socketUp = socket_connect($this->_socket, self::getConfig('server'), self::getConfig('port'));
74 if ($socketUp===false) {
75 throw new Exception('Impossible de se connecter au server IRC ! ('.socket_strerror ( socket_last_error() ).' (errno:'.socket_last_error().'))' ,0);
78 $this->connected = true;
80 if (self::getConfig('password') !== false) {
81 $this->put('PASS '.self::getConfig('password'));
83 // TODO : be sure for the validity of the connection password (what chain server return if fail ?)
85 $this->put('USER '.self::getConfig('nick').' '.self::getConfig('nick').'@'.self::getConfig('ip').' '.self::getConfig('server').' :'.self::getConfig('nick'));
86 $this->put('NICK '.self::getConfig('nick'));
88 /*$this->tick->setTick('all5sec',5);
89 $this->tick->addJob('all5sec','hello chan','privmsg',array(self::$channel,"I'm a tick. I show this msg all 5sec."),0,$this);
92 // Into the while !
93 while (1) {
95 // current data
96 $this->incomingData = $this->getIncomingData();
98 if (!is_array($this->incomingData)) {
99 $this->incomingData = array($this->incomingData);
102 foreach ($this->incomingData as $data) {
103 if (!empty($data)) {
104 $event = $this->event()->setIncoming($data);
106 $action = $event->getAction();
108 switch ($action) {
109 case Event::ACT_DISCONNECT:
110 $this->disconnect('ERROR: Closing Link');
111 sleep(3);
112 throw new Exception('Closing Link.',1);
113 break;
114 case Event::ACT_PING:
115 $pong = split(':',$data);
116 $this->put('PONG '.$pong[1]);
117 echo "PING :{$pong[1]}\nPONG {$pong[1]}\n\n";
118 break;
119 case Event::ACT_KICK:
120 sleep(1);
121 $this->joinChannel(self::getConfig('channel'));
122 break;
125 $this->dataInformation = $dataInformation = $event->getData();
127 switch ($dataInformation['type']) {
129 case Plugins_Command_Abstract::EVENT_PRIVMSG:
131 // ctcp
132 if ($event->isCtcp()) {
133 $ctcp = new IRCMain_Ctcp($action,$this);
134 $responce = $ctcp->getResponce();
136 self::notice($event->getDataSendBy(),$this->ctcp($responce));
137 } else {
138 if (self::_authentification($event)) {
139 continue;
142 break;
143 case 'NOTICE':
144 break;
146 //$this->plugins()->set_event($event);
150 } catch (myRuntimeException $e) {
151 $x = $e->getMessage();
152 echo $x;
153 $x.= backtrace($e->getTrace());
154 file_put_contents('errorlog',$x);
155 //if ($e->_level < 2 || ($e->_level >= 256 && $e->_level < 1024)) {
156 echo 'Error level : '.$e->_level."\n\n";
157 throw new Exception('Error occured',0);
158 /*} else {
159 echo 'Error level : '.$e->_level."\n\n\n\n";
160 throw new Exception("Error occured. Please see errorlog for details.",1);
162 } catch (Exception $e) {
163 throw $e;