00001 <?php
00002
00024 class maxIrcBot {
00025
00026 private $stream = null;
00027
00028 private $nick = 'unkn0wn';
00029
00030 private $user = '';
00031
00032 private $names = array();
00033
00034 private $comm = array();
00035
00036 private $master = '';
00037
00038 private $replyTo = '';
00039
00046 public function __construct($server, $port) {
00047
00048 $this->stream = fsockopen($server, $port, $errno, $errstr);
00049
00050 if (is_bool($this->stream)) {
00051 die ($errno . ' : ' . $errstr);
00052 }
00053
00054 }
00055
00061 private function _writeLog($msg) {
00062
00063 $cnts = implode('', file('bot.txt'));
00064
00065 $fp = fopen('bot.txt', 'w');
00066
00067 fwrite($fp, $cnts . "\n" . date("d-m-Y H:i:s") . ':' . $msg . "\n");
00068
00069 fclose($fp);
00070
00071 }
00072
00078 public function plugin($plugin) {
00079
00080 $classe = 'plugin_' . $plugin;
00081
00082 require './plugin/' . $plugin . '.plugin.inc';
00083
00084 return (new $classe($this));
00085
00086 }
00087
00096 private function error($pattern, $string, $mess, $die = false) {
00097
00098 $errIs = ereg($pattern, $string, $reg);
00099
00100 if ($errIs and $die) {
00101 $this->quit();
00102 die ($mess);
00103 }
00104
00105 if ($errIs) {
00106
00107 $this->_writeLog($mess . ' : maxIrcBot : ' . implode(';', $reg));
00108
00109 }
00110
00111 }
00112
00113
00114 private function error_ERR_ERRONEUSNICKNAME($read) {
00115
00116 $this->error("(.*)\:Erroneous nickname", $read, 'Nick inválido', true);
00117
00118
00119 }
00120
00121 private function error_ERR_NICKNAMEINUSE($read) {
00122
00123 $this->error("(.*)\:Nickname is already in use", $read, 'Esse nick está em uso');
00124
00125
00126 }
00127
00128 private function error_ERR_NICKCOLLISION($read) {
00129
00130 $this->error("(.*)\:Nickname collision KILL from (.*)", $read, 'Nick suspenso');
00131
00132
00133 }
00134
00135 private function error_ERR_UNAVAILRESOURCE($read) {
00136
00137 $this->error("(.*)\:Nick\/channel is temporarily unavailable", $read, 'Nick temporariamente inativo');
00138
00139 }
00140
00141
00142 private function error_ERR_NOTONCHANNEL($read) {
00143
00144 $this->error("(.*)\:(.+) not on that channel", $read, 'Você não está em um canal');
00145
00146 }
00147
00148
00149 private function error_ERR_NOTREGISTERED_Brasnet($read) {
00150
00151 $this->error('(.*)\:Register first', $read, 'Você deve registrar o nick', true);
00152
00153 }
00154
00155 private function error_ERR_NOTREGISTERED($read) {
00156
00157 $this->error('(.*)\:You have not registered', $read, 'Você deve registrar o nick', true);
00158
00159 }
00160
00161 private function error_ERR_NEEDMOREPARAMS($read) {
00162
00163 $this->error('(.*)\:Not enough parameters', $read, 'Comando inválido');
00164
00165 }
00166
00167 private function error_ERR_ALREADYREGISTRED($read) {
00168
00169 $this->error('(.*)\:Unauthorized command \(already registered\)', $read, 'Comando não permitido');
00170
00171 }
00172
00173 private function error_ERR_CANNOTJOIN($read) {
00174
00175 $this->error('(.*)\:Cannot join channel', $read, 'Este usuário não pode entrar');
00176
00177 }
00178
00179 private function error_ERR_CHANOPRIVSNEEDED($read) {
00180
00181 $this->error('(.*)\:You\'re not channel operator', $read, 'Você não é operador');
00182
00183 }
00184
00185 public function write($string) {
00186
00187 fwrite($this->stream, $string . "\n");
00188
00189 }
00190
00191 private function read() {
00192
00193 return fgets($this->stream, 2048);
00194
00195 }
00196
00202 public function setNick($nick) {
00203
00204 $this->nick = $nick;
00205
00206 $this->write('NICK ' . $nick);
00207
00208 }
00209
00217 public function setUser($user, $mode = 0, $realname = 'PHPbot') {
00218
00219 $this->user = $user;
00220
00221 $this->write('USER ' . $user . ' ' . $mode . ' * :' . $realname);
00222
00223 }
00224
00230 public function setPass($pass) {
00231
00232 $this->write('PASS ' . $pass);
00233
00234 }
00235
00241 public function setMaster($master) {
00242
00243 $this->master = $master;
00244 }
00245
00246
00255 public function createCommand($comm, $rep) {
00256
00257 $this->comm[$comm] = $rep;
00258
00259 }
00260
00266 private function identifyCommand($read) {
00267
00268 if (ereg($this->master . '(.*) PRIVMSG ' . $this->nick . ' \:(.*)', $read)) {
00269
00270 $read_ = explode(':', $read);
00271 $read_0 = explode(' ', $read_[1]);
00272
00273
00274 $this->write('PRIVMSG ' . $this->replyTo . ' :' . $this->comm[trim($read_[2])]);
00275
00276 }
00277
00278 }
00279
00285 public function replyTo($local) {
00286
00287 $this->replyTo = $local;
00288
00289 }
00290
00296 public function joinChannel($channel) {
00297
00298 $this->write('JOIN ' . $channel);
00299
00300 $this->listNames($channel);
00301
00302 }
00303
00310 public function leaveChannel($channel, $msg = 'leaving the channel') {
00311
00312 $this->write('PART ' . $channel . ' ' . $msg);
00313
00314 }
00315
00321 private function getNames($read) {
00322
00323 if (ereg('353 (.*) = (.*) :(.*)', $read)) {
00324
00325 $read_ = explode('=', $read);
00326 $ret = explode("\n", $read_[1]);
00327 $canal = explode(' :', $ret[0]);
00328
00329 $this->names[trim($canal[0])] = explode(' ', $canal[1]);
00330
00331 }
00332
00333
00334 }
00335
00341 public function getNamesByChannel($channel) {
00342
00343 return $this->names[$channel];
00344
00345 }
00346
00352 private function listNames($channel) {
00353
00354 $this->write('NAMES ' . $channel);
00355
00356 }
00357
00364 public function inviteUser($user, $channel) {
00365
00366 $this->write('INVITE ' . $user . ' ' . $channel);
00367
00368 }
00369
00375 public function away($msg = 'Busy') {
00376
00377 $this->write('AWAY :' . $msg);
00378
00379 }
00380
00381
00382
00388 public function quit($msg = 'bye bye') {
00389
00390 $this->write('QUIT ' . $msg);
00391
00392 }
00393
00398 public function close() {
00399
00400 while (!feof($this->stream)) {
00401
00402 $ret = $this->read();
00403
00404 $this->_writeLog($ret);
00405
00406 $this->error_ERR_NOTONCHANNEL($ret);
00407 $this->error_ERR_ERRONEUSNICKNAME($ret);
00408 $this->error_ERR_NICKCOLLISION($ret);
00409 $this->error_ERR_NICKNAMEINUSE($ret);
00410 $this->error_ERR_UNAVAILRESOURCE($ret);
00411 $this->error_ERR_NOTREGISTERED($ret);
00412 $this->error_ERR_NOTREGISTERED_Brasnet($ret);
00413 $this->error_ERR_ALREADYREGISTRED($ret);
00414 $this->error_ERR_NEEDMOREPARAMS($ret);
00415 $this->error_ERR_CANNOTJOIN($ret);
00416 $this->error_ERR_CHANOPRIVSNEEDED($ret);
00417
00418 $this->getNames($ret);
00419
00420 $this->identifyCommand($ret);
00421
00422 }
00423
00424 fclose($this->stream);
00425
00426 }
00427
00428 }
00429
00430 ?>