12
[irbot.git] / sources / IRCConn-class.inc.php
blob5895cbe2f8ffda721e796f1d9f6cf10d83e7965d
1 <?php
3 // old file, will be deleted
5 Class IRCConn {
7 // Timeout avant une reconnexion
8 public $socketTimeout = 280;
10 // Variables privées
11 static public $instance = FALSE;
12 protected $C;
13 static $botVersion = '1.0';
14 static $server;
15 static $port;
16 static $channel;
17 static $myBotName;
18 static $ip;
19 static $domain;
20 static $connection_password;
23 private function __construct() {
24 $this->connect(self::$server, self::$port);
27 static function Init($server, $port, $channel, $myBotName='XboT', $ip='127.0.0.1', $domain='localhost', $connection_password=false) {
28 self::$server = $server;
29 self::$port = $port;
30 self::$channel = $channel;
31 self::$myBotName = preg_replace('`[^_[:alnum:]\`\\\\[\]^-]`', '', $myBotName);
32 self::$ip = $ip;
33 self::$domain = $domain;
34 self::$connection_password = $connection_password;
37 static function GetInstance() {
38 if (!IRCConn::$instance) {
39 IRCConn::$instance = new IRCConn();
41 return IRCConn::$instance;
44 public function disconnect($msg='EOL ;') {
45 echo 'Closing Link...'."\n";
46 $this->put('QUIT :'.$msg);
47 if (is_resource($this->C)) {
48 @fclose($this->C);
50 return true;
53 protected function connect() {
54 $this->C = @fsockopen(self::$server, self::$port, $errno, $errstr, 10);
55 if (!$this->C) {
56 echo "Impossible de se connecter au server IRC !\n";
57 return 0;
59 if (self::$connection_password!==false) {
60 $this->put('PASS '.self::$connection_password);
62 // TODO : be sure for the validity of the connection password
63 // User
64 $this->put('USER '.self::$myBotName.' '.self::$myBotName.'@'.self::$ip.' '.self::$domain.' :XBOT');
65 // Nick
66 $this->put('NICK '.self::$myBotName);
69 public function joinChannel($channel) {
70 $this->put('JOIN '.$channel);
71 echo "Join channel $channel ...\n";
74 private function nick_change() {
75 echo "New nick : ".self::$myBotName."\n";
76 $this->put('NICK :'.self::$myBotName);
79 public function newNick($new=false) {
80 switch ($new) {
81 case self::$myBotName:
82 echo "New nick : [ERR] no changes :". self::$myBotName . ' == ' . $new ."\n";
83 break;
84 case false:
85 self::$myBotName .= '_';
86 self::nick_change();
87 break;
88 default:
89 self::$myBotName = $new;
90 self::nick_change();
91 break;
95 /**
96 * Envoie une notice a un salon / utilisateur
98 * @param string $to
99 * @param string $message
101 public function notice ($to,$message) {
102 echo "ERROR : deprecated use of notice function. Please use IRCMain::notice\n";
103 return self::put('NOTICE '.$to.' :'.$message."\n");
107 * Envoie un message (PRIVMSG) a un salon / utilisateur
109 * @param string $to
110 * @param string $message
112 public function privmsg ($to,$message) {
113 echo "ERROR : deprecated use of privmsg function. Please use IRCMain::privmsg\n";
114 return self::put('PRIVMSG '.$to.' :'.$message."\n");
117 public function put($command) {
118 if (!is_resource($this->C)) {
119 self::error(1,'Connection lost...');
120 return false;
122 echo debug() ? '[->' . $command . "" : '';
123 fputs($this->C, $command . "\n");
124 return true;
127 public function get() {
128 // Stream Timeout
129 stream_set_timeout($this->C, $this->socketTimeout);
130 $tmp1 = time();
131 $content = fgets($this->C, 1024);
132 echo debug() ? "<-]$content" : '';
133 if ($content != '') {
134 return $content;
136 // TIMEOUT
137 if (time()-$tmp1 >= $this->socketTimeout) {
138 self::error(0,'TIMEOUT');
139 return false;