<?php/** * iBot is a php irc bot * Copyright (C) 2007 Luis Filipe * * iBot 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 3 of the License, or * (at your option) any later version. * * iBot 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * @license http://www.gnu.org/licenses/gpl-3.0.txt * * @author Luis Filipe (kingless) - <blastelf@gmail.com> */$iBot =& new iBot();$iBot->run();class iBot { /** O servidor em que o bot se vai conectar */ var $server = 'irc.ptnet.org'; /** A porta do servidor normalmente 6667 */ var $port = '6667'; /** Os canais em que o bot vai entrar depois de se conectar */ var $channels = '#p@p, #phpbot'; /** O nickname do bot */ var $nickname = 'phpbot'; /** O nickname alternativo do bot para que seja utilizado * caso o primeiro ja estja a ser utilizado por outra pessoa */ var $nickname2 = 'botphp'; /** O nome do bot que aparece quando se faz /whois nickname*/ var $name = 'PHP Bot 0.1 (beta) by kingless'; /** O nome de identificação do bot */ var $identd = 'phpbot'; /** O modo de autenticação pode ser nick ou hostname*/ var $authmode = 'nick'; /** Os admin do bot se forem mais que dois colocar os nicks * separados por virgulas */ var $admins = 'kingless'; /** Entrar automaticamente nos canais depois de se conectar ? true = sim, false = não */ var $autojoin = true; /** Não funciona ainda */ var $partMsg = 'php beta 0.1 (beta)'; var $quitMsg = 'test bot'; /** Não modificar */ var $datetype = 'd/m @ H:i:s'; var $version = '0.1 (Beta)'; var $timeout = 30; var $values = array(); var $resource = null; var $response = ''; var $connected = false; function run() { if(!$this->connect()) { return false; } $this->sendHeaders(); $this->start(); } function connect() { $this->resource = pfsockopen( $this->server, $this->port, $errno, $errstr, $this->timeout ); if(!$this->resource) { $this->showResponse( "Não foi possivel conectar: $errstr ($errno)" ); return false; } return true; } function showResponse( $msg ) { $date = date('['. $this->datetype .']'); $response = $date ." - ". $msg ."\x0a"; echo $response; } function start() { while(!feof( $this->resource )) { $this->response = trim(fgets( $this->resource, 4096 )); $this->showResponse( $this->response ); if(preg_match( '/\:(.*)\s376\s(.*)\:(.*)/', $this->response, $match )) { $this->connected = true; } $this->changeNickname(); $this->setValues(); $this->pong(); $this->joinChannels(); $this->runModules(); } } function sendHeaders() { $headers = "USER $this->identd $this->server $this->server : $this->name\x0d\x0a"; $headers .= "NICK $this->nickname $this->server"; $this->send( $headers, true, 'Connecting to '. $this->server .' at port '. $this->port .' ... ' ); } function send( $response, $showresponse = true, $custom = '' ) { fwrite( $this->resource, $response ."\x0d\x0a" ); if($showresponse) { $msg = $custom ? $custom : $response; $this->showResponse( $msg ); } } function send_once( $response, $showresponse = true, $custom = '' ) { static $instance; if(isset($instance[$response])) { return; } $this->send( $response, $showresponse, $custom ); $instance[$response] = true; } function setValues() { $response = explode( ' ', $this->response, 4 ); $this->values['nick'] = substr($response[0], 1, strpos( $response[0], "!")-1); $pos1 = strpos( $response[0], "!"); $pos2 = strpos( $response[0], "@"); $this->values['identd'] = substr($response[0], $pos1+1, $pos2-$pos1-1); $this->values['hostname'] = substr($response[0], strpos($response[0], "@")+1); $this->values['userhost'] = substr($response[0],1); $this->values['channel'] = $response[2]; $this->values['msg'] = substr( $response[3], 1 ); } function get( $value ) { if(isset( $this->values[$value] )) { return $this->values[$value]; } } function changeNickname() { if($this->connected) { return false; } if(preg_match( '/\:(.*)\s433\s\*(.*)\:(.*)/', $this->response, $match )) { if($match[3] != 'Nickname is already in use.') { return; } elseif( trim($match[2]) != $this->nickname2 ) { $this->send( "NICK $this->nickname2 $this->server" ); $this->nickname = $this->nickname2; } } } function runModules() { if($this->connected) { require_once( 'modules.php' ); $modules = new iBotModules(); $modules->loadModules( $this ); } } function execute( $cmd ) { $admins = explode( ',', $this->admins ); foreach( $admins as $admin ) { switch( $authmode ) { case "nick": if($this->get('nick') == $admin) { $this->send( $cmd ); } break; case "admin": if($this->get('hostname') == $admin) { $this->send( $cmd ); } break; default: $this->send( $cmd ); } } } function pong() { if(substr( $this->response, 0, 6 ) == 'PING :') { $this->send( "PONG :". substr( $this->response, 6 ) ); } } function joinChannels() { if(!$this->connected) { return; } if($this->autojoin) { $channels = explode( ',', $this->channels ); for($x = 0; $x < count( $channels ); $x++) { $this->send_once( 'JOIN '. $channels[$x], true, 'joining channel '.$channels[$x] ); } } }}?>