improve plugins adapter, plugins command adapter and more
[irbot.git] / sources / IRCMain / Adapter.php
blob83309ac584284c19f19116e04d53e94d9079d8b5
1 <?php
3 require "sources/Registry.php";
5 class IRCMain_Adapter {
7 static public $_instance = false;
8 static public $_pluginsInstance = false;
10 protected $_socket;
11 protected $_lastTimeData = 0;
13 protected $_config = array(
14 'botVersion' => '1.0',
15 'server' => 'localhost',
16 'port' => 6667,
17 'channel' => '#test',
18 'nick' => 'irBot',
19 'ip' => '',
20 'domain' => '',
21 'password' => false,
22 'nickserv' => false,
23 'socketTimeout' => 180,
26 abstract function __construct(array $options);
28 public function getIncomingData() {
30 $buffer = '';
31 socket_set_nonblock($this->C);
33 while (1) {
34 //echo "TOC - ",time(),"\n";
35 $this->tick->doAllTicks();
36 $buf = @socket_read($this->_socket, 4096);
38 if (empty($buf)) {
39 $this->_checkTimeout();
40 sleep(1);
41 continue;
44 $this->_lastTimeData = mktime();
46 if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise
47 $buffer = $buffer.$buf;
48 $data = ""; //rien è envoyer
49 } else {
50 //Si contient au moins un retour,
51 //on vèrifie que le dernier caractère en est un
52 if (substr($buf, -1, 1) == "\n") {
53 //alors on additionne ces donnèes au buffer
54 $data = $buffer.$buf;
55 $buffer = ""; //on vide le buffer
56 } else {
57 //si le dernier caractère n'est pas un retour è la
58 //ligne, alors on envoit tout jusqu'au dernier retour
59 //puis on bufferise le reste
60 $buffer = $buffer.substr($buf, strrchr($buf, "\n"));
61 $data = substr($buf, 0, strrchr($buf, "\n"));
62 $data = $buffer.$data;
63 $buffer = ""; //on vide le buffer
67 if ($data != '') {
68 $data = split("\n", $data);
69 return $data;
72 continue;
76 private function _checkTimeout() {
77 $now = mktime();
78 if ($this->_lastTimeData+self::getConfig('socketTimeout') < mktime()) {
79 throw new Exception('Connection lost (Timeout).',1);
81 $lag = $now-$this->_lastTimeData;
82 if ( $lag > 20 && ($lag % 10) == 0) {
83 echo "Lag: ".$lag." seconds\n";
87 public function launch() {
88 $this->_pluginsInstance = new plugins();
89 $this->_pluginsInstance->add_command('core','shownick',0,'Show the current nick used (debug)','mixed');
90 $this->_pluginsInstance->add_command('core','nick',1,'Change the current nick','mixed');
91 $this->_pluginsInstance->add_command('core','quit',0,'Disconnect and stop the process','mixed');
92 $this->_pluginsInstance->add_command('core','restart',0,'Disconnect and restart the process','mixed');
93 $this->_pluginsInstance->add_command('core','help',0,'Hmm, je dois recoder cette fonction','mixed');
94 $this->_pluginsInstance->Init();
95 $this->_lastTimeData = mktime();
98 public function getConfig($name) {
99 return $this->_config[$name];
102 public function setConfig(array $options) {
103 foreach ($options as $option => $value) {
104 if ($option == 'botVersion') {
105 return false;
108 $this->_config[$option] = $value;
110 return true;
113 public function joinChannel($channel) {
114 $this->put('JOIN '.$channel);
115 echo "Join channel $channel ...\n";
118 public function newNick($new=false) {
119 switch ($new) {
120 case self::getConfig('nick'):
121 echo "New nick : [ERR] no changes :". self::getConfig('nick') . ' == ' . $new ."\n";
122 break;
123 case false:
124 self::setConfig(array(
125 'nick' => self::getConfig('nick').'_',
128 self::nick_change();
129 break;
130 default:
131 self::setConfig(array(
132 'nick' => $new
135 self::nick_change();
136 break;
141 * Envoie une notice a un salon / utilisateur
143 * @param string $to
144 * @param string $message
146 public function notice ($to,$message) {
147 self::put('NOTICE '.$to.' :'.$message."\n");
151 * Envoie un message (PRIVMSG) a un salon / utilisateur
153 * @param string $to
154 * @param string $message
155 * @todo wrap message to 512 char (max)
157 public function privmsg ($to,$message) {
158 $search = array('#name');
159 $replace = array(self::$myBotName);
160 $message = str_replace($search,$replace,$message);
161 self::put('PRIVMSG '.$to.' :'.$message."\n");
164 public function mprivmsg($to,$messages,$interval=650000) {
165 if (is_array($messages)) {
166 foreach ($messages as $msg) {
167 $this->privmsg($to,$msg);
168 usleep($interval);
173 public function put($data) {
174 if (!is_resource($this->_socket)) {
175 throw new Exception('Connection lost...',1);
176 return;
179 echo debug() ? 'bot::put() -> ' . $data . "\n" : '';
181 $ok = socket_write($this->_socket, $data ."\n");
182 if ($ok) {
183 return true;
184 } else {
185 return false;
188 return true;
191 public function colors($msg,$text,$background=false) {
192 $color_tag = chr(3);
193 $first = ($background)?$color_tag.$text.','.$background:$color_tag.$text;
194 return $first.$msg.$color_tag;
197 public function bold($msg) {
198 return chr(2).$msg.chr(2);
201 public function ctcp($msg) {
202 return chr(1).$msg.chr(1);
205 public function reverse_color($msg) {
206 return chr(22).$msg.chr(22);
209 public function underline($msg) {
210 return chr(31).$msg.chr(31);
213 public function paragraphe($msg) {
214 return explode("\n",$msg);
217 public function getBotName() {
218 return self::getConfig('nick');
221 function __destruct() {
222 if (is_resource($this->_socket)) {
223 if ($this->put('QUIT :Error occured')) {
224 socket_close($this->_socket);