6
[irbot.git] / sources / IRCConn-class.inc.php
blob085208073850b09d0d48524de0aa2b11fc8dcc24
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 /**
86 * Envoie une notice a un salon / utilisateur
88 * @param string $to
89 * @param string $message
91 public function notice ($to,$message) {
92 self::put('NOTICE '.$to.' :'.$message."\n");
95 /**
96 * Envoie un message (PRIVMSG) a un salon / utilisateur
98 * @param string $to
99 * @param string $message
101 public function privmsg ($to,$message) {
102 self::put('PRIVMSG '.$to.' :'.$message."\n");
105 public function put($command) {
106 if (!is_resource($this->C)) {
107 die("Connection lost...\n");
109 echo debug() ? '[->' . $command . "" : '';
110 fputs($this->C, $command . "\n");
113 public function get() {
114 // Stream Timeout
115 stream_set_timeout($this->C, $this->socketTimeout);
116 $tmp1 = time();
117 $content = fgets($this->C, 1024);
118 echo debug() ? "<-]$content" : '';
119 if ($content != ''){
120 return $content;
122 // TIMEOUT
123 if (time()-$tmp1 >= $this->socketTimeout) {
124 die('TIMEOUT'."\n");