5
[irbot.git] / sources / plug_base-class.inc.php
blob6a7b4a1827c8625ae074f3b83464d0576c5fc167
1 <?php
3 interface plugin {
4 public function __construct($main);
5 public function start($IRCtxt);
6 public function help();
9 class plug_base implements plugin {
11 private $main;
12 private $msg;
13 public $auth = false;
15 public function __construct($main) {
16 $this->main = $main;
19 public function start($IRCtext) {
20 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.IRCConn::$myBotName." :connect ([^ ]+) ([^ ]+)\r\n`",$IRCtext,$T)) {
21 if ($T[2] == 'admin' && $T[3] == 'mypass') {
22 $this->auth = true;
23 $this->main->MyConn->put('PRIVMSG '.$T[1].' :Vous etes bien connecte.');
24 echo "Connection de {$T[1]} en tant qu'administrateur.";
25 } else {
26 $this->main->MyConn->put('PRIVMSG '.$T[1].' :Erreur.');
28 } else {
29 $this->Deco($IRCtext);
30 $this->Ok($IRCtext);
31 $this->Pong($IRCtext);
32 $this->kick($IRCtext);
33 $this->NickUsed($IRCtext);
34 $this->IllegalChNick($IRCtext);
35 $this->CTCP($IRCtext);
36 $this->native_cmd($IRCtext);
40 public function help() {
43 private function native_cmd($IRCtext) {
44 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.preg_quote(IRCConn::$channel, '`')." :!([^\r\n]+)\r\n`",$IRCtext,$T)) {
45 switch ($T[2]) {
46 case 'quit':
47 if ($this->auth) {
48 $this->main->MyConn->disconnect();
49 die(0);
50 } else {
51 $this->main->MyConn->put('PRIVMSG '.$T[1]." :Vous n'êtes pas authentifié.");
53 break;
54 case 'restart':
55 if ($this->auth) {
56 echo 'Restart...'."\n";
57 $this->main->MyConn->disconnect();
58 sleep(5);
59 die(1);
60 } else {
61 $this->main->MyConn->put('PRIVMSG '.$T[1]." :Vous n'êtes pas authentifié.");
63 break;
64 case 'help':
65 self::doHelp($T[1]);
66 break;
67 case 'shownick':
68 $this->main->MyConn->put('PRIVMSG '.IRCConn::$channel.' :'.IRCConn::$myBotName."\n");
69 break;
70 default:
71 if (strpos($T[2],' ')!==false) {
72 list ($command,$param) = explode(' ',$T[2]);
73 switch ($command) {
74 case 'nick':
75 $this->main->MyConn->newNick($param);
76 break;
78 default:
79 break;
81 } else {
82 echo 'Command '.$T[2].' not found.'."\n";
84 break;
89 private function doHelp($replyto) {
90 $this->msg[] = '!help : cette aide';
91 $this->msg[] = '!quit : déconection';
92 $this->msg[] = '!restart : reconnection';
93 $this->msg[] = '!nick <nick> : change le pseudo';
94 foreach ($this->msg as $m) {
95 $this->main->MyConn->put('PRIVMSG '.$replyto.' :'.$m);
98 foreach($this->main->Plist as $key => $val) {
99 if ($key == 'plug_base')
100 continue;
102 $msg = $this->main->Plist[$key]->help();
104 $this->main->MyConn->privmsg($replyto,"$key functions :");
105 if (is_array($msg)) {
106 foreach($msg as $m) {
107 $this->main->MyConn->privmsg($replyto,' :'.$m);
109 } else {
110 $this->main->MyConn->privmsg($replyto,' :'.$msg);
115 // DECO
116 private function Deco($IRCtext) {
117 if (preg_match("`^ERROR :(Closing Link: )?(.*)\r?$`i", $IRCtext)) {
118 @fclose($this->main->MyConn->C);
119 sleep(3);
120 echo 'Closing Link'."\n";
121 die(1);
125 // OK
126 private function Ok($IRCtext) {
127 if (preg_match("`^:[^ ]+ 001 .*?\r?\n`", $IRCtext)) {
128 $this->main->MyConn->joinChannel(IRCConn::$channel);
132 // PONG
133 private function Pong($IRCtext) {
134 if (preg_match("`^PING :(.*)\r?\n`", $IRCtext, $T)) {
135 $this->main->MyConn->put('PONG '.$T[1]);
136 echo "PING : {$T[1]}\nPONG {$T[1]}\n\n";
140 // KICK
141 private function kick($IRCtext) {
142 if (preg_match('`^:(.*?)!.*?@.*? KICK '.IRCConn::$channel.' ([^ ]+)`', $IRCtext, $T)) {
143 if($T[2] == IRCConn::$myBotName) {
144 sleep(1);
145 $this->main->MyConn->joinChannel(IRCConn::$channel);
146 $this->main->MyConn->privmsg(IRCConn::$channel,'Merci '.$T[1].' !');
151 // Illegals characters in Nickname
152 private function IllegalChNick ($IRCtext) {
153 if (preg_match('`^:[^ ]+ 432 (.*?)\r?\n`', $IRCtext)){
154 IRCConn::$myBotName = 'XboT';
155 $this->main->MyConn->put('NICK :'.IRCConn::$myBotName);
159 // Nick already in use
160 private function NickUsed($IRCtext) {
161 if (preg_match('`^:[^ ]+ 433 (.*?)\r?\n`', $IRCtext)){
162 echo "Nick already in use\n\n";
163 $this->main->MyConn->newNick(false);
168 * Gestion des CTCP
170 * @param string $IRCtext
172 private function CTCP($IRCtext) {
173 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.preg_quote(IRCConn::$myBotName, '`').'(.*?)(VERSION|USERINFO|CLIENTINFO)`', $IRCtext, $T)) {
174 $this->main->MyConn->notice($T[1],'RPGBot version '.IRCConn::$botVersion.' - PHP '.phpversion().' -- par Tornald');
176 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.preg_quote(IRCConn::$myBotName, '`').'(.*?)PING (.*?)\r?\n`', $IRCtext, $T)) {
177 $this->main->MyConn->notice($T[1],"PING\1".time()."\1");
179 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG '.preg_quote(IRCConn::$myBotName,'`').'(.*?)(TIME)`', $IRCtext, $T)) {
180 $this->main->MyConn->notice($T[1],date('Y-m-d H:i:s'));