<?php
/**
* maxIrcBot
*
* @version 2.1
* @author Max Rosan <hide@address.com>
*
* LICENSE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
class maxIrcBot {
private $stream = null;
private $nick = 'unkn0wn';
private $user = '';
private $names = array();
private $comm = array();
private $master = '';
private $replyTo = '';
/**
* @return void
* @param String $server
* @param Integer $port
* @desc Construtor
*/
public function __construct($server, $port) {
$this->stream = fsockopen($server, $port, $errno, $errstr);
if (is_bool($this->stream)) {
die ($errno . ' : ' . $errstr);
}
}
/**
* @return void
* @param String $msg
* @desc Guarda mensagem no histórico - it writes a message in the file bot.txt
*/
private function _writeLog($msg) {
$cnts = implode('', file('bot.txt'));
$fp = fopen('bot.txt', 'w');
fwrite($fp, $cnts . "\n" . date("d-m-Y H:i:s") . ':' . $msg . "\n");
fclose($fp);
}
/**
* @return Object
* @param String $plugin
* @desc Carrega plugin - it loads a plugin
*/
public function plugin($plugin) {
$classe = 'plugin_' . $plugin;
require './plugin/' . $plugin . '.plugin.inc';
return (new $classe($this));
}
/**
* @return void
* @param String $pattern
* @param String $string
* @param String $mess
* @param String $die
* @desc Debugger
*/
private function error($pattern, $string, $mess, $die = false) {
$errIs = ereg($pattern, $string, $reg);
if ($errIs and $die) {
$this->quit();
die ($mess);
}
if ($errIs) {
$this->_writeLog($mess . ' : maxIrcBot : ' . implode(';', $reg));
}
}
private function error_ERR_ERRONEUSNICKNAME($read) {
$this->error("(.*)\:Erroneous nickname", $read, 'Nick inválido', true);
}
private function error_ERR_NICKNAMEINUSE($read) {
$this->error("(.*)\:Nickname is already in use", $read, 'Esse nick está em uso');
}
private function error_ERR_NICKCOLLISION($read) {
$this->error("(.*)\:Nickname collision KILL from (.*)", $read, 'Nick suspenso');
}
private function error_ERR_UNAVAILRESOURCE($read) {
$this->error("(.*)\:Nick\/channel is temporarily unavailable", $read, 'Nick temporariamente inativo');
}
private function error_ERR_NOTONCHANNEL($read) {
$this->error("(.*)\:(.+) not on that channel", $read, 'Você não está em um canal');
}
private function error_ERR_NOTREGISTERED_Brasnet($read) {
$this->error('(.*)\:Register first', $read, 'Você deve registrar o nick', true);
}
private function error_ERR_NOTREGISTERED($read) {
$this->error('(.*)\:You have not registered', $read, 'Você deve registrar o nick', true);
}
private function error_ERR_NEEDMOREPARAMS($read) {
$this->error('(.*)\:Not enough parameters', $read, 'Comando inválido');
}
private function error_ERR_ALREADYREGISTRED($read) {
$this->error('(.*)\:Unauthorized command \(already registered\)', $read, 'Comando não permitido');
}
private function error_ERR_CANNOTJOIN($read) {
$this->error('(.*)\:Cannot join channel', $read, 'Este usuário não pode entrar');
}
private function error_ERR_CHANOPRIVSNEEDED($read) {
$this->error('(.*)\:You\'re not channel operator', $read, 'Você não é operador');
}
public function write($string) {
fwrite($this->stream, $string . "\n");
}
private function read() {
return fgets($this->stream, 2048);
}
/**
* @return void
* @param String $nick
* @desc Indica nick - set nick
*/
public function setNick($nick) {
$this->nick = $nick;
$this->write('NICK ' . $nick);
}
/**
* @return void
* @param String $user
* @param String $mode
* @param String $realname
* @desc Define usuário - set user
*/
public function setUser($user, $mode = 0, $realname = 'PHPbot') {
$this->user = $user;
$this->write('USER ' . $user . ' ' . $mode . ' * :' . $realname);
}
/**
* @return void
* @param String $pass
* @desc Indica senha - set password
*/
public function setPass($pass) {
$this->write('PASS ' . $pass);
}
/**
* @return void
* @param String $master
* @desc Indica administrador do bot - set the master user of bot
*/
public function setMaster($master) {
$this->master = $master;
}
/**
* @return void
* @param String $comm
* @param String $rep
* @desc Cria um comando a ser repondido - it creates a command for reply of the bot
* $comm - Comando - Command
* $rep - Resposta ao comando - reply of the bot
*/
public function createCommand($comm, $rep) {
$this->comm[$comm] = $rep;
}
/**
* @return void
* @param String $read
* @desc Identifica os comandos - it identifies the commands
*/
private function identifyCommand($read) {
if (ereg($this->master . '(.*) PRIVMSG ' . $this->nick . ' \:(.*)', $read)) {
$read_ = explode(':', $read);
$read_0 = explode(' ', $read_[1]);
$this->write('PRIVMSG ' . $this->replyTo . ' :' . $this->comm[trim($read_[2])]);
}
}
/**
* @return void
* @param String $local
* @desc Local de resposta do bot - the bot replies to a place
*/
public function replyTo($local) {
$this->replyTo = $local;
}
/**
* @return void
* @param String $channel
* @desc Entra em um canal - the bot enters in a channel
*/
public function joinChannel($channel) {
$this->write('JOIN ' . $channel);
$this->listNames($channel);
}
/**
* @return void
* @param String $channel
* @param String $msg
* @desc Deixa o canal - the bot goes out
*/
public function leaveChannel($channel, $msg = 'leaving the channel') {
$this->write('PART ' . $channel . ' ' . $msg);
}
/**
* @return void
* @param String $read
* @desc Pega a lista dos usuários no canais e armazena - it gets users of a channel
*/
private function getNames($read) {
if (ereg('353 (.*) = (.*) :(.*)', $read)) {
$read_ = explode('=', $read);
$ret = explode("\n", $read_[1]);
$canal = explode(' :', $ret[0]);
$this->names[trim($canal[0])] = explode(' ', $canal[1]);
}
}
/**
* @return Array
* @param String $channel
* @desc Retorna os usuários do canal - it returns the users of a channel
*/
public function getNamesByChannel($channel) {
return $this->names[$channel];
}
/**
* @return void
* @param String $channel
* @desc Lista usuários do canal
*/
private function listNames($channel) {
$this->write('NAMES ' . $channel);
}
/**
* @return void
* @param String $user
* @param String $channel
* @desc Convida um usuário para um canal - it invites a user to go in a channel
*/
public function inviteUser($user, $channel) {
$this->write('INVITE ' . $user . ' ' . $channel);
}
/**
* @return void
* @param String $msg
* @desc Muda o estado para alway - it sets away mode
*/
public function away($msg = 'Busy') {
$this->write('AWAY :' . $msg);
}
/**
* @return void
* @param String $msg
* @desc Sai do servidor - the bot goes out
*/
public function quit($msg = 'bye bye') {
$this->write('QUIT ' . $msg);
}
/**
* @return void
* @desc finaliza e roda o bot - it closes the actions
*/
public function close() {
while (!feof($this->stream)) {
$ret = $this->read();
$this->_writeLog($ret);
$this->error_ERR_NOTONCHANNEL($ret);
$this->error_ERR_ERRONEUSNICKNAME($ret);
$this->error_ERR_NICKCOLLISION($ret);
$this->error_ERR_NICKNAMEINUSE($ret);
$this->error_ERR_UNAVAILRESOURCE($ret);
$this->error_ERR_NOTREGISTERED($ret);
$this->error_ERR_NOTREGISTERED_Brasnet($ret);
$this->error_ERR_ALREADYREGISTRED($ret);
$this->error_ERR_NEEDMOREPARAMS($ret);
$this->error_ERR_CANNOTJOIN($ret);
$this->error_ERR_CHANOPRIVSNEEDED($ret);
$this->getNames($ret);
$this->identifyCommand($ret);
}
fclose($this->stream);
}
}
?>