x2Fusion

PHP qBot

Sep 12th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 45.81 KB | None | 0 0
  1. <?php
  2.  
  3. /** PHP IRC QuoterBot **
  4.  *
  5.  *  ####TODO: Think and then some..
  6.  *  ####TODO: THINK of extras.
  7.  */
  8.  
  9. $qbot = QuoterBot::getInst();
  10.  
  11. $qbot->config = array(
  12.         'serv'      => 'localhost',
  13.         'port'      => 6667,
  14.         'nick'      => 'qbot',
  15.         'name'      => 'qbot',
  16.         'chan'      => array('#chats', '#test'),
  17.        
  18.         'confs_data'    => './config.db',
  19.         'quote_data'    => './quotes.db',
  20.         'admin_data'    => './admins.db',
  21.         'admin_logs'    => './admin.log',
  22.         'admin_list'    => array('Bansha', 'Test@localhost', 'Test2!user@localhost')
  23.         );
  24.  
  25. $qbot->Init();
  26.  
  27. ###########################################################
  28. class QuoterBot
  29. {  
  30.     private $is_CLI = false;
  31.     private $socket = null;
  32.     private $ex     = null;
  33.     private $tstamp = '0000-00-00 00:00:00';
  34.    
  35.     private $nicks  = array();
  36.     private $chans  = array();
  37.     private $admins = array();
  38.     private $quotes = array();
  39.    
  40.     private $primer   = false;
  41.     private $privmsg  = false;
  42.     private $curruser = null;
  43.     private $currchan = null;
  44.    
  45.     private $cmdRated  = false;
  46.     private $cmdTicker = 0;
  47.     private $cmdtstamp = '0000-00-00 00:00:00';
  48.     private $userwhois = null;
  49.    
  50.     private $options = array(
  51.         'addquote'      => 'on',
  52.         'autochat'      => 'off',
  53.         'autojoke'      => 'off',
  54.         'joketime'      => '30',
  55.         'ratelimit'     => '10/25'
  56.         );
  57.        
  58.     public $config  = array(
  59.         'serv'          => 'please.change.me',      # Bot Serv
  60.         'port'          => 6667,            # Bot Port
  61.         'nick'          => 'qbot',          # Bot Nick
  62.         'name'          => 'QuoterBot',         # Bot Name
  63.         'chan'          => array('#chan1','#chan2'),    # Chan to join
  64.         'confs_data'    => './config.db',           # Confs Stor DataFile
  65.         'quote_data'    => './quotes.db',           # Quote Stor DataFile
  66.         'admin_data'    => './admins.db',           # Admin User DataFile
  67.         'admin_logs'    => './admin.log',           # Admin Logs DataFile
  68.         'admin_list'    => array('nick1',
  69.                     'nick2@localhost',
  70.                     'nick3!user@localhost')     # Hardcoded Admin List
  71.         );
  72.    
  73.     private $_addr_alloc = array(
  74.                 '199.27.128.0/21',
  75.                 '173.245.48.0/20',
  76.                 '103.21.244.0/22',
  77.                 '103.22.200.0/22',
  78.                 '103.31.4.0/22',
  79.                 '141.101.64.0/18',
  80.                 '108.162.192.0/18',
  81.                 '190.93.240.0/20',
  82.                 '188.114.96.0/20',
  83.                 '197.234.240.0/22',
  84.                 '198.41.128.0/17',
  85.                 '162.158.0.0/15',
  86.                 '104.16.0.0/12',
  87.                 '172.64.0.0/13'
  88.             );
  89.    
  90.     private $_subd_alloc = array(
  91.                 'mail',
  92.                 'direct',
  93.                 'direct-connect',
  94.                 'cpanel',
  95.                 'ftp',
  96.                 'forum',
  97.                 'forums',
  98.                 'blog',
  99.                 'blogs',
  100.                 'test',
  101.                 'dev',
  102.                 'webmail',
  103.                 'testing',
  104.                 'secure',
  105.                 'server',
  106.                 'irc',
  107.                 'cp',
  108.                 'portal',
  109.                 'ssl',
  110.                 'www'
  111.             );
  112.    
  113.     public static function getInst()
  114.     {
  115.         static $inst = null;
  116.         if ($inst === null)
  117.         {
  118.             $inst = new self;
  119.         }
  120.         return $inst;
  121.     }
  122.    
  123.     // Initital functions
  124.     public function Init()
  125.     {
  126.         if(php_sapi_name() == 'cli')
  127.         {
  128.             $this->is_CLI = true;
  129.         }
  130.         else
  131.         {
  132.             if(!set_time_limit(0))
  133.             {
  134.                 echo "<font color='red'><b>Unable to Set_Time_Limit();</b></font><br>" . PHP_EOL;
  135.             }
  136.             flush();
  137.         }
  138.        
  139.         $this->initCore();
  140.     }
  141.    
  142.     // Initital Core functions
  143.     private function InitCore()
  144.     {
  145.         // FileDB Checks
  146.         $this->chkFileDbs();
  147.        
  148.         // Timestamps
  149.         $this->tstamp = date(time());
  150.         $this->cmdtstamp = $this->tstamp;
  151.        
  152.         // Start connection to IRC
  153.         $this->socket = fsockopen($this->config['serv'], $this->config['port']);
  154.        
  155.         // Send USER/NICK to IRC
  156.         $this->sendData('USER', "{$this->config['nick']} irc.land {$this->config['nick']} :{$this->config['name']}\r\n");
  157.         $this->sendData('NICK', "{$this->config['nick']}\r\n");
  158.        
  159.         while(true)
  160.         {  
  161.             // Bot Core
  162.             $this->ex = $this->recvData();
  163.            
  164.             // Play Ping-Pong
  165.             if($this->ex[0] == 'PING')
  166.             {
  167.                 $this->sendData('PONG', "{$this->ex[1]}");
  168.             }
  169.  
  170.             // Auto Functions
  171.             if($this->options['autojoke'] == 'on' &&
  172.                 $this->tstamp < date(time()) - (60 * (int)$this->options['joketime'])) // 60 seconds * minutes;
  173.             {
  174.                 $this->tstamp = date(time());
  175.                 foreach($this->chans as $chan)
  176.                 {
  177.                     $this->randJoke($chan);
  178.                 }
  179.             }
  180.            
  181.             // Set Current User/Chan
  182.             $this->curruser = substr(explode('!', $this->ex[0])[0], 1);
  183.             $this->currchan = @$this->ex[2];
  184.            
  185.             if($this->options['autochat'] == 'on')
  186.             {
  187.                 if(@$this->ex[1] == 'PRIVMSG' && $this->ex[3] != ':!qbot' && $this->ex[3] != ':!chat')
  188.                 {
  189.                     $ex = $this->ex;
  190.                     $ex[3] = substr($ex[3], 1);
  191.                     foreach($ex as $key => $word)
  192.                     {
  193.                         if(trim($word) == $this->config['nick'])
  194.                         {
  195.                             unset($this->ex[$key]);
  196.                             $this->qbotChat(true);
  197.                         }
  198.                     }
  199.                 }
  200.             }
  201.            
  202.             // Switch Functions
  203.             $this->preSwitches();
  204.             $this->cmdSwitches();
  205.         } // While(true)
  206.     }
  207.    
  208.     private function chkFileDbs()
  209.     {
  210.         // Check Admin Data
  211.         if(file_exists($this->config['admin_data']))
  212.         {
  213.             $data = strtolower(file_get_contents($this->config['admin_data']));
  214.             $this->admins = array_unique(array_merge(unserialize($data)));
  215.         }
  216.         else
  217.         {
  218.             $admins = $this->config['admin_list'];
  219.             $this->admins = explode('|', strtolower(implode('|', $admins)));
  220.             file_put_contents($this->config['admin_data'], '');
  221.             file_put_contents($this->config['admin_data'], serialize($this->admins));
  222.         }
  223.        
  224.         // Check Quote Data
  225.         if(file_exists($this->config['quote_data']))
  226.         {
  227.             $data = file_get_contents($this->config['quote_data']);
  228.             $this->quotes = unserialize($data);
  229.         }
  230.         else
  231.         {
  232.             file_put_contents($this->config['quote_data'], '');
  233.             file_put_contents($this->config['quote_data'], serialize($this->quotes));
  234.         }
  235.        
  236.         // Check Config Data
  237.         if(file_exists($this->config['confs_data']))
  238.         {
  239.             $data = file_get_contents($this->config['confs_data']);
  240.             $this->options = unserialize($data);
  241.         }
  242.         else
  243.         {
  244.             file_put_contents($this->config['confs_data'], '');
  245.             file_put_contents($this->config['confs_data'], serialize($this->options));
  246.         }
  247.     }
  248.    
  249.     private function preSwitches()
  250.     {
  251.         // preSwitches include Pre commands, and post nick/join/kick/part/quit
  252.         switch(@$this->ex[1])
  253.         {
  254.             case '001':  // Check for time to join
  255.                 if(is_array($this->config['chan']))
  256.                 {
  257.                     foreach($this->config['chan'] as $chan)
  258.                     {
  259.                         $this->sendData('JOIN', "$chan\r\n");
  260.                     }
  261.                 }
  262.                 else
  263.                 {
  264.                     $this->sendData('JOIN', "{$this->config['chan']}\r\n");
  265.                 }
  266.             break;
  267.             #####################
  268.             case '311': // Fetch Whois Info
  269.                 $ex = $this->ex;
  270.                 unset($ex[0], $ex[1], $ex[2], $ex[6], $ex[7]);
  271.                 $this->userwhois = $ex;
  272.             break;
  273.             #####################
  274.             case '353': // Fetch Nicks from Channel
  275.                 $ex = $this->ex;
  276.                 $chan = $ex[4];
  277.                 unset($ex[0], $ex[1], $ex[2], $ex[3], $ex[4]);
  278.                 $nicks = implode(' ', $ex);
  279.                 $nicks = str_replace(':', '', $nicks);
  280.                 $nicks = str_replace('@', '', $nicks);
  281.                 $this->nicks[$chan] = explode(' ', ucwords($nicks));
  282.             break;
  283.             #####################
  284.             case '433': // Set random nick, if Nick inuse
  285.                 $randId = substr(mt_rand(), 0, strlen(mt_rand())/2);
  286.                 $this->sendData('NICK', "{$this->config['nick']}$randId\r\n");
  287.             break;
  288.             #####################
  289.             case 'NICK': // Update Nicks array on Nick Change
  290.                 $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
  291.                 $newuser = trim(ucwords($this->ex[2]));
  292.                 if($olduser == ucwords($this->config['nick']))
  293.                 {
  294.                     $this->sendData('NICK', "{$this->config['nick']}\r\n");
  295.                 }
  296.                 foreach($this->chans as $chan)
  297.                 {
  298.                     foreach($this->nicks[$chan] as $nickId => $nick)
  299.                     {
  300.                         if($olduser == $nick)
  301.                         {
  302.                             $this->nicks[$chan][$nickId] = $newuser;
  303.                         }
  304.                     }
  305.                 }
  306.             break;
  307.             #####################
  308.             case 'JOIN':
  309.                 $chan = trim(strtolower(substr($this->ex[2], 1)));
  310.                 $user = trim(ucwords(explode("!", substr($this->ex[0], 1))[0]));
  311.                 $this->chans[$chan] = $chan;
  312.                 $this->nicks[$chan][] = $user;
  313.             break;
  314.             #####################
  315.             case 'KICK':
  316.                 $this->sendData('JOIN', "{$this->ex[2]}\r\n");
  317.             break;
  318.             #####################
  319.             case 'PART':
  320.                 $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
  321.                 $this->currchan = trim($this->currchan);
  322.                 foreach($this->nicks[$this->currchan] as $nickId => $nick)
  323.                 {
  324.                     if($olduser == $nick)
  325.                     {
  326.                         unset($this->nicks[$this->currchan][$nickId]);
  327.                     }
  328.                 }
  329.             break;
  330.             #####################
  331.             case 'QUIT':
  332.                 $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
  333.                 foreach($this->chans as $chan)
  334.                 {
  335.                     foreach($this->nicks[$chan] as $nickId => $nick)
  336.                     {
  337.                         if($olduser == $nick)
  338.                         {
  339.                             unset($this->nicks[$chan][$nickId]);
  340.                         }
  341.                     }
  342.                 }
  343.             break;
  344.         }
  345.     }
  346.    
  347.     private function cmdSwitches()
  348.     {
  349.         // Executed Command
  350.         $command = str_replace(array(chr(10), chr(13)), '', isset($this->ex[3])?$this->ex[3]:null);
  351.  
  352.         // User/Admin Command switches outside limiter
  353.         switch($command)
  354.         {
  355.             case ':!qbot':
  356.             case ':!chat':
  357.                 $this->qbotChat();
  358.             break;
  359.             ######################
  360.             case ':!delquote':
  361.             case ':!delquotes':
  362.                 if($this->isAdmin())
  363.                 {
  364.                     $this->delQuote();
  365.                 }
  366.             break;
  367.             #####################
  368.             case ':!addadmin':
  369.                 if($this->isAdmin())
  370.                 {
  371.                     $this->addAdmin();
  372.                 }
  373.             break;
  374.             ######################
  375.             case ':!deladmin':
  376.                 if($this->isAdmin())
  377.                 {
  378.                     $this->delAdmin();
  379.                 }
  380.             break;
  381.             ######################
  382.             case ':!listadmin':
  383.             case ':!listadmins':
  384.                 if($this->isAdmin())
  385.                 {
  386.                     $this->listAdmins();
  387.                 }
  388.             break;
  389.             ######################
  390.             case ':!set':
  391.                 if($this->isAdmin())
  392.                 {
  393.                     $this->setOption();
  394.                 }
  395.             break;
  396.             ######################
  397.             case ':!get':
  398.                 if($this->isAdmin())
  399.                 {
  400.                     $this->getOption();
  401.                 }
  402.             break;
  403.             #####################
  404.             case ':!say':
  405.                 if($this->isAdmin())
  406.                 {
  407.                     $this->say2Chan();
  408.                 }
  409.             break;
  410.             ######################
  411.             case ':!join':
  412.                 if($this->isAdmin())
  413.                 {
  414.                     $this->joinChan();
  415.                 }
  416.             break;
  417.             ######################
  418.             case ':!part':
  419.                 if($this->isAdmin())
  420.                 {
  421.                     $this->partChan();
  422.                 }
  423.             break;
  424.             ######################
  425.             case ':!restart':
  426.             case ':!cycle':
  427.                 if($this->isAdmin())
  428.                 {
  429.                     $this->sendData("QUIT", "I'll be BACK...\r\n");
  430.                     sleep(2);
  431.                     $this->initCore();
  432.                 }
  433.             break;
  434.             ######################
  435.             case ':!quit':
  436.             case ':!kill':
  437.                 if($this->isAdmin())
  438.                 {
  439.                     $this->sendData("QUIT", "Bubye...\r\n");
  440.                     exit;
  441.                 }
  442.             break;
  443.         }
  444.        
  445.         // Ignore Privmsg to bot.
  446.         if(@$this->ex[1] == 'PRIVMSG' && @$this->ex[2] == $this->config['nick'])
  447.         {
  448.             $this->privmsg = true;
  449.         }
  450.        
  451.         // Rate Limits Cmds/Secs
  452.         list($rateCmds, $rateSecs) = explode("/", $this->options['ratelimit']);
  453.        
  454.         // if ! command detected and
  455.         //  rate limiter not hit then
  456.         //  increment ticker if primer is false
  457.         if(substr($command, 0, 2) == ':!' && $this->cmdTicker <= (int)$rateCmds)
  458.         {
  459.             // Check primer = false
  460.             //  to not trigger rate limiter
  461.             //  on Admin functions, or others
  462.             if($this->primer == false)
  463.             {
  464.                 $this->cmdtstamp = date(time());
  465.                 $this->cmdTicker++;
  466.             }
  467.         }
  468.        
  469.         // If Time in seconds hit
  470.         //  reset cmdTicker and cmdRated
  471.         if($this->cmdtstamp < date(time()) - (int)$rateSecs) // Time in seconds
  472.         {
  473.             $this->cmdTicker = 0;
  474.             $this->cmdRated = false;
  475.         }
  476.        
  477.         // Check cmdTicker and not a privmsg
  478.         if($this->cmdTicker <= (int)$rateCmds && $this->privmsg == false)
  479.         {
  480.             // User command switches
  481.             switch($command)
  482.             {
  483.                 case ':!help':
  484.                 case ':!info':
  485.                     $this->showHelp();
  486.                 break;
  487.                 ######################
  488.                 case ':!addquote':
  489.                     $this->addQuote();
  490.                 break;
  491.                 ######################
  492.                 case ':!quote':
  493.                     $this->listQuotes();
  494.                 break;
  495.                 ######################
  496.                 case ':!randquote':
  497.                     $this->randQuote();
  498.                 break;
  499.                 ######################
  500.                 case ':!joke':
  501.                     $this->randJoke();
  502.                 break;
  503.                 ######################
  504.                 case ':!wiki':
  505.                     $this->grabWiki();
  506.                 break;
  507.                 ######################
  508.                 case ':!google':
  509.                     $this->grabGoogle();
  510.                 break;
  511.                 ######################
  512.                 case ':!yahoo':
  513.                     $this->grabYahoo();
  514.                 break;
  515.                 ######################
  516.                 case ':!bing':
  517.                     $this->grabBing();
  518.                 break;
  519.                 ######################
  520.                 case ':!cf':
  521.                     $this->checkCloudflare();
  522.                 break;
  523.                 ######################
  524.                 case ':!ip':
  525.                     $this->getIp();
  526.                 break;
  527.                 ######################
  528.                 case ':!dns':
  529.                     $this->getDns();
  530.                 break;
  531.                 ######################
  532.                 case ':!url':
  533.                     $this->checkUrl();
  534.                 break;
  535.                 ######################
  536.                 case ':!port':
  537.                     $this->checkPort();
  538.                 break;
  539.                 ######################
  540.                 case ':!md5':
  541.                     $this->checkMD5();
  542.                 break;
  543.                 ######################
  544.                 case preg_match("/(www\.)?imdb\.com\/title\/[A-Za-z0-9]+/i", $command, $url) ? $command : !$command:
  545.                     $page = file_get_contents("http://{$url[0]}");
  546.                     preg_match("/<title>(.*)<\/title>/siU", $page, $title);
  547.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "IMDb: {$url[0]} Title: {$title[1]}\r\n");
  548.                 break;
  549.                 ######################
  550.                 case preg_match("/(www\.)?pastebin\.com\/[a-zA-Z0-9]+/i", $command, $url) ? $command : !$command:
  551.                     $page = file_get_contents("http://{$url[0]}");
  552.                     preg_match("/<title>(.*)<\/title>/siU", $page, $title);
  553.                     $title = htmlspecialchars_decode($title[1]);
  554.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Paste: {$url[0]} Title: $title\r\n");
  555.                 break;
  556.                 ######################
  557.                 case preg_match("/(www\.)?youtube\.com\/watch\?v=[a-zA-Z0-9-_]+/i", $command, $url) ? $command : !$command:
  558.                     $page = file_get_contents("https://{$url[0]}");
  559.                     preg_match("/<title>(.*)<\/title>/siU", $page, $title);
  560.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "YouTube: {$url[0]} Title: {$title[1]}\r\n");
  561.                 break;
  562.                 ######################
  563.                 case preg_match("/(www\.)?twitter\.com\/[a-zA-Z0-9-_]+/i", $command, $url) ? $command : !$command:
  564.                     $page = file_get_contents("https://{$url[0]}");
  565.                     preg_match("/<title>(.*)<\/title>/siU", $page, $title);
  566.                     $title = htmlspecialchars_decode($title[1]);
  567.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Twitter: {$url[0]} Title: $title\r\n");
  568.                 break;
  569.                 ######################
  570.                 case preg_match("/(www\.)?instagram\.com\/p\/[a-zA-Z0-9-_]+/i", $command, $url) ? $command : !$command:
  571.                     $page = file_get_contents("https://{$url[0]}");
  572.                     preg_match("/<meta property=\"og:title\" content=\"(.*)\”\" \/>/siU", $page, $title);
  573.                     $title = htmlspecialchars_decode($title[1]);
  574.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "InstaGram: {$url[0]} Title: $title\r\n");
  575.                 break;
  576.                 ######################
  577.                 case preg_match("/(en\.)?wikipedia\.org\/wiki\/[a-zA-Z0-9-_]+/i", $command, $url) ? $command : !$command:
  578.                     $page = file_get_contents("https://{$url[0]}");
  579.                     preg_match("/<title>(.*)<\/title>/siU", $page, $title);
  580.                     $title = htmlspecialchars_decode($title[1]);
  581.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Twitter: {$url[0]} Title: $title\r\n");
  582.                 break;
  583.             }
  584.         }
  585.         elseif($this->privmsg == true)
  586.         {
  587.             // Reset Privmsg check
  588.             $this->privmsg = false;
  589.         }
  590.         else
  591.         {
  592.             // Show rate limit once
  593.             if($this->cmdRated == false)
  594.             {
  595.                 $this->cmdRated = true;
  596.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Rate Limit; Wait 30 seconds ({$rateCmds}cmds/{$rateSecs}secs)\r\n");
  597.             }
  598.         }
  599.         // Reset primer check.
  600.         $this->primer = false;
  601.     }
  602.    
  603.     private function isAdmin($help = false)
  604.     {
  605.         // Set primer check true,
  606.         //  to not trigger rate limiter
  607.         $this->primer = true;
  608.        
  609.         $command = (string)strtolower(str_replace(array(chr(10), chr(13)), '', isset($this->ex[3])?$this->ex[3]:''));
  610.         $this->adminLog("'$command' executed by '{$this->ex[0]}'");
  611.        
  612.         if(count($this->admins) == 0)
  613.         {
  614.             $this->sendData("PRIVMSG {$this->ex[2]} :", "No Admins exist\r\n");
  615.             return false;
  616.         }
  617.         else
  618.         {
  619.             $data = strtolower(file_get_contents($this->config['admin_data']));        
  620.             $user = trim(substr($this->ex[0], 1));
  621.            
  622.             preg_match("/(.*)!(.*)@(.*)/", $user, $usermask);
  623.             unset($usermask[0]);
  624.             $usermask = strtolower(implode("|", $usermask));
  625.            
  626.             list($nickname, $username, $hostname) = explode("|", $usermask);
  627.            
  628.             $hostname = str_replace('127.0.0.1', 'localhost', $hostname);
  629.             $hostname = str_replace('0::1', 'localhost', $hostname);
  630.                        
  631.             if(in_array((string)"$nickname!$username@$hostname", $this->admins)) // Nickname!Username@Hostname
  632.             {
  633.                 return true;
  634.             }
  635.            
  636.             if(in_array((string)"$nickname@$hostname", $this->admins)) // Nickname@Hostname
  637.             {
  638.                 return true;
  639.             }
  640.            
  641.             if(in_array((string)"$nickname", $this->admins)) // Just Nickname
  642.             {
  643.                 return true;
  644.             }
  645.             if($help == false)
  646.             {
  647.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Unable to process; $nickname is not on the admin list\r\n");
  648.             }
  649.             return false;
  650.         }
  651.     }
  652.    
  653.     // Post User functions
  654.     private function showHelp()
  655.     {
  656.         $this->sendData("PRIVMSG {$this->ex[2]} :", "QuoterBot Help/Info sent over PM!\r\n");
  657.         $this->sendData("PRIVMSG {$this->curruser} :", "QuoterBot Help/Info ====\r\n");
  658.         $this->sendData("PRIVMSG {$this->curruser} :", "========================\r\n");
  659.         $this->sendData("PRIVMSG {$this->curruser} :", "User Functions:\r\n");
  660.         $this->sendData("PRIVMSG {$this->curruser} :", "!quote BLANK|NICK|NICK ID\r\n");
  661.         $this->sendData("PRIVMSG {$this->curruser} :", "!randquote\r\n");
  662.         $this->sendData("PRIVMSG {$this->curruser} :", "!addquote TEXT\r\n");
  663.         $this->sendData("PRIVMSG {$this->curruser} :", "!joke BLANK|NICK\r\n");
  664.         $this->sendData("PRIVMSG {$this->curruser} :", "!wiki QUERY\r\n");
  665.         $this->sendData("PRIVMSG {$this->curruser} :", "!google QUERY\r\n");
  666.         $this->sendData("PRIVMSG {$this->curruser} :", "!yahoo QUERY\r\n");
  667.         $this->sendData("PRIVMSG {$this->curruser} :", "!bing QUERY\r\n");
  668.         $this->sendData("PRIVMSG {$this->curruser} :", "!cf DNS\r\n");
  669.         $this->sendData("PRIVMSG {$this->curruser} :", "!ip DNS\r\n");
  670.         $this->sendData("PRIVMSG {$this->curruser} :", "!dns IP\r\n");
  671.         $this->sendData("PRIVMSG {$this->curruser} :", "!url IP|DNS|URI\r\n");
  672.         $this->sendData("PRIVMSG {$this->curruser} :", "!port DNS|IP PORT\r\n");
  673.         $this->sendData("PRIVMSG {$this->curruser} :", "!md5 STR|MD5\r\n");
  674.         $this->sendData("PRIVMSG {$this->curruser} :", "!chat MSG\r\n");
  675.         if($this->isAdmin(true))
  676.         {
  677.             $this->sendData("PRIVMSG {$this->curruser} :", "=========================\r\n");
  678.             $this->sendData("PRIVMSG {$this->curruser} :", "Admin Functions:\r\n");
  679.             $this->sendData("PRIVMSG {$this->curruser} :", "!say CHAN MSG\r\n");
  680.             $this->sendData("PRIVMSG {$this->curruser} :", "!join #CHAN & !part #CHAN\r\n");
  681.             $this->sendData("PRIVMSG {$this->curruser} :", "!delquote BLANK|NICK QuoteID\r\n");
  682.             $this->sendData("PRIVMSG {$this->curruser} :", "!listadmin & !addadmin NICK & !deladmin NICK\r\n");
  683.             $this->sendData("PRIVMSG {$this->curruser} :", "!get addquote & !set addquote on|off\r\n");
  684.             $this->sendData("PRIVMSG {$this->curruser} :", "!get autojoke & !set autojoke on|off\r\n");
  685.             $this->sendData("PRIVMSG {$this->curruser} :", "!get joketime & !set joketime Minute(s)\r\n");
  686.             $this->sendData("PRIVMSG {$this->curruser} :", "!get ratelimit & !set ratelimit Cmds/Secs\r\n");
  687.             $this->sendData("PRIVMSG {$this->curruser} :", "!restart & !quit\r\n");
  688.         }
  689.     }
  690.    
  691.     private function addQuote()
  692.     {  
  693.         if($this->options['addquote'] == 'on')
  694.         {
  695.             $ex = $this->ex;
  696.             $byuser = trim(strtolower(substr(explode('!', $ex[0])[0], 1)));
  697.             unset($ex[0], $ex[1], $ex[2], $ex[3]);
  698.            
  699.             $quotetxt = trim(implode(' ', $ex));
  700.             $quotetxt = substr($quotetxt, 0, 128);
  701.            
  702.             if($quotetxt != '')
  703.             {
  704.                 if(!in_array($quotetxt, $this->quotes[$byuser]))
  705.                 {
  706.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Adding Quote by $byuser: [$quotetxt]\r\n");
  707.                     $this->quotes[$byuser][] = $quotetxt;
  708.                    
  709.                     file_put_contents($this->config['quote_data'], '');
  710.                     file_put_contents($this->config['quote_data'], (string)serialize($this->quotes));
  711.                 }
  712.                 else
  713.                 {
  714.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "That quote already exists by $byuser: [$quotetxt]\r\n");
  715.                 }
  716.             }
  717.             else
  718.             {
  719.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "A blank quote has been entered by $byuser: [$quotetxt]\r\n");
  720.             }
  721.         }
  722.         else
  723.         {
  724.             $this->sendData("PRIVMSG {$this->ex[2]} :", "Add Quotes Disabled\r\n");
  725.         }
  726.     }
  727.    
  728.     private function delQuote()
  729.     {
  730.         $byuser = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  731.         $quoteid = trim(isset($this->ex[5])?$this->ex[5]:null);
  732.        
  733.         if($quoteid != null)
  734.         {
  735.             if(is_array($this->quotes[$byuser]))
  736.             {
  737.                 $quotetxt = $this->quotes[$byuser][$quoteid-1];
  738.                 unset($this->quotes[$byuser][$quoteid-1]);
  739.                
  740.                 if(count($this->quotes[$byuser]) == 0)
  741.                 {
  742.                     unset($this->quotes[$byuser]);
  743.                 }
  744.                
  745.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Deleted Quote by $byuser with id#$quoteid: [$quotetxt]\r\n");
  746.                
  747.                 file_put_contents($this->config['quote_data'], '');
  748.                 file_put_contents($this->config['quote_data'], (string)serialize($this->quotes));
  749.             }
  750.             else
  751.             {
  752.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Unable to Delete Quote by $byuser with id#$quoteid: [User quote doesn't exist]\r\n");
  753.             }
  754.         }
  755.         else
  756.         {
  757.             if($byuser != null)
  758.             {
  759.                 if(is_array(@$this->quotes[$byuser]))
  760.                 {
  761.                     foreach($this->quotes[$byuser] as $quoteid => $quotetxt)
  762.                     {
  763.                         unset($this->quotes[$byuser][$quoteid]);
  764.                     }
  765.                     unset($this->quotes[$byuser]);
  766.  
  767.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Deleted all Quotes by $byuser\r\n");
  768.                    
  769.                     file_put_contents($this->config['quote_data'], '');
  770.                     file_put_contents($this->config['quote_data'], (string)serialize($this->quotes));
  771.                 }
  772.                 else
  773.                 {
  774.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Quotes by $byuser exist\r\n");
  775.                 }
  776.             }
  777.             else
  778.             {
  779.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Unable to Delete Quote by $byuser [User Quotes don't exist]\r\n");
  780.             }
  781.         }
  782.     }
  783.    
  784.     private function listQuotes()
  785.     {
  786.         $byuser = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  787.         $quoteId = trim(isset($this->ex[5])?$this->ex[5]:null);
  788.  
  789.         if($byuser != null)
  790.         {
  791.             if($quoteId != null)
  792.             {
  793.                 if(isset($this->quotes[$byuser][$quoteId-1]))
  794.                 {
  795.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "$byuser#$quoteId) {$this->quotes[$byuser][$quoteId-1]}\r\n");
  796.                 }
  797.                 else
  798.                 {
  799.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Quotes by $byuser exists with ID: $quoteId\r\n");
  800.                 }
  801.             }
  802.             else
  803.             {
  804.                 if(is_array($this->quotes[$byuser]))
  805.                 {
  806.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Listing Quotes by $byuser over PM\r\n");
  807.                     $this->sendData("PRIVMSG {$this->curruser} :", "Listing Quotes by $byuser\r\n");
  808.                     foreach($this->quotes[$byuser] as $quoteid => $quotetxt)
  809.                     {
  810.                         $quoteid++;
  811.                         $this->sendData("PRIVMSG {$this->curruser} :", "$quoteid) $quotetxt\r\n");
  812.                     }
  813.                 }
  814.                 else
  815.                 {
  816.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Quotes by $byuser exist\r\n");
  817.                 }
  818.             }
  819.         }
  820.         else
  821.         {
  822.             if(count($this->quotes) > 0)
  823.             {
  824.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Listing Quotes over PM\r\n");
  825.                 $this->sendData("PRIVMSG {$this->curruser} :", "Listing Quotes\r\n");
  826.                 foreach($this->quotes as $byuser => $quotes)
  827.                 {
  828.                     foreach($quotes as $quoteid => $quotetxt)
  829.                     {
  830.                         $quoteid++;
  831.                         if($quotetxt != null)
  832.                         {
  833.                             $this->sendData("PRIVMSG {$this->curruser} :", "$byuser#$quoteid) $quotetxt\r\n");
  834.                         }
  835.                     }
  836.                 }
  837.             }
  838.             else
  839.             {
  840.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Quotes exist\r\n");
  841.             }
  842.         }
  843.     }
  844.    
  845.     private function randQuote()
  846.     {
  847.         $byuser = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  848.         if($byuser != null)
  849.         {
  850.             if(is_array($this->quotes[$byuser]))
  851.             {
  852.                 $quoteId = array_rand($this->quotes[$byuser]);
  853.                 $quoteId++;
  854.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Quote: $byuser#$quoteId) {$this->quotes[$byuser][$quoteId-1]}\r\n");
  855.             }
  856.             else
  857.             {
  858.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Quotes by $byuser exist\r\n");
  859.             }
  860.         }
  861.         else
  862.         {
  863.             if(count($this->quotes) > 0)
  864.             {
  865.                 $byuser = array_rand($this->quotes);
  866.                 $quotes = array_values($this->quotes);
  867.                 $quoteId = array_rand($this->quotes[$byuser]);
  868.                 $quoteId++;
  869.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Quote: {$this->quotes[$byuser][$quoteId-1]}\r\n");
  870.             }
  871.             else
  872.             {
  873.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Quotes exist\r\n");
  874.             }
  875.         }
  876.     }
  877.    
  878.     private function randJoke($chan = null)
  879.     {
  880.         $ex = $this->ex;
  881.         unset($ex[0], $ex[1], $ex[2], $ex[3]);
  882.        
  883.         $foruser = trim(ucwords(str_replace('\\', '', substr(implode(' ', $ex), 0, 20))));
  884.         $chan = ($chan == null)?$this->ex[2]:$chan;
  885.        
  886.         $nickId = array_rand($this->nicks[$chan]);
  887.         $nickRnd = $this->nicks[$chan][$nickId];
  888.        
  889.         if($foruser != '' && in_array($foruser, $this->nicks[$chan]))
  890.         {
  891.             $jokeAr = array_merge($this->fetchJoke($foruser), $this->fetchJoke($foruser));
  892.             $jokeId = array_rand($jokeAr);
  893.             $joke = $jokeAr[$jokeId];
  894.         }
  895.         else
  896.         {
  897.             $jokeAr = array_merge($this->fetchJoke($nickRnd), $this->fetchJoke($nickRnd));
  898.             $jokeId = array_rand($jokeAr);
  899.             $joke = $jokeAr[$jokeId];
  900.         }
  901.        
  902.         $this->sendData("PRIVMSG $chan :", "Joke: $joke\r\n");
  903.     }
  904.    
  905.     private function fetchJoke($nick = null)
  906.     {  
  907.         $data = file_get_contents("http://api.icndb.com/jokes/random?firstName=".urlencode($nick));
  908.         $data = json_decode($data, true)['value']['joke'];
  909.         $data = str_replace(':', '', $data);
  910.         $data = str_replace(' Norris', '', $data);
  911.         $joke = htmlspecialchars_decode($data);
  912.        
  913.         $data2 = file_get_contents("http://api.yomomma.info/");
  914.         $data2 = json_decode($data2, true)['joke'];
  915.         $joke2 = htmlspecialchars_decode($data2);
  916.         $joke2 = "$nick, $joke2";
  917.        
  918.         if($nick == null)
  919.         {
  920.             $data3 = file_get_contents("http://tambal.azurewebsites.net/joke/random");
  921.             $data3 = json_decode($data3, true)['joke'];
  922.             $joke3 = htmlspecialchars_decode($data3);
  923.             $joke3 = "$joke3";
  924.            
  925.             $jokes = array_merge(array($joke), array($joke2), array($joke3));
  926.         }
  927.         else
  928.         {
  929.             $jokes = array_merge(array($joke), array($joke2));
  930.         }
  931.        
  932.         return (count($jokes)==2||count($jokes)==3)?$jokes:false;
  933.     }
  934.    
  935.     private function grabWiki()
  936.     {
  937.         $ex = $this->ex;
  938.         unset($ex[0], $ex[1], $ex[2], $ex[3]);
  939.        
  940.         $searchtxt = trim(implode('+', $ex));
  941.         $searchtxt = substr($searchtxt, 0, 128);
  942.        
  943.         $ch = curl_init();
  944.         curl_setopt($ch, CURLOPT_URL, "https://en.wikipedia.org/w/index.php?search=$searchtxt&title=Special%3ASearch&go=Go");
  945.         curl_setopt($ch, CURLOPT_HEADER, TRUE);
  946.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  947.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  948.         $data = curl_exec($ch);
  949.        
  950.         if(preg_match('#Location: (.*)#', $data, $wikiUri))
  951.         {
  952.             $this->sendData("PRIVMSG {$this->ex[2]} :", "Wiki: {$wikiUri[1]}\r\n");
  953.         }
  954.         else
  955.         {
  956.             $this->sendData("PRIVMSG {$this->ex[2]} :", "Wiki: Unable to process search\r\n");
  957.         }
  958.     }
  959.    
  960.     private function grabGoogle()
  961.     {
  962.         $ex = $this->ex;
  963.         unset($ex[0], $ex[1], $ex[2], $ex[3]);
  964.        
  965.         $searchtxt = trim(implode('+', $ex));
  966.         $searchtxt = substr($searchtxt, 0, 128);
  967.        
  968.         $data = file_get_contents("https://search.disconnect.me/searchTerms/search?query=$searchtxt&lang=en&location=US&location_option=US&ses=Google&ses_nojs=Google&option=Web");
  969.         preg_match("/<a href=\"(.*)\" class=\"title\">(.*)<\/a>/i", $data, $result);
  970.         $this->sendData("PRIVMSG {$this->ex[2]} :", "Google: {$result[1]} Title: {$result[2]}\r\n");
  971.     }
  972.    
  973.     private function grabYahoo()
  974.     {
  975.         $ex = $this->ex;
  976.         unset($ex[0], $ex[1], $ex[2], $ex[3]);
  977.        
  978.         $searchtxt = trim(implode('+', $ex));
  979.         $searchtxt = substr($searchtxt, 0, 128);
  980.        
  981.         $data = file_get_contents("https://search.disconnect.me/searchTerms/search?query=$searchtxt&lang=en&location=US&location_option=US&ses=Yahoo&ses_nojs=Yahoo&option=Web");
  982.         preg_match("/<a href=\"(.*)\" class=\"title\">(.*)<\/a>/i", $data, $result);
  983.         $this->sendData("PRIVMSG {$this->ex[2]} :", "Yahoo: {$result[1]} Title: {$result[2]}\r\n");
  984.     }
  985.    
  986.     private function grabBing()
  987.     {
  988.         $ex = $this->ex;
  989.         unset($ex[0], $ex[1], $ex[2], $ex[3]);
  990.        
  991.         $searchtxt = trim(implode('+', $ex));
  992.         $searchtxt = substr($searchtxt, 0, 128);
  993.        
  994.         $data = file_get_contents("https://search.disconnect.me/searchTerms/search?query=$searchtxt&lang=en&location=US&location_option=US&ses=Bing&ses_nojs=Bing&option=Web");
  995.         preg_match("/<a href=\"(.*)\" class=\"title\">(.*)<\/a>/i", $data, $result);
  996.         $this->sendData("PRIVMSG {$this->ex[2]} :", "Bing: {$result[1]} Title: {$result[2]}\r\n");
  997.     }
  998.    
  999.     private function checkCloudflare()
  1000.     {
  1001.         $dnsname = (string)trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  1002.        
  1003.         if($dnsname != null)
  1004.         {
  1005.             $dnsname = str_replace('http://', '', $dnsname);
  1006.             $dnsname = str_replace('https://', '', $dnsname);
  1007.            
  1008.             $protocol = '';
  1009.             if(fsockopen($dnsname, 80, $errno, $errstr, 1))
  1010.             {
  1011.                 $protocol = 'http';
  1012.             }
  1013.             elseif(fsockopen($dnsname, 443, $errno, $errstr, 1))
  1014.             {
  1015.                 $protocol = 'https';
  1016.             }
  1017.            
  1018.             if($protocol != '')
  1019.             {
  1020.                 if(!$this->isValidUrl($dnsname))
  1021.                 {
  1022.                     $url = "$protocol://$dnsname";
  1023.                 }
  1024.                
  1025.                 if($this->isCloudFlare($url))
  1026.                 {
  1027.                     $host = parse_url($url)['host'];
  1028.                     if(strstr($host, 'www.'))
  1029.                     {
  1030.                         $host = explode('www.', $host)[1];
  1031.                     }
  1032.                    
  1033.                     foreach($this->_subd_alloc as $subdomain)
  1034.                     {
  1035.                         $host_pre_ = "{$subdomain}.{$host}";
  1036.                         $host_ip = gethostbyname($host_pre_);
  1037.                         if($this->isValidIp($host_ip))
  1038.                         {
  1039.                             if($this->isCloudFlare($host_ip))
  1040.                             {
  1041.                                 # To show or not to show?
  1042.                                 //$this->sendData("PRIVMSG {$this->ex[2]} :", "{$host_pre_} - {$host_ip} is Cloudflare\r\n");
  1043.                             }
  1044.                             else
  1045.                             {
  1046.                                 $this->sendData("PRIVMSG {$this->ex[2]} :", "{$host_pre_} - {$host_ip} is a Direct IP\r\n");
  1047.                             }
  1048.                         }
  1049.                     }
  1050.                 }
  1051.             }
  1052.             else
  1053.             {
  1054.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "CFlare: Unable to continue, Perhaps no webserver is running?\r\n");
  1055.             }
  1056.         }
  1057.         else
  1058.         {
  1059.             $this->sendData("PRIVMSG {$this->ex[2]} :", "CFlare: You must enter a DNS Name\r\n");
  1060.         }
  1061.     }
  1062.  
  1063.     private function isCloudFlare($host)
  1064.     {
  1065.         if($this->isValidIp($host))
  1066.         {
  1067.             foreach($this->_addr_alloc as $addr_alloc)
  1068.             {
  1069.                 if($this->cidr_match($host, $addr_alloc))
  1070.                 {
  1071.                     return true;
  1072.                 }
  1073.             }
  1074.             return false;
  1075.         }
  1076.         elseif($this->isValidUrl($host))
  1077.         {
  1078.             $headers = implode("\n", get_headers($host));
  1079.             $headers = explode("cloudflare", $headers);
  1080.             return count($headers)>1 ? true : false;
  1081.         }
  1082.     }
  1083.    
  1084.     private function getIp()
  1085.     {
  1086.         $dnsname = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  1087.         if($dnsname != null)
  1088.         {
  1089.             $dnsname = str_replace('http://', '', $dnsname);
  1090.             $dnsname = str_replace('https://', '', $dnsname);
  1091.             $ipaddr = gethostbynamel($dnsname);
  1092.             $ipaddr = implode("|", $ipaddr);
  1093.             $this->sendData("PRIVMSG {$this->ex[2]} :", "GetIP: $ipaddr DNS: $dnsname\r\n");
  1094.         }
  1095.         else
  1096.         {
  1097.             $this->sendData("PRIVMSG {$this->ex[2]} :", "GetIP: You must enter a DNS Name\r\n");
  1098.         }
  1099.     }
  1100.    
  1101.     private function getDns()
  1102.     {
  1103.         $ipaddr = trim(isset($this->ex[4])?$this->ex[4]:null);
  1104.         if($ipaddr != null)
  1105.         {
  1106.             $ipaddr = str_replace('http://', '', $ipaddr);
  1107.             $ipaddr = str_replace('https://', '', $ipaddr);
  1108.             $dnsname = gethostbyaddr($ipaddr);
  1109.             $dnsname = ($dnsname!=$ipaddr)?$dnsname:'dns-not-set';
  1110.             $this->sendData("PRIVMSG {$this->ex[2]} :", "GetDNS: $ipaddr DNS: $dnsname\r\n");
  1111.         }
  1112.         else
  1113.         {
  1114.             $this->sendData("PRIVMSG {$this->ex[2]} :", "GetDNS: You must enter an IP Address\r\n");
  1115.         }
  1116.     }
  1117.    
  1118.     private function checkUrl()
  1119.     {
  1120.         $forurl = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  1121.         if($forurl != null)
  1122.         {
  1123.             $ch = curl_init();
  1124.             curl_setopt($ch, CURLOPT_URL, "$forurl");
  1125.             curl_setopt($ch, CURLOPT_HEADER, FALSE);
  1126.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  1127.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  1128.             $data = curl_exec($ch);
  1129.             if(!curl_errno($ch))
  1130.             {
  1131.                 $info = curl_getinfo($ch);
  1132.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckUrl: {$info['url']} is UP and took {$info['total_time']} seconds to respond\r\n");
  1133.             }
  1134.             else
  1135.             {
  1136.                 $info = curl_getinfo($ch);
  1137.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckUrl: {$info['url']} is DOWN.\r\n");
  1138.             }
  1139.             curl_close($ch);
  1140.         }
  1141.         else
  1142.         {
  1143.             $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckUrl: Please enter a URL to check, making sure to include the protocol\r\n");
  1144.         }
  1145.     }
  1146.    
  1147.     private function checkPort()
  1148.     {
  1149.         $dnsname = (string)trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  1150.         $portnum = (int)trim(isset($this->ex[5])?$this->ex[5]:0);
  1151.         if($dnsname != null)
  1152.         {
  1153.             if($portnum >= 1 && $portnum <= 65535)
  1154.             {
  1155.                 $dnsname = str_replace('http://', '', $dnsname);
  1156.                 $dnsname = str_replace('https://', '', $dnsname);
  1157.                 if(@fsockopen($dnsname, $portnum, $errno, $errstr, 2))
  1158.                 {
  1159.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckPort: $dnsname:$portnum is UP\r\n");
  1160.                 }
  1161.                 else
  1162.                 {
  1163.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckPort: $dnsname:$portnum is DOWN\r\n");
  1164.                 }
  1165.             }
  1166.             else
  1167.             {
  1168.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckPort: You must enter a Port Number (1-65535)\r\n");
  1169.             }
  1170.         }
  1171.         else
  1172.         {
  1173.             $this->sendData("PRIVMSG {$this->ex[2]} :", "CheckPort: You must enter a DNS Name\r\n");
  1174.         }
  1175.     }
  1176.    
  1177.     private function checkMD5()
  1178.     {
  1179.         $md5str = (string)trim(isset($this->ex[4])?$this->ex[4]:null);
  1180.         if($md5str != null)
  1181.         {
  1182.             if(preg_match('/^[a-f0-9]{32}$/i', $md5str))
  1183.             {
  1184.                 $return = json_decode(file_get_contents("http://api.md5crack.com/crack/mZKoDn98W9l843ra/".$md5str));
  1185.                 $return = ($return->phrase!='')?$return->phrase:$return->response;
  1186.                 $len = strlen($return);
  1187.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "MD5: $md5str String($len): $return\r\n");
  1188.             }
  1189.             else
  1190.             {
  1191.                 $ex = $this->ex;
  1192.                 unset($ex[0], $ex[1], $ex[2], $ex[3]);
  1193.                 $ex = trim(implode(" ", $ex));
  1194.                 $return = json_decode(file_get_contents("http://api.md5crack.com/hash/mZKoDn98W9l843ra/".$ex));
  1195.                 $len = strlen($ex);
  1196.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "MD5: {$return->phrase} String($len): $ex\r\n");
  1197.             }
  1198.         }
  1199.         else
  1200.         {
  1201.             $this->sendData("PRIVMSG {$this->ex[2]} :", "MD5: You must enter a String or an MD5\r\n");
  1202.         }
  1203.     }
  1204.    
  1205.     private function qbotChat($privmsg = false)
  1206.     {
  1207.         // Set primer check true,
  1208.         //  to not trigger rate limiter
  1209.         $this->primer = true;
  1210.         $ex = $this->ex;
  1211.         unset($ex[0], $ex[1], $ex[2]);
  1212.         if($privmsg == false)
  1213.         {
  1214.             unset($ex[3]);
  1215.         }
  1216.         $chattxt = trim(substr(implode(" ", $ex), 1));
  1217.        
  1218.         $byuser = substr(explode('!', $this->ex[0])[0], 1);
  1219.         $chattxt = urlencode($chattxt);
  1220.         $result = file_get_contents("http://www.simsimi.com/requestChat?lc=en&ft=1.0&req=$chattxt&uid=85422145");
  1221.         $chattxt = json_decode($result, true);
  1222.         if(strpos($chattxt['res'], 'I HAVE NO RESPONSE') === false)
  1223.         {
  1224.             $this->sendData("PRIVMSG {$this->ex[2]} :", "$byuser, {$chattxt['res']}\r\n");
  1225.         }
  1226.         else
  1227.         {
  1228.             $this->sendData("PRIVMSG {$this->ex[2]} :", "$byuser, Does not compute!? I didn't understand that...\r\n");
  1229.         }
  1230.     }
  1231.    
  1232.     // Post Admin Functions
  1233.     private function addAdmin()
  1234.     {
  1235.         $foruser = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  1236.        
  1237.         if($foruser != null)
  1238.         {
  1239.             if(!in_array($foruser, $this->admins))
  1240.             {
  1241.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Adding Admin for $foruser\r\n");
  1242.                 $this->admins[] = $foruser;
  1243.                
  1244.                 file_put_contents($this->config['admin_data'], '');
  1245.                 file_put_contents($this->config['admin_data'], (string)serialize($this->admins));
  1246.             }
  1247.             else
  1248.             {
  1249.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "The Admin for $foruser already exists\r\n");
  1250.             }
  1251.         }
  1252.         else
  1253.         {
  1254.             $this->sendData("PRIVMSG {$this->ex[2]} :", "You've entered a blank nickname\r\n");
  1255.         }
  1256.     }
  1257.    
  1258.     private function delAdmin()
  1259.     {
  1260.         $foruser = trim(strtolower(isset($this->ex[4])?$this->ex[4]:null));
  1261.        
  1262.         if($foruser != null)
  1263.         {
  1264.             if(in_array($foruser, $this->admins))
  1265.             {
  1266.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Deleting Admin for $foruser\r\n");
  1267.                
  1268.                 foreach($this->admins as $adminId => $admin)
  1269.                 {
  1270.                     if($admin == $foruser)
  1271.                     {
  1272.                         unset($this->admins[$adminId]);
  1273.                     }
  1274.                 }
  1275.                
  1276.                 file_put_contents($this->config['admin_data'], '');
  1277.                 file_put_contents($this->config['admin_data'], (string)serialize($this->admins));
  1278.             }
  1279.             else
  1280.             {
  1281.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "The Admin for $foruser doesn't exist\r\n");
  1282.             }
  1283.         }
  1284.         else
  1285.         {
  1286.             $this->sendData("PRIVMSG {$this->ex[2]} :", "You've entered a blank nickname\r\n");
  1287.         }
  1288.     }
  1289.    
  1290.     private function listAdmins()
  1291.     {
  1292.         if(count($this->admins) >= 1 )
  1293.         {
  1294.             $this->sendData("PRIVMSG {$this->ex[2]} :", "Listing Admins over PM\r\n");
  1295.             $this->sendData("PRIVMSG {$this->curruser} :", "Listing Admins\r\n");
  1296.             $this->admins = array_unique($this->admins);
  1297.             foreach($this->admins as $adminId => $admin)
  1298.             {
  1299.                 $adminId++;
  1300.                 $this->sendData("PRIVMSG {$this->curruser} :", "$adminId) $admin\r\n");
  1301.             }
  1302.         }
  1303.         else
  1304.         {
  1305.             $this->sendData("PRIVMSG {$this->ex[2]} :", "Currently no Admins exist\r\n");
  1306.         }
  1307.     }
  1308.    
  1309.     private function setOption()
  1310.     {
  1311.         $option = trim(isset($this->ex[4])?$this->ex[4]:null);
  1312.         if($option != null)
  1313.         {
  1314.             $setting = trim(isset($this->ex[5])?$this->ex[5]:null);
  1315.             switch(strtolower($option))
  1316.             {
  1317.                 case 'addquote':
  1318.                 case 'addquotes':
  1319.                     switch($setting)
  1320.                     {
  1321.                         case 'on':
  1322.                             $this->options['addquote'] = 'on';
  1323.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Add Quotes Enabled\r\n");
  1324.                         break;
  1325.                         case 'off':
  1326.                             $this->options['addquote'] = 'off';
  1327.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Add Quotes Disabled\r\n");
  1328.                         break;
  1329.                         default:
  1330.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Err: !set addquote on|off (To enable or disable adding quotes)\r\n");
  1331.                         break;
  1332.                     }
  1333.                 break;
  1334.                 case 'autochat':
  1335.                 case 'autochats':
  1336.                     switch($setting)
  1337.                     {
  1338.                         case 'on':
  1339.                             $this->options['autochat'] = 'on';
  1340.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Auto Chat Enabled\r\n");
  1341.                         break;
  1342.                         case 'off':
  1343.                             $this->options['autochat'] = 'off';
  1344.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Auto Chat Disabled\r\n");
  1345.                         break;
  1346.                         default:
  1347.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Err: !set autochat on|off (To enable or disable automatic chat)\r\n");
  1348.                         break;
  1349.                     }
  1350.                 break;
  1351.                 case 'autojoke':
  1352.                 case 'autojoker':
  1353.                     switch($setting)
  1354.                     {
  1355.                         case 'on':
  1356.                             $this->options['autojoke'] = 'on';
  1357.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Auto Joke Enabled\r\n");
  1358.                         break;
  1359.                         case 'off':
  1360.                             $this->options['autojoke'] = 'off';
  1361.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Auto Joke Disabled\r\n");
  1362.                         break;
  1363.                         default:
  1364.                             $this->sendData("PRIVMSG {$this->ex[2]} :", "Err: !set joke on|off (To enable or disable auto joke)\r\n");
  1365.                         break;
  1366.                     }
  1367.                 break;
  1368.                 case 'joketime':
  1369.                 case 'joketimer':
  1370.                     if((int)$setting)
  1371.                     {
  1372.                         $this->options['joketime'] = $setting;
  1373.                         $this->sendData("PRIVMSG {$this->ex[2]} :", "Auto joke timer set to $setting minute(s)\r\n");
  1374.                     }
  1375.                     else
  1376.                     {
  1377.                         $this->sendData("PRIVMSG {$this->ex[2]} :", "Err: !set joketime Minute(s) (To set joke timer in Minutes(s))\r\n");
  1378.                     }
  1379.                 break;
  1380.                 case 'ratelimit':
  1381.                 case 'ratelimter':
  1382.                     if((string)$setting)
  1383.                     {
  1384.                         $this->options['ratelimit'] = (string)$setting;
  1385.                         $this->sendData("PRIVMSG {$this->ex[2]} :", "Rate Limiter set to $setting Commands in Second(s)\r\n");
  1386.                     }
  1387.                     else
  1388.                     {
  1389.                         $this->sendData("PRIVMSG {$this->ex[2]} :", "Err: !set ratelimit Cmds/Secs (To set rate limter in Cmd/Secs ratio)\r\n");
  1390.                     }
  1391.                 break;
  1392.             }
  1393.             file_put_contents($this->config['confs_data'], '');
  1394.             file_put_contents($this->config['confs_data'], (string)serialize($this->options));
  1395.         }
  1396.         else
  1397.         {
  1398.             $this->sendData("PRIVMSG {$this->ex[2]} :", "You must choose an option!\r\n");
  1399.             $this->sendData("PRIVMSG {$this->ex[2]} :", "==========================\r\n");
  1400.             foreach($this->options as $opt => $val)
  1401.             {
  1402.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "!set $opt on|off\r\n");
  1403.             }
  1404.         }
  1405.     }
  1406.  
  1407.     private function getOption()
  1408.     {
  1409.         $option = trim(isset($this->ex[4])?$this->ex[4]:null);
  1410.         if($option != null)
  1411.         {
  1412.             switch(strtolower($option))
  1413.             {
  1414.                 default:
  1415.                     $optId = 0;
  1416.                     foreach($this->options as $opt => $val)
  1417.                     {
  1418.                         $optId++;
  1419.                         $this->sendData("PRIVMSG {$this->ex[2]} :", "$optId) Opt: $opt Val: $val\r\n");
  1420.                     }
  1421.                 break;
  1422.                 case 'addquote':
  1423.                 case 'addquotes':
  1424.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Opt: $option Val: {$this->options['addquote']}\r\n");
  1425.                 break;
  1426.                 case 'autochat':
  1427.                 case 'autochats':
  1428.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Opt: $option Val: {$this->options['autochat']}\r\n");
  1429.                 break;
  1430.                 case 'autojoke':
  1431.                 case 'autojoker':
  1432.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Opt: $option Val: {$this->options['autojoke']}\r\n");
  1433.                 break;
  1434.                 case 'joketime':
  1435.                 case 'joketimer':
  1436.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Opt: $option Val: {$this->options['joketime']}\r\n");
  1437.                 break;
  1438.                 case 'ratelimit':
  1439.                 case 'ratelimiter':
  1440.                     $this->sendData("PRIVMSG {$this->ex[2]} :", "Opt: $option Val: {$this->options['ratelimter']}\r\n");
  1441.                 break;
  1442.             }
  1443.         }
  1444.         else
  1445.         {
  1446.             $this->sendData("PRIVMSG {$this->ex[2]} :", "Options sent over PM\r\n");
  1447.             $this->sendData("PRIVMSG {$this->curruser} :", "You must choose an option!\r\n");
  1448.             $this->sendData("PRIVMSG {$this->curruser} :", "==========================\r\n");
  1449.             foreach($this->options as $opt => $val)
  1450.             {
  1451.                 $this->sendData("PRIVMSG {$this->curruser} :", "!get $opt\r\n");
  1452.             }
  1453.         }
  1454.     }
  1455.    
  1456.     private function say2Chan()
  1457.     {  
  1458.         $ex = $this->ex;
  1459.         unset($ex[0], $ex[1], $ex[2], $ex[3], $ex[4]);
  1460.        
  1461.         $msg = implode(" ", $ex);
  1462.         $msg = trim($msg!=''?$msg:null);
  1463.         $chan = trim(isset($this->ex[4])?$this->ex[4]:null);
  1464.        
  1465.         if($chan != null)
  1466.         {
  1467.             if($msg != null)
  1468.             {
  1469.                 $this->sendData("PRIVMSG $chan :", "$msg\r\n");
  1470.             }
  1471.             else
  1472.             {
  1473.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "You must write a message! (!say chan msg)\r\n");
  1474.             }
  1475.         }
  1476.         else
  1477.         {
  1478.             $this->sendData("PRIVMSG {$this->ex[2]} :", "You must give a channel! (!say chan msg)\r\n");
  1479.         }
  1480.     }
  1481.    
  1482.     private function joinChan()
  1483.     {
  1484.         $chan = trim(isset($this->ex[4])?$this->ex[4]:null);
  1485.         if($chan != null)
  1486.         {
  1487.             if(!isset($this->chans[$chan]))
  1488.             {
  1489.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Joining Channel $chan\r\n");
  1490.                 $this->sendData("JOIN", "{$this->ex[4]}");
  1491.                 $this->chans[$this->ex[4]] = $this->ex[4];
  1492.             }
  1493.             else
  1494.             {
  1495.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Already joined to $chan\r\n");
  1496.             }
  1497.         }
  1498.         else
  1499.         {
  1500.             $this->sendData("PRIVMSG {$this->ex[2]} :", "You must enter a channel to join!\r\n");
  1501.         }
  1502.     }
  1503.    
  1504.     private function partChan()
  1505.     {
  1506.         $chan = trim(isset($this->ex[4])?$this->ex[4]:null);
  1507.         if($chan != null)
  1508.         {
  1509.             if(isset($this->chans[$chan]))
  1510.             {
  1511.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Parting Channel $chan\r\n");
  1512.                 $this->sendData("PART", "{$this->ex[4]}");
  1513.                 unset($this->chans[$chan]);
  1514.             }
  1515.             else
  1516.             {
  1517.                 $this->sendData("PRIVMSG {$this->ex[2]} :", "Already parted from $chan\r\n");
  1518.             }
  1519.         }
  1520.         else
  1521.         {
  1522.             $this->sendData("PART", "{$this->ex[2]}");
  1523.         }
  1524.     }
  1525.    
  1526.     // Send/Recv/AdminLog Functions
  1527.     private function sendData($command, $message = null)
  1528.     {
  1529.         echo 'SND-MSG: ';
  1530.         if($message == null)
  1531.         {
  1532.             fputs($this->socket, (string)"$command\r\n");
  1533.             if($this->is_CLI)
  1534.             {
  1535.                 echo "$command" . PHP_EOL;
  1536.             }
  1537.             else
  1538.             {
  1539.                 echo "<font color='green'><b>$command</b></font><br>" . PHP_EOL;
  1540.             }
  1541.         }
  1542.         else
  1543.         {
  1544.             fputs($this->socket, (string)"$command $message\r\n");
  1545.             if($this->is_CLI)
  1546.             {
  1547.                 echo "$command -> $message" . PHP_EOL;
  1548.             }
  1549.             else
  1550.             {
  1551.                 echo "<font color='green'><b>$command -> $message</b></font><br>" . PHP_EOL;
  1552.                 flush();
  1553.             }
  1554.         }
  1555.     }
  1556.    
  1557.     private function recvData()
  1558.     {
  1559.         $data = fgets($this->socket, 1024);
  1560.         if($data != '')
  1561.         {
  1562.             echo 'RCV-MSG: ';
  1563.             if($this->is_CLI)
  1564.             {
  1565.                 echo "$data" . PHP_EOL;
  1566.             }
  1567.             else
  1568.             {
  1569.                 echo "<font color='blue'><b>$data</b></font><br>" . PHP_EOL;
  1570.                 flush();
  1571.             }
  1572.         }
  1573.         return explode(' ', $data);
  1574.     }
  1575.    
  1576.     private function adminLog($input = null)
  1577.     {
  1578.         echo 'SYS-MSG: ';
  1579.         if($this->is_CLI)
  1580.         {
  1581.             echo "Append Log: [$input]" . PHP_EOL . PHP_EOL;
  1582.         }
  1583.         else
  1584.         {
  1585.             echo "<font color='yellow'><b>Append Log: [$input]</b></font><br>" . PHP_EOL;
  1586.         }
  1587.         flush();
  1588.         file_put_contents($this->config['admin_logs'], (string)(new \DateTime())->format('Y-m-d H:i:s') . " $input" . PHP_EOL, FILE_APPEND);
  1589.     }
  1590.    
  1591.     // Misc Functions
  1592.     private function isValidIp($ip)
  1593.     {
  1594.         return filter_var($ip, FILTER_VALIDATE_IP) ? true : false;
  1595.     }
  1596.    
  1597.     private function isValidUrl($url)
  1598.     {
  1599.         return filter_var($url, FILTER_VALIDATE_URL) ? true : false;
  1600.     }
  1601.    
  1602.     private function cidr_match($ip, $range)
  1603.     {
  1604.         list ($subnet, $bits) = explode('/', $range);
  1605.         $ip = ip2long($ip);
  1606.         $subnet = ip2long($subnet);
  1607.         $mask = -1 << (32 - $bits);
  1608.         $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
  1609.         return ($ip & $mask) == $subnet;
  1610.     }
  1611. }
Advertisement
Add Comment
Please, Sign In to add comment