Advertisement
Guest User

Untitled

a guest
May 6th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 25.86 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 handleBotCommands() {
  246.     global $IRC;
  247.     if((startsWith($IRC->cM->mess, $IRC->cmdChar)) && (strlen($IRC->cM->mess) > strlen($IRC->cmdChar)) && (strtolower($IRC->cM->nick) != strtolower($IRC->cNick))) {
  248.         $userprivs = fetchmasters($IRC->cM->nick, true);
  249.         $userprivs = $userprivs[0] ? $userprivs[0] : 0;
  250.         $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
  251.         if(((!$IRC->locked) || ($userprivs > 49)) && ($userprivs >= 0)) {
  252.             $a = explode(" ", substr($IRC->cM->mess, strlen($IRC->cmdChar)));
  253.             $r = implodefrom(" ", $a, 1);
  254.             $reqprivs = fetchcmd($a[0], "privs");
  255.             $hasprivs = $userprivs >= $reqprivs;
  256.             $chan = ($ispm ? $IRC->cM->nick : $IRC->cM->param[0]);
  257.             eval(fetchcmd($a[0]));
  258.             if($userprivs >= 100) {
  259.                 $chan = ($ispm ? $IRC->cM->nick : $IRC->cM->param[0]);
  260.                 switch(strtoupper($a[0])) {
  261.                     case "BIND":
  262.                         switch(strtoupper($a[1])) {
  263.                             case "ADD":
  264.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 4))) || (!(is_numeric($a[3])))) {
  265.                                     break;
  266.                                 }
  267.                                 $p = min($a[3], 100);
  268.                                 send("PRIVMSG " . $chan . " :Binding command " . strtoupper($a[2]) . " with " . $p . " privs");
  269.                                 dumpDB("Binding command " . strtoupper($a[2]) . " with " . $p . " privs", fetchcmd(strtoupper($a[2])), $o);
  270.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  271.                                 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);
  272.                                 break;
  273.                             case "DEL":
  274.                             case "DELETE":
  275.                                 $co = fetchcmd($a[2]);
  276.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  277.                                 dumpDB("Deleted command " . strtoupper($a[2]), $co);
  278.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was deleted.");
  279.                                 break;
  280.                             case "REN":
  281.                             case "RENAME":
  282.                                 if((sizeof($a) < 4) ) {
  283.                                     break;
  284.                                 }
  285.                                 if($c = fetchcmd($a[2])) {
  286.                                     $b = fetchcmd($a[3]);
  287.                                     mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  288.                                 }
  289.                                 dumpDB("Renaming command \"" . strtoupper($a[2]) . "\" to \"" . strtoupper($a[3]) . "\"", $b ? $b : "");
  290.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET name='" . mysql_real_escape_string(strtoupper($a[3])) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  291.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
  292.                                 break;
  293.                             case "LNK":
  294.                             case "LINK":
  295.                                 if((sizeof($a) < 4) || (strtoupper($a[2]) == strtoupper($a[3]))) {
  296.                                     break;
  297.                                 }
  298.                                 send("PRIVMSG " . $chan . " :Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  299.                                 dumpDB("Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  300.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  301.                                 $p = fetchcmd($a[2], "privs");
  302.                                 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);
  303.                                 break;
  304.                             case "APP":
  305.                             case "APPEND":
  306.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
  307.                                     break;
  308.                                 }
  309.                                 send("PRIVMSG " . $chan . " :Appending command " . strtoupper($a[2]));
  310.                                 dumpDB("Appending command " . strtoupper($a[2]), $co = fetchcmd(strtoupper($a[2])), $o);
  311.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . mysql_real_escape_string($co . $o) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  312.                                 break;
  313.                             case "PRV":
  314.                             case "PRIVS":
  315.                                 if(sizeof($a) < 4) {
  316.                                     break;
  317.                                 }
  318.                                 $p = min($a[3], 100);
  319.                                 send("PRIVMSG " . $chan . " :Changing privlages of command " . strtoupper($a[2]) . " to " . $p);
  320.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET privs=" . $p . " WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  321.                                 break;
  322.                             case "UPD":
  323.                             case "UPDATE":
  324.                                 if((sizeof($a) < 5) || (!($o = implodefrom(" ", $a, 4))) || (!is_numeric($a[3]))) {
  325.                                     break;
  326.                                 }
  327.                                 $car = explode("\r\n", fetchcmd(strtoupper($a[2])));
  328.                                 send("PRIVMSG " . $chan . " :Updating line " . $a[3] . " of command " . strtoupper($a[2]));
  329.                                 dumpDB("Updating line " . $a[3] . " of command " . strtoupper($a[2]), $car[$a[3]], $o);
  330.                                 $car[$a[3]] = $o;
  331.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . mysql_real_escape_string(implode("\r\n", $car)) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  332.                                 break;
  333.                         }
  334.                         break;
  335.                     case "HOOK":
  336.                         switch(strtoupper($a[1])) {
  337.                             case "ADD":
  338.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
  339.                                     break;
  340.                                 }
  341.                                 send("PRIVMSG " . $chan . " :Adding hook " . strtoupper($a[2]));
  342.                                 mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  343.                                 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);
  344.                                 break;
  345.                             case "DEL":
  346.                             case "DELETE":
  347.                                 $co = explode(" ", implodefrom(" ", $a, 2));
  348.                                 foreach($co as $del) {
  349.                                     mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . mysql_real_escape_string($del) . "'", $IRC->con);
  350.                                 }
  351.                                 $s = ((sizeof($co) - 1) ? "s" : "");
  352.                                 send("PRIVMSG " . $chan . " :Hook" . $s . " '" . strtoupper($a[2]) . "' was deleted.");
  353.                                 break;
  354.                             case "REN":
  355.                             case "RENAME":
  356.                                 if((sizeof($a) < 4) ) {
  357.                                     break;
  358.                                 }
  359.                                 if($c = fetchcmd($a[2])) {
  360.                                     $b = fetchcmd($a[3]);
  361.                                     mysql_query("DELETE FROM " . $IRC->hTableName . " WHERE name='" . mysql_real_escape_string($a[3]) . "'", $IRC->con);
  362.                                 }
  363.                                 mysql_query("UPDATE " . $IRC->hTableName . " SET name='" . mysql_real_escape_string(strtoupper($a[3])) . "' WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  364.                                 send("PRIVMSG " . $chan . " :Hook '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
  365.                                 break;
  366.                             case "ACT":
  367.                             case "ACTIVATE":
  368.                                 if(sizeof($a) < 3) {
  369.                                     break;
  370.                                 }
  371.                                 switch(strtoupper($a[3])) {
  372.                                     case "OFF":
  373.                                     case "0":
  374.                                     case "FALSE":
  375.                                         $nstate = "0";
  376.                                         break;
  377.                                     default:
  378.                                         $nstate = "1";
  379.                                 }
  380.                                 send("PRIVMSG " . $chan . " :" . ($nstate ? "A" : "Dea") . "ctivating hook " . strtoupper($a[2]));
  381.                                 mysql_query("UPDATE " . $IRC->hTableName . " SET active=" . $nstate . " WHERE name='" . mysql_real_escape_string($a[2]) . "'");
  382.                                 break;
  383.                         }
  384.                         break;
  385.                 }
  386.             }
  387.         }
  388.     }
  389. }
  390.  
  391. function handleChatCommands() {
  392.     global $IRC;
  393.     if(!$IRC->cM->nick) {
  394.         return false;
  395.     }
  396.     $chr1 = chr(1);
  397.     if((startsWith($IRC->cM->mess[0], $chr1)) && ($IRC->cM->mess[strlen($IRC->cM->mess) - 1] == $chr1)) {
  398.         $a = explode(" ", $IRC->cM->mess);
  399.         $tok = implodefrom(" ", $a, 1);
  400.         $tok = trim(substr($tok, 0, strlen($tok) - 1));
  401.         if($tok) {
  402.             $tok2 = strtoupper(substr($a[0], 1));
  403.             if($tok2 != "ACTION") {
  404.                 $tokk = ": ";
  405.             }
  406.         }else {
  407.             $tok2 = strtoupper(substr($a[0], 1, strlen($tok2) - 1));
  408.         }
  409.         if($tok2 != "ACTION") {
  410.             output($IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] requested CTCP " . $tok2 . " from " . $IRC->cM->param[0] . $tokk . $tok);
  411.         }
  412.         switch($tok2) {
  413.             case "ACTION":
  414.                 output("[" . $IRC->cM->param[0] . "] * " . $IRC->cM->nick . " " . $tok);
  415.                 break;
  416.             case "PING":
  417.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "PING " . $tok . $chr1);
  418.                 break;
  419.             case "TIME":
  420.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "TIME " . date("D M jS H:i:s") . $chr1);
  421.                 break;
  422.             case "VERSION":
  423.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "VERSION Toot PHP Bot " . $IRC->version . $chr1);
  424.                 break;
  425.             case "FINGER":
  426.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "FINGER perv" . $chr1);
  427.                 break;
  428.         }
  429.         return true;
  430.     }
  431.     if(strtolower($IRC->cM->param[0]) != strtolower($IRC->cNick)) {
  432.         output("[" . $IRC->cM->param[0] . "] <" . $IRC->channames[strtolower($IRC->cM->param[0])][$IRC->cM->nick] . $IRC->cM->nick . "> " . $IRC->cM->mess);
  433.     }else {
  434.         output("-*" . $IRC->cM->nick . "*- " . $IRC->cM->mess);
  435.     }
  436.     if(startsWith($IRC->cM->mess, $IRC->cmdChar)) {
  437.         handleBotCommands();
  438.     }
  439.     return true;
  440. }
  441.  
  442. function handleNormalCommands() {
  443.     global $IRC;
  444.     $h = true;
  445.     switch(strtoupper(trim($IRC->cM->cmd))) {
  446.         case "KICK":
  447.             if(strtolower($IRC->cM->param[1]) == strtolower($IRC->cNick)) {
  448.                 $who = "You were";
  449.             }else {
  450.                 $who = $IRC->cM->param[1] . " was";
  451.             }
  452.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->param[1]]);
  453.             output("-!- " . $who . " kicked from " . $IRC->cM->param[0] . " by " . $IRC->cM->nick . " [" . $IRC->cM->mess . "]");
  454.             if($IRC->cM->param[1] == $IRC->cNick) {
  455.                 send("JOIN :" . $IRC->cM->param[0]);
  456.             }
  457.             break;
  458.         case "MODE":
  459.             if(strtolower($IRC->cNick) == strtolower($IRC->cM->param[0])) {
  460.                 $o = $IRC->cM->mess;
  461.             }else {
  462.                 $o = implodefrom(" ", $IRC->cM->param, 1);
  463.             }
  464.             if($IRC->cM->host) {
  465.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->nick);
  466.             }else {
  467.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->host);
  468.             }
  469.             break;
  470.         case "NOTICE":
  471.             output("-" . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "]- " . $IRC->cM->mess);
  472.             break;
  473.         case "PRIVMSG":
  474.             $h = handleChatCommands();
  475.             break;
  476.         case "PART":
  477.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has left " . $IRC->cM->param[0] . " [" . $IRC->cM->mess . "]");
  478.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->nick]);
  479.             break;
  480.         case "JOIN":
  481.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has joined " . $IRC->cM->mess);
  482.             $IRC->channicks[strtolower($IRC->cM->mess)][$IRC->cM->nick] = " ";
  483.             break;
  484.         case "QUIT":
  485.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has quit [" . $IRC->cM->mess . "]");
  486.             $a = array_keys($IRC->channames);
  487.             for($i = 0; $i < sizeof($a); $i++) {
  488.                 unset($IRC->channames[$a[$i]][$IRC->cM->nick]);
  489.             }
  490.             break;
  491.         case "NICK":
  492.             if(strtolower($IRC->cM->nick) == strtolower($IRC->cNick)) {
  493.                 $who = "You are";
  494.                 $IRC->cNick = $IRC->cM->mess;
  495.             }else {
  496.                 $who = $IRC->cM->nick . " is";
  497.             }
  498.             output("-!- " . $who . " now known as " . $IRC->cM->mess);
  499.             $aa = array_keys($IRC->channames);
  500.             for($i = 0; $i < sizeof($aa); $i++) {
  501.                 $IRC->channames[$aa[$i]][$IRC->cM->mess] = $IRC->channames[$aa[$i]][$IRC->cM->nick];
  502.                 unset($IRC->channames[$aa[$i]][$IRC->cM->nick]);
  503.             }
  504.             addmasters(array($IRC->cM->nick), array($IRC->cM->mess), true, true);
  505.             break;
  506.         case "TOPIC":
  507.             output("-!- " . $IRC->cM->nick . " changed the topic of " . $IRC->cM->param[0] . " to: " . $IRC->cM->mess);
  508.             break;
  509.         case "PONG":
  510.             if($IRC->pingpong) {
  511.                 output("-!- Server PONG reply from " . $IRC->cM->nick . ": " . $IRC->cM->param[0] . "; Time since PING request sent: " . (microtime(true) - $IRC->pingsent));
  512.             }
  513.             $IRC->lastping = time();
  514.             $IRC->pongwait = false;
  515.             break;
  516.         default:
  517.             $h = false;
  518.     }
  519.     $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
  520.     $q = mysql_query("SELECT * FROM " . $IRC->hTableName, $IRC->con);
  521.     while($row = mysql_fetch_array($q)) {
  522.         if($row["active"]) {
  523.             eval($row["code"]);
  524.         }
  525.     }
  526.     return($h);
  527. }
  528.  
  529. function handleNumberCommands() {
  530.     global $IRC;
  531.     switch($IRC->cM->cmd) {
  532.         case "001":
  533.             $IRC->server = $IRC->cM->nick;
  534.             output($IRC->cM->mess);
  535.             $IRC->cNick = trim(substr($IRC->cM->mess, strrpos($IRC->cM->mess, " ")));
  536.             send("PRIVMSG NickServ :ID " . $IRC->auth);
  537.             foreach($IRC->joinchan as $c) {
  538.                 send("JOIN " . $c);
  539.             }
  540.             $IRC->firstcon = false;
  541.             break;
  542.         case "004":
  543.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  544.             break;
  545.         case "005":
  546.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  547.             if($a = substr($IRC->cM->mess, $p = (strpos(strtoupper($IRC->cM->mess), "PREFIX=") + 8), strpos($IRC->cM->mess, " ", $p) - $p)) {
  548.                 $aa = explode(")", $a);
  549.                 $IRC->powers = array(array(), array());
  550.                 for($i = 0; $i < strlen($aa[0]); $i++) {
  551.                     $IRC->powers[0][] = $aa[0][$i];
  552.                 }
  553.                 for($i = 0; $i < strlen($aa[1]); $i++) {
  554.                     $IRC->powers[1][] = $aa[1][$i];
  555.                 }
  556.             }
  557.         case "252":
  558.         case "254":
  559.             output(trim($IRC->cM->param[0], $IRC->cNick . " ") . " " . $IRC->cM->mess);
  560.             break;
  561.         case "332":
  562.             output("Topic for " . $IRC->cM->param[1] . " is: " . $IRC->cM->mess);
  563.             break;
  564.         case "333":
  565.             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]));
  566.             break;
  567.         case "353":
  568.             $cc = $IRC->cM->param[2];
  569.             $c = strtolower($cc);
  570.             $ndat = $IRC->cM->wline . "\n";
  571.             while(!instr("366 " . $IRC->cNick . " " . $c, $ndat)) {
  572.                 if($rd = fgets($IRC->fp, 1024)) {
  573.                     $ndat .= $rd;
  574.                 }
  575.             }
  576.             unset($IRC->tempnames[$c]);
  577.             $IRC->tempnames[$c] = "";
  578.             $e = explode("\n", $ndat);
  579.             foreach($e as $a) {
  580.                 parseMessage(trim($a), false);
  581.                 if($IRC->cM->cmd != "353") {
  582.                     break;
  583.                 }
  584.                 $IRC->tempnames[$c] .= $IRC->cM->mess;
  585.                 $IRC->cM->mess = "";
  586.             }
  587.             $names = explode(" ", $IRC->tempnames[$c]);
  588.             unset($IRC->tempnames[$c]);
  589.             sort($names);
  590.             output("[Users " . $cc . "]", false);
  591.             output("", false, false);
  592.             for($i = 0; $i < sizeof($names); $i++) {
  593.                 if(in_array($names[$i][0], $IRC->powers[1])) {
  594.                     $IRC->channames[$c][substr($names[$i], 1)] = $names[$i][0];
  595.                 }else {
  596.                     $IRC->channames[$c][$names[$i]] = " ";
  597.                 }
  598.                 if(($i % 5) == 0) {
  599.                     output("", true, false);
  600.                     output("", false);
  601.                 }
  602.                 output("[" . str_pad($IRC->channames[$c][$names[$i]] . $names[$i], 17, " ", STR_PAD_RIGHT) . "] ", false, false);
  603.             }
  604.             output("", true, false);
  605.             output("-!- " . $cc . ": Total of " . sizeof($names) . " nicks");
  606.             break;
  607.         case "421":
  608.             output("Unknown command: \"" . $IRC->cM->param[1] . "\"");
  609.             break;
  610.         case '433':
  611.             output("Nickname \"" . $IRC->cM->param[1] . "\" is already in use.");
  612.             if($IRC->firstcon) {
  613.                 send("NICK " . $IRC->joinnick . rand(100, 999));
  614.                 send("PRIVMSG NickServ :ghost " . $IRC->joinnick . " " . $IRC->auth);
  615.             }          
  616.             break;
  617.         case '474':
  618.             output('Unable to join channel ' . $IRC->cM->param[1] . ': ' . $IRC->cM->mess);
  619.             break;
  620.         default:
  621.             output($IRC->cM->mess);
  622.             break;
  623.     }
  624.     return true;
  625. }
  626.  
  627. function parseMessage($buffer, $handle = true) {
  628.     global $IRC;
  629.     $buffer = explode("\n", $buffer);
  630.     $IRC->cM->wline = $buffer = trim($buffer[0], "\r");
  631.     if(!trim($buffer)) {
  632.         return;
  633.     }
  634.     if($IRC->debug) {
  635.         output($buffer);
  636.     }
  637.     switch($s = startsWith(strtoupper($IRC->cM->wline), array(":", "NOTICE", "PING", "ERROR"))) {
  638.         case ":":
  639.             //*
  640.             $IRC->cM->sender = substr(strtok($IRC->cM->wline, " "), 1);
  641.             $IRC->cM->cmd = strtok(" ");
  642.             $IRC->cM->param = explode(" ", trim(strtok(" " . strtok(""), ":")));
  643.             $IRC->cM->mess = strtok("");
  644.             $IRC->cM->nick = strtok($IRC->cM->sender, "!");
  645.             $IRC->cM->user = strtok("@");
  646.             $IRC->cM->host = strtok("");
  647.             //*/
  648.             if($handle) {
  649.                 if(is_numeric($IRC->cM->cmd)) {
  650.                     if(handleNumberCommands()) {
  651.                         break;
  652.                     }
  653.                 }
  654.                 if(handleNormalCommands()) {
  655.                     break;
  656.                 }
  657.                 output("Unknown message: " . $IRC->cM->wline);
  658.             }
  659.             break;
  660.         case "NOTICE":
  661.             $IRC->cM->mess = substr($IRC->cM->wline, strpos($IRC->cM->wline, ":") + 1);
  662.             $ss = $IRC->server;
  663.             if(startsWith(strtoupper($IRC->cM->wline), "NOTICE AUTH")) {
  664.                 $ss = $IRC->nick;
  665.             }
  666.             output("!" . $ss . " " . $IRC->cM->mess);
  667.             break;
  668.         case "PING":
  669.             if($IRC->pingpong) {
  670.                 output($buffer);
  671.             }
  672.             $IRC->cM->wline[1] = "O";
  673.             send($IRC->cM->wline, $IRC->pingpong);
  674.             break;
  675.         case "ERROR":
  676.             output($IRC->cM->wline);
  677.             break;
  678.         default:
  679.             output("Unknown message: " . $IRC->cM->wline);
  680.             break;
  681.     }
  682. }
  683.  
  684. $IRC->connected = false;
  685. $IRC->lastping = time();
  686. $IRC->pongwait = false;
  687. $IRC->pingsent = microtime(true);
  688. $IRC->masterNick = fetchmasters();
  689. stream_set_blocking(STDIN, 0);
  690.  
  691. function loop() {
  692.     global $IRC;
  693.     if((($IRC->pongwait) && ((time() - $IRC->lastping) > 240)) || (((!$IRC->connected) || (feof($IRC->fp)) && (!($IRC->quit))))) {
  694.         if($IRC->connected) {
  695.             if(!feof($IRC->fp)) {
  696.                 send("QUIT :Lost connection");
  697.             }
  698.             fclose($IRC->fp);
  699.         }
  700.         $IRC->connected = false;
  701.         output("-!- Attempting to open connection to " . $IRC->host . ":" . $IRC->port);
  702.         if($IRC->fp = fsockopen($IRC->host, $IRC->port, $errno, $errdesc)) {
  703.             output("-!- Connection established to " . $IRC->host . ":" . $IRC->port);
  704.             stream_set_timeout($IRC->fp, 0, 1);
  705.             $IRC->firstcon = true;
  706.             $IRC->connected = true;
  707.             $IRC->pongwait = false;
  708.             $IRC->lastping = time();
  709.             send("USER " . $IRC->username . " * * :" . $IRC->realname);
  710.             send("NICK " . $IRC->joinnick);
  711.         }else {
  712.             sleep(30);
  713.             continue;
  714.         }
  715.     }
  716.     if((!$IRC->pongwait) && (time() - $IRC->lastping) > 30) {
  717.         $IRC->pongwait = true;
  718.         $IRC->pingsent = microtime(true);
  719.         send("PING :" . $IRC->server, $IRC->pingpong);
  720.     }
  721.     if($rawdata = fgets($IRC->fp, 1024)) {
  722.         parseMessage($rawdata);
  723.     }
  724.     if($rawdata1 = fgets(STDIN, 1024)) {
  725.         eval($rawdata1);
  726.     }
  727.     return($rawdata);
  728. }
  729.  
  730. while(!$IRC->quit) {
  731.     usleep(10000);
  732.     loop();
  733. }
  734. if(!feof($IRC->fp)) {
  735.     send("QUIT :I couldn't think of a whitty quit message");
  736. }
  737. fclose($IRC->fp);
  738. fclose($IRC->logfile);
  739. mysql_close($IRC->con);
  740. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement