Advertisement
Guest User

Untitled

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