Guest User

Untitled

a guest
Mar 13th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!--
  2. - Author: Om (at0mic)
  3. - Date: 3/12/2016
  4. - Project: None
  5. - Description: Single script based IRC Bot.
  6. - Do not remove the copyrights. you are free to use it.
  7. -->
  8. #!/usr/bin/php
  9. <?php
  10.  
  11. set_time_limit(0);
  12. date_default_timezone_set("Asia/Kolkata");
  13. ini_set('display_errors', 'on');
  14.  
  15. $conn = mysqli_connect("localhost", "irc", "password", "irc");
  16. if (!$conn) {
  17.     die("MySQL Error");
  18. }
  19. $query = mysqli_query($conn, "SELECT * FROM irc_config");
  20. $row = mysqli_fetch_assoc($query);
  21. $server = $row['server'];
  22. $port = $row['port'];
  23. $nick = $row['nick'];
  24. $password = $row['password'];
  25. $channel = $row['channel'];
  26.  
  27. $configure = array(
  28.     'server' => $server,
  29.     'port' => $port,
  30.     'nick' => $nick,
  31.     'name' => $nick,
  32.     'channel' => $channel,
  33.     'pass' => $password
  34. );
  35. class DragonPHP{
  36.     public $socket;
  37.     public $msg = array();
  38.  
  39.     function __construct($configure){
  40.          $this->socket = fsockopen($configure['server'], $configure['port']);
  41.          $this->login($configure);
  42.          $this->main();
  43.          $this->send_data('JOIN', $this->configure['channel']);
  44.          
  45.     }
  46.    
  47.      
  48.     function login($configure){
  49.         $this->send_data('USER', $configure['nick'] . ' ombhende.com ' . $configure['nick'] . ' :' . $configure['name']);
  50.         $this->send_data('NICK', $configure['nick']);
  51.     }
  52.    
  53.    
  54.     function main(){
  55.         while (true):
  56.             $data = fgets($this->socket, 128);
  57.             echo nl2br($data);
  58.             flush();
  59.             $this->ex = explode(' ', $data);
  60.        
  61.             if($this->ex[0] == 'PING'){
  62.                 $this->send_data('PONG', $this->ex[1]);
  63.             }
  64.            
  65.             // Channel which bot joins when script is started.
  66.             $queryCon = mysqli_connect("localhost", "irc", "password", "irc");
  67.             $irc_chans = mysqli_query($queryCon, "SELECT * FROM irc_chans");
  68.             while ($chan = mysqli_fetch_assoc($irc_chans)) {
  69.             $joinList = $chan['channel'];
  70.             $this->joinChannelOnStart($joinList);
  71.         }
  72.            
  73.            
  74.             $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
  75.        
  76.             $channel = $this->ex[2];
  77.             switch($command){
  78.  
  79.                 case ':!server':
  80.                     $this->getServerInfo();
  81.                 break;
  82.  
  83.                 case ':!status':
  84.                     $this->serverStatus();
  85.                 break;
  86.  
  87.                 case ':!join':
  88.                     $this->join_channel($this->ex[4]);
  89.                 break;             
  90.            
  91.                 case ':!part':
  92.                     $this->part_channel($this->ex[4]);
  93.                 break;
  94.            
  95.  
  96.                 case ':!time':
  97.                     $this->send_data("PRIVMSG", "$channel " . date("D, d M Y H:i"));
  98.                 break;
  99.                
  100.                 case ':!quit':
  101.                     $this->quit();
  102.                 break;
  103.                
  104.                 case ':!updates':
  105.                     $this->showUpdates();
  106.                 break;
  107.                
  108.                 case ':!uptime':
  109.                 $this->showIRCUptime();
  110.                 break;
  111.                
  112.                  case ':!addupdate':
  113.                  $this->addUpdate();
  114.                  break;
  115.                  
  116.                  case ':!identify':
  117.                  $this->identify();
  118.                  break;
  119.  
  120.                  case ':!say':
  121.                  $this->say();
  122.                  break;
  123.  
  124.            
  125.             }
  126.         endwhile;
  127.     }
  128.     function identify() {
  129.         global $password;
  130.     $this->send_data("NS IDENTIFY $password");
  131.     $this->send_data("PRIVMSG " . $this->ex[2] . " :Successfully Identified!");
  132.     }
  133.  
  134.     function serverStatus() {
  135.         // Memory Usage
  136.     $free = shell_exec('free');
  137.     $free = (string)trim($free);
  138.     $free_arr = explode("\n", $free);
  139.     $mem = explode(" ", $free_arr[1]);
  140.     $mem = array_filter($mem);
  141.     $mem = array_merge($mem);
  142.     $memory_usage = $mem[2]/$mem[1]*100;
  143.  
  144.         // CPU Usage
  145.     $load = sys_getloadavg();
  146.  
  147.     $this->notice("Server Status | CPU: " . $load[0] . " -|- Memory: " . round($memory_usage, 2), str_replace(":", "", strstr($this->ex[0], '!', true)));
  148.     }
  149.  
  150.  
  151.     function getServerInfo() {
  152.         $conn = mysqli_connect("localhost", "irc", "password", "irc");
  153.         if (!$conn) {
  154.             die("MySQL Error");
  155.         }
  156.         $query = mysqli_query($conn, "SELECT * FROM irc_config");
  157.         $row = mysqli_fetch_assoc($query);
  158.         $server = $row['server'];
  159.         $port = $row['port'];
  160.         $this->notice("Server: $server:$port | Connected To Network!", str_replace(":", "", strstr($this->ex[0], '!', true)));
  161.     }
  162.    
  163.     function say(){
  164.         $arraysize = sizeof($this->ex);
  165.         $count = 4;
  166.         while($count <= $arraysize) {
  167.             $text = $text . " " . $this->ex[$count];
  168.             $count++;
  169.         }
  170.         if (strstr($this->ex[0], '!', true) == ":Om") {
  171.         $this->privmsg($text, $this->ex[2]);
  172.         unset($text);
  173.         } else {
  174.         $this->privmsg("Access Denied.", $this->ex[2]);
  175.         }
  176.     }
  177.    
  178.     function privmsg($message, $to){
  179.         fputs($this->socket,"PRIVMSG " . $to . " :" . $message . "\n");
  180.     }
  181.  
  182.     function notice($message, $to) {
  183.         fputs($this->socket, "NOTICE " . $to . " :" . $message . "\n");
  184.     }
  185.    
  186.     /*
  187.      * Sends data to the server.
  188.      */
  189.     function send_data($cmd, $msg = null){
  190.          if($msg == null){
  191.              fputs($this->socket, $cmd . "\n");
  192.              echo "<b>$cmd</b>";
  193.          }else{
  194.             fputs($this->socket, $cmd.' '.$msg."\n");
  195.             echo "<b>$cmd $msg</b>";
  196.          }
  197.     }
  198.    
  199.     function joinChannelOnStart($channel) {
  200.         $this->send_data('JOIN', $channel);
  201.     }
  202.    
  203.  
  204.     function join_channel($channel){
  205.         if (empty($channel)) {
  206.             $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Parameter: !join #<channel>");
  207.             } elseif (strstr($this->ex[0], '!', true) == ':Om') {
  208.         $this->send_data('JOIN', $channel);
  209.         $this->send_data("PRIVMSG #om :Successfully Joined $channel!");
  210.         } else {
  211.         $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Access Denied");
  212.         $this->send_data("PRIVMSG #om :User: " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " Used command: !join");
  213.         }
  214.     }
  215.    
  216.     function part_channel($channel) {
  217.         if (empty($channel)) {
  218.             $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Parameter: !part #<channel>");
  219.             } elseif (strstr($this->ex[0], '!', true) == ':Om') {
  220.         $this->send_data('PART', $channel);
  221.         $this->send_data("PRIVMSG #om :Successfully Left $channel");
  222.         } else {
  223.         $this->send_data("PRIVMSG #om :User: " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " Used command: !part");
  224.         $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Access Denied");
  225.         }
  226.     }
  227.    
  228.     function showUpdates() {
  229.         $conn = mysqli_connect("localhost", "irc", "password", "irc");
  230.         mysqli_select_db($conn, "gameserver");
  231.         if (mysqli_connect_errno($conn)) {
  232.             $this->send_data("PRIVMSG " . $this->ex[2] . " :Failed to connect to MySQL: " . mysqli_connect_error);
  233.         } else {
  234.             echo "Successfully Connected to MySQL Database.";
  235.         }
  236.         $query = mysqli_query($conn,"SELECT * FROM (
  237.    SELECT * FROM `irc_updates` ORDER BY id DESC LIMIT 10
  238. ) sub
  239. ORDER BY id DESC");
  240.         $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Last 10 Updates:");
  241.        
  242.         while ($row = mysqli_fetch_assoc($query)) {
  243.            
  244.             $id = $row['id'];
  245.             $update = $row['updates'];
  246.             $date = $row['dat'];
  247.             $dev = $row['developer'];
  248.             $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :$update"); 
  249.         }
  250.        
  251.         }
  252.    
  253.    
  254.     function quit() {
  255.         if (strstr($this->ex[0], '!', true) == ':Om') {
  256.         $this->send_data('QUIT Quit message by ' . strstr($this->ex[0], '!', true));
  257.         exit;
  258.         echo "\n";
  259.         } else {
  260.         $this->send_data("PRIVMSG " . $this->ex[2] . " :Access Denied");
  261.         }
  262.     }
  263.    
  264.     function addUpdate() {
  265.         $conn = mysqli_connect("localhost", "irc", "password", "irc");
  266.         mysqli_select_db($conn, "gameserver");
  267.         if (mysqli_connect_errno($conn)) {
  268.             $this->send_data("PRIVMSG " . $this->ex[2] . " :Failed to connect to MySQL: " . mysqli_connect_error);
  269.         } else {
  270.             echo "Querying...";
  271.         }
  272.         $arraysize = sizeof($this->ex);
  273.         $count = 4;
  274.         while($count <= $arraysize) {
  275.             $text = $text . " " . $this->ex[$count];
  276.             $count++;
  277.         }
  278.         if (strstr($this->ex[0], '!', true) == ":Om") {
  279.         $date = date("D, d M Y H:i");
  280.         $query = mysqli_query($conn, "INSERT INTO irc_updates (updates, dat, developer) VALUES ('$text','$date','" . str_replace(":", "", strstr($this->ex[0], '!', true)) . "');");
  281.         if (!$query) {
  282.             $this->privmsg("Failed to insert update | Error: " . mysqli_error($query), $this->ex[2]);
  283.         }
  284.         $this->privmsg("Successfull Query.", $this->ex[2]);
  285.         $this->notice("Update has been successfully added!", str_replace(":", "", strstr($this->ex[0], '!', true)));
  286.         unset($text);
  287.         } else {
  288.         $this->privmsg("Access Denied", $this->ex[2]);
  289.         }
  290.     }
  291.    
  292.     function showIRCUptime() {
  293.         $exec = exec('uptime');
  294.         $this->send_data("PRIVMSG " . $this->ex[2] . " :Uptime: $exec");
  295.     }
  296.  
  297. }
  298. $bot = new DragonPHP($configure);
  299.        
  300. ?>
Add Comment
Please, Sign In to add comment