Advertisement
Guest User

Untitled

a guest
May 5th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.08 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.  
  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.     $q = mysql_query("SELECT " . ($n = ($privs ? "privs" : "name")) . " FROM " . ($active ? $IRC->mTableName : $IRC->iTableName) . ($name ? " WHERE name='" . $name . "'" : ""), $IRC->con);
  175.     $r = array();
  176.     while($row = mysql_fetch_array($q)) {
  177.         $r[] = $row[$n];
  178.     }
  179.     return($r);
  180. }
  181.  
  182. function removemasters($names, $all = false, $active = true) {
  183.     global $IRC;
  184.     if($active) {
  185.         $t = $IRC->mTableName;
  186.     }else {
  187.         $t = $IRC->iTableName;
  188.     }
  189.     if(!$all) {
  190.         if(!is_array($names)) {
  191.             $names = array($names);
  192.         }
  193.         foreach($names as $n) {
  194.             mysql_query("DELETE FROM " . $t . " WHERE name='" . $n . "'", $IRC->con);
  195.         }
  196.     }else {
  197.         mysql_query("DELETE FROM " . $t, $IRC->con);
  198.     }
  199. }
  200.  
  201. function addmasters($names, $privs, $active = true, $upd = false) {
  202.     global $IRC;
  203.     if(sizeof($names) != sizeof($privs)) {
  204.         $p = $privs;
  205.         $privs = array();
  206.         for($i = 0; $i < sizeof($names); $i++) {
  207.             $privs[$i] = $p;
  208.         }
  209.     }
  210.     if($active) {
  211.         $t = $IRC->mTableName;
  212.     }else {
  213.         $t = $IRC->iTableName;
  214.     }
  215.     if(!$upd) {
  216.         removemasters($names);
  217.     }
  218.     for($i = 0; $i < sizeof($names); $i++) {
  219.         if(!$upd) {
  220.             mysql_query("INSERT INTO " . $t . "(name, privs) VALUES ('" . $names[$i] . "', " . min($privs[$i], 100) . ")", $IRC->con);
  221.         }else {
  222.             mysql_query("UPDATE " . $t . " SET name='" . $privs[$i] . "' WHERE name='" . $names[$i] . "'", $IRC->con);
  223.         }
  224.     }
  225. }
  226.  
  227. function swapmasters($names) {
  228.     global $IRC;
  229.     if(!is_array($names)) {
  230.         $names = array($names);
  231.     }
  232.     foreach($names as $n) {
  233.         $r = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con));
  234.         mysql_query("DELETE FROM " . $IRC->mTableName . " WHERE name='" . $n . "'", $IRC->con);
  235.         mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $r["name"] . "', " . $r["privs"] . ")", $IRC->con);
  236.         mysql_query("DELETE FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con);
  237.     }
  238. }
  239.  
  240. function onConnect() {
  241.     global $IRC;
  242.     send("USER " . $IRC->username . " * * :" . $IRC->realname);
  243.     send("NICK " . $IRC->joinnick);
  244.     $IRC->cNick = $IRC->joinnick;
  245.     if($IRC->auth) {
  246.         send("PRIVMSG NickServ :ID " . $IRC->joinnick . " " . $IRC->auth);
  247.     }
  248.     foreach($IRC->joinchan as $c) {
  249.         send("JOIN " . $c);
  250.     }
  251. }
  252.  
  253. function handleBotCommands() {
  254.     global $IRC;
  255.     if((startsWith($IRC->cM->mess, $IRC->cmdChar)) && (strlen($IRC->cM->mess) > strlen($IRC->cmdChar)) && (strtolower($IRC->cM->nick) != strtolower($IRC->cNick))) {
  256.         $userprivs = fetchmasters($IRC->cM->nick, true);
  257.         $userprivs = $userprivs[0] ? $userprivs[0] : 0;
  258.         $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
  259.         if(((!$IRC->locked) || ($userprivs > 49)) && ($userprivs >= 0)) {
  260.             $a = explode(" ", substr($IRC->cM->mess, strlen($IRC->cmdChar)));
  261.             $r = implodefrom(" ", $a, 1);
  262.             $reqprivs = fetchcmd($a[0], "privs");
  263.             $hasprivs = $userprivs >= $reqprivs;
  264.             eval(fetchcmd($a[0]));
  265.             if($userprivs >= 100) {
  266.                 switch(strtoupper($a[0])) {
  267.                     case "BIND":
  268.                         if($ispm) {
  269.                             $chan = $IRC->cM->nick;
  270.                         }else {
  271.                             $chan = $IRC->cM->param[0];
  272.                         }
  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 ('" . strtoupper($a[2]) . "', '" . $o . "', " . $p . ")", $IRC->con);
  283.                                 break;
  284.                             case "DEL":
  285.                                 $co = fetchcmd($a[2]);
  286.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  287.                                 dumpDB("Deleted command " . strtoupper($a[2]), $co);
  288.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was deleted.");
  289.                                 break;
  290.                             case "REN":
  291.                                 if((sizeof($a) < 4) ) {
  292.                                     break;
  293.                                 }
  294.                                 if($c = fetchcmd($a[2])) {
  295.                                     $b = fetchcmd($a[3]);
  296.                                     mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  297.                                 }
  298.                                 dumpDB("Renaming command \"" . strtoupper($a[2]) . "\" to \"" . strtoupper($a[3]) . "\"", $b ? $b : "");
  299.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET name='" . strtoupper($a[3]) . "' WHERE name='" . $a[2] . "'");
  300.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
  301.                                 break;
  302.                             case "LNK":
  303.                                 if((sizeof($a) < 4) || (strtoupper($a[2]) == strtoupper($a[3]))) {
  304.                                     break;
  305.                                 }
  306.                                 send("PRIVMSG " . $chan . " :Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  307.                                 dumpDB("Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  308.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  309.                                 $p = fetchcmd($a[2], "privs");
  310.                                 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);
  311.                                 break;
  312.                             case "APP":
  313.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
  314.                                     break;
  315.                                 }
  316.                                 send("PRIVMSG " . $chan . " :Appending command " . strtoupper($a[2]));
  317.                                 dumpDB("Appending command " . strtoupper($a[2]), $co = fetchcmd(strtoupper($a[2])), $o);
  318.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . $co . $o . "' WHERE name='" . $a[2] . "'");
  319.                                 break;
  320.                             case "PRV":
  321.                                 if(sizeof($a) < 4) {
  322.                                     break;
  323.                                 }
  324.                                 $p = min($a[3], 100);
  325.                                 send("PRIVMSG " . $chan . " :Changing privlages of command " . strtoupper($a[2]) . " to " . $p);
  326.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET privs=" . $p . " WHERE name='" . $a[2] . "'");
  327.                                 break;
  328.                             case "UPD":
  329.                                 if((sizeof($a) < 5) || (!($o = implodefrom(" ", $a, 4))) || (!is_numeric($a[3]))) {
  330.                                     break;
  331.                                 }
  332.                                 $car = explode("\r\n", fetchcmd(strtoupper($a[2])));
  333.                                 send("PRIVMSG " . $chan . " :Updating line " . $a[3] . " of command " . strtoupper($a[2]));
  334.                                 dumpDB("Updating line " . $a[3] . " of command " . strtoupper($a[2]), $car[$a[3]], $o);
  335.                                 $car[$a[3]] = $o;
  336.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . implode("\r\n", $car) . "' WHERE name='" . $a[2] . "'");
  337.                                 break;
  338.                         }
  339.                         break;
  340.                 }
  341.             }
  342.         }
  343.     }
  344. }
  345.  
  346. function handleChatCommands() {
  347.     global $IRC;
  348.     if(!$IRC->cM->nick) {
  349.         return false;
  350.     }
  351.     $chr1 = chr(1);
  352.     if((startsWith($IRC->cM->mess[0], $chr1)) && ($IRC->cM->mess[strlen($IRC->cM->mess) - 1] == $chr1)) {
  353.         $a = explode(" ", $IRC->cM->mess);
  354.         $tok = implodefrom(" ", $a, 1);
  355.         $tok = trim(substr($tok, 0, strlen($tok) - 1));
  356.         if($tok) {
  357.             $tok2 = strtoupper(substr($a[0], 1));
  358.             if($tok2 != "ACTION") {
  359.                 $tokk = ": ";
  360.             }
  361.         }else {
  362.             $tok2 = strtoupper(substr($a[0], 1, strlen($tok2) - 1));
  363.         }
  364.         if($tok2 != "ACTION") {
  365.             output($IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] requested CTCP " . $tok2 . " from " . $IRC->cM->param[0] . $tokk . $tok);
  366.         }
  367.         switch($tok2) {
  368.             case "ACTION":
  369.                 output("[" . $IRC->cM->param[0] . "] * " . $IRC->cM->nick . " " . $tok);
  370.                 break;
  371.             case "PING":
  372.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "PING " . $tok . $chr1);
  373.                 break;
  374.             case "TIME":
  375.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "TIME " . date("D M jS H:i:s") . $chr1);
  376.                 break;
  377.             case "VERSION":
  378.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "VERSION Toot PHP Bot " . $IRC->version . $chr1);
  379.                 break;
  380.             case "FINGER":
  381.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "FINGER wat" . $chr1);
  382.                 break;
  383.         }
  384.         return true;
  385.     }
  386.     if(strtolower($IRC->cM->param[0]) != strtolower($IRC->cNick)) {
  387.         output("[" . $IRC->cM->param[0] . "] <" . $IRC->channames[strtolower($IRC->cM->param[0])][$IRC->cM->nick] . $IRC->cM->nick . "> " . $IRC->cM->mess);
  388.     }else {
  389.         output("-*" . $IRC->cM->nick . "*- " . $IRC->cM->mess);
  390.     }
  391.     if(startsWith($IRC->cM->mess, $IRC->cmdChar)) {
  392.         handleBotCommands();
  393.     }
  394.     return true;
  395. }
  396.  
  397. function handleNormalCommands() {
  398.     global $IRC;
  399.     switch(strtoupper(trim($IRC->cM->cmd))) {
  400.         case "KICK":
  401.             if(strtolower($IRC->cM->param[1]) == strtolower($IRC->cNick)) {
  402.                 $who = "You were";
  403.             }else {
  404.                 $who = $IRC->cM->param[1] . " was";
  405.             }
  406.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->param[1]]);
  407.             output("-!- " . $who . " kicked from " . $IRC->cM->param[0] . " by " . $IRC->cM->nick . " [" . $IRC->cM->mess . "]");
  408.             if($IRC->cM->param[1] == $IRC->cNick) {
  409.                 send("JOIN :" . $IRC->cM->param[0]);
  410.             }
  411.             break;
  412.         case "MODE":
  413.             if(strtolower($IRC->cNick) == strtolower($IRC->cM->param[0])) {
  414.                 $o = $IRC->cM->mess;
  415.             }else {
  416.                 $o = implodefrom(" ", $IRC->cM->param, 1);
  417.             }
  418.             if($IRC->cM->host) {
  419.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->nick);
  420.             }else {
  421.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->host);
  422.             }
  423.             break;
  424.         case "NOTICE":
  425.             output("-" . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "]- " . $IRC->cM->mess);
  426.             break;
  427.         case "PRIVMSG":
  428.             return handleChatCommands();
  429.         case "PART":
  430.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has left " . $IRC->cM->param[0] . " [" . $IRC->cM->mess . "]");
  431.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->nick]);
  432.             break;
  433.         case "JOIN":
  434.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has joined " . $IRC->cM->mess);
  435.             $IRC->channicks[strtolower($IRC->cM->mess)][$IRC->cM->nick] = " ";
  436.             break;
  437.         case "QUIT":
  438.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has quit [" . $IRC->cM->mess . "]");
  439.             $a = array_keys($IRC->channames);
  440.             for($i = 0; $i < sizeof($a); $i++) {
  441.                 unset($IRC->channames[$a[$i]][$IRC->cM->nick]);
  442.             }
  443.             break;
  444.         case "NICK":
  445.             if(strtolower($IRC->cM->nick) == strtolower($IRC->cNick)) {
  446.                 $who = "You are";
  447.                 $IRC->cNick = $IRC->cM->mess;
  448.             }else {
  449.                 $who = $IRC->cM->nick . " is";
  450.             }
  451.             output("-!- " . $who . " now known as " . $IRC->cM->mess);
  452.             $aa = array_keys($IRC->channames);
  453.             for($i = 0; $i < sizeof($aa); $i++) {
  454.                 $IRC->channames[$aa[$i]][$IRC->cM->mess] = $IRC->channames[$aa[$i]][$IRC->cM->nick];
  455.                 unset($IRC->channames[$aa[$i]][$IRC->cM->nick]);
  456.             }
  457.             addmasters(array($IRC->cM->nick), array($IRC->cM->mess), true, true);
  458.             break;
  459.         case "TOPIC":
  460.             output("-!- " . $IRC->cM->nick . " changed the topic of " . $IRC->cM->param[0] . " to: " . $IRC->cM->mess);
  461.             break;
  462.         case "PONG":
  463.             if($IRC->pingpong) {
  464.                 output("-!- Server PONG reply from " . $IRC->cM->nick . ": " . $IRC->cM->param[0] . "; Time since PING request sent: " . (microtime(true) - $IRC->pingsent));
  465.             }
  466.             $IRC->lastping = time();
  467.             $IRC->pongwait = false;
  468.             break;
  469.         default:
  470.             return false;
  471.     }
  472.     return true;
  473. }
  474.  
  475. function handleNumberCommands() {
  476.     global $IRC;
  477.     switch($IRC->cM->cmd) {
  478.         case "001":
  479.             $IRC->server = $IRC->cM->nick;
  480.             output($IRC->cM->mess);
  481.             send("NICK " . $IRC->joinnick);
  482.             send("PRIVMSG NickServ :ID " . $IRC->joinnick . " " . $IRC->auth);
  483.             foreach($IRC->joinchan as $c) {
  484.                 send("JOIN " . $c);
  485.             }
  486.             break;
  487.         case "004":
  488.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  489.             break;
  490.         case "005":
  491.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  492.             if($a = substr($IRC->cM->mess, $p = (strpos(strtoupper($IRC->cM->mess), "PREFIX=") + 8), strpos($IRC->cM->mess, " ", $p) - $p)) {
  493.                 $aa = explode(")", $a);
  494.                 $IRC->powers = array(array(), array());
  495.                 for($i = 0; $i < strlen($aa[0]); $i++) {
  496.                     $IRC->powers[0][] = $aa[0][$i];
  497.                 }
  498.                 for($i = 0; $i < strlen($aa[1]); $i++) {
  499.                     $IRC->powers[1][] = $aa[1][$i];
  500.                 }
  501.             }
  502.         case "252":
  503.         case "254":
  504.             output(trim($IRC->cM->param[0], $IRC->cNick . " ") . " " . $IRC->cM->mess);
  505.             break;
  506.         case "332":
  507.             output("Topic for " . $IRC->cM->param[1] . " is: " . $IRC->cM->mess);
  508.             break;
  509.         case "333":
  510.             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]));
  511.             break;
  512.         case "353":
  513.             $cc = $IRC->cM->param[2];
  514.             $c = strtolower($cc);
  515.             $ndat = $IRC->cM->wline . "\n";
  516.             while(!instr("366 " . $IRC->cNick . " " . $c, $ndat, true)) {
  517.                 if($rd = fgets($IRC->fp, 1024)) {
  518.                     $ndat .= $rd;
  519.                 }
  520.             }
  521.             unset($IRC->tempnames[$c]);
  522.             $IRC->tempnames[$c] = "";
  523.             $e = explode("\n", $ndat);
  524.             foreach($e as $a) {
  525.                 parseMessage(trim($a), false);
  526.                 if($IRC->cM->cmd != "353") {
  527.                     break;
  528.                 }
  529.                 $IRC->tempnames[$c] .= $IRC->cM->mess;
  530.                 $IRC->cM->mess = "";
  531.             }
  532.             $names = explode(" ", $IRC->tempnames[$c]);
  533.             unset($IRC->tempnames[$c]);
  534.             sort($names);
  535.             output("[Users " . $cc . "]", false);
  536.             output("", false, false);
  537.             for($i = 0; $i < sizeof($names); $i++) {
  538.                 if(in_array($names[$i][0], $IRC->powers[1])) {
  539.                     $IRC->channames[$c][substr($names[$i], 1)] = $names[$i][0];
  540.                 }else {
  541.                     $IRC->channames[$c][$names[$i]] = " ";
  542.                 }
  543.                 if(($i % 5) == 0) {
  544.                     output("", true, false);
  545.                     output("", false);
  546.                 }
  547.                 output("[" . str_pad($IRC->channames[$c][$names[$i]] . $names[$i], 17, " ", STR_PAD_RIGHT) . "] ", false, false);
  548.             }
  549.             output("", true, false);
  550.             output("-!- " . $cc . ": Total of " . sizeof($names) . " nicks");
  551.             break;
  552.         case "421":
  553.             output("Unknown command: \"" . $IRC->cM->param[1] . "\"");
  554.             break;
  555.         case '433':
  556.             output("Nickname \"" . $IRC->cM->param[1] . "\" is already in use.");
  557.             if($IRC->firstcon) {
  558.                 send("NICK " . $IRC->joinnick . rand(100, 999));
  559.                 send("PRIVMSG NickServ :ghost " . $IRC->joinnick . " " . $IRC->auth);
  560.             }          
  561.             break;
  562.         case '474':
  563.             output('Unable to join channel ' . $IRC->cM->param[1] . ': ' . $IRC->cM->mess);
  564.             break;
  565.         default:
  566.             output($IRC->cM->mess);
  567.             break;
  568.     }
  569.     return true;
  570. }
  571.  
  572. function parseMessage($buffer, $handle = true) {
  573.     global $IRC;
  574.     $buffer = explode("\n", $buffer);
  575.     $IRC->cM->wline = $buffer = trim($buffer[0], "\r");
  576.     if(!trim($buffer)) {
  577.         return;
  578.     }
  579.     if($IRC->debug) {
  580.         output($buffer);
  581.     }
  582.     switch($s = startsWith(strtoupper($IRC->cM->wline), array(":", "NOTICE", "PING", "ERROR"))) {
  583.         case ":":
  584.             $a = explode(" ", $IRC->cM->wline);
  585.             $IRC->cM->sender = substr($a[0], 1);
  586.             $aa = explode("!", $IRC->cM->sender);
  587.             $IRC->cM->nick = $aa[0];
  588.             $aa = explode("@", implodefrom(" ", $aa, 1));
  589.             $IRC->cM->user = $aa[0];
  590.             $IRC->cM->host = $aa[1];
  591.             $IRC->cM->cmd = $a[1];
  592.             $aa = explode(":", implodefrom(" ", $a, 2));
  593.             $IRC->cM->param = explode(" ", trim($aa[0]));
  594.             $IRC->cM->mess = implodefrom(":", $aa, 1);
  595.             if($handle) {
  596.                 if(is_numeric($IRC->cM->cmd)) {
  597.                     if(handleNumberCommands()) {
  598.                         break;
  599.                     }
  600.                 }
  601.                 if(handleNormalCommands()) {
  602.                     break;
  603.                 }
  604.                 output("Unknown message: " . $IRC->cM->wline);
  605.             }
  606.             break;
  607.         case "NOTICE":
  608.             $IRC->cM->mess = substr($IRC->cM->wline, strpos($IRC->cM->wline, ":") + 1);
  609.             $ss = $IRC->server;
  610.             if(startsWith(strtoupper($IRC->cM->wline), "NOTICE AUTH")) {
  611.                 $ss = $IRC->nick;
  612.             }
  613.             output("!" . $ss . " " . $IRC->cM->mess);
  614.             break;
  615.         case "PING":
  616.             if($IRC->pingpong) {
  617.                 output($buffer);
  618.             }
  619.             $IRC->cM->wline[1] = "O";
  620.             send($IRC->cM->wline, $IRC->pingpong);
  621.             break;
  622.         case "ERROR":
  623.             output($IRC->cM->wline);
  624.             break;
  625.         default:
  626.             output("Unknown message: " . $IRC->cM->wline);
  627.             break;
  628.     }
  629. }
  630.  
  631. $IRC->connected = false;
  632. $IRC->lastping = time();
  633. $IRC->pongwait = false;
  634. $IRC->pingsent = microtime(true);
  635. $IRC->masterNick = fetchmasters();
  636.  
  637. while(!$IRC->quit) {
  638.     usleep(10000);
  639.     if((($IRC->pongwait) && ((time() - $IRC->lastping) > 300)) || (((!$IRC->connected) || (feof($IRC->fp)) && (!($IRC->quit))))) {
  640.         if($IRC->connected) {
  641.             if(!feof($IRC->fp)) {
  642.                 send("QUIT :Lost connection");
  643.             }
  644.             fclose($IRC->fp);
  645.         }
  646.         $IRC->connected = false;
  647.         output("-!- Attempting to open connection to " . $IRC->host . ":" . $IRC->port);
  648.         if($IRC->fp = fsockopen($IRC->host, $IRC->port, $errno, $errdesc)) {
  649.             output("-!- Connection established to " . $IRC->host . ":" . $IRC->port);
  650.             stream_set_timeout($IRC->fp, 0, 1);
  651.             $IRC->firstcon = true;
  652.             $IRC->connected = true;
  653.             $IRC->pongwait = false;
  654.             $IRC->lastping = time();
  655.             send("USER " . $IRC->username . " * * :" . $IRC->realname);
  656.             send("NICK " . $IRC->joinnick);
  657.         }else {
  658.             sleep(30);
  659.             continue;
  660.         }
  661.     }
  662.     if((!$IRC->pongwait) && (time() - $IRC->lastping) > 30) {
  663.         $IRC->pongwait = true;
  664.         $IRC->pingsent = microtime(true);
  665.         send("PING :" . $IRC->server, $IRC->pingpong);
  666.     }
  667.     if($rawdata = fgets($IRC->fp, 1024)) {
  668.         parseMessage($rawdata);
  669.     }
  670.    
  671. }
  672. if(!feof($IRC->fp)) {
  673.     send("QUIT :I couldn't think of a whitty quit message");
  674. }
  675. fclose($IRC->fp);
  676. fclose($IRC->logfile);
  677. mysql_close($IRC->con);
  678. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement