Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- TODO:
- if a master quit or parts all channels, or was never on the channel(s) when added, move to inactive list (inactive list is un-viewable to non-masters)
- ^ basicly if there's no way to verify they'll be using the bot, move them over
- if an inactive master joins/nicks, readd to active masters list (perhaps auth with a user-generated password?)
- i probably should comment more stuff, it's getting kinda complex; would hate to have to rewrite/relearn
- */
- class aMessage {
- var $nick, $user, $host, $sender, $cmd, $param, $mess, $wline;
- }
- class aIRC {
- var $cM;
- var $lastping, $pongwait, $pingsent, $pingpong;
- var $host, $fp, $port, $logfile, $con, $server, $cmdChar;
- var $cNick, $joinnick, $username, $realname, $joinchan, $auth, $authnick, $masterNick;
- var $channames, $powers, $tempnames, $cnameslist;
- var $debug, $firstcon, $locked, $quit, $version;
- var $mTableName, $cTableName, $iTableName, $MySQLServer, $MySQLDatabase, $MySQLAccountName, $MySQLAccountpassword;
- }
- $IRC = new aIRC;
- $IRC->cM = new aMessage;
- $IRC->cM->param = array();
- $IRC->host = "irc.rizon.net";
- $IRC->port = 6667;
- $IRC->joinchan = array("#Tootoot222", "#mixster", "#SRL-School", "#SRL");
- $IRC->joinnick = "Furry";
- $IRC->username = "Furry";
- $IRC->realname = "Furry";
- $IRC->authnick = "Tootoot222";
- $IRC->cNick = $IRC->joinnick;
- $IRC->auth = "";
- $IRC->server = "";
- $IRC->cmdChar = "!";
- $IRC->locked = false;
- $IRC->debug = false;
- $IRC->pingpong = false;
- $IRC->powers = array(array(), array());
- $IRC->channames = array(array(array()));
- $IRC->tempnames = array();
- $IRC->cnameslist = "";
- $IRC->version = "Version 3";
- set_time_limit(0); //to ensure the script doesn't time out
- date_default_timezone_set("America/Chicago"); //change this to your timezone
- $firstmaster = "Tootoot222"; //set this if you haven't created the database/tables yet
- $IRC->MySQLServer = "localhost";
- $IRC->MySQLDatabase = "ircbotv3";
- $IRC->MySQLAccountName = "root";
- $IRC->MySQLAccountpassword = "";
- $IRC->mTableName = "masters5";
- $IRC->iTableName = "imasters2";
- $IRC->cTableName = "cmd3";
- $IRC->hTableName = "hooks1";
- $IRC->logfile = fopen("logfile.txt", "a");
- fwrite($IRC->logfile, "\r\n*** Log opened on " . date("D M jS H:i:s", time()) . " ***\r\n\r\n");
- $IRC->con = mysql_connect($IRC->MySQLServer, $IRC->MySQLAccountName, $IRC->MySQLAccountpassword) or die("MySQL error: " . mysql_error() . "\r\n");
- mysql_query("CREATE DATABASE IF NOT EXISTS " . $IRC->MySQLDatabase, $IRC->con);
- mysql_select_db($IRC->MySQLDatabase, $IRC->con);
- function table_exists($tablename) {
- global $IRC;
- return(mysql_result(mysql_query("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '" . $IRC->MySQLDatabase . "' AND table_name = '" . $tablename . "'", $IRC->con), 0) == 1);
- }
- if(!table_exists($IRC->cTableName)) {
- mysql_query("CREATE TABLE " . $IRC->cTableName . "(name VARCHAR(25), code MEDIUMTEXT, privs INT, maker VARCHAR(20))", $IRC->con);
- }
- if(!table_exists($IRC->mTableName)) {
- mysql_query("CREATE TABLE " . $IRC->mTableName . "(name VARCHAR(20), privs INT)", $IRC->con);
- mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $firstmaster . "', 100)", $IRC->con);
- }
- if(!table_exists($IRC->iTableName)) {
- mysql_query("CREATE TABLE " . $IRC->iTableName . "(name VARCHAR(20), pass VARCHAR(32), privs INT)", $IRC->con);
- }
- if(!table_exists($IRC->hTableName)) {
- mysql_query("CREATE TABLE " . $IRC->hTableName . "(name VARCHAR(25), code MEDIUMTEXT, active BOOL)", $IRC->con);
- }
- function output($message, $newline = true, $timestamp = true) {
- global $IRC;
- foreach(explode("\r\n", $message) as $m) {
- $out = ($timestamp ? "[" . date("H:i:s") . "] " : "") . $m . ($newline ? "\r\n" : "");
- echo($out);
- fwrite($IRC->logfile, $out);
- }
- }
- function send($data, $out = true, $b = false) {
- if(!trim($data)) {
- return;
- }
- global $IRC;
- $a = explode("\r\n", $data);
- foreach($a as $aa) {
- fputs($IRC->fp, $aa . "\r\n");
- if($out) {
- output(($b ? "--" : "") . "-> " . $aa);
- }
- }
- }
- function implodefrom($glue, $pieces, $from = 0) {
- $r = "";
- if(is_array($pieces)) {
- $from = max(0, $from);
- $r = $pieces[$from];
- for($i = ($from + 1); $i < sizeof($pieces); $i++) {
- $r .= $glue . $pieces[$i];
- }
- }
- return($r);
- }
- function notnull($c1, $c2) {
- return($c1 ? $c1 : $c2);
- }
- function startsWith($haystack, $needle) {
- if(is_array($needle)) {
- foreach($needle as $n) {
- if(strpos($haystack, $n) === 0) {
- return($n);
- }
- }
- }
- return(strpos($haystack, $needle) === 0);
- }
- function instr($needle, $haystack, $sensitive = false) {
- if($sensitive) {
- return((false !== strpos($haystack, $needle)) ? true : false);
- }else {
- return((false !== stristr($haystack, $needle)) ? true : false);
- }
- }
- function dumpDB($message = "", $oldcode = "", $newcode = "") {
- global $IRC;
- if(!file_exists("./DB_backup/")) {
- mkdir("./DB_backup/");
- }
- $basename = "db_backup";
- $n = 1;
- while(file_exists($filename = ("./DB_backup/" . $basename . $n . ".txt"))) {
- $n++;
- }
- if(!$fh = fopen($filename, 'w')) {
- return("failed to open file: " . $filename);
- }
- fwrite($fh, "//Dumpfile " . $n . " created on " . date("D M jS H:i:s", time()) . "\r\n\r\n");
- fwrite($fh, "//" . $IRC->cM->sender . ";" . ($message ? " " . $message : "") . ($oldcode ? "\r\n\r\n//Old code:\r\n" . $oldcode : "") . ($newcode ? "\r\n\r\n//New code:\r\n" . $newcode : ""));
- $q = mysql_query("SELECT * FROM " . $IRC->cTableName . " ORDER BY name ASC", $IRC->con);
- while($row = mysql_fetch_array($q)) {
- fwrite($fh, "\r\n\r\n\r\n//" . strtoupper($row["name"]) . ":\r\n" . $row["code"]);
- }
- fclose($fh);
- return(true);
- }
- function fetchcmd($cmd, $r = "code") {
- global $IRC;
- $row = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->cTableName . ($cmd ? " WHERE name='" . $cmd . "'" : ""), $IRC->con));
- return($row[$r]);
- }
- function fetchmasters($name = "", $privs = false, $active = true) {
- global $IRC;
- $q = mysql_query("SELECT " . ($n = ($privs ? "privs" : "name")) . " FROM " . ($active ? $IRC->mTableName : $IRC->iTableName) . ($name ? " WHERE name='" . $name . "'" : ""), $IRC->con);
- $r = array();
- while($row = mysql_fetch_array($q)) {
- $r[] = $row[$n];
- }
- return($r);
- }
- function removemasters($names, $all = false, $active = true) {
- global $IRC;
- if($active) {
- $t = $IRC->mTableName;
- }else {
- $t = $IRC->iTableName;
- }
- if(!$all) {
- if(!is_array($names)) {
- $names = array($names);
- }
- foreach($names as $n) {
- mysql_query("DELETE FROM " . $t . " WHERE name='" . $n . "'", $IRC->con);
- }
- }else {
- mysql_query("DELETE FROM " . $t, $IRC->con);
- }
- }
- function addmasters($names, $privs, $active = true, $upd = false) {
- global $IRC;
- if(sizeof($names) != sizeof($privs)) {
- $p = $privs;
- $privs = array();
- for($i = 0; $i < sizeof($names); $i++) {
- $privs[$i] = $p;
- }
- }
- if($active) {
- $t = $IRC->mTableName;
- }else {
- $t = $IRC->iTableName;
- }
- if(!$upd) {
- removemasters($names);
- }
- for($i = 0; $i < sizeof($names); $i++) {
- if(!$upd) {
- mysql_query("INSERT INTO " . $t . "(name, privs) VALUES ('" . $names[$i] . "', " . min($privs[$i], 100) . ")", $IRC->con);
- }else {
- mysql_query("UPDATE " . $t . " SET name='" . $privs[$i] . "' WHERE name='" . $names[$i] . "'", $IRC->con);
- }
- }
- }
- function swapmasters($names) {
- global $IRC;
- if(!is_array($names)) {
- $names = array($names);
- }
- foreach($names as $n) {
- $r = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con));
- mysql_query("DELETE FROM " . $IRC->mTableName . " WHERE name='" . $n . "'", $IRC->con);
- mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $r["name"] . "', " . $r["privs"] . ")", $IRC->con);
- mysql_query("DELETE FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con);
- }
- }
- function onConnect() {
- global $IRC;
- send("USER " . $IRC->username . " * * :" . $IRC->realname);
- send("NICK " . $IRC->joinnick);
- $IRC->cNick = $IRC->joinnick;
- if($IRC->auth) {
- send("PRIVMSG NickServ :ID " . $IRC->auth);
- }
- foreach($IRC->joinchan as $c) {
- send("JOIN " . $c);
- }
- }
- function handleBotCommands() {
- global $IRC;
- if((startsWith($IRC->cM->mess, $IRC->cmdChar)) && (strlen($IRC->cM->mess) > strlen($IRC->cmdChar)) && (strtolower($IRC->cM->nick) != strtolower($IRC->cNick))) {
- $userprivs = fetchmasters($IRC->cM->nick, true);
- $userprivs = $userprivs[0] ? $userprivs[0] : 0;
- $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
- if(((!$IRC->locked) || ($userprivs > 49)) && ($userprivs >= 0)) {
- $a = explode(" ", substr($IRC->cM->mess, strlen($IRC->cmdChar)));
- $r = implodefrom(" ", $a, 1);
- $reqprivs = fetchcmd($a[0], "privs");
- $hasprivs = $userprivs >= $reqprivs;
- eval(fetchcmd($a[0]));
- if($userprivs >= 100) {
- $chan = ($ispm ? $IRC->cM->nick : $IRC->cM->param[0]);
- switch(strtoupper($a[0])) {
- case "BIND":
- switch(strtoupper($a[1])) {
- case "ADD":
- if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 4))) || (!(is_numeric($a[3])))) {
- break;
- }
- $p = min($a[3], 100);
- send("PRIVMSG " . $chan . " :Binding command " . strtoupper($a[2]) . " with " . $p . " privs");
- dumpDB("Binding command " . strtoupper($a[2]) . " with " . $p . " privs", fetchcmd(strtoupper($a[2])), $o);
- mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
- mysql_query("INSERT INTO " . $IRC->cTableName . "(name, code, privs) VALUES ('" . mysql_real_escape_string(strtoupper($a[2])) . "', '" . mysql_real_escape_string($o) . "', " . $p . ")", $IRC->con);
- break;
- case "DEL":
- case "DELETE":
- $co = fetchcmd($a[2]);
- mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
- dumpDB("Deleted command " . strtoupper($a[2]), $co);
- send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was deleted.");
- break;
- case "REN":
- case "RENAME":
- if((sizeof($a) < 4) ) {
- break;
- }
- if($c = fetchcmd($a[2])) {
- $b = fetchcmd($a[3]);
- mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
- }
- dumpDB("Renaming command \"" . strtoupper($a[2]) . "\" to \"" . strtoupper($a[3]) . "\"", $b ? $b : "");
- mysql_query("UPDATE " . $IRC->cTableName . " SET name='" . mysql_real_escape_string(strtoupper($a[3])) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
- send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
- break;
- case "LNK":
- case "LINK":
- if((sizeof($a) < 4) || (strtoupper($a[2]) == strtoupper($a[3]))) {
- break;
- }
- send("PRIVMSG " . $chan . " :Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
- dumpDB("Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
- mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
- $p = fetchcmd($a[2], "privs");
- mysql_query("INSERT INTO " . $IRC->cTableName . "(name, code, privs) VALUES ('" . strtoupper($a[3]) . "', " . mysql_real_escape_string('$reqprivs = fetchcmd("' . $a[2] . '", "privs"); $hasprivs = $userprivs >= $reqprivs; eval(fetchcmd("' . $a[2] . '"));') . ", " . ($a[4] ? min($a[4], 100) : $p[0]) . ")", $IRC->con);
- break;
- case "APP":
- case "APPEND":
- if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
- break;
- }
- send("PRIVMSG " . $chan . " :Appending command " . strtoupper($a[2]));
- dumpDB("Appending command " . strtoupper($a[2]), $co = fetchcmd(strtoupper($a[2])), $o);
- mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . mysql_real_escape_string($co . $o) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
- break;
- case "PRV":
- case "PRIVS":
- if(sizeof($a) < 4) {
- break;
- }
- $p = min($a[3], 100);
- send("PRIVMSG " . $chan . " :Changing privlages of command " . strtoupper($a[2]) . " to " . $p);
- mysql_query("UPDATE " . $IRC->cTableName . " SET privs=" . $p . " WHERE name='" . mysql_real_escape_string($a[2]) . "'");
- break;
- case "UPD":
- case "UPDATE":
- if((sizeof($a) < 5) || (!($o = implodefrom(" ", $a, 4))) || (!is_numeric($a[3]))) {
- break;
- }
- $car = explode("\r\n", fetchcmd(strtoupper($a[2])));
- send("PRIVMSG " . $chan . " :Updating line " . $a[3] . " of command " . strtoupper($a[2]));
- dumpDB("Updating line " . $a[3] . " of command " . strtoupper($a[2]), $car[$a[3]], $o);
- $car[$a[3]] = $o;
- mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . mysql_real_escape_string(implode("\r\n", $car)) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
- break;
- }
- break;
- case "HOOK": //!hook add thename thecode();
- switch(strtoupper($a[1])) {
- case "ADD":
- if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
- break;
- }
- send("PRIVMSG " . $chan . " :Adding hook " . strtoupper($a[2]));
- mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
- mysql_query("INSERT INTO " . $IRC->hTableName . "(name, code, active) VALUES ('" . mysql_real_escape_string(strtoupper($a[2])) . "', '" . mysql_real_escape_string($o) . "', 0)", $IRC->con);
- break;
- case "DEL":
- case "DELETE":
- $co = fetchcmd($a[2]);
- mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
- send("PRIVMSG " . $chan . " :Hook '" . strtoupper($a[2]) . "' was deleted.");
- break;
- case "REN":
- case "RENAME":
- if((sizeof($a) < 4) ) {
- break;
- }
- if($c = fetchcmd($a[2])) {
- $b = fetchcmd($a[3]);
- mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . mysql_real_escape_string($a[3]) . "'", $IRC->con);
- }
- mysql_query("UPDATE " . $IRC->hTableName . " SET name='" . mysql_real_escape_string(strtoupper($a[3])) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
- send("PRIVMSG " . $chan . " :Hook '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
- break;
- case "ACT":
- case "ACTIVATE":
- if(sizeof($a) < 3) {
- break;
- }
- switch(strtoupper($a[3])) {
- case "OFF":
- case "0":
- case "FALSE":
- $nstate = "0";
- break;
- default:
- $nstate = "1";
- }
- send("PRIVMSG " . $chan . " :" . ($nstate ? "A" : "Dea") . "ctivating hook " . strtoupper($a[2]));
- mysql_query("UPDATE " . $IRC->hTableName . " SET active='" . $nstate . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
- break;
- }
- break;
- }
- }
- }
- }
- }
- function handleChatCommands() {
- global $IRC;
- if(!$IRC->cM->nick) {
- return false;
- }
- $chr1 = chr(1);
- if((startsWith($IRC->cM->mess[0], $chr1)) && ($IRC->cM->mess[strlen($IRC->cM->mess) - 1] == $chr1)) {
- $a = explode(" ", $IRC->cM->mess);
- $tok = implodefrom(" ", $a, 1);
- $tok = trim(substr($tok, 0, strlen($tok) - 1));
- if($tok) {
- $tok2 = strtoupper(substr($a[0], 1));
- if($tok2 != "ACTION") {
- $tokk = ": ";
- }
- }else {
- $tok2 = strtoupper(substr($a[0], 1, strlen($tok2) - 1));
- }
- if($tok2 != "ACTION") {
- output($IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] requested CTCP " . $tok2 . " from " . $IRC->cM->param[0] . $tokk . $tok);
- }
- switch($tok2) {
- case "ACTION":
- output("[" . $IRC->cM->param[0] . "] * " . $IRC->cM->nick . " " . $tok);
- break;
- case "PING":
- send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "PING " . $tok . $chr1);
- break;
- case "TIME":
- send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "TIME " . date("D M jS H:i:s") . $chr1);
- break;
- case "VERSION":
- send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "VERSION Toot PHP Bot " . $IRC->version . $chr1);
- break;
- case "FINGER":
- send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "FINGER wat" . $chr1);
- break;
- }
- return true;
- }
- if(strtolower($IRC->cM->param[0]) != strtolower($IRC->cNick)) {
- output("[" . $IRC->cM->param[0] . "] <" . $IRC->channames[strtolower($IRC->cM->param[0])][$IRC->cM->nick] . $IRC->cM->nick . "> " . $IRC->cM->mess);
- }else {
- output("-*" . $IRC->cM->nick . "*- " . $IRC->cM->mess);
- }
- if(startsWith($IRC->cM->mess, $IRC->cmdChar)) {
- handleBotCommands();
- }
- return true;
- }
- function handleNormalCommands() {
- global $IRC;
- $h = true;
- switch(strtoupper(trim($IRC->cM->cmd))) {
- case "KICK":
- if(strtolower($IRC->cM->param[1]) == strtolower($IRC->cNick)) {
- $who = "You were";
- }else {
- $who = $IRC->cM->param[1] . " was";
- }
- unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->param[1]]);
- output("-!- " . $who . " kicked from " . $IRC->cM->param[0] . " by " . $IRC->cM->nick . " [" . $IRC->cM->mess . "]");
- if($IRC->cM->param[1] == $IRC->cNick) {
- send("JOIN :" . $IRC->cM->param[0]);
- }
- break;
- case "MODE":
- if(strtolower($IRC->cNick) == strtolower($IRC->cM->param[0])) {
- $o = $IRC->cM->mess;
- }else {
- $o = implodefrom(" ", $IRC->cM->param, 1);
- }
- if($IRC->cM->host) {
- output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->nick);
- }else {
- output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->host);
- }
- break;
- case "NOTICE":
- output("-" . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "]- " . $IRC->cM->mess);
- break;
- case "PRIVMSG":
- $h = handleChatCommands();
- break;
- case "PART":
- output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has left " . $IRC->cM->param[0] . " [" . $IRC->cM->mess . "]");
- unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->nick]);
- break;
- case "JOIN":
- output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has joined " . $IRC->cM->mess);
- $IRC->channicks[strtolower($IRC->cM->mess)][$IRC->cM->nick] = " ";
- break;
- case "QUIT":
- output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has quit [" . $IRC->cM->mess . "]");
- $a = array_keys($IRC->channames);
- for($i = 0; $i < sizeof($a); $i++) {
- unset($IRC->channames[$a[$i]][$IRC->cM->nick]);
- }
- break;
- case "NICK":
- if(strtolower($IRC->cM->nick) == strtolower($IRC->cNick)) {
- $who = "You are";
- $IRC->cNick = $IRC->cM->mess;
- }else {
- $who = $IRC->cM->nick . " is";
- }
- output("-!- " . $who . " now known as " . $IRC->cM->mess);
- $aa = array_keys($IRC->channames);
- for($i = 0; $i < sizeof($aa); $i++) {
- $IRC->channames[$aa[$i]][$IRC->cM->mess] = $IRC->channames[$aa[$i]][$IRC->cM->nick];
- unset($IRC->channames[$aa[$i]][$IRC->cM->nick]);
- }
- addmasters(array($IRC->cM->nick), array($IRC->cM->mess), true, true);
- break;
- case "TOPIC":
- output("-!- " . $IRC->cM->nick . " changed the topic of " . $IRC->cM->param[0] . " to: " . $IRC->cM->mess);
- break;
- case "PONG":
- if($IRC->pingpong) {
- output("-!- Server PONG reply from " . $IRC->cM->nick . ": " . $IRC->cM->param[0] . "; Time since PING request sent: " . (microtime(true) - $IRC->pingsent));
- }
- $IRC->lastping = time();
- $IRC->pongwait = false;
- break;
- default:
- $h = false;
- }
- $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
- $q = mysql_query("SELECT * FROM " . $IRC->hTableName, $IRC->con);
- while($row = mysql_fetch_array($q)) {
- if($row["active"]) {
- eval($row["code"]);
- }
- }
- return($h);
- }
- function handleNumberCommands() {
- global $IRC;
- switch($IRC->cM->cmd) {
- case "001":
- $IRC->server = $IRC->cM->nick;
- output($IRC->cM->mess);
- send("NICK " . $IRC->joinnick);
- send("PRIVMSG NickServ :ID " . $IRC->auth);
- foreach($IRC->joinchan as $c) {
- send("JOIN " . $c);
- }
- break;
- case "004":
- $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
- break;
- case "005":
- $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
- if($a = substr($IRC->cM->mess, $p = (strpos(strtoupper($IRC->cM->mess), "PREFIX=") + 8), strpos($IRC->cM->mess, " ", $p) - $p)) {
- $aa = explode(")", $a);
- $IRC->powers = array(array(), array());
- for($i = 0; $i < strlen($aa[0]); $i++) {
- $IRC->powers[0][] = $aa[0][$i];
- }
- for($i = 0; $i < strlen($aa[1]); $i++) {
- $IRC->powers[1][] = $aa[1][$i];
- }
- }
- case "252":
- case "254":
- output(trim($IRC->cM->param[0], $IRC->cNick . " ") . " " . $IRC->cM->mess);
- break;
- case "332":
- output("Topic for " . $IRC->cM->param[1] . " is: " . $IRC->cM->mess);
- break;
- case "333":
- output("Topic for " . $IRC->cM->param[1] . " set by " . $IRC->cM->param[2] . " on " . date("D M jS H:i:s", $IRC->cM->param[3]));
- break;
- case "353":
- $cc = $IRC->cM->param[2];
- $c = strtolower($cc);
- $ndat = $IRC->cM->wline . "\n";
- while(!instr("366 " . $IRC->cNick . " " . $c, $ndat)) {
- if($rd = fgets($IRC->fp, 1024)) {
- $ndat .= $rd;
- }
- }
- unset($IRC->tempnames[$c]);
- $IRC->tempnames[$c] = "";
- $e = explode("\n", $ndat);
- foreach($e as $a) {
- parseMessage(trim($a), false);
- if($IRC->cM->cmd != "353") {
- break;
- }
- $IRC->tempnames[$c] .= $IRC->cM->mess;
- $IRC->cM->mess = "";
- }
- $names = explode(" ", $IRC->tempnames[$c]);
- unset($IRC->tempnames[$c]);
- sort($names);
- output("[Users " . $cc . "]", false);
- output("", false, false);
- for($i = 0; $i < sizeof($names); $i++) {
- if(in_array($names[$i][0], $IRC->powers[1])) {
- $IRC->channames[$c][substr($names[$i], 1)] = $names[$i][0];
- }else {
- $IRC->channames[$c][$names[$i]] = " ";
- }
- if(($i % 5) == 0) {
- output("", true, false);
- output("", false);
- }
- output("[" . str_pad($IRC->channames[$c][$names[$i]] . $names[$i], 17, " ", STR_PAD_RIGHT) . "] ", false, false);
- }
- output("", true, false);
- output("-!- " . $cc . ": Total of " . sizeof($names) . " nicks");
- break;
- case "421":
- output("Unknown command: \"" . $IRC->cM->param[1] . "\"");
- break;
- case '433':
- output("Nickname \"" . $IRC->cM->param[1] . "\" is already in use.");
- if($IRC->firstcon) {
- send("NICK " . $IRC->joinnick . rand(100, 999));
- send("PRIVMSG NickServ :ghost " . $IRC->joinnick . " " . $IRC->auth);
- }
- break;
- case '474':
- output('Unable to join channel ' . $IRC->cM->param[1] . ': ' . $IRC->cM->mess);
- break;
- default:
- output($IRC->cM->mess);
- break;
- }
- return true;
- }
- function parseMessage($buffer, $handle = true) {
- global $IRC;
- $buffer = explode("\n", $buffer);
- $IRC->cM->wline = $buffer = trim($buffer[0], "\r");
- if(!trim($buffer)) {
- return;
- }
- if($IRC->debug) {
- output($buffer);
- }
- switch($s = startsWith(strtoupper($IRC->cM->wline), array(":", "NOTICE", "PING", "ERROR"))) {
- case ":":
- //*
- $IRC->cM->sender = substr(strtok($IRC->cM->wline, " "), 1);
- $IRC->cM->cmd = strtok(" ");
- $IRC->cM->param = explode(" ", trim(strtok(" " . strtok(""), ":")));
- $IRC->cM->mess = strtok("");
- $IRC->cM->nick = strtok($IRC->cM->sender, "!");
- $IRC->cM->user = strtok("@");
- $IRC->cM->host = strtok("");
- //*/
- if($handle) {
- if(is_numeric($IRC->cM->cmd)) {
- if(handleNumberCommands()) {
- break;
- }
- }
- if(handleNormalCommands()) {
- break;
- }
- output("Unknown message: " . $IRC->cM->wline);
- }
- break;
- case "NOTICE":
- $IRC->cM->mess = substr($IRC->cM->wline, strpos($IRC->cM->wline, ":") + 1);
- $ss = $IRC->server;
- if(startsWith(strtoupper($IRC->cM->wline), "NOTICE AUTH")) {
- $ss = $IRC->nick;
- }
- output("!" . $ss . " " . $IRC->cM->mess);
- break;
- case "PING":
- if($IRC->pingpong) {
- output($buffer);
- }
- $IRC->cM->wline[1] = "O";
- send($IRC->cM->wline, $IRC->pingpong);
- break;
- case "ERROR":
- output($IRC->cM->wline);
- break;
- default:
- output("Unknown message: " . $IRC->cM->wline);
- break;
- }
- }
- $IRC->connected = false;
- $IRC->lastping = time();
- $IRC->pongwait = false;
- $IRC->pingsent = microtime(true);
- $IRC->masterNick = fetchmasters();
- stream_set_blocking(STDIN, 0);
- function loop() {
- global $IRC;
- if((($IRC->pongwait) && ((time() - $IRC->lastping) > 240)) || (((!$IRC->connected) || (feof($IRC->fp)) && (!($IRC->quit))))) {
- if($IRC->connected) {
- if(!feof($IRC->fp)) {
- send("QUIT :Lost connection");
- }
- fclose($IRC->fp);
- }
- $IRC->connected = false;
- output("-!- Attempting to open connection to " . $IRC->host . ":" . $IRC->port);
- if($IRC->fp = fsockopen($IRC->host, $IRC->port, $errno, $errdesc)) {
- output("-!- Connection established to " . $IRC->host . ":" . $IRC->port);
- stream_set_timeout($IRC->fp, 0, 1);
- $IRC->firstcon = true;
- $IRC->connected = true;
- $IRC->pongwait = false;
- $IRC->lastping = time();
- send("USER " . $IRC->username . " * * :" . $IRC->realname);
- send("NICK " . $IRC->joinnick);
- }else {
- sleep(30);
- continue;
- }
- }
- if((!$IRC->pongwait) && (time() - $IRC->lastping) > 30) {
- $IRC->pongwait = true;
- $IRC->pingsent = microtime(true);
- send("PING :" . $IRC->server, $IRC->pingpong);
- }
- if($rawdata = fgets($IRC->fp, 1024)) {
- parseMessage($rawdata);
- }
- if($rawdata = fgets(STDIN, 1024)) {
- eval($rawdata);
- }
- return($rawdata);
- }
- while(!$IRC->quit) {
- usleep(10000);
- loop();
- }
- if(!feof($IRC->fp)) {
- send("QUIT :I couldn't think of a whitty quit message");
- }
- fclose($IRC->fp);
- fclose($IRC->logfile);
- mysql_close($IRC->con);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement