<!--?php
define("WARNING", "You do not have access to this command.");
class IrcBot {
var $server;
var $port;
var $nick;
var $buffer;
var $fsock;
var $errno;
var $errstr;
var $stage;
var $autojoin;
var $nickserv; //Nickserv password.
#!stats variables.
var $start_ts; //Starting timestamp for !stats uptime data.
var $ping_count;
# Variables for use after parsing.
var $adminpass; //Administrator password.
var $admins = array(); //Array of currently logged in admins.
var $user_nick; //Nick of the person who sent the message.
var $ident; //Ident of the person who sent the message.
var $host; //Host of the person who sent the message.
var $action; //Action: PRIVMSG, JOIN etc.
var $chan; //The channel the message came from or the person.
var $msg; //The message that was sent can be exploded.
public function __construct() {
$this->server = 'irc.freenode.net';
$this->port = 6667;
$this->nick = 'bot';
$this->autojoin = '#bots';
$this->adminpass = 'password';
$this->nickserv = 'pass';
$this->start_ts = date('U');
$this->ping_count = 0;
$this->fsock = fsockopen($this->server, $this->port, $this->errno, $this->errstr, 30);
IrcBot::Connect();
}
public function Connect() {
$this->stage = 'connect';
if(!$this->fsock) {
die('(' . $this->errno . ') ' . $this->errstr . ' at ' . $this->server . "\n");
}
IrcBot::GetData();
}
public function GetData() {
while(!feof($this->fsock)) {
$this->buffer = fgets($this->fsock, 1024);
if($this->buffer != '') {
echo date('G:i:s - ') . $this->buffer;
} else {
usleep(5);
IrcBot::GetData();
}
IrcBot::Parse($this->buffer);
}
}
public function Parse() {
if($this->stage == 'connect') {
fwrite($this->fsock, sprintf("USER %s %s %s %s\r\n", $this->nick, $this->server, $this->server, $this->nick));
fwrite($this->fsock, sprintf("NICK %s\r\n", $this->nick));
$this->stage = 'identify';
}
if (preg_match('^PING (:.+?)$^', $this->buffer, $arr)) {
fwrite($this->fsock, 'PONG ' . $arr[0] . "\r\n");
$this->ping_count++;
}
if($this->stage == 'identify') {
IrcBot::SendMsg('Nickserv', $this->nickserv);
fwrite($this->fsock, sprintf("MODE %s +B\r\n", $this->nick));
fwrite($this->fsock, sprintf("JOIN %s\r\n", $this->autojoin));
$this->stage = 'normal';
}
$pattern = '^:(.+?)!(.*)@(.*\S+) (\S+) (\S+) :(.+?)$^';
preg_match($pattern, $this->buffer , $block);
$this->user_nick = $block[1];
$this->ident = $block[2];
$this->host = $block[3];
$this->action = $block[4];
$this->chan = $block[5];
$this->msg = trim($block[6]);
$this->msg = explode(' ', $this->msg);
$push = array_shift($this->msg);
switch(strtolower($push)) {
case '!say':
IrcBot::Say();
break;
case '!chansay':
IrcBot::ChanSay();
break;
case '!nick':
IrcBot::Nick();
break;
case '!join':
IrcBot::Join();
break;
case '!part':
IrcBot::Part();
break;
case '!quit':
IrcBot::Quit();
break;;
case '!system':
IrcBot::System();
break;
case '!login':
IrcBot::Login();
break;
case '!stats':
IrcBot::Stats();
break;
case '!rss':
IrcBot::Rss();
break;
}
IrcBot::GetData();
}
public function SendMsg($channel, $msg) {
#Sends the given message to the given channel or user.
fwrite($this->fsock, sprintf("PRIVMSG %s :%s\r\n", $channel, $msg));
echo date('G:i:s - ') . 'PRIVMSG ' . $channel . ' :' . $msg . "\n";
}
# Functions associated with Parse() go here.
public function Say() {
IrcBot::SendMsg($this->chan, implode(' ',$this->msg));
IrcBot::GetData();
}
public function ChanSay() {
if(in_array($this->user_nick, $this->admins)) {
$push = array_shift($this->msg);
IrcBot::SendMsg($push, trim(implode(' ', $this->msg)));
} else {
IrcBot::SendMsg($this->chan, WARNING);
}
IrcBot::GetData();
}
public function Nick() {
if(in_array($this->user_nick, $this->admins)) {
fwrite($this->fsock, sprintf("NICK %s\r\n", $this->msg[0]));
} else {
IrcBot::SendMsg($this->chan, WARNING);
}
IrcBot::GetData();
}
public function Join() {
if(in_array($this->user_nick, $this->admins)) {
fwrite($this->fsock, sprintf("JOIN %s\r\n", $this->msg[0]));
} else {
IrcBot::SendMsg($this->chan, WARNING);
}
IrcBot::GetData();
}
public function Part() {
if(in_array($this->user_nick, $this->admins)) {
if($this->msg[0] == '') {
fwrite($this->fsock, sprintf("PART %s\r\n", $this->chan));
} else {
fwrite($this->fsock, sprintf("PART %s\r\n", $this->msg[0]));
}
} else {
IrcBot::SendMsg($this->chan, WARNING);
}
IrcBot::GetData();
}
public function Quit() {
if(in_array($this->user_nick, $this->admins)) {
fwrite($this->fsock, "QUIT\r\n");
die('User closed session.' . "\n");
} else {
IrcBot::SendMsg($this->chan, WARNING);
IrcBot::GetData();
}
}
public function System() {
if(in_array($this->user_nick, $this->admins)) {
switch($this->msg[0]) {
case 'uptime':
$return = shell_exec('uptime');
IrcBot::SendMsg($this->chan, $return);
break;
case 'shutdown':
IrcBot::SendMsg($this->chan, 'Shutting down now.');
system('halt');
break;
case 'reboot':
IrcBot::SendMsg($this->chan, 'Rebooting now.');
system('reboot');
break;
case 'wget':
if(!file_exists('wget/'))
mkdir('wget/');
IrcBot::SendMsg($this->chan, 'Fetching ' . $this->msg[1] . ' (' . gethostbyname($this->msg[1]) . ')');
system('wget -x -P wget/ ' . $this->msg[1]);
IrcBot::SendMsg($this->chan, 'Done.');
break;
case 'user':
if($this->msg[1] != '') {
$this->msg = array_slice($this->msg, 1);
$return = shell_exec(implode(' ',$this->msg));
if($return != '') {
IrcBot::SendMsg($this->chan, $return);
} else {
IrcBot::SendMsg($this->chan, 'No result.');
}
}
break;
case 'ping':
exec("ping -c2 -w2 " . $this->msg[1], $output, $return);
if($return == 0) {
IrcBot::SendMsg($this->chan, $this->msg[1] . ' : UP');
} elseif($return == 2) {
IrcBot::SendMsg($this->chan, $this->msg[1] . ' : UNKNOWN HOST');
} else {
IrcBot::SendMsg($this->chan, $this->msg[1] . ' : DOWN');
}
break;
default:
IrcBot::SendMsg($this->chan, 'Commands: ping , user <command></command>, wget , uptime');
break;
}
} else {
IrcBot::SendMsg($this->chan, WARNING);
}
IrcBot::GetData();
}
public function Login() {
switch($this->msg[0]) {
case 'pass':
if($this->msg[1] == $this->adminpass) {
$this->admins[] = $this->user_nick;
IrcBot::SendMsg($this->user_nick, 'Password accepted, admin status given.');
echo 'Admin status given to user: \'' . $this->user_nick . "'\n";
} else {
IrcBot::SendMsg($this->user_nick, 'Password Incorrect - Attempt logged.');
echo 'Incorrect admin password attempt - user: \'' . $this->user_nick . '!' . $this->ident . '@' . $this->host . '\' password: \'' . $this->msg[1] . "'\n";
}
break;
case 'logout':
if(in_array($this->user_nick, $this->admins)) {
$key = array_search($this->user_nick, $this->admins);
unset($this->admins[$key]);
echo $this->user_nick . " logged out.\n";
IrcBot::SendMsg($this->user_nick, 'Successfully logged out.');
}
break;
case 'users':
if(in_array($this->user_nick, $this->admins))
IrcBot::SendMsg($this->user_nick, 'Logged in admins: ' . implode(' ', $this->admins));
break;
default:
IrcBot::SendMsg($this->user_nick, 'Commands: pass, users, logout.');
break;
}
IrcBot::GetData();
}
public function Stats() {
echo "Stats requested.\n";
$current_time = date('U');
$current_time = ($current_time - $this->start_ts);
$final_ts = $current_time/60;
$filesize = filesize(basename(__FILE__));
IrcBot::SendMsg($this->chan, sprintf('Current uptime: %01.2f minutes. Ping Count: %d. File size: %d bytes.', $final_ts, $this->ping_count, $filesize));
IrcBot::GetData();
}
public function Rss() {
switch($this->msg[0]) {
case 'bbc':
$data = file_get_contents('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml');
$xml = new SimpleXMLElement($data);
IrcBot::SendMsg($this->chan, $xml->channel->item[0]->title . ' - ' . $xml->channel->item[0]->link);
break;
case 'slashdot':
$data = file_get_contents('http://rss.slashdot.org/Slashdot/slashdot');
$xml = new SimpleXMLElement($data);
IrcBot::SendMsg($this->chan, $xml->item[0]->title . ' - ' . $xml->item[0]->link);
break;
default:
IrcBot::SendMsg($this->chan, "No news source selected. bbc | slashdot");
break;
}
IrcBot::GetData();
}
}
$new = new IrcBot;
?>
Recent Comments