5 // Timeout avant une reconnexion
6 public $socketTimeout = 280;
9 static public $instance = FALSE;
13 static $botVersion = '1.0';
20 static $connection_password;
22 private $irc, $plugin;
26 public $plugins = array();
29 private $core_commands;
32 public static function GetInstance() {
33 if (!self
::$instance) {
34 self
::$instance = new main();
36 return self
::$instance;
39 private function __construct() {
40 // load config and other things
43 $this->plugin
= new plugin
;
44 $this->formater
= text_format
::GetInstance();
46 // core plugin is an exeption in do_command
47 $this->plugin
->add_command('core','shownick',0,'Show the current nick used (debug)');
48 $this->plugin
->add_command('core','nick',1,'Change the current nick');
49 $this->plugin
->add_command('core','quit',0,'Disconnect and stop the process');
50 $this->plugin
->add_command('core','restart',0,'Disconnect and restart the process');
51 $this->plugin
->add_command('core','help',0,'Hmm, je dois recoder cette fonction');
54 public function launch() {
56 $this->C
= @fsockopen
(self
::$server, self
::$port, $errno, $errstr, 10);
58 throw new Exception('Impossible de se connecter au server IRC !',0);
61 if (self
::$connection_password !== false) {
62 $this->put('PASS '.self
::$connection_password);
64 // TODO : be sure for the validity of the connection password (what chain server return if fail ?)
66 $this->put('USER '.self
::$myBotName.' '.self
::$myBotName.'@'.self
::$ip.' '.self
::$domain.' :XBOT');
67 $this->put('NICK '.self
::$myBotName);
70 $this->msg
= $this->get();
71 $this->irc
->parse_get($msg);
73 $msg_info = $this->irc
->get_msg_info();
75 switch ($msg_info['type']) {
80 if ($msg_info['message'][0] == chr(001)) {
82 list($cmd,$query) = explode(chr(032),$msg_info['message']);
84 // we don't need this character
85 $cmd = str_replace(chr(001), "", $msg_info['message']);
89 self
::notice($msg_info['from'],$this->formater
->ctcp('PING VERSION TIME USERINFO CLIENTINFO'));
93 self
::notice($msg_info['from'],$this->formater
->ctcp('RPGBot version '.main
::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed'));
97 self
::notice($msg_info['from'],$this->formater
->ctcp('RPGBot'));
101 self
::notice($msg_info['from'],$this->formater
->ctcp(date('Y-m-d H:i:s')));
105 self
::notice($msg_info['from'],$this->formater
->ctcp("PING ".$query));
109 if ($msg_info['to'] == main
::$myBotName) {
111 if (preg_match('`^connect ([^ ]+) ([^ ]+)`',$msg_info['message'],$m)) {
112 if ($m[1] == 'admin' && $m[2] == 'mypass') {
114 self
::notice($msg_info['from'],'Vous êtes bien authentifié comme administrateur.');
116 self
::notice($msg_info['from'],'Erreur dans votre login et / ou mot de passe.');
122 if ($msg_info['message'][0] == '!') {
123 $message = substr($msg_info['message'],1);
124 $query = explode(' ',$message);
125 $query_count = count($query);
127 if (isset($this->commands
[$query[0]])) {
128 if ($query_count > 1) {
129 if ($this->commands
[$query[0]][$query[1]]['type'] == 'mixed' ||
130 $msg_info['to'] == main
::$myBotName && $this->commands
[$query[0]][$query[1]]['type'] == 'private' ||
131 $msg_info['to'] == main
::$channel && $this->commands
[$query[0]][$query[1]]['type'] == 'public') {
133 * $query = array( plugins, method[, arg[, ...]] )
134 * !plugin method[ arg[ ...]]
136 call_user_func_array(array($this,'do_command'),array_merge(array('msg_info'=>$msg_info),$query));
139 // no method : need a list of commands
142 $commands = self
::list_command($plugin);
143 foreach ($commands as $command_info) {
144 if ($command_info['type'] == 'public') {
145 $get_plugin_method = '!'.$plugin;
147 $get_plugin_method = '/msg ' . main
::$myBotName . ' !' . $plugin;
150 $this->privmsg($msg_info['from'],$this->formater
->bold('Command :')." $get_plugin_method {$command_info['method']}".(($command_info['accepted_args']>0)?
' [some args...]':''));
152 $help = explode("\n",$command_info['help']);
153 foreach($help as $help_msg) {
154 $this->privmsg($msg_info['from'],$help_msg);
169 } catch (Exception
$e) {
170 switch ($e->getCode()) {
172 echo "Processe restarting ...";
182 public function joinChannel($channel) {
183 $this->put('JOIN '.$channel);
184 echo "Join channel $channel ...\n";
187 private function nick_change() {
188 echo "New nick : ".self
::$myBotName."\n";
189 $this->put('NICK :'.self
::$myBotName);
192 public function newNick($new=false) {
194 case self
::$myBotName:
195 echo "New nick : [ERR] no changes :". self
::$myBotName . ' == ' . $new ."\n";
198 self
::$myBotName .= '_';
202 self
::$myBotName = $new;
209 * Envoie une notice a un salon / utilisateur
212 * @param string $message
214 public function notice ($to,$message) {
215 self
::put('NOTICE '.$to.' :'.$message."\n");
219 * Envoie un message (PRIVMSG) a un salon / utilisateur
222 * @param string $message
224 public function privmsg ($to,$message) {
225 self
::put('PRIVMSG '.$to.' :'.$message."\n");
228 public function put($command) {
229 if (!is_resource($this->C
)) {
230 throw new Exception('Connection lost...',1);
232 echo debug() ?
'[->' . $command . "" : '';
233 fputs($this->C
, $command . "\n");
237 public function get() {
239 stream_set_timeout($this->C
, $this->socketTimeout
);
242 $content = fgets($this->C
, 1024);
244 echo debug() ?
"<-]$content" : '';
246 if ($content != '') {
251 if (time()-$tmp1 >= $this->socketTimeout
) {
252 throw new Exception('TIMEOUT',0);
256 public function disconnect($msg='EOL ;') {
257 echo 'Closing Link...'."\n";
258 $this->put('QUIT :'.$msg);
259 if (is_resource($this->C
)) {
265 private function load_user($file='users.db') {
266 echo "Loading users ...";
267 $ulist = explode("\n",file_get_contents($file));
268 foreach ($ulist as $user) {
269 list($uname,$upass,$uright) = explode(':',$user);
270 $this->users_list
[$uname] = array($upass,$uright);
272 echo "\t\t\tdone.\n";
278 public function __construct($main);
279 public function commands_list();
280 public function current_message($message);