Skip to content

Instantly share code, notes, and snippets.

@Robertof
Created July 6, 2009 21:04
Show Gist options
  • Save Robertof/141678 to your computer and use it in GitHub Desktop.
Save Robertof/141678 to your computer and use it in GitHub Desktop.
<?php
/*
* InfernoBot 3.0
* Created by Robertof
* Released under GNU/GPL 3.0
*
* http://www.informarts.org
*/
class InfernoBot {
private $server = null;
private $port = null;
private $nick = null;
private $realname = null;
private $owner = null;
private $admins = null;
private $channel = null;
private $plugins = array (); // THIS ARRAY MUST BE EMPTY!
private $sock = null; // DON'T TOUCH THIS VAR
private $botperms = null;
private $password = null;
private $debug = null;
function __construct ($server, $port, $ssl, $nick, $realname, $owner, $admins, $channel, $perms, $password, $debug) {
$tmp = $server;
if ($ssl) $server = "ssl://{$server}";
$this->password = $password;
$this->botperms = $perms;
$this->channel = $channel;
$this->admins = $admins;
$this->owner = $owner;
$this->realname = $realname;
$this->nick = $nick;
$this->debug = $debug;
print "[.] Connecting to {$tmp}:{$port} ...\r\n";
$sock = @fsockopen ($server, $port);
if (!$sock) die ("Unable to create socket.\r\n");
print "[+] Ok, connected to {$tmp}:{$port}" . (($ssl) ? " (with SSL)" : "") . "\r\n";
$this->sock = $sock;
$this->server = $server;
$this->or_serv = $tmp;
$this->port = $port;
unset ($tmp);
}
public function getData ($value) {
return $this->$value;
}
public function match ($data, $mod="") {
if (preg_match ("/{$data}/{$mod}", $this->recv, $matches)) {
return (($matches > 1) ? $matches : true);
} else {
return false;
}
}
public function run () {
if (!$this->sock) die ("[-] Socket not created");
print "[.] Sending NICK and USER commands...\r\n";
$this->write ("NICK {$this->nick}");
print "[+.] NICK sended. Sending USER ...\r\n";
$this->write ("USER {$this->nick} 0 * :{$this->realname}");
print "[+] Done! Entering in the while...\r\n";
print "[+] Now I am in the while.\r\n";
$tmpvar = 0;
while (!feof ($this->sock)) {
$this->recv = fread ($this->sock, 1024);
if ($this->debug) print $this->recv;
if (($match = $this->match ("PING :(.+)"))) {
print "[+.] Pinged. Sending a reply to server...\r\n";
fwrite ($this->sock, "PONG :{$match[1]}");
print "[+] Done.\r\n";
} elseif ($this->match (".+00.+") && !$tmpvar) {
print "[+.] Server is ready. Identifying...\r\n";
/* Why I use fwrite? Write display all data sended to server.. if I use write he display the password! */
fwrite ($this->sock, "PRIVMSG NickServ :identify {$this->password}\r\n");
print "[+.] Identified. Joining in the channel/s\r\n";
$this->write ("JOIN ".implode (",", $this->channel));
print "[+.] Done. Running init plugins...\r\n";
$this->execPlugins ("init");
print "[+] Done.\r\n";
$tmpvar = 1;
} else {
$this->execPlugins ("listen");
}
}
}
private function execPlugins ($type = "init") {
foreach ($this->plugins as $name => $istance) {
if ($type == "init") {
$istance->init ($this);
} else {
$istance->listen ($this);
}
}
}
public function write ($data) {
if (!$this->sock) die ("[-] Called function \"write\", but there isn't the socket!");
print "{ [.] Writing data: ".$data."\r\n";
fwrite ($this->sock, $data."\r\n");
print "[+] Done. }\r\n";
}
public function load_plugin ($name, $class, $filename) {
if (!file_exists ("plugins/{$filename}.php")) die ("[-] {$filename} plugins doesn't exists\r\n");
require_once "plugins/{$filename}.php";
if (!class_exists ($class)) die ("[-] Class of plugin \"{$name}\" doesn't exists!\r\n");
$this->plugins[$name] = new $class;
print "{ [+] Loaded plugin \"{$name}\" }\r\n";
}
public function deunload_plugin ($name) {
if (!isset ($this->plugins[$name])) die ("[-] Plugin {$name} doesn't exists\r\n");
unset ($this->plugins[$name]);
}
public function is_registered ($nick) {
$this->write ("WHOIS {$nick}");
while (!feof ($this->sock)) {
if (preg_match ("/registered|identified/i", fread ($this->sock, 4024))) {
return true;
} else {
return false;
}
break;
}
}
public function havePerms ($nick, $what) {
if (!$this->is_registered ($nick)) return false;
if ($what == "owner" and $nick != $this->owner) return false;
if ($what == "admin" and (!in_array ($nick, $this->admins) and $nick != $this->owner)) return false;
return true;
}
public function getNick ($servString) {
preg_match ("/:(.+)!(~{0,1})/", $servString, $match);
if (count ($match) < 2) return false;
return trim ($match[1]);
}
public function matchCommand ($command, $where, $params = array ()) {
if (isset ($where["channel"])) $channel = $where["channel"];
else $channel = "";
if (isset ($where["privpub"]) && ($tmp = $this->match ("(.+)PRIVMSG ({$this->nick}|#.+) :!{$command}" . ( (count ($params) > 0) ? " " . implode (" ", array_values ($params)) : "")))) {
$tmps = array ("nickname" => $this->getNick ($tmp[1]));
if (trim ($tmp[2]) == $this->nick) $tmps["where"] = $this->getNick ($tmp[1]);
else $tmps["where"] = trim ($tmp[2]);
$i = 3;
foreach ($params as $param => $regex) {
if (!isset ($tmp[$i])) { print "Param {$param} doesn't exists!\r\n"; continue; }
$ls = $tmp[$i];
unset ($tmp[$i]);
$tmps["_".$param] = trim ($ls);
$i++;
}
return $tmps;
} elseif (($tmp = $this->match ("(.+)PRIVMSG ". ( ($channel) ? $channel : ( (@$where["private"]) ? $this->nick : "(.+)")) . " :!{$command}" . ( (count ($params) > 0) ? " " . implode (" ", array_values ($params)) : "")))) {
$tmps = array ("nickname" => $this->getNick ($tmp[1]));
if ($channel) $tmps["where"] = $channel;
elseif (@$where["private"]) $tmps["where"] = $this->getNick ($tmp[1]);
else $tmps["where"] = trim ($tmp[2]);
$i = (@$where["private"]) ? 2 : 3;
foreach ($params as $param => $regex) {
if (!isset ($tmp[$i])) { print "Param {$param} doesn't exists!\r\n"; continue; }
$ls = $tmp[$i];
unset ($tmp[$i]);
$tmps["_".$param] = trim ($ls);
$i++;
}
return $tmps;
} else {
return false;
}
}
public function sendMsg ($message, $where) {
$this->write ("PRIVMSG {$where} :{$message}");
}
public function addChan ($chan) {
$this->channel[] = $chan;
}
public function delChan ($chan) {
unset ($this->channel[$chan]);
}
public function setVar ($var, $content) {
$this->$var = $content;
}
}
$bot = new InfernoBot ("irc.hackers-uf.org", 9999, 1, "InfernoBot", "Roberto", "Robertof", array ("Malex", "SystemFAILURE"), array ("#huf", "#devel", "#miao", "#pythoniano", "#archlinux"), array ("isHop" => 0, "isOp" => 0, "isSop" => 0, "isOwner" => 0), '***', 1);
#array ("saluta" => new Saluta, "registered" => new IsRegistered, "admin" => new AdminFunctions)
#$bot->load_plugin ("saluta", "Saluta", "saluta");
$bot->load_plugin ("registered", "IsRegistered", "registered");
$bot->load_plugin ("admin", "AdminFunctions", "admins");
$bot->load_plugin ("tinyurl", "Tinyurl", "tinyurl");
$bot->load_plugin ("pingtimeout", "PingTimeout", "pingtimeout");
$bot->run ();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment