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/>.
20 * @package IrBot_IRCMain
21 * @copyright Copyright (c) 2008 Bellière Ludovic
22 * @license http://www.gnu.org/licenses/gpl-3.0.html
28 require_once "sources/Registry.php";
30 abstract class IRCMain_Adapter
{
32 static public $_instance = false;
33 static public $_pluginsInstance = false;
36 protected $_lastTimeData = 0;
38 protected $_config = array(
39 'botVersion' => '1.0',
40 'server' => 'localhost',
48 'socketTimeout' => 180,
51 abstract function __construct(array $options);
53 protected function _checkTimeout() {
55 if ($this->_lastTimeData+self
::getConfig('socketTimeout') < mktime()) {
56 throw new Exception('Connection lost (Timeout).',1);
58 $lag = $now-$this->_lastTimeData
;
59 if ( $lag > 20 && ($lag %
10) == 0) {
60 echo "Lag: ".$lag." seconds\n";
64 protected function getIncomingData() {
67 socket_set_nonblock($this->_socket
);
70 //echo "TOC - ",time(),"\n";
71 //$this->tick->doAllTicks();
72 $buf = @socket_read
($this->_socket
, 4096);
75 $this->_checkTimeout();
80 $this->_lastTimeData
= mktime();
82 if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise
83 $buffer = $buffer.$buf;
84 $data = ""; //rien è envoyer
86 //Si contient au moins un retour,
87 //on vèrifie que le dernier caractère en est un
88 if (substr($buf, -1, 1) == "\n") {
89 //alors on additionne ces donnèes au buffer
91 $buffer = ""; //on vide le buffer
93 //si le dernier caractère n'est pas un retour è la
94 //ligne, alors on envoit tout jusqu'au dernier retour
95 //puis on bufferise le reste
96 $buffer = $buffer.substr($buf, strrchr($buf, "\n"));
97 $data = substr($buf, 0, strrchr($buf, "\n"));
98 $data = $buffer.$data;
99 $buffer = ""; //on vide le buffer
104 $data = split("\n", $data);
112 public function launch() {
113 //$this->_pluginsInstance = new plugins($this);
114 $this->_lastTimeData
= mktime();
117 public function plugins() {
118 if ($this->_pluginsInstance
instanceof plugins
) {
119 return $this->_pluginsInstance
;
121 throw new Exception('Plugins is not initialized');
125 protected function event() {
126 return new Event($this);
129 public function getConfig($name) {
130 return array_key_exists($name, $this->_config
) ?
$this->_config
[$name] : false ;
133 public function setConfig(array $options) {
134 foreach ($options as $option => $value) {
135 if ($option == 'botVersion') {
139 $this->_config
[$option] = $value;
144 public function joinChannel($channel) {
145 $this->put('JOIN '.$channel);
146 echo "Join channel $channel ...\n";
149 final protected function nick_change() {
150 echo "New nick : ".self
::$myBotName."\n";
151 if ($this->put('NICK :'.self
::$myBotName)) {
157 public function newNick($new=false) {
159 case self
::getConfig('nick'):
160 echo "New nick : [ERR] no changes :". self
::getConfig('nick') . ' == ' . $new ."\n";
163 self
::setConfig(array(
164 'nick' => self
::getConfig('nick').'_',
170 self
::setConfig(array(
180 * Envoie une notice a un salon / utilisateur
183 * @param string $message
185 public function notice ($to,$message) {
186 self
::put('NOTICE '.$to.' :'.$message."\n");
190 * Envoie un message (PRIVMSG) a un salon / utilisateur
193 * @param string $message
194 * @todo wrap message to 512 char (max)
196 public function privmsg ($to,$message) {
197 $search = array('#name');
198 $replace = array(self
::$myBotName);
199 $message = str_replace($search,$replace,$message);
200 self
::put('PRIVMSG '.$to.' :'.$message."\n");
204 * Multi message to send.
206 * @param string $to The reveiver
207 * @param array $messages The messages
208 * @param int $interval Delay execution in microseconds
210 public function mprivmsg($to,array $messages,$interval=650000) {
211 if (is_array($messages)) {
212 foreach ($messages as $msg) {
213 $this->privmsg($to,$msg);
220 * Send data to server.
221 * Return false if it fail
223 * @param string $data Raw data to send
226 public function put($data) {
227 if (!is_resource($this->_socket
)) {
228 throw new Exception('Connection lost...',1);
232 echo debug() ?
'core::put() -> ' . $data . "\n" : '';
234 $ok = socket_write($this->_socket
, $data . "\n");
244 public function colors($msg,$colorText,$colorBackground=false) {
246 $first = ($background) ?
$colorTag . $colorText . ',' . $colorBackground : $colorTag . $colorText;
247 return $first.$msg.$colorTag;
250 public function bold($msg) {
251 return chr(2).$msg.chr(2);
254 public function ctcp($msg) {
255 return chr(1).$msg.chr(1);
258 public function reverse_color($msg) {
259 return chr(22).$msg.chr(22);
262 public function underline($msg) {
263 return chr(31).$msg.chr(31);
267 * Return an array for multilines paragraphe
269 * @param string $msg Message
272 public function paragraphe($msg) {
273 return explode("\n",$msg);
277 * Return the nickname of the bot
281 public function getBotName() {
282 return self
::getConfig('nick');
285 public function restart() {
287 echo 'Restart...'."\n";
288 $this->disconnect('Restarting, please wait ...');
290 throw new Exception('Restart...',1);
292 $this->privmsg($this->msg_info
['from'],"Vous n'etes pas authentifie.");
296 public function quit() {
299 throw new Exception('Quit from',3);
301 $this->privmsg($this->msg_info
['from'],"Vous n'etes pas authentifie.");
305 public function disconnect($msg='EOL ;') {
306 //$this->plugins->unload_plugin('_all');
307 echo "core::disconnect() -> Closing Link...";
308 if (is_resource($this->_socket
)) {
309 if ($this->put('QUIT :'.$msg)) {
310 $this->connected
= false;
311 socket_close($this->_socket
);
313 echo "\t\t\tdone.\n";
315 echo "\t\t\tError.\n";
316 echo "core::disconnect() -> Connection already breack down.\n";
321 function __destruct() {
322 if (is_resource($this->_socket
)) {
323 echo "Error occured.";
324 if ($this->put('QUIT :Error occured')) {
325 socket_close($this->_socket
);
328 echo "\n\nAll process shut down, bye.";