Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 25.91 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. TODO:
  5. 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)
  6. ^ basicly if there's no way to verify they'll be using the bot, move them over
  7. if an inactive master joins/nicks, readd to active masters list (perhaps auth with a user-generated password?)
  8. i probably should comment more stuff, it's getting kinda complex; would hate to have to rewrite/relearn
  9. */
  10.  
  11. class aMessage {
  12.     var $nick, $user, $host, $sender, $cmd, $param, $mess, $wline;
  13. }
  14.  
  15. class aIRC {
  16.     var $cM;
  17.     var $lastping, $pongwait, $pingsent, $pingpong;
  18.     var $host, $fp, $port, $logfile, $con, $server, $cmdChar;
  19.     var $cNick, $joinnick, $username, $realname, $joinchan, $auth, $authnick, $masterNick;
  20.     var $channames, $powers, $tempnames, $cnameslist;
  21.     var $debug, $firstcon, $locked, $quit, $version;
  22.     var $mTableName, $cTableName, $iTableName, $MySQLServer, $MySQLDatabase, $MySQLAccountName, $MySQLAccountpassword;
  23. }
  24.  
  25. $IRC = new aIRC;
  26. $IRC->cM = new aMessage;
  27. $IRC->cM->param = array();
  28. $IRC->host = "irc.rizon.net";
  29. $IRC->port = 6667;
  30. $IRC->joinchan = array("#Tootoot222", "#mixster", "#SRL-School", "#SRL");
  31. $IRC->joinnick = "Furry";
  32. $IRC->username = "Furry";
  33. $IRC->realname = "Furry";
  34. $IRC->authnick = "Tootoot222";
  35. $IRC->cNick = $IRC->joinnick;
  36. $IRC->auth = "";
  37. $IRC->server = "";
  38. $IRC->cmdChar = "!";
  39. $IRC->locked = false;
  40. $IRC->debug = false;
  41. $IRC->pingpong = false;
  42. $IRC->powers = array(array(), array());
  43. $IRC->channames = array(array(array()));
  44. $IRC->tempnames = array();
  45. $IRC->cnameslist = "";
  46. $IRC->version = "Version 3";
  47.  
  48. set_time_limit(0); //to ensure the script doesn't time out
  49.  
  50. date_default_timezone_set("America/Chicago"); //change this to your timezone
  51.  
  52. $firstmaster = "Tootoot222"; //set this if you haven't created the database/tables yet
  53.  
  54. $IRC->MySQLServer = "localhost";   
  55. $IRC->MySQLDatabase = "ircbotv3";
  56. $IRC->MySQLAccountName = "root";
  57. $IRC->MySQLAccountpassword = "";
  58. $IRC->mTableName = "masters5";
  59. $IRC->iTableName = "imasters2";
  60. $IRC->cTableName = "cmd3";
  61. $IRC->hTableName = "hooks1";
  62.  
  63.  
  64. $IRC->logfile = fopen("logfile.txt", "a");
  65. fwrite($IRC->logfile, "\r\n*** Log opened on " . date("D M jS H:i:s", time()) . " ***\r\n\r\n");
  66.  
  67. $IRC->con = mysql_connect($IRC->MySQLServer, $IRC->MySQLAccountName, $IRC->MySQLAccountpassword) or die("MySQL error: " . mysql_error() . "\r\n");
  68. mysql_query("CREATE DATABASE IF NOT EXISTS " . $IRC->MySQLDatabase, $IRC->con);
  69. mysql_select_db($IRC->MySQLDatabase, $IRC->con);
  70.  
  71. function table_exists($tablename) {
  72.     global $IRC;
  73.     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);
  74. }
  75.  
  76. if(!table_exists($IRC->cTableName)) {
  77.     mysql_query("CREATE TABLE " . $IRC->cTableName . "(name VARCHAR(25), code MEDIUMTEXT, privs INT, maker VARCHAR(20))", $IRC->con);
  78. }
  79. if(!table_exists($IRC->mTableName)) {
  80.     mysql_query("CREATE TABLE " . $IRC->mTableName . "(name VARCHAR(20), privs INT)", $IRC->con);
  81.     mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $firstmaster . "', 100)", $IRC->con);
  82. }
  83. if(!table_exists($IRC->iTableName)) {
  84.     mysql_query("CREATE TABLE " . $IRC->iTableName . "(name VARCHAR(20), pass VARCHAR(32), privs INT)", $IRC->con);
  85. }
  86. if(!table_exists($IRC->hTableName)) {
  87.     mysql_query("CREATE TABLE " . $IRC->hTableName . "(name VARCHAR(25), code MEDIUMTEXT, active BOOL)", $IRC->con);
  88. }
  89.  
  90. function output($message, $newline = true, $timestamp = true) {
  91.     global $IRC;
  92.     foreach(explode("\r\n", $message) as $m) {
  93.         $out = ($timestamp ? "[" . date("H:i:s") . "] " : "") . $m . ($newline ? "\r\n" : "");
  94.         echo($out);
  95.         fwrite($IRC->logfile, $out);
  96.     }
  97. }
  98.  
  99. function send($data, $out = true, $b = false) {
  100.     if(!trim($data)) {
  101.       return;
  102.     }
  103.     global $IRC;
  104.     $a = explode("\r\n", $data);
  105.     foreach($a as $aa) {
  106.         fputs($IRC->fp, $aa . "\r\n");
  107.         if($out) {
  108.             output(($b ? "--" : "") . "-> " . $aa);
  109.         }
  110.     }
  111. }
  112.  
  113. function implodefrom($glue, $pieces, $from = 0) {
  114.     $r = "";
  115.     if(is_array($pieces)) {
  116.         $from = max(0, $from);
  117.         $r = $pieces[$from];
  118.         for($i = ($from + 1); $i < sizeof($pieces); $i++) {
  119.             $r .= $glue . $pieces[$i];
  120.         }
  121.     }
  122.     return($r);
  123. }
  124.  
  125. function notnull($c1, $c2) {
  126.     return($c1 ? $c1 : $c2);
  127. }
  128.  
  129. function startsWith($haystack, $needle) {
  130.     if(is_array($needle)) {
  131.         foreach($needle as $n) {
  132.             if(strpos($haystack, $n) === 0) {
  133.                 return($n);
  134.             }
  135.         }
  136.     }
  137.     return(strpos($haystack, $needle) === 0);
  138. }
  139.  
  140. function instr($needle, $haystack, $sensitive = false) {
  141.     if($sensitive) {
  142.         return((false !== strpos($haystack, $needle)) ? true : false);
  143.     }else {
  144.         return((false !== stristr($haystack, $needle)) ? true : false);
  145.     }
  146. }
  147.  
  148. function dumpDB($message = "", $oldcode = "", $newcode = "") {
  149.     global $IRC;
  150.     if(!file_exists("./DB_backup/")) {
  151.         mkdir("./DB_backup/");
  152.     }
  153.     $basename = "db_backup";
  154.     $n = 1;
  155.     while(file_exists($filename = ("./DB_backup/" . $basename . $n . ".txt"))) {
  156.         $n++;
  157.     }
  158.     if(!$fh = fopen($filename, 'w')) {
  159.         return("failed to open file: " . $filename);
  160.     }
  161.     fwrite($fh, "//Dumpfile " . $n . " created on " . date("D M jS H:i:s", time()) . "\r\n\r\n");
  162.     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 : ""));
  163.     $q = mysql_query("SELECT * FROM " . $IRC->cTableName . " ORDER BY name ASC", $IRC->con);
  164.     while($row = mysql_fetch_array($q)) {
  165.         fwrite($fh, "\r\n\r\n\r\n//" . strtoupper($row["name"]) . ":\r\n" . $row["code"]);
  166.     }
  167.     fclose($fh);
  168.     return(true);
  169. }
  170.  
  171. function fetchcmd($cmd, $r = "code") {
  172.     global $IRC;
  173.     $row = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->cTableName . ($cmd ? " WHERE name='" . $cmd . "'" : ""), $IRC->con));
  174.     return($row[$r]);
  175. }
  176.  
  177. function fetchmasters($name = "", $privs = false, $active = true) {
  178.     global $IRC;
  179.     $q = mysql_query("SELECT " . ($n = ($privs ? "privs" : "name")) . " FROM " . ($active ? $IRC->mTableName : $IRC->iTableName) . ($name ? " WHERE name='" . $name . "'" : ""), $IRC->con);
  180.     $r = array();
  181.     while($row = mysql_fetch_array($q)) {
  182.         $r[] = $row[$n];
  183.     }
  184.     return($r);
  185. }
  186.  
  187. function removemasters($names, $all = false, $active = true) {
  188.     global $IRC;
  189.     if($active) {
  190.         $t = $IRC->mTableName;
  191.     }else {
  192.         $t = $IRC->iTableName;
  193.     }
  194.     if(!$all) {
  195.         if(!is_array($names)) {
  196.             $names = array($names);
  197.         }
  198.         foreach($names as $n) {
  199.             mysql_query("DELETE FROM " . $t . " WHERE name='" . $n . "'", $IRC->con);
  200.         }
  201.     }else {
  202.         mysql_query("DELETE FROM " . $t, $IRC->con);
  203.     }
  204. }
  205.  
  206. function addmasters($names, $privs, $active = true, $upd = false) {
  207.     global $IRC;
  208.     if(sizeof($names) != sizeof($privs)) {
  209.         $p = $privs;
  210.         $privs = array();
  211.         for($i = 0; $i < sizeof($names); $i++) {
  212.             $privs[$i] = $p;
  213.         }
  214.     }
  215.     if($active) {
  216.         $t = $IRC->mTableName;
  217.     }else {
  218.         $t = $IRC->iTableName;
  219.     }
  220.     if(!$upd) {
  221.         removemasters($names);
  222.     }
  223.     for($i = 0; $i < sizeof($names); $i++) {
  224.         if(!$upd) {
  225.             mysql_query("INSERT INTO " . $t . "(name, privs) VALUES ('" . $names[$i] . "', " . min($privs[$i], 100) . ")", $IRC->con);
  226.         }else {
  227.             mysql_query("UPDATE " . $t . " SET name='" . $privs[$i] . "' WHERE name='" . $names[$i] . "'", $IRC->con);
  228.         }
  229.     }
  230. }
  231.  
  232. function swapmasters($names) {
  233.     global $IRC;
  234.     if(!is_array($names)) {
  235.         $names = array($names);
  236.     }
  237.     foreach($names as $n) {
  238.         $r = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con));
  239.         mysql_query("DELETE FROM " . $IRC->mTableName . " WHERE name='" . $n . "'", $IRC->con);
  240.         mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $r["name"] . "', " . $r["privs"] . ")", $IRC->con);
  241.         mysql_query("DELETE FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con);
  242.     }
  243. }
  244.  
  245. function onConnect() {
  246.     global $IRC;
  247.     send("USER " . $IRC->username . " * * :" . $IRC->realname);
  248.     send("NICK " . $IRC->joinnick);
  249.     $IRC->cNick = $IRC->joinnick;
  250.     if($IRC->auth) {
  251.         send("PRIVMSG NickServ :ID " . $IRC->auth);
  252.     }
  253.     foreach($IRC->joinchan as $c) {
  254.         send("JOIN " . $c);
  255.     }
  256. }
  257.  
  258. function handleBotCommands() {
  259.     global $IRC;
  260.     if((startsWith($IRC->cM->mess, $IRC->cmdChar)) && (strlen($IRC->cM->mess) > strlen($IRC->cmdChar)) && (strtolower($IRC->cM->nick) != strtolower($IRC->cNick))) {
  261.         $userprivs = fetchmasters($IRC->cM->nick, true);
  262.         $userprivs = $userprivs[0] ? $userprivs[0] : 0;
  263.         $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
  264.         if(((!$IRC->locked) || ($userprivs > 49)) && ($userprivs >= 0)) {
  265.             $a = explode(" ", substr($IRC->cM->mess, strlen($IRC->cmdChar)));
  266.             $r = implodefrom(" ", $a, 1);
  267.             $reqprivs = fetchcmd($a[0], "privs");
  268.             $hasprivs = $userprivs >= $reqprivs;
  269.             eval(fetchcmd($a[0]));
  270.             if($userprivs >= 100) {
  271.                 $chan = ($ispm ? $IRC->cM->nick : $IRC->cM->param[0]);
  272.                 switch(strtoupper($a[0])) {
  273.                     case "BIND":
  274.                         switch(strtoupper($a[1])) {
  275.                             case "ADD":
  276.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 4))) || (!(is_numeric($a[3])))) {
  277.                                     break;
  278.                                 }
  279.                                 $p = min($a[3], 100);
  280.                                 send("PRIVMSG " . $chan . " :Binding command " . strtoupper($a[2]) . " with " . $p . " privs");
  281.                                 dumpDB("Binding command " . strtoupper($a[2]) . " with " . $p . " privs", fetchcmd(strtoupper($a[2])), $o);
  282.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  283.                                 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);
  284.                                 break;
  285.                             case "DEL":
  286.                             case "DELETE":
  287.                                 $co = fetchcmd($a[2]);
  288.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  289.                                 dumpDB("Deleted command " . strtoupper($a[2]), $co);
  290.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was deleted.");
  291.                                 break;
  292.                             case "REN":
  293.                             case "RENAME":
  294.                                 if((sizeof($a) < 4) ) {
  295.                                     break;
  296.                                 }
  297.                                 if($c = fetchcmd($a[2])) {
  298.                                     $b = fetchcmd($a[3]);
  299.                                     mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  300.                                 }
  301.                                 dumpDB("Renaming command \"" . strtoupper($a[2]) . "\" to \"" . strtoupper($a[3]) . "\"", $b ? $b : "");
  302.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET name='" . mysql_real_escape_string(strtoupper($a[3])) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  303.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
  304.                                 break;
  305.                             case "LNK":
  306.                             case "LINK":
  307.                                 if((sizeof($a) < 4) || (strtoupper($a[2]) == strtoupper($a[3]))) {
  308.                                     break;
  309.                                 }
  310.                                 send("PRIVMSG " . $chan . " :Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  311.                                 dumpDB("Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  312.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  313.                                 $p = fetchcmd($a[2], "privs");
  314.                                 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);
  315.                                 break;
  316.                             case "APP":
  317.                             case "APPEND":
  318.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
  319.                                     break;
  320.                                 }
  321.                                 send("PRIVMSG " . $chan . " :Appending command " . strtoupper($a[2]));
  322.                                 dumpDB("Appending command " . strtoupper($a[2]), $co = fetchcmd(strtoupper($a[2])), $o);
  323.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . mysql_real_escape_string($co . $o) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  324.                                 break;
  325.                             case "PRV":
  326.                             case "PRIVS":
  327.                                 if(sizeof($a) < 4) {
  328.                                     break;
  329.                                 }
  330.                                 $p = min($a[3], 100);
  331.                                 send("PRIVMSG " . $chan . " :Changing privlages of command " . strtoupper($a[2]) . " to " . $p);
  332.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET privs=" . $p . " WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  333.                                 break;
  334.                             case "UPD":
  335.                             case "UPDATE":
  336.                                 if((sizeof($a) < 5) || (!($o = implodefrom(" ", $a, 4))) || (!is_numeric($a[3]))) {
  337.                                     break;
  338.                                 }
  339.                                 $car = explode("\r\n", fetchcmd(strtoupper($a[2])));
  340.                                 send("PRIVMSG " . $chan . " :Updating line " . $a[3] . " of command " . strtoupper($a[2]));
  341.                                 dumpDB("Updating line " . $a[3] . " of command " . strtoupper($a[2]), $car[$a[3]], $o);
  342.                                 $car[$a[3]] = $o;
  343.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . mysql_real_escape_string(implode("\r\n", $car)) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  344.                                 break;
  345.                         }
  346.                         break;
  347.                     case "HOOK": //!hook add thename thecode();
  348.                         switch(strtoupper($a[1])) {
  349.                             case "ADD":
  350.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
  351.                                     break;
  352.                                 }
  353.                                 send("PRIVMSG " . $chan . " :Adding hook " . strtoupper($a[2]));
  354.                                 mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  355.                                 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);
  356.                                 break;
  357.                             case "DEL":
  358.                             case "DELETE":
  359.                                 $co = fetchcmd($a[2]);
  360.                                 mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  361.                                 send("PRIVMSG " . $chan . " :Hook '" . strtoupper($a[2]) . "' was deleted.");
  362.                                 break;
  363.                             case "REN":
  364.                             case "RENAME":
  365.                                 if((sizeof($a) < 4) ) {
  366.                                     break;
  367.                                 }
  368.                                 if($c = fetchcmd($a[2])) {
  369.                                     $b = fetchcmd($a[3]);
  370.                                     mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . mysql_real_escape_string($a[3]) . "'", $IRC->con);
  371.                                 }
  372.                                 mysql_query("UPDATE " . $IRC->hTableName . " SET name='" . mysql_real_escape_string(strtoupper($a[3])) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  373.                                 send("PRIVMSG " . $chan . " :Hook '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
  374.                                 break;
  375.                             case "ACT":
  376.                             case "ACTIVATE":
  377.                                 if(sizeof($a) < 3) {
  378.                                     break;
  379.                                 }
  380.                                 switch(strtoupper($a[3])) {
  381.                                     case "OFF":
  382.                                     case "0":
  383.                                     case "FALSE":
  384.                                         $nstate = "0";
  385.                                         break;
  386.                                     default:
  387.                                         $nstate = "1";
  388.                                 }
  389.                                 send("PRIVMSG " . $chan . " :" . ($nstate ? "A" : "Dea") . "ctivating hook " . strtoupper($a[2]));
  390.                                 mysql_query("UPDATE " . $IRC->hTableName . " SET active='" . $nstate . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  391.                                 break;
  392.                         }
  393.                         break;
  394.                 }
  395.             }
  396.         }
  397.     }
  398. }
  399.  
  400. function handleChatCommands() {
  401.     global $IRC;
  402.     if(!$IRC->cM->nick) {
  403.         return false;
  404.     }
  405.     $chr1 = chr(1);
  406.     if((startsWith($IRC->cM->mess[0], $chr1)) && ($IRC->cM->mess[strlen($IRC->cM->mess) - 1] == $chr1)) {
  407.         $a = explode(" ", $IRC->cM->mess);
  408.         $tok = implodefrom(" ", $a, 1);
  409.         $tok = trim(substr($tok, 0, strlen($tok) - 1));
  410.         if($tok) {
  411.             $tok2 = strtoupper(substr($a[0], 1));
  412.             if($tok2 != "ACTION") {
  413.                 $tokk = ": ";
  414.             }
  415.         }else {
  416.             $tok2 = strtoupper(substr($a[0], 1, strlen($tok2) - 1));
  417.         }
  418.         if($tok2 != "ACTION") {
  419.             output($IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] requested CTCP " . $tok2 . " from " . $IRC->cM->param[0] . $tokk . $tok);
  420.         }
  421.         switch($tok2) {
  422.             case "ACTION":
  423.                 output("[" . $IRC->cM->param[0] . "] * " . $IRC->cM->nick . " " . $tok);
  424.                 break;
  425.             case "PING":
  426.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "PING " . $tok . $chr1);
  427.                 break;
  428.             case "TIME":
  429.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "TIME " . date("D M jS H:i:s") . $chr1);
  430.                 break;
  431.             case "VERSION":
  432.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "VERSION Toot PHP Bot " . $IRC->version . $chr1);
  433.                 break;
  434.             case "FINGER":
  435.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "FINGER wat" . $chr1);
  436.                 break;
  437.         }
  438.         return true;
  439.     }
  440.     if(strtolower($IRC->cM->param[0]) != strtolower($IRC->cNick)) {
  441.         output("[" . $IRC->cM->param[0] . "] <" . $IRC->channames[strtolower($IRC->cM->param[0])][$IRC->cM->nick] . $IRC->cM->nick . "> " . $IRC->cM->mess);
  442.     }else {
  443.         output("-*" . $IRC->cM->nick . "*- " . $IRC->cM->mess);
  444.     }
  445.     if(startsWith($IRC->cM->mess, $IRC->cmdChar)) {
  446.         handleBotCommands();
  447.     }
  448.     return true;
  449. }
  450.  
  451. function handleNormalCommands() {
  452.     global $IRC;
  453.     $h = true;
  454.     switch(strtoupper(trim($IRC->cM->cmd))) {
  455.         case "KICK":
  456.             if(strtolower($IRC->cM->param[1]) == strtolower($IRC->cNick)) {
  457.                 $who = "You were";
  458.             }else {
  459.                 $who = $IRC->cM->param[1] . " was";
  460.             }
  461.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->param[1]]);
  462.             output("-!- " . $who . " kicked from " . $IRC->cM->param[0] . " by " . $IRC->cM->nick . " [" . $IRC->cM->mess . "]");
  463.             if($IRC->cM->param[1] == $IRC->cNick) {
  464.                 send("JOIN :" . $IRC->cM->param[0]);
  465.             }
  466.             break;
  467.         case "MODE":
  468.             if(strtolower($IRC->cNick) == strtolower($IRC->cM->param[0])) {
  469.                 $o = $IRC->cM->mess;
  470.             }else {
  471.                 $o = implodefrom(" ", $IRC->cM->param, 1);
  472.             }
  473.             if($IRC->cM->host) {
  474.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->nick);
  475.             }else {
  476.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->host);
  477.             }
  478.             break;
  479.         case "NOTICE":
  480.             output("-" . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "]- " . $IRC->cM->mess);
  481.             break;
  482.         case "PRIVMSG":
  483.             $h = handleChatCommands();
  484.             break;
  485.         case "PART":
  486.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has left " . $IRC->cM->param[0] . " [" . $IRC->cM->mess . "]");
  487.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->nick]);
  488.             break;
  489.         case "JOIN":
  490.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has joined " . $IRC->cM->mess);
  491.             $IRC->channicks[strtolower($IRC->cM->mess)][$IRC->cM->nick] = " ";
  492.             break;
  493.         case "QUIT":
  494.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has quit [" . $IRC->cM->mess . "]");
  495.             $a = array_keys($IRC->channames);
  496.             for($i = 0; $i < sizeof($a); $i++) {
  497.                 unset($IRC->channames[$a[$i]][$IRC->cM->nick]);
  498.             }
  499.             break;
  500.         case "NICK":
  501.             if(strtolower($IRC->cM->nick) == strtolower($IRC->cNick)) {
  502.                 $who = "You are";
  503.                 $IRC->cNick = $IRC->cM->mess;
  504.             }else {
  505.                 $who = $IRC->cM->nick . " is";
  506.             }
  507.             output("-!- " . $who . " now known as " . $IRC->cM->mess);
  508.             $aa = array_keys($IRC->channames);
  509.             for($i = 0; $i < sizeof($aa); $i++) {
  510.                 $IRC->channames[$aa[$i]][$IRC->cM->mess] = $IRC->channames[$aa[$i]][$IRC->cM->nick];
  511.                 unset($IRC->channames[$aa[$i]][$IRC->cM->nick]);
  512.             }
  513.             addmasters(array($IRC->cM->nick), array($IRC->cM->mess), true, true);
  514.             break;
  515.         case "TOPIC":
  516.             output("-!- " . $IRC->cM->nick . " changed the topic of " . $IRC->cM->param[0] . " to: " . $IRC->cM->mess);
  517.             break;
  518.         case "PONG":
  519.             if($IRC->pingpong) {
  520.                 output("-!- Server PONG reply from " . $IRC->cM->nick . ": " . $IRC->cM->param[0] . "; Time since PING request sent: " . (microtime(true) - $IRC->pingsent));
  521.             }
  522.             $IRC->lastping = time();
  523.             $IRC->pongwait = false;
  524.             break;
  525.         default:
  526.             $h = false;
  527.     }
  528.     $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
  529.     $q = mysql_query("SELECT * FROM " . $IRC->hTableName, $IRC->con);
  530.     while($row = mysql_fetch_array($q)) {
  531.         if($row["active"]) {
  532.             eval($row["code"]);
  533.         }
  534.     }
  535.     return($h);
  536. }
  537.  
  538. function handleNumberCommands() {
  539.     global $IRC;
  540.     switch($IRC->cM->cmd) {
  541.         case "001":
  542.             $IRC->server = $IRC->cM->nick;
  543.             output($IRC->cM->mess);
  544.             send("NICK " . $IRC->joinnick);
  545.             send("PRIVMSG NickServ :ID " . $IRC->auth);
  546.             foreach($IRC->joinchan as $c) {
  547.                 send("JOIN " . $c);
  548.             }
  549.             break;
  550.         case "004":
  551.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  552.             break;
  553.         case "005":
  554.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  555.             if($a = substr($IRC->cM->mess, $p = (strpos(strtoupper($IRC->cM->mess), "PREFIX=") + 8), strpos($IRC->cM->mess, " ", $p) - $p)) {
  556.                 $aa = explode(")", $a);
  557.                 $IRC->powers = array(array(), array());
  558.                 for($i = 0; $i < strlen($aa[0]); $i++) {
  559.                     $IRC->powers[0][] = $aa[0][$i];
  560.                 }
  561.                 for($i = 0; $i < strlen($aa[1]); $i++) {
  562.                     $IRC->powers[1][] = $aa[1][$i];
  563.                 }
  564.             }
  565.         case "252":
  566.         case "254":
  567.             output(trim($IRC->cM->param[0], $IRC->cNick . " ") . " " . $IRC->cM->mess);
  568.             break;
  569.         case "332":
  570.             output("Topic for " . $IRC->cM->param[1] . " is: " . $IRC->cM->mess);
  571.             break;
  572.         case "333":
  573.             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]));
  574.             break;
  575.         case "353":
  576.             $cc = $IRC->cM->param[2];
  577.             $c = strtolower($cc);
  578.             $ndat = $IRC->cM->wline . "\n";
  579.             while(!instr("366 " . $IRC->cNick . " " . $c, $ndat)) {
  580.                 if($rd = fgets($IRC->fp, 1024)) {
  581.                     $ndat .= $rd;
  582.                 }
  583.             }
  584.             unset($IRC->tempnames[$c]);
  585.             $IRC->tempnames[$c] = "";
  586.             $e = explode("\n", $ndat);
  587.             foreach($e as $a) {
  588.                 parseMessage(trim($a), false);
  589.                 if($IRC->cM->cmd != "353") {
  590.                     break;
  591.                 }
  592.                 $IRC->tempnames[$c] .= $IRC->cM->mess;
  593.                 $IRC->cM->mess = "";
  594.             }
  595.             $names = explode(" ", $IRC->tempnames[$c]);
  596.             unset($IRC->tempnames[$c]);
  597.             sort($names);
  598.             output("[Users " . $cc . "]", false);
  599.             output("", false, false);
  600.             for($i = 0; $i < sizeof($names); $i++) {
  601.                 if(in_array($names[$i][0], $IRC->powers[1])) {
  602.                     $IRC->channames[$c][substr($names[$i], 1)] = $names[$i][0];
  603.                 }else {
  604.                     $IRC->channames[$c][$names[$i]] = " ";
  605.                 }
  606.                 if(($i % 5) == 0) {
  607.                     output("", true, false);
  608.                     output("", false);
  609.                 }
  610.                 output("[" . str_pad($IRC->channames[$c][$names[$i]] . $names[$i], 17, " ", STR_PAD_RIGHT) . "] ", false, false);
  611.             }
  612.             output("", true, false);
  613.             output("-!- " . $cc . ": Total of " . sizeof($names) . " nicks");
  614.             break;
  615.         case "421":
  616.             output("Unknown command: \"" . $IRC->cM->param[1] . "\"");
  617.             break;
  618.         case '433':
  619.             output("Nickname \"" . $IRC->cM->param[1] . "\" is already in use.");
  620.             if($IRC->firstcon) {
  621.                 send("NICK " . $IRC->joinnick . rand(100, 999));
  622.                 send("PRIVMSG NickServ :ghost " . $IRC->joinnick . " " . $IRC->auth);
  623.             }          
  624.             break;
  625.         case '474':
  626.             output('Unable to join channel ' . $IRC->cM->param[1] . ': ' . $IRC->cM->mess);
  627.             break;
  628.         default:
  629.             output($IRC->cM->mess);
  630.             break;
  631.     }
  632.     return true;
  633. }
  634.  
  635. function parseMessage($buffer, $handle = true) {
  636.     global $IRC;
  637.     $buffer = explode("\n", $buffer);
  638.     $IRC->cM->wline = $buffer = trim($buffer[0], "\r");
  639.     if(!trim($buffer)) {
  640.         return;
  641.     }
  642.     if($IRC->debug) {
  643.         output($buffer);
  644.     }
  645.     switch($s = startsWith(strtoupper($IRC->cM->wline), array(":", "NOTICE", "PING", "ERROR"))) {
  646.         case ":":
  647.             //*
  648.             $IRC->cM->sender = substr(strtok($IRC->cM->wline, " "), 1);
  649.             $IRC->cM->cmd = strtok(" ");
  650.             $IRC->cM->param = explode(" ", trim(strtok(" " . strtok(""), ":")));
  651.             $IRC->cM->mess = strtok("");
  652.             $IRC->cM->nick = strtok($IRC->cM->sender, "!");
  653.             $IRC->cM->user = strtok("@");
  654.             $IRC->cM->host = strtok("");
  655.             //*/
  656.             if($handle) {
  657.                 if(is_numeric($IRC->cM->cmd)) {
  658.                     if(handleNumberCommands()) {
  659.                         break;
  660.                     }
  661.                 }
  662.                 if(handleNormalCommands()) {
  663.                     break;
  664.                 }
  665.                 output("Unknown message: " . $IRC->cM->wline);
  666.             }
  667.             break;
  668.         case "NOTICE":
  669.             $IRC->cM->mess = substr($IRC->cM->wline, strpos($IRC->cM->wline, ":") + 1);
  670.             $ss = $IRC->server;
  671.             if(startsWith(strtoupper($IRC->cM->wline), "NOTICE AUTH")) {
  672.                 $ss = $IRC->nick;
  673.             }
  674.             output("!" . $ss . " " . $IRC->cM->mess);
  675.             break;
  676.         case "PING":
  677.             if($IRC->pingpong) {
  678.                 output($buffer);
  679.             }
  680.             $IRC->cM->wline[1] = "O";
  681.             send($IRC->cM->wline, $IRC->pingpong);
  682.             break;
  683.         case "ERROR":
  684.             output($IRC->cM->wline);
  685.             break;
  686.         default:
  687.             output("Unknown message: " . $IRC->cM->wline);
  688.             break;
  689.     }
  690. }
  691.  
  692. $IRC->connected = false;
  693. $IRC->lastping = time();
  694. $IRC->pongwait = false;
  695. $IRC->pingsent = microtime(true);
  696. $IRC->masterNick = fetchmasters();
  697. stream_set_blocking(STDIN, 0);
  698.  
  699. function loop() {
  700.     global $IRC;
  701.     if((($IRC->pongwait) && ((time() - $IRC->lastping) > 240)) || (((!$IRC->connected) || (feof($IRC->fp)) && (!($IRC->quit))))) {
  702.         if($IRC->connected) {
  703.             if(!feof($IRC->fp)) {
  704.                 send("QUIT :Lost connection");
  705.             }
  706.             fclose($IRC->fp);
  707.         }
  708.         $IRC->connected = false;
  709.         output("-!- Attempting to open connection to " . $IRC->host . ":" . $IRC->port);
  710.         if($IRC->fp = fsockopen($IRC->host, $IRC->port, $errno, $errdesc)) {
  711.             output("-!- Connection established to " . $IRC->host . ":" . $IRC->port);
  712.             stream_set_timeout($IRC->fp, 0, 1);
  713.             $IRC->firstcon = true;
  714.             $IRC->connected = true;
  715.             $IRC->pongwait = false;
  716.             $IRC->lastping = time();
  717.             send("USER " . $IRC->username . " * * :" . $IRC->realname);
  718.             send("NICK " . $IRC->joinnick);
  719.         }else {
  720.             sleep(30);
  721.             continue;
  722.         }
  723.     }
  724.     if((!$IRC->pongwait) && (time() - $IRC->lastping) > 30) {
  725.         $IRC->pongwait = true;
  726.         $IRC->pingsent = microtime(true);
  727.         send("PING :" . $IRC->server, $IRC->pingpong);
  728.     }
  729.     if($rawdata = fgets($IRC->fp, 1024)) {
  730.         parseMessage($rawdata);
  731.     }
  732.     if($rawdata = fgets(STDIN, 1024)) {
  733.         eval($rawdata);
  734.     }
  735.     return($rawdata);
  736. }
  737.  
  738. while(!$IRC->quit) {
  739.     usleep(10000);
  740.     loop();
  741. }
  742. if(!feof($IRC->fp)) {
  743.     send("QUIT :I couldn't think of a whitty quit message");
  744. }
  745. fclose($IRC->fp);
  746. fclose($IRC->logfile);
  747. mysql_close($IRC->con);
  748. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement