Advertisement
Guest User

Untitled

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