Advertisement
Guest User

Untitled

a guest
May 8th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.43 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. /*
  6.  
  7. TODO:
  8.  
  9. 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)
  10.  
  11. ^ basicly if there's no way to verify they'll be using the bot, move them over
  12.  
  13. if an inactive master joins/nicks, readd to active masters list (perhaps auth with a user-generated password?)
  14.  
  15. i probably should comment more stuff, it's getting kinda complex; would hate to have to rewrite/relearn
  16.  
  17. */
  18.  
  19.  
  20.  
  21. class aMessage {
  22.  
  23.     var $nick, $user, $host, $sender, $cmd, $param, $mess, $wline;
  24.  
  25. }
  26.  
  27.  
  28.  
  29. class aIRC {
  30.  
  31.     var $cM;
  32.  
  33.     var $lastping, $pongwait, $pingsent, $pingpong;
  34.  
  35.     var $host, $fp, $port, $logfile, $con, $server, $cmdChar;
  36.  
  37.     var $cNick, $joinnick, $username, $realname, $joinchan, $auth, $authnick, $masterNick;
  38.  
  39.     var $channames, $powers, $tempnames, $cnameslist;
  40.  
  41.     var $debug, $firstcon, $locked, $quit, $version;
  42.  
  43.     var $mTableName, $cTableName, $iTableName, $MySQLServer, $MySQLDatabase, $MySQLAccountName, $MySQLAccountpassword;
  44.  
  45. }
  46.  
  47.  
  48.  
  49. $IRC = new aIRC;
  50.  
  51. $IRC->cM = new aMessage;
  52.  
  53. $IRC->cM->param = array();
  54.  
  55. $IRC->host = "irc.dal.net";
  56.  
  57. $IRC->port = 6667;
  58.  
  59. $IRC->joinchan = array("#Tootoot222", "#mixster", "#SRL-School", "#SRL");
  60.  
  61. $IRC->joinnick = "Furry";
  62.  
  63. $IRC->username = "Furry";
  64.  
  65. $IRC->realname = "Furry";
  66.  
  67. $IRC->authnick = "Tootoot222";
  68.  
  69. $IRC->cNick = $IRC->joinnick;
  70.  
  71. $IRC->auth = ""; //will be sent to nickserv to identify
  72.  
  73. $IRC->server = "";
  74.  
  75. $IRC->cmdChar = "!";
  76.  
  77. $IRC->locked = false;
  78.  
  79. $IRC->debug = false;
  80.  
  81. $IRC->pingpong = false;
  82.  
  83. $IRC->powers = array(array(), array());
  84.  
  85. $IRC->channames = array(array(array()));
  86.  
  87. $IRC->tempnames = array();
  88.  
  89. $IRC->cnameslist = "";
  90.  
  91. $IRC->version = "Version 3";
  92.  
  93.  
  94.  
  95. set_time_limit(0); //to ensure the script doesn't time out
  96.  
  97.  
  98.  
  99. date_default_timezone_set("America/Chicago"); //change this to your timezone
  100.  
  101.  
  102.  
  103. $firstmaster = "Tootoot222"; //set this if you haven't created the database/tables yet
  104.  
  105.  
  106.  
  107. $IRC->MySQLServer = "localhost";   
  108.  
  109. $IRC->MySQLDatabase = "ircbotv3";
  110.  
  111. $IRC->MySQLAccountName = "root";
  112.  
  113. $IRC->MySQLAccountpassword = "";
  114.  
  115. $IRC->mTableName = "masters5";
  116.  
  117. $IRC->iTableName = "imasters2";
  118.  
  119. $IRC->cTableName = "cmd3";
  120.  
  121.  
  122.  
  123. $IRC->logfile = fopen("logfile.txt", "a");
  124.  
  125. fwrite($IRC->logfile, "\r\n*** Log opened on " . date("D M jS H:i:s", time()) . " ***\r\n\r\n");
  126.  
  127.  
  128.  
  129. $IRC->con = mysql_connect($IRC->MySQLServer, $IRC->MySQLAccountName, $IRC->MySQLAccountpassword) or die("MySQL error: " . mysql_error() . "\r\n");
  130.  
  131. mysql_query("CREATE DATABASE IF NOT EXISTS " . $IRC->MySQLDatabase, $IRC->con);
  132.  
  133. mysql_select_db($IRC->MySQLDatabase, $IRC->con);
  134.  
  135.  
  136.  
  137. function table_exists($tablename) {
  138.  
  139.     global $IRC;
  140.  
  141.     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);
  142.  
  143. }
  144.  
  145.  
  146.  
  147. if(!table_exists($IRC->cTableName)) {
  148.  
  149.     mysql_query("CREATE TABLE " . $IRC->cTableName . "(name VARCHAR(25), code MEDIUMTEXT, privs INT, maker VARCHAR(20))", $IRC->con);
  150.  
  151. }
  152.  
  153. if(!table_exists($IRC->mTableName)) {
  154.  
  155.     mysql_query("CREATE TABLE " . $IRC->mTableName . "(name VARCHAR(20), privs INT)", $IRC->con);
  156.  
  157.     mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $firstmaster . "', 100)", $IRC->con);
  158.  
  159. }
  160.  
  161. if(!table_exists($IRC->iTableName)) {
  162.  
  163.     mysql_query("CREATE TABLE " . $IRC->iTableName . "(name VARCHAR(20), privs INT)", $IRC->con);
  164.  
  165. }
  166.  
  167.  
  168.  
  169. function output($message, $newline = true, $timestamp = true) {
  170.  
  171.     global $IRC;
  172.  
  173.     foreach(explode("\r\n", $message) as $m) {
  174.  
  175.         $out = ($timestamp ? "[" . date("H:i:s") . "] " : "") . $m . ($newline ? "\r\n" : "");
  176.  
  177.         echo($out);
  178.  
  179.         fwrite($IRC->logfile, $out);
  180.  
  181.     }
  182.  
  183. }
  184.  
  185.  
  186.  
  187. function send($data, $out = true, $b = false) {
  188.  
  189.     if(!trim($data)) {
  190.  
  191.       return;
  192.  
  193.     }
  194.  
  195.     global $IRC;
  196.  
  197.     $a = explode("\r\n", $data);
  198.  
  199.     foreach($a as $aa) {
  200.  
  201.         fputs($IRC->fp, $aa . "\r\n");
  202.  
  203.         if($out) {
  204.  
  205.             output(($b ? "--" : "") . "-> " . $aa);
  206.  
  207.         }
  208.  
  209.     }
  210.  
  211. }
  212.  
  213.  
  214.  
  215. function implodefrom($glue, $pieces, $from = 0) {
  216.  
  217.     $r = "";
  218.  
  219.     if(is_array($pieces)) {
  220.  
  221.         $from = max(0, $from);
  222.  
  223.         $r = $pieces[$from];
  224.  
  225.         for($i = ($from + 1); $i < sizeof($pieces); $i++) {
  226.  
  227.             $r .= $glue . $pieces[$i];
  228.  
  229.         }
  230.  
  231.     }
  232.  
  233.     return($r);
  234.  
  235. }
  236.  
  237.  
  238.  
  239. function notnull($c1, $c2) {
  240.  
  241.     return($c1 ? $c1 : $c2);
  242.  
  243. }
  244.  
  245.  
  246.  
  247. function startsWith($haystack, $needle) {
  248.  
  249.     if(is_array($needle)) {
  250.  
  251.         foreach($needle as $n) {
  252.  
  253.             if(strpos($haystack, $n) === 0) {
  254.  
  255.                 return($n);
  256.  
  257.             }
  258.  
  259.         }
  260.  
  261.     }
  262.  
  263.     return(strpos($haystack, $needle) === 0);
  264.  
  265. }
  266.  
  267.  
  268.  
  269. function instr($needle, $haystack, $insensitive = 0) {
  270.  
  271.     if($insensitive) {
  272.  
  273.         return((false !== stristr($haystack, $needle)) ? true : false);
  274.  
  275.     }else {
  276.  
  277.         return((false !== strpos($haystack, $needle)) ? true : false);
  278.  
  279.     }
  280.  
  281. }
  282.  
  283.  
  284.  
  285. function dumpDB($message = "", $oldcode = "", $newcode = "") {
  286.  
  287.     global $IRC;
  288.  
  289.     if(!file_exists("./DB_backup/")) {
  290.  
  291.         mkdir("./DB_backup/");
  292.  
  293.     }
  294.  
  295.     $basename = "db_backup";
  296.  
  297.     $n = 1;
  298.  
  299.     while(file_exists($filename = ("./DB_backup/" . $basename . $n . ".txt"))) {
  300.  
  301.         $n++;
  302.  
  303.     }
  304.  
  305.     if(!$fh = fopen($filename, 'w')) {
  306.  
  307.         return("failed to open file: " . $filename);
  308.  
  309.     }
  310.  
  311.     fwrite($fh, "//Dumpfile " . $n . " created on " . date("D M jS H:i:s", time()) . "\r\n\r\n");
  312.  
  313.     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 : ""));
  314.  
  315.     $q = mysql_query("SELECT * FROM " . $IRC->cTableName . " ORDER BY name ASC", $IRC->con);
  316.  
  317.     while($row = mysql_fetch_array($q)) {
  318.  
  319.         fwrite($fh, "\r\n\r\n\r\n//" . strtoupper($row["name"]) . ":\r\n" . $row["code"]);
  320.  
  321.     }
  322.  
  323.     fclose($fh);
  324.  
  325.     return(true);
  326.  
  327. }
  328.  
  329.  
  330.  
  331. function fetchcmd($cmd, $r = "code") {
  332.  
  333.     global $IRC;
  334.  
  335.     $row = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->cTableName . ($cmd ? " WHERE name='" . $cmd . "'" : ""), $IRC->con));
  336.  
  337.     return($row[$r]);
  338.  
  339. }
  340.  
  341.  
  342.  
  343. function fetchmasters($name = "", $privs = false, $active = true) {
  344.  
  345.     global $IRC;
  346.  
  347.     $q = mysql_query("SELECT " . ($n = ($privs ? "privs" : "name")) . " FROM " . ($active ? $IRC->mTableName : $IRC->iTableName) . ($name ? " WHERE name='" . $name . "'" : ""), $IRC->con);
  348.  
  349.     $r = array();
  350.  
  351.     while($row = mysql_fetch_array($q)) {
  352.  
  353.         $r[] = $row[$n];
  354.  
  355.     }
  356.  
  357.     return($r);
  358.  
  359. }
  360.  
  361.  
  362.  
  363. function removemasters($names, $all = false, $active = true) {
  364.  
  365.     global $IRC;
  366.  
  367.     if($active) {
  368.  
  369.         $t = $IRC->mTableName;
  370.  
  371.     }else {
  372.  
  373.         $t = $IRC->iTableName;
  374.  
  375.     }
  376.  
  377.     if(!$all) {
  378.  
  379.         if(!is_array($names)) {
  380.  
  381.             $names = array($names);
  382.  
  383.         }
  384.  
  385.         foreach($names as $n) {
  386.  
  387.             mysql_query("DELETE FROM " . $t . " WHERE name='" . $n . "'", $IRC->con);
  388.  
  389.         }
  390.  
  391.     }else {
  392.  
  393.         mysql_query("DELETE FROM " . $t, $IRC->con);
  394.  
  395.     }
  396.  
  397. }
  398.  
  399.  
  400.  
  401. function addmasters($names, $privs, $active = true, $upd = false) {
  402.  
  403.     global $IRC;
  404.  
  405.     if(sizeof($names) != sizeof($privs)) {
  406.  
  407.         $p = $privs;
  408.  
  409.         $privs = array();
  410.  
  411.         for($i = 0; $i < sizeof($names); $i++) {
  412.  
  413.             $privs[$i] = $p;
  414.  
  415.         }
  416.  
  417.     }
  418.  
  419.     if($active) {
  420.  
  421.         $t = $IRC->mTableName;
  422.  
  423.     }else {
  424.  
  425.         $t = $IRC->iTableName;
  426.  
  427.     }
  428.  
  429.     if(!$upd) {
  430.  
  431.         removemasters($names);
  432.  
  433.     }
  434.  
  435.     for($i = 0; $i < sizeof($names); $i++) {
  436.  
  437.         if(!$upd) {
  438.  
  439.             mysql_query("INSERT INTO " . $t . "(name, privs) VALUES ('" . $names[$i] . "', " . min($privs[$i], 100) . ")", $IRC->con);
  440.  
  441.         }else {
  442.  
  443.             mysql_query("UPDATE " . $t . " SET name='" . $privs[$i] . "' WHERE name='" . $names[$i] . "'", $IRC->con);
  444.  
  445.         }
  446.  
  447.     }
  448.  
  449. }
  450.  
  451.  
  452.  
  453. function swapmasters($names) {
  454.  
  455.     global $IRC;
  456.  
  457.     if(!is_array($names)) {
  458.  
  459.         $names = array($names);
  460.  
  461.     }
  462.  
  463.     foreach($names as $n) {
  464.  
  465.         $r = mysql_fetch_array(mysql_query("SELECT * FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con));
  466.  
  467.         mysql_query("DELETE FROM " . $IRC->mTableName . " WHERE name='" . $n . "'", $IRC->con);
  468.  
  469.         mysql_query("INSERT INTO " . $IRC->mTableName . "(name, privs) VALUES ('" . $r["name"] . "', " . $r["privs"] . ")", $IRC->con);
  470.  
  471.         mysql_query("DELETE FROM " . $IRC->iTableName . " WHERE name='" . $n . "'", $IRC->con);
  472.  
  473.     }
  474.  
  475. }
  476.  
  477.  
  478.  
  479. function onConnect() {
  480.  
  481.     global $IRC;
  482.  
  483.     send("USER " . $IRC->username . " * * :" . $IRC->realname);
  484.  
  485.     send("NICK " . $IRC->joinnick);
  486.  
  487.     $IRC->cNick = $IRC->joinnick;
  488.  
  489.     if($IRC->auth) {
  490.  
  491.         send("PRIVMSG NickServ :ID " . $IRC->joinnick . " " . $IRC->auth);
  492.  
  493.     }
  494.  
  495.     foreach($IRC->joinchan as $c) {
  496.  
  497.         send("JOIN " . $c);
  498.  
  499.     }
  500.  
  501. }
  502.  
  503.  
  504.  
  505. function handleBotCommands() {
  506.  
  507.     global $IRC;
  508.  
  509.     if((startsWith($IRC->cM->mess, $IRC->cmdChar)) && (strlen($IRC->cM->mess) > strlen($IRC->cmdChar)) && (strtolower($IRC->cM->nick) != strtolower($IRC->cNick))) {
  510.  
  511.         $userprivs = fetchmasters($IRC->cM->nick, true);
  512.  
  513.         $userprivs = $userprivs[0] ? $userprivs[0] : 0;
  514.  
  515.         $ispm = (strtolower($IRC->cM->param[0]) == strtolower($IRC->cNick));
  516.  
  517.         if(((!$IRC->locked) || ($userprivs > 49)) && ($userprivs >= 0)) {
  518.  
  519.             $a = explode(" ", substr($IRC->cM->mess, strlen($IRC->cmdChar)));
  520.  
  521.             $r = implodefrom(" ", $a, 1);
  522.  
  523.             $reqprivs = fetchcmd($a[0], "privs");
  524.  
  525.             $hasprivs = $userprivs >= $reqprivs;
  526.  
  527.             eval(fetchcmd($a[0]));
  528.  
  529.             if($userprivs >= 100) {
  530.  
  531.                 switch(strtoupper($a[0])) {
  532.  
  533.                     case "BIND":
  534.  
  535.                         if($ispm) {
  536.  
  537.                             $chan = $IRC->cM->nick;
  538.  
  539.                         }else {
  540.  
  541.                             $chan = $IRC->cM->param[0];
  542.  
  543.                         }
  544.  
  545.                         switch(strtoupper($a[1])) {
  546.  
  547.                             case "ADD":
  548.  
  549.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 4))) || (!(is_numeric($a[3])))) {
  550.  
  551.                                     break;
  552.  
  553.                                 }
  554.  
  555.                                 $p = min($a[3], 100);
  556.  
  557.                                 send("PRIVMSG " . $chan . " :Binding command " . strtoupper($a[2]) . " with " . $p . " privs");
  558.  
  559.                                 dumpDB("Binding command " . strtoupper($a[2]) . " with " . $p . " privs", fetchcmd(strtoupper($a[2])), $o);
  560.  
  561.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  562.  
  563.                                 mysql_query("INSERT INTO " . $IRC->cTableName . "(name, code, privs) VALUES ('" . strtoupper($a[2]) . "', '" . $o . "', " . $p . ")", $IRC->con);
  564.  
  565.                                 break;
  566.  
  567.                             case "DEL":
  568.  
  569.                                 $co = fetchcmd($a[2]);
  570.  
  571.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[2] . "'", $IRC->con);
  572.  
  573.                                 dumpDB("Deleted command " . strtoupper($a[2]), $co);
  574.  
  575.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was deleted.");
  576.  
  577.                                 break;
  578.  
  579.                             case "REN":
  580.  
  581.                                 if((sizeof($a) < 4) ) {
  582.  
  583.                                     break;
  584.  
  585.                                 }
  586.  
  587.                                 if($c = fetchcmd($a[2])) {
  588.  
  589.                                     $b = fetchcmd($a[3]);
  590.  
  591.                                     mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  592.  
  593.                                 }
  594.  
  595.                                 dumpDB("Renaming command \"" . strtoupper($a[2]) . "\" to \"" . strtoupper($a[3]) . "\"", $b ? $b : "");
  596.  
  597.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET name='" . strtoupper($a[3]) . "' WHERE name='" . $a[2] . "'");
  598.  
  599.                                 send("PRIVMSG " . $chan . " :Command '" . strtoupper($a[2]) . "' was renamed to '" . strtoupper($a[3]) . "'.");
  600.  
  601.                                 break;
  602.  
  603.                             case "LNK":
  604.  
  605.                                 if((sizeof($a) < 4) || (strtoupper($a[2]) == strtoupper($a[3]))) {
  606.  
  607.                                     break;
  608.  
  609.                                 }
  610.  
  611.                                 send("PRIVMSG " . $chan . " :Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  612.  
  613.                                 dumpDB("Linking command " . strtoupper($a[2]) . " to " . strtoupper($a[3]));
  614.  
  615.                                 mysql_query("DELETE FROM " . $IRC->cTableName . " WHERE name='" . $a[3] . "'", $IRC->con);
  616.  
  617.                                 $p = fetchcmd($a[2], "privs");
  618.  
  619.                                 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);
  620.  
  621.                                 break;
  622.  
  623.                             case "APP":
  624.  
  625.                                 if((sizeof($a) < 4) || (!($o = implodefrom(" ", $a, 3)))) {
  626.  
  627.                                     break;
  628.  
  629.                                 }
  630.  
  631.                                 send("PRIVMSG " . $chan . " :Appending command " . strtoupper($a[2]));
  632.  
  633.                                 dumpDB("Appending command " . strtoupper($a[2]), $co = fetchcmd(strtoupper($a[2])), $o);
  634.  
  635.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . $co . $o . "' WHERE name='" . $a[2] . "'");
  636.  
  637.                                 break;
  638.  
  639.                             case "PRV":
  640.  
  641.                                 if(sizeof($a) < 4) {
  642.  
  643.                                     break;
  644.  
  645.                                 }
  646.  
  647.                                 $p = min($a[3], 100);
  648.  
  649.                                 send("PRIVMSG " . $chan . " :Changing privlages of command " . strtoupper($a[2]) . " to " . $p);
  650.  
  651.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET privs=" . $p . " WHERE name='" . $a[2] . "'");
  652.  
  653.                                 break;
  654.  
  655.                             case "UPD":
  656.  
  657.                                 if((sizeof($a) < 5) || (!($o = implodefrom(" ", $a, 4))) || (!is_numeric($a[3]))) {
  658.  
  659.                                     break;
  660.  
  661.                                 }
  662.  
  663.                                 $car = explode("\r\n", fetchcmd(strtoupper($a[2])));
  664.  
  665.                                 send("PRIVMSG " . $chan . " :Updating line " . $a[3] . " of command " . strtoupper($a[2]));
  666.  
  667.                                 dumpDB("Updating line " . $a[3] . " of command " . strtoupper($a[2]), $car[$a[3]], $o);
  668.  
  669.                                 $car[$a[3]] = $o;
  670.  
  671.                                 mysql_query("UPDATE " . $IRC->cTableName . " SET code='" . implode("\r\n", $car) . "' WHERE name='" . $a[2] . "'");
  672.  
  673.                                 break;
  674.  
  675.                         }
  676.  
  677.                         break;
  678.  
  679.                 }
  680.  
  681.             }
  682.  
  683.         }
  684.  
  685.     }
  686.  
  687. }
  688.  
  689.  
  690.  
  691. function handleChatCommands() {
  692.  
  693.     global $IRC;
  694.  
  695.     if(!$IRC->cM->nick) {
  696.  
  697.         return false;
  698.  
  699.     }
  700.  
  701.     $chr1 = chr(1);
  702.  
  703.     if((startsWith($IRC->cM->mess[0], $chr1)) && ($IRC->cM->mess[strlen($IRC->cM->mess) - 1] == $chr1)) {
  704.  
  705.         $a = explode(" ", $IRC->cM->mess);
  706.  
  707.         $tok = implodefrom(" ", $a, 1);
  708.  
  709.         $tok = trim(substr($tok, 0, strlen($tok) - 1));
  710.  
  711.         if($tok) {
  712.  
  713.             $tok2 = strtoupper(substr($a[0], 1));
  714.  
  715.             if($tok2 != "ACTION") {
  716.  
  717.                 $tokk = ": ";
  718.  
  719.             }
  720.  
  721.         }else {
  722.  
  723.             $tok2 = strtoupper(substr($a[0], 1, strlen($tok2) - 1));
  724.  
  725.         }
  726.  
  727.         if($tok2 != "ACTION") {
  728.  
  729.             output($IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] requested CTCP " . $tok2 . " from " . $IRC->cM->param[0] . $tokk . $tok);
  730.  
  731.         }
  732.  
  733.         switch($tok2) {
  734.  
  735.             case "ACTION":
  736.  
  737.                 output("[" . $IRC->cM->param[0] . "] * " . $IRC->cM->nick . " " . $tok);
  738.  
  739.                 break;
  740.  
  741.             case "PING":
  742.  
  743.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "PING " . $tok . $chr1);
  744.  
  745.                 break;
  746.  
  747.             case "TIME":
  748.  
  749.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "TIME " . date("D M jS H:i:s") . $chr1);
  750.  
  751.                 break;
  752.  
  753.             case "VERSION":
  754.  
  755.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "VERSION Toot PHP Bot " . $IRC->version . $chr1);
  756.  
  757.                 break;
  758.  
  759.             case "FINGER":
  760.  
  761.                 send("NOTICE " . $IRC->cM->nick . " :" . $chr1 . "FINGER wat" . $chr1);
  762.  
  763.                 break;
  764.  
  765.         }
  766.  
  767.         return true;
  768.  
  769.     }
  770.  
  771.     if(strtolower($IRC->cM->param[0]) != strtolower($IRC->cNick)) {
  772.  
  773.         output("[" . $IRC->cM->param[0] . "] <" . $IRC->channames[strtolower($IRC->cM->param[0])][$IRC->cM->nick] . $IRC->cM->nick . "> " . $IRC->cM->mess);
  774.  
  775.     }else {
  776.  
  777.         output("-*" . $IRC->cM->nick . "*- " . $IRC->cM->mess);
  778.  
  779.     }
  780.  
  781.     if(startsWith($IRC->cM->mess, $IRC->cmdChar)) {
  782.  
  783.         handleBotCommands();
  784.  
  785.     }
  786.  
  787.     return true;
  788.  
  789. }
  790.  
  791.  
  792.  
  793. function handleNormalCommands() {
  794.  
  795.     global $IRC;
  796.  
  797.     switch(strtoupper(trim($IRC->cM->cmd))) {
  798.  
  799.         case "KICK":
  800.  
  801.             if(strtolower($IRC->cM->param[1]) == strtolower($IRC->cNick)) {
  802.  
  803.                 $who = "You were";
  804.  
  805.             }else {
  806.  
  807.                 $who = $IRC->cM->param[1] . " was";
  808.  
  809.             }
  810.  
  811.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->param[1]]);
  812.  
  813.             output("-!- " . $who . " kicked from " . $IRC->cM->param[0] . " by " . $IRC->cM->nick . " [" . $IRC->cM->mess . "]");
  814.  
  815.             if($IRC->cM->param[1] == $IRC->cNick) {
  816.  
  817.                 send("JOIN :" . $IRC->cM->param[0]);
  818.  
  819.             }
  820.  
  821.             break;
  822.  
  823.         case "MODE":
  824.  
  825.             if(strtolower($IRC->cNick) == strtolower($IRC->cM->param[0])) {
  826.  
  827.                 $o = $IRC->cM->mess;
  828.  
  829.             }else {
  830.  
  831.                 $o = implodefrom(" ", $IRC->cM->param, 1);
  832.  
  833.             }
  834.  
  835.             if($IRC->cM->host) {
  836.  
  837.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->nick);
  838.  
  839.             }else {
  840.  
  841.                 output("-!- mode/" . $IRC->cM->param[0] . " [" . $o . "] by " . $IRC->cM->host);
  842.  
  843.             }
  844.  
  845.             break;
  846.  
  847.         case "NOTICE":
  848.  
  849.             output("-" . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "]- " . $IRC->cM->mess);
  850.  
  851.             break;
  852.  
  853.         case "PRIVMSG":
  854.  
  855.             return handleChatCommands();
  856.  
  857.         case "PART":
  858.  
  859.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has left " . $IRC->cM->param[0] . " [" . $IRC->cM->mess . "]");
  860.  
  861.             unset($IRC->channicks[strtolower($IRC->cM->param[0])][$IRC->cM->nick]);
  862.  
  863.             break;
  864.  
  865.         case "JOIN":
  866.  
  867.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has joined " . $IRC->cM->mess);
  868.  
  869.             $IRC->channicks[strtolower($IRC->cM->mess)][$IRC->cM->nick] = " ";
  870.  
  871.             break;
  872.  
  873.         case "QUIT":
  874.  
  875.             output("-!- " . $IRC->cM->nick . " [" . $IRC->cM->user . "@" . $IRC->cM->host . "] has quit [" . $IRC->cM->mess . "]");
  876.  
  877.             $a = array_keys($IRC->channames);
  878.  
  879.             for($i = 0; $i < sizeof($a); $i++) {
  880.  
  881.                 unset($IRC->channames[$a[$i]][$IRC->cM->nick]);
  882.  
  883.             }
  884.  
  885.             break;
  886.  
  887.         case "NICK":
  888.  
  889.             if(strtolower($IRC->cM->nick) == strtolower($IRC->cNick)) {
  890.  
  891.                 $who = "You are";
  892.  
  893.                 $IRC->cNick = $IRC->cM->mess;
  894.  
  895.             }else {
  896.  
  897.                 $who = $IRC->cM->nick . " is";
  898.  
  899.             }
  900.  
  901.             output("-!- " . $who . " now known as " . $IRC->cM->mess);
  902.  
  903.             $aa = array_keys($IRC->channames);
  904.  
  905.             for($i = 0; $i < sizeof($aa); $i++) {
  906.  
  907.                 $IRC->channames[$aa[$i]][$IRC->cM->mess] = $IRC->channames[$aa[$i]][$IRC->cM->nick];
  908.  
  909.                 unset($IRC->channames[$aa[$i]][$IRC->cM->nick]);
  910.  
  911.             }
  912.  
  913.             addmasters(array($IRC->cM->nick), array($IRC->cM->mess), true, true);
  914.  
  915.             break;
  916.  
  917.         case "TOPIC":
  918.  
  919.             output("-!- " . $IRC->cM->nick . " changed the topic of " . $IRC->cM->param[0] . " to: " . $IRC->cM->mess);
  920.  
  921.             break;
  922.  
  923.         case "PONG":
  924.  
  925.             if($IRC->pingpong) {
  926.  
  927.                 output("-!- Server PONG reply from " . $IRC->cM->nick . ": " . $IRC->cM->param[0] . "; Time since PING request sent: " . (microtime(true) - $IRC->pingsent));
  928.  
  929.             }
  930.  
  931.             $IRC->lastping = time();
  932.  
  933.             $IRC->pongwait = false;
  934.  
  935.             break;
  936.  
  937.         default:
  938.  
  939.             return false;
  940.  
  941.     }
  942.  
  943.     return true;
  944.  
  945. }
  946.  
  947.  
  948.  
  949. function handleNumberCommands() {
  950.  
  951.     global $IRC;
  952.  
  953.     switch($IRC->cM->cmd) {
  954.  
  955.         case "001":
  956.  
  957.             $IRC->server = $IRC->cM->nick;
  958.  
  959.             output($IRC->cM->mess);
  960.  
  961.             send("NICK " . $IRC->joinnick);
  962.  
  963.             send("PRIVMSG NickServ :ID " . $IRC->joinnick . " " . $IRC->auth);
  964.  
  965.             foreach($IRC->joinchan as $c) {
  966.  
  967.                 send("JOIN " . $c);
  968.  
  969.             }
  970.  
  971.             break;
  972.  
  973.         case "004":
  974.  
  975.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  976.  
  977.             break;
  978.  
  979.         case "005":
  980.  
  981.             $IRC->cM->mess = implodefrom(" ", $IRC->cM->param, 1) . $IRC->cM->mess;
  982.  
  983.             if($a = substr($IRC->cM->mess, $p = (strpos(strtoupper($IRC->cM->mess), "PREFIX=") + 8), strpos($IRC->cM->mess, " ", $p) - $p)) {
  984.  
  985.                 $aa = explode(")", $a);
  986.  
  987.                 $IRC->powers = array(array(), array());
  988.  
  989.                 for($i = 0; $i < strlen($aa[0]); $i++) {
  990.  
  991.                     $IRC->powers[0][] = $aa[0][$i];
  992.  
  993.                 }
  994.  
  995.                 for($i = 0; $i < strlen($aa[1]); $i++) {
  996.  
  997.                     $IRC->powers[1][] = $aa[1][$i];
  998.  
  999.                 }
  1000.  
  1001.             }
  1002.  
  1003.         case "252":
  1004.  
  1005.         case "254":
  1006.  
  1007.             output(trim($IRC->cM->param[0], $IRC->cNick . " ") . " " . $IRC->cM->mess);
  1008.  
  1009.             break;
  1010.  
  1011.         case "332":
  1012.  
  1013.             output("Topic for " . $IRC->cM->param[1] . " is: " . $IRC->cM->mess);
  1014.  
  1015.             break;
  1016.  
  1017.         case "333":
  1018.  
  1019.             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]));
  1020.  
  1021.             break;
  1022.  
  1023.         case "353":
  1024.  
  1025.             $cc = $IRC->cM->param[2];
  1026.  
  1027.             $c = strtolower($cc);
  1028.  
  1029.             $ndat = $IRC->cM->wline . "\n";
  1030.  
  1031.             while(!instr("366 " . $IRC->cNick . " " . $c, $ndat, true)) {
  1032.  
  1033.                 if($rd = fgets($IRC->fp, 1024)) {
  1034.  
  1035.                     $ndat .= $rd;
  1036.  
  1037.                 }
  1038.  
  1039.             }
  1040.  
  1041.             unset($IRC->tempnames[$c]);
  1042.  
  1043.             $IRC->tempnames[$c] = "";
  1044.  
  1045.             $e = explode("\n", $ndat);
  1046.  
  1047.             foreach($e as $a) {
  1048.  
  1049.                 parseMessage(trim($a), false);
  1050.  
  1051.                 if($IRC->cM->cmd != "353") {
  1052.  
  1053.                     break;
  1054.  
  1055.                 }
  1056.  
  1057.                 $IRC->tempnames[$c] .= $IRC->cM->mess;
  1058.  
  1059.                 $IRC->cM->mess = "";
  1060.  
  1061.             }
  1062.  
  1063.             $names = explode(" ", $IRC->tempnames[$c]);
  1064.  
  1065.             unset($IRC->tempnames[$c]);
  1066.  
  1067.             sort($names);
  1068.  
  1069.             output("[Users " . $cc . "]", false);
  1070.  
  1071.             output("", false, false);
  1072.  
  1073.             for($i = 0; $i < sizeof($names); $i++) {
  1074.  
  1075.                 if(in_array($names[$i][0], $IRC->powers[1])) {
  1076.  
  1077.                     $IRC->channames[$c][substr($names[$i], 1)] = $names[$i][0];
  1078.  
  1079.                 }else {
  1080.  
  1081.                     $IRC->channames[$c][$names[$i]] = " ";
  1082.  
  1083.                 }
  1084.  
  1085.                 if(($i % 5) == 0) {
  1086.  
  1087.                     output("", true, false);
  1088.  
  1089.                     output("", false);
  1090.  
  1091.                 }
  1092.  
  1093.                 output("[" . str_pad($IRC->channames[$c][$names[$i]] . $names[$i], 17, " ", STR_PAD_RIGHT) . "] ", false, false);
  1094.  
  1095.             }
  1096.  
  1097.             output("", true, false);
  1098.  
  1099.             output("-!- " . $cc . ": Total of " . sizeof($names) . " nicks");
  1100.  
  1101.             break;
  1102.  
  1103.         case "421":
  1104.  
  1105.             output("Unknown command: \"" . $IRC->cM->param[1] . "\"");
  1106.  
  1107.             break;
  1108.  
  1109.         case '433':
  1110.  
  1111.             output("Nickname \"" . $IRC->cM->param[1] . "\" is already in use.");
  1112.  
  1113.             if($IRC->firstcon) {
  1114.  
  1115.                 send("NICK " . $IRC->joinnick . rand(100, 999));
  1116.  
  1117.                 send("PRIVMSG NickServ :ghost " . $IRC->joinnick . " " . $IRC->auth);
  1118.  
  1119.             }          
  1120.  
  1121.             break;
  1122.  
  1123.         case '474':
  1124.  
  1125.             output('Unable to join channel ' . $IRC->cM->param[1] . ': ' . $IRC->cM->mess);
  1126.  
  1127.             break;
  1128.  
  1129.         default:
  1130.  
  1131.             output($IRC->cM->mess);
  1132.  
  1133.             break;
  1134.  
  1135.     }
  1136.  
  1137.     return true;
  1138.  
  1139. }
  1140.  
  1141.  
  1142.  
  1143. function parseMessage($buffer, $handle = true) {
  1144.  
  1145.     global $IRC;
  1146.  
  1147.     $buffer = explode("\n", $buffer);
  1148.  
  1149.     $IRC->cM->wline = $buffer = trim($buffer[0], "\r");
  1150.  
  1151.     if(!trim($buffer)) {
  1152.  
  1153.         return;
  1154.  
  1155.     }
  1156.  
  1157.     if($IRC->debug) {
  1158.  
  1159.         output($buffer);
  1160.  
  1161.     }
  1162.  
  1163.     switch($s = startsWith(strtoupper($IRC->cM->wline), array(":", "NOTICE", "PING", "ERROR"))) {
  1164.  
  1165.         case ":":
  1166.  
  1167.             $a = explode(" ", $IRC->cM->wline);
  1168.  
  1169.             $IRC->cM->sender = substr($a[0], 1);
  1170.  
  1171.             $aa = explode("!", $IRC->cM->sender);
  1172.  
  1173.             $IRC->cM->nick = $aa[0];
  1174.  
  1175.             $aa = explode("@", implodefrom(" ", $aa, 1));
  1176.  
  1177.             $IRC->cM->user = $aa[0];
  1178.  
  1179.             $IRC->cM->host = $aa[1];
  1180.  
  1181.             $IRC->cM->cmd = $a[1];
  1182.  
  1183.             $aa = explode(":", implodefrom(" ", $a, 2));
  1184.  
  1185.             $IRC->cM->param = explode(" ", trim($aa[0]));
  1186.  
  1187.             $IRC->cM->mess = implodefrom(":", $aa, 1);
  1188.  
  1189.             if($handle) {
  1190.  
  1191.                 if(is_numeric($IRC->cM->cmd)) {
  1192.  
  1193.                     if(handleNumberCommands()) {
  1194.  
  1195.                         break;
  1196.  
  1197.                     }
  1198.  
  1199.                 }
  1200.  
  1201.                 if(handleNormalCommands()) {
  1202.  
  1203.                     break;
  1204.  
  1205.                 }
  1206.  
  1207.                 output("Unknown message: " . $IRC->cM->wline);
  1208.  
  1209.             }
  1210.  
  1211.             break;
  1212.  
  1213.         case "NOTICE":
  1214.  
  1215.             $IRC->cM->mess = substr($IRC->cM->wline, strpos($IRC->cM->wline, ":") + 1);
  1216.  
  1217.             $ss = $IRC->server;
  1218.  
  1219.             if(startsWith(strtoupper($IRC->cM->wline), "NOTICE AUTH")) {
  1220.  
  1221.                 $ss = $IRC->nick;
  1222.  
  1223.             }
  1224.  
  1225.             output("!" . $ss . " " . $IRC->cM->mess);
  1226.  
  1227.             break;
  1228.  
  1229.         case "PING":
  1230.  
  1231.             if($IRC->pingpong) {
  1232.  
  1233.                 output($buffer);
  1234.  
  1235.             }
  1236.  
  1237.             $IRC->cM->wline[1] = "O";
  1238.  
  1239.             send($IRC->cM->wline, $IRC->pingpong);
  1240.  
  1241.             break;
  1242.  
  1243.         case "ERROR":
  1244.  
  1245.             output($IRC->cM->wline);
  1246.  
  1247.             break;
  1248.  
  1249.         default:
  1250.  
  1251.             output("Unknown message: " . $IRC->cM->wline);
  1252.  
  1253.             break;
  1254.  
  1255.     }
  1256.  
  1257. }
  1258.  
  1259.  
  1260.  
  1261. $IRC->connected = false;
  1262.  
  1263. $IRC->lastping = time();
  1264.  
  1265. $IRC->pongwait = false;
  1266.  
  1267. $IRC->pingsent = microtime(true);
  1268.  
  1269. $IRC->masterNick = fetchmasters();
  1270.  
  1271.  
  1272.  
  1273. while(!$IRC->quit) {
  1274.  
  1275.     usleep(10000);
  1276.  
  1277.     if((($IRC->pongwait) && ((time() - $IRC->lastping) > 300)) || (((!$IRC->connected) || (feof($IRC->fp)) && (!($IRC->quit))))) {
  1278.  
  1279.         if($IRC->connected) {
  1280.  
  1281.             if(!feof($IRC->fp)) {
  1282.  
  1283.                 send("QUIT :Lost connection");
  1284.  
  1285.             }
  1286.  
  1287.             fclose($IRC->fp);
  1288.  
  1289.         }
  1290.  
  1291.         $IRC->connected = false;
  1292.  
  1293.         output("-!- Attempting to open connection to " . $IRC->host . ":" . $IRC->port);
  1294.  
  1295.         if($IRC->fp = fsockopen($IRC->host, $IRC->port, $errno, $errdesc)) {
  1296.  
  1297.             output("-!- Connection established to " . $IRC->host . ":" . $IRC->port);
  1298.  
  1299.             stream_set_timeout($IRC->fp, 0, 1);
  1300.  
  1301.             $IRC->firstcon = true;
  1302.  
  1303.             $IRC->connected = true;
  1304.  
  1305.             $IRC->pongwait = false;
  1306.  
  1307.             $IRC->lastping = time();
  1308.  
  1309.             send("USER " . $IRC->username . " * * :" . $IRC->realname);
  1310.  
  1311.             send("NICK " . $IRC->joinnick);
  1312.  
  1313.         }else {
  1314.  
  1315.             sleep(30);
  1316.  
  1317.             continue;
  1318.  
  1319.         }
  1320.  
  1321.     }
  1322.  
  1323.     if((!$IRC->pongwait) && (time() - $IRC->lastping) > 30) {
  1324.  
  1325.         $IRC->pongwait = true;
  1326.  
  1327.         $IRC->pingsent = microtime(true);
  1328.  
  1329.         send("PING :" . $IRC->server, $IRC->pingpong);
  1330.  
  1331.     }
  1332.  
  1333.     if($rawdata = fgets($IRC->fp, 1024)) {
  1334.  
  1335.         parseMessage($rawdata);
  1336.  
  1337.     }
  1338.  
  1339.    
  1340.  
  1341. }
  1342.  
  1343. if(!feof($IRC->fp)) {
  1344.  
  1345.     send("QUIT :I couldn't think of a whitty quit message");
  1346.  
  1347. }
  1348.  
  1349. fclose($IRC->fp);
  1350.  
  1351. fclose($IRC->logfile);
  1352.  
  1353. mysql_close($IRC->con);
  1354.  
  1355. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement