3 require "sources/Registry.php";
5 class IRCMain_Adapter
{
7 static public $_instance = false;
8 static public $_pluginsInstance = false;
11 protected $_lastTimeData = 0;
13 protected $_config = array(
14 'botVersion' => '1.0',
15 'server' => 'localhost',
23 'socketTimeout' => 180,
26 abstract function __construct(array $options);
28 public function getIncomingData() {
31 socket_set_nonblock($this->C
);
34 //echo "TOC - ",time(),"\n";
35 $this->tick
->doAllTicks();
36 $buf = @socket_read
($this->_socket
, 4096);
39 $this->_checkTimeout();
44 $this->_lastTimeData
= mktime();
46 if (!strpos($buf, "\n")) { //Si ne contient aucun retour, on bufferise
47 $buffer = $buffer.$buf;
48 $data = ""; //rien è envoyer
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
55 $buffer = ""; //on vide le buffer
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
68 $data = split("\n", $data);
76 private function _checkTimeout() {
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') {
108 $this->_config
[$option] = $value;
113 public function joinChannel($channel) {
114 $this->put('JOIN '.$channel);
115 echo "Join channel $channel ...\n";
118 public function newNick($new=false) {
120 case self
::getConfig('nick'):
121 echo "New nick : [ERR] no changes :". self
::getConfig('nick') . ' == ' . $new ."\n";
124 self
::setConfig(array(
125 'nick' => self
::getConfig('nick').'_',
131 self
::setConfig(array(
141 * Envoie une notice a un salon / utilisateur
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
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);
173 public function put($data) {
174 if (!is_resource($this->_socket
)) {
175 throw new Exception('Connection lost...',1);
179 echo debug() ?
'bot::put() -> ' . $data . "\n" : '';
181 $ok = socket_write($this->_socket
, $data ."\n");
191 public function colors($msg,$text,$background=false) {
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
);