3
[irbot.git] / sources / IRCConn-class.inc.php
blob76b38fba942aa03423257b8a16cadc8cab5b976d
1 <?php
3 Class IRCConn {
5 // Timeout avant une reconnexion
6 public $socketTimeout = 280;
8 // Variables privées
9 static public $instance = FALSE;
10 protected $C;
11 static $botVersion = 1.0;
12 static $server;
13 static $port;
14 static $channel;
15 static $myBotName;
16 static $ip;
17 static $domain;
20 private function __construct() {
21 $this->connect(self::$server, self::$port);
24 static function Init($server, $port, $channel, $myBotName='XboT', $ip='127.0.0.1', $domain='localhost') {
25 self::$server = $server;
26 self::$port = $port;
27 self::$channel = $channel;
28 self::$myBotName = preg_replace('`[^_[:alnum:]\`\\\\[\]^-]`', '', $myBotName);
29 self::$ip = $ip;
30 self::$domain = $domain;
33 static function GetInstance() {
34 if(!IRCConn::$instance) {
35 IRCConn::$instance = new IRCConn();
37 return IRCConn::$instance;
40 public function disconnect($msg='EOL ;') {
41 echo 'Closing Link...'."\n";
42 $this->put('QUIT :'.$msg);
43 if(is_resource($this->C)) {
44 @fclose($this->C);
48 protected function connect() {
49 $this->C = @fsockopen(self::$server, self::$port, $errno, $errstr, 10);
50 if (!$this->C) {
51 die('Impossible de se connecter au server IRC !'."\n");
53 // User
54 $this->put('USER '.self::$myBotName.' '.self::$myBotName.'@'.self::$ip.' '.self::$domain.' :XBOT');
55 // Nick
56 $this->put('NICK '.self::$myBotName);
59 public function joinChannel($channel) {
60 $this->put('JOIN '.$channel);
61 echo "Join channel $channel ...\n";
64 private function nick_change() {
65 echo "New nick : ".self::$myBotName."\n";
66 $this->put('NICK :'.self::$myBotName);
69 public function newNick($new=false) {
70 switch ($new) {
71 case self::$myBotName:
72 echo "New nick : [ERR] no changes :". self::$myBotName . ' == ' . $new ."\n";
73 break;
74 case false:
75 self::$myBotName .= '_';
76 self::nick_change();
77 break;
78 default:
79 self::$myBotName = $new;
80 self::nick_change();
81 break;
85 public function put($command) {
86 if (!is_resource($this->C)) {
87 die("Connection lost...\n");
89 echo '[->' . $command . "\n";
90 fputs($this->C, $command . "\n");
93 public function get() {
94 // Stream Timeout
95 stream_set_timeout($this->C, $this->socketTimeout);
96 $tmp1 = time();
97 $content = fgets($this->C, 1024);
98 echo "<-]$content\n";
99 if ($content != ''){
100 return $content;
102 // TIMEOUT
103 if (time()-$tmp1 >= $this->socketTimeout) {
104 die('TIMEOUT'."\n");