Skip to content

Instantly share code, notes, and snippets.

@JoshuaEstes
Created February 1, 2012 16:47
Show Gist options
  • Save JoshuaEstes/1717964 to your computer and use it in GitHub Desktop.
Save JoshuaEstes/1717964 to your computer and use it in GitHub Desktop.
shitty irc bot to fire jarvis
<?php
/**
*
*/
set_time_limit(0);
ini_set('display_errors', 'on');
class jBot {
var $_socket,
$_config = array(),
$_ex = array(),
$_fired = 0;
public function __construct($config) {
$this->_config = $config;
$this->_socket = fsockopen($config['server'], $config['port']);
stream_set_blocking(STDIN, 0);
$this->login();
do {
$this->main();
}while(true);
}
public function __destruct() {
fwrite(STDOUT,'Disconnected' . PHP_EOL);
fclose($this->_socket);
}
public function login() {
$this->sendData('USER', $this->_config['nick'] . ' acidavengers.co.uk ' . $this->_config['nick'] . ' :' . $this->_config['name']);
$this->sendData('NICK', $this->_config['nick']);
}
public function main() {
$data = fgets($this->_socket, 128);
if (trim($data))
{
fwrite(STDOUT, '<- ' . $data . PHP_EOL);
}
preg_match('/^(?:[:@]([^\s]+) )?([^\s]+)(?: ((?:[^:\s][^\s]* ?)*))?(?: ?:(.*))?$/', $data, $matches);
if (!isset($matches[1]))
{
return;
}
$a = str_replace(array(chr(10), chr(13)), '', $matches[1]);
$b = str_replace(array(chr(10), chr(13)), '', $matches[2]);
$c = str_replace(array(chr(10), chr(13)), '', $matches[3]);
$d = str_replace(array(chr(10), chr(13)), '', $matches[4]);
if (isset($matches[0]))
{
$command = str_replace(array(chr(10), chr(13)), '', $matches[0]);
fwrite(STDOUT, '[0]>>' . $command . PHP_EOL);
}
if (isset($matches[1]))
{
$command = str_replace(array(chr(10), chr(13)), '', $matches[1]);
fwrite(STDOUT, '[1]>>' . $command . PHP_EOL);
}
if (isset($matches[2]))
{
$command = str_replace(array(chr(10), chr(13)), '', $matches[2]);
fwrite(STDOUT, '[2]>>' . $command . PHP_EOL);
switch($command)
{
case('PING'):
$this->sendData('PONG', ':' . $matches[4]);
break;
case ('ERROR'):
exit();
break;
case('PRIVMSG'):
if (strcasecmp($d,'how many times has jarvis been fired') == 0)
{
$user = explode('!~',$matches[1]);
if ($c != $this->_config['nick'])
{
$this->sendData('PRIVMSG ' . $c, ':He has been fired: '. $this->_fired);
}
else
{
$this->sendData('PRIVMSG ' . $user[0], ':He has been fired: '. $this->_fired);
}
}
if (strcasecmp($d,'fire jarvis') == 0)
{
$this->_fired++;
}
$params = explode(' ',$d);
if (strcasecmp($params[0],'join') == 0)
{
$this->sendData('JOIN '.$params[1]);
}
break;
}
}
if (isset($matches[3]))
{
$command = str_replace(array(chr(10), chr(13)), '', $matches[3]);
fwrite(STDOUT, '[3]>>' . $command . PHP_EOL);
}
if (isset($matches[4]))
{
$command = str_replace(array(chr(10), chr(13)), '', $matches[4]);
fwrite(STDOUT, '[4]>>' . $command . PHP_EOL);
}
}
function sendData($cmd, $msg = null) {
//displays stuff to the broswer and sends data to the server.
if ($msg == null) {
fwrite($this->_socket, $cmd . "\r\n");
fwrite(STDOUT, '->'.$cmd . PHP_EOL);
} else {
fwrite($this->_socket, $cmd . ' ' . $msg . "\r\n");
fwrite(STDOUT, '->'.$cmd . ' ' . $msg . PHP_EOL);
}
}
}
$jbot = new jBot(array(
'server' => 'irc.freenode.net',
'port' => 6667,
'nick' => 'testjbot',
'name' => 'J Bot',
'pass' => 'jbot',
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment