1
[irbot.git] / sources / IRCConn-class.inc.php
blob62a3a355c40f0175dfa4520ddedd9dd2cced502c
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 $this->put('QUIT :'.$msg);
42 if(is_resource($this->C)) {
43 @fclose($this->C);
47 protected function connect() {
48 $this->C = @fsockopen(self::$server, self::$port, $errno, $errstr, 10);
49 if (!$this->C) {
50 die('Impossible de se connecter au server IRC !'."\n");
52 // User
53 $this->put('USER '.self::$myBotName.' '.self::$myBotName.'@'.self::$ip.' '.self::$domain.' :XBOT');
54 // Nick
55 $this->put('NICK '.self::$myBotName);
58 public function joinChannel($channel) {
59 $this->put('JOIN '.$channel);
62 public function newNick() {
63 self::$myBotName .= '_';
66 public function put($command) {
67 if (!is_resource($this->C)) {
68 self::disconnect();
69 die("Connection lost...\n");
71 echo '[-> ' . $command . "\n";
72 fputs($this->C, $command . "\n");
75 public function get() {
76 // Stream Timeout
77 stream_set_timeout($this->C, $this->socketTimeout);
78 $tmp1 = time();
79 $content = fgets($this->C, 1024);
80 echo "<-]$content\n";
81 if ($content != ''){
82 return $content;
84 // TIMEOUT
85 if (time()-$tmp1 >= $this->socketTimeout) {
86 die('TIMEOUT'."\n");