Public feed #726495

PHP IRC Bot

public php v1 · immutable
Raw Edit
·published 2007-10-05 07:48 UTC
rendered paste body
<?phpclass Bot {    public $_alive = true;    protected $_listeners = array();    protected $_socket;    public $quiet = false;    public $nick = null;    public function __construct($nick)    {        $this->nick = $nick;        $this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);    }    public function connect($host, $port = 6667)    {        if (!socket_connect($this->_socket, $host, $port)) {            return false;        }        $this->sendRaw('USER EduBot blog.everyone-here.is-a-geek.com GN :GeekNet bot');        return true;    }    public function sendRaw($message)    {        echo $message . "\n";        socket_write($this->_socket, $message . "\r\n");    }    public function setNick($nick = null)    {        if (!$nick) {            $nick = $this->nick;        }        $this->sendRaw('NICK ' . $nick);        $this->nick = $nick;    }    public function join($channel)    {        $this->sendRaw('JOIN ' . $channel);    }    public function part($channel, $message = '')    {        $this->sendRaw('PART ' . $channel . ' :' . $message);    }    public function quit($message = '')    {        $this->sendRaw('QUIT');    }    public function getError()    {        $errorCode = socket_last_error($this->_socket);        return $errorCode . ':' . socket_strerror($errorCode);    }    public function say($target, $message)    {        if (!$this->quiet) {            $this->sendRaw('PRIVMSG ' . $target . ' :' . $message);        }    }    public function listen()    {        while($this->_alive) {            $line = socket_read($this->_socket, 510);             $line = str_replace(array(chr(10), chr(13)), '', $line);            $this->dispatchEvent($line);        }    }    public function dispatchEvent($line)    {        foreach ($this->_listeners as $listener) {            $listener->onEvent($line);        }    }    public function stopListening()    {        $this->_alive = false;    }    public function registerListener(IrcListener $listener)    {        $this->_listeners[] = $listener;        $listener->registerBot($this);    }}class IrcListener{    public $bot = null;    public function registerBot($bot)    {        $this->bot = $bot;    }    public function onEvent($line)    {        // parse the prefix        if (preg_match('~:([^!@\s]+)(?:!([^@\s]+))?(?:@([^\s]+))?\s+(\w+)\s+(.*)~', $line, $matches)) {            list($line, $nick, $user, $host, $command, $params) = $matches;        } elseif (preg_match('~(\w+)\s+(.*)~', $line, $matches)) {            $nick = $user = $host = null;            list($line, $command, $params) = $matches;        }        if (is_numeric($command)) {            return;        }        switch ($command) {            case 'PRIVMSG':                if (preg_match('~([^\s]+)\s+(?::(.*))?~', $params, $matches)) {;                    list($params, $target, $message) = $matches;                    $this->onMessage($nick, $user, $host, $target, $message);                }                break;            case 'NOTICE':                if (preg_match('~([^\s]+)\s+(?::(.*))?~', $params, $matches)) {;                    list($params, $target, $message) = $matches;                    $this->onNotice($nick, $user, $host, $target, $message);                }                break;            case 'JOIN':                if (preg_match('~(?::([^\s]+))?~', $params, $matches)) {;                    list($params, $channel) = $matches;                    $this->onJoin($nick, $user, $host, $channel);                }                break;            case 'PART':                if (preg_match('~([^\s]+)\s+(?::(.*))?~', $params, $matches)) {;                    list($params, $channel, $message) = $matches;                    $this->onPart($nick, $user, $host, $channel, $message);                }            case 'MODE':                if (preg_match('~([^\s]+)\s+(.*)?~', $params, $matches)) {;                    list($params, $channel, $modeString) = $matches;                    $this->onMode($nick, $user, $host, $channel, $modeString);                }            case 'PING':                if (preg_match('~(?::([^\s]+))?~', $params, $matches)) {;                    list($params, $message) = $matches;                    $this->onPing($nick, $user, $host, $message);                }                break;            case 'QUIT':	        break;            default:                echo "Unknown Command '$command'\n";                break;        }    }    public function onMessage($nick, $user, $host, $target, $message)    {    }    public function onNotice($nick, $user, $host, $target, $message)    {    }    public function onJoin($nick, $user, $host, $channel)    {    }    public function onPart($nick, $user, $host, $channel, $message)    {    }    public function onMode($nick, $user, $host, $channel, $modeString)    {    }    public function onPing($nick, $user, $host, $message)    {        $this->bot->sendRaw('PONG :' . $message);    }}class WelcomeListener extends IrcListener{    function onJoin($nick, $user, $host, $channel)    {        if ($nick !== $this->bot->nick) {            $this->bot->say($channel, 'Welcome ' . $nick . '!');        }    }}class GetListener extends IrcListener{    function onMessage($nick, $user, $host, $target, $message)    {        if (preg_match('~(http://[^\s]+)~', $message, $matches)) {            $url = $matches[1];            echo '[' . date('Y-m-d H:i:s') . ']' . " $nick@$target: $message";            file_put_contents('urls.log', $url . "\n", FILE_APPEND);        }    }}/*****TO USE******/set_time_limit(0);$bot = new Bot('GN-Test');//Add listeners$bot->registerListener(new WelcomeListener());$bot->registerListener(new GetListener());if (!$bot->connect('irc.freenode.net')) {    die('Failed to connect:' . $bot->getError());}$bot->setNick();$channels = array(	'#test');foreach ($channels as $channel) {    $bot->join($channel);}$bot->listen();foreach ($channels as $channel) {    $bot->part($channel, 'MyPHPBot - Out!');}$bot->quit();