6
[irbot.git] / sources / IRCMain-class.inc.php
blobd720214852b859119978da745218d4f61a2f5115
1 <?php
3 class IRCMain {
5 public $MyConn;
6 public $base;
7 public $Plist = array();
8 public $msg;
9 public $plugins;
10 public $commands;
12 private $core_commands;
14 // Constructeur de la classe
15 public function __construct($server, $port, $channel, $name, $myip, $mydomain) {
16 // Récupération d'une connexion unique
17 IRCConn::Init($server, $port, $channel, $name, $myip, $mydomain);
18 $this->IRCConn = IRCConn::GetInstance();
19 // On charge le plugin de base indispensable
20 require_once('./sources/plug_base-class.inc.php');
21 $this->Plist['plug_base'] = new plug_base();
23 self::register_command('core','!shownick');
24 self::register_command('core','!nick');
27 public function run() {
28 while (true) {
29 $this->msg = $this->IRCConn->get();
31 $msg = false;
33 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.IRCConn::$myBotName.'|'.IRCConn::$channel.") :(.*)\r`",$this->msg,$T)) {
34 $msg = array (
35 'type' => 'PRIVMSG',
36 'from' => $T[1],
37 'to' => $T[2],
38 'message' => $T[3]
40 } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.IRCConn::$myBotName." :(.*)\r`",$this->msg,$T)) {
41 $msg = array (
42 'type' => 'NOTICE',
43 'from' => $T[1],
44 'to' => IRCConn::$myBotName,
45 'message' => $T[2]
49 if ($msg !== false) {
50 switch ($msg['type']) {
51 case 'PRIVMSG':
52 if ($msg['message'][0] == chr(001)) { // ctcp
53 list($cmd,$query) = explode(chr(032),$msg['message']);
54 $cmd = str_replace(chr(001), "",$msg['message']);
55 switch ($cmd) {
56 case 'CLIENTINFO':
57 $this->IRCConn->notice($msg['from'],chr(001).'PING VERSION TIME USERINFO CLIENTINFO'.chr(001));
58 break;
59 case 'VERSION':
60 $this->IRCConn->notice($msg['from'],chr(001).'RPGBot version '.IRCConn::$botVersion.' - PHP '.phpversion().' -- par Tornald et Bloodshed'.chr(001));
61 break;
62 case 'USERINFO':
63 $this->IRCConn->notice($msg['from'],chr(001).'RPGBot'.chr(001));
64 break;
65 case 'TIME':
66 $this->IRCConn->notice($msg['from'],chr(001).date('Y-m-d H:i:s').chr(001));
67 break;
68 case 'PING':
69 $this->IRCConn->notice($msg['from'],chr(001)."PING ".$query.chr(001));
70 break;
72 } else {
73 if ($to == IRCConn::$myBotName) {
74 if (preg_match('^connect ([^ ]+) ([^ ]+)',$msg['message'],$m)) {
75 if ($m[1] == 'admin' && $m[2] == 'mypass') {
76 $this->IRCConn->notice($msg['from'],'Vous êtes bien authentifié comme administrateur.');
77 } else {
78 $this->IRCConn->notice($msg['from'],'Erreur dans votre login et / ou mot de passe.');
81 } else {
82 if ($msg['message'][0] == '!') {
83 $pos_space = strpos($msg['message'],' ');
84 if ($pos_space !== false) {
85 $command = substr($msg['message'],0,$pos_space);
86 $query = substr($msg['message'],$pos_space+1);
87 } else {
88 $command = $msg['message'];
94 break;
96 default:
97 break;
101 foreach ($this->plugins['_run'] as $plugin => $methods) {
102 foreach ($methods as $method) {
103 $this->plugins[$plugin]->$method($msg);
109 public function AddPlug($Pname) {
110 if ( !array_key_exists ( $Pname, $this->Plist ) ) {
111 $this->Plist[$Pname] = new $Pname($this);
115 public function load_plugin($pname) {
116 if ( !array_key_exists ( $pname, $this->plugins ) ) {
117 if ( is_readable('./plugins/'.$class.'-plugin.inc.php') ) {
118 require_once './plugins/'.$class.'-plugin.inc.php';
119 $this->plugins[$pname] = new $pname($this);
120 self::plugin_register_cmd($pname, $this->plugins[$pname]->commands_list());
121 } else {
122 trigger_error ("Class '$class' not found",E_USER_ERROR);
127 public function unload_plugin($plugin_name) {
128 if ( array_key_exists ( $plugin_name, $this->plugins ) ) {
129 unset( $this->plugins[$plugin_name] );
133 protected function plugin_register_cmd($plugin,$commands) {
134 foreach ($commands as $method => $accepted_args) {
135 if (!method_exists($this->plugins[$plugin],$method) || !is_callable(array($this->plugins[$plugin],$method))) {
136 trigger_error("ERROR : method '$plugin:$method' in the commands list is not callable.\n",E_USER_WARNING);
137 } else {
138 self::add_command($plugin,$method,$accepted_args);
143 public function UnloadPlug($Pname) {
144 if ( array_key_exists ( $Pname, $this->Plist ) ) {
145 unset( $this->Plist[$Pname] );
149 private function add_command($plugin,$method_to_add,$accepted_args=0) {
150 if (isset($this->commands[$plugin])) {
151 foreach ($this->commands[$plugin] as $method) {
152 if (isset($method[$method_to_add]))
153 return true;
157 $this->commands[$plugin][] = array(
158 'method'=>$method_to_add,
159 'accepted_args'=>$accepted_args,
161 return true;
164 private function remove_command($plugin,$method_to_remove, $accepted_args = 0) {
165 if (isset($this->commands[$plugin])) {
166 if ($method_to_remove != '_all_method') {
167 foreach($this->commands[$plugin] as $method) {
168 if ($method['method'] != $method_to_remove)
169 $new_method_list[] = $method;
172 $this->commands[$plugin] = $new_method_list;
173 } else {
174 unset($this->commands[$plugin]);
177 return false;
181 interface plugin {
182 public function __construct($main);
183 public function start($IRCtxt);
184 public function help();