LiquidFenrir

PHP bot

May 18th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.78 KB | None | 0 0
  1. <?php
  2. date_default_timezone_set("UTC");
  3.  
  4. $server = "irc.rizon.net";
  5. $port = 6667;
  6. $nickname = "imabot";
  7. $ident = "imanooBOT";
  8. $gecos = "LiquidFenrir.irc.bot";
  9. $chan = "#imanoob";
  10.  
  11. $quotes = json_decode(file_get_contents("./quotes.json"), true);
  12. print_r($quotes);
  13.  
  14. $trigger_char = "!";
  15. $uno_mode = false;
  16.  
  17. require("modules.php");
  18. print_r($commands);
  19.  
  20. function write($msg) {
  21.     echo $msg."\n";
  22. }
  23.  
  24. function isOwner($user) {
  25.     write("owner check");
  26.     $tmpvar = false;
  27.     print_r($user);
  28.     if ($user["host"] == "LiquidFenrir.irc.name") {
  29.         $tmpvar = true;
  30.     }
  31.     write($tmpvar);
  32.     return $tmpvar;
  33. }
  34.  
  35. $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP);
  36. $error = socket_connect($socket,$server,$port);
  37.  
  38. if ($socket === false) {
  39.   $errorCode = socket_last_error();
  40.   $errorString = socket_strerror($errorCode);
  41.   die("Error $errorCode: $errorString\n");
  42. }
  43.  
  44. socket_write($socket, "NICK $nickname\r\n");
  45. socket_write($socket, "USER $ident * 8 :$gecos\r\n");
  46.  
  47. while(is_resource($socket)) {
  48.    
  49.   $data = trim(socket_read($socket, 1024, PHP_NORMAL_READ));
  50.   echo $data."\r\n";
  51.   $d = explode(" ", $data);
  52.   $d = array_pad($d, 10, "");
  53.    
  54.     $d[0] = substr($d[0],1);
  55.    
  56.     if ($d[0] === "ING") { //ping, first character missing due to substr above
  57.         socket_write($socket, "PONG ".$d[1]."\r\n");
  58.         $sender = array("nick" => "ping", "host" => "ping");
  59.     } elseif (substr($d[0],0,4) == "irc.") {
  60.         $sender = array("nick" => "server", "host" => "server");
  61.     } else {
  62.         $nick_stop = strpos($d[0],"!");
  63.         $user_stop = strpos($d[0],"@");
  64.         $sender = array(
  65.             "nick" => substr($d[0],0,$nick_stop),
  66.             "user" => substr($d[0],$nick_stop+2,$user_stop-$nick_stop-2),
  67.             "host" => substr($d[0],$user_stop+1)
  68.         );
  69.     }
  70.     unset($d[0]);
  71.     $action = $d[1];
  72.     unset($d[1]);
  73.     $channel = $d[2];
  74.     unset($d[2]);
  75.    
  76.     $message = substr(trim(implode(" ", $d)),1);
  77.     $word = strtolower(substr($d[3],1));
  78.     unset($d[3]);
  79.     $msg = trim(implode(" ", $d));
  80.     $info = array(
  81.         "socket" => $socket,
  82.         "message" => $message,
  83.         "msg" => $msg,
  84.         "d" => $d,
  85.         "sender" => $sender,
  86.         "channel" => $channel,
  87.         "action" => $action
  88.     );
  89.    
  90.   if ($action === "INVITE") {
  91.     socket_write($socket, "JOIN $message\r\n");
  92.   }
  93.   if ($action === "376" || $action === "422") {
  94.     socket_write($socket, "JOIN $chan\r\n");
  95.   }
  96.     if (substr($word,0,1) === $trigger_char) {
  97.         $word = substr($word,1);
  98.         if ($word == "reload") {
  99.             if (isOwner($info["sender"])) {
  100.                 unset($commands);
  101.                 require("modules.php");
  102.                 print_r($commands);
  103.                 socket_write($socket, "PRIVMSG $channel :Reload complete.\r\n");
  104.             }
  105.         } elseif ($word == "uno") {
  106.             $uno_mode = !$uno_mode;
  107.             if ($uno_mode) {
  108.                 socket_write($socket, "PRIVMSG $channel :UNO mode engaged!\r\n");
  109.                 socket_write($socket, "PRIVMSG UNOBot :.pce-on\r\n");
  110.                 socket_write($socket, "PRIVMSG $channel :.uj\r\n");
  111.             } else {
  112.                 socket_write($socket, "PRIVMSG $channel :UNO mode disengaged!\r\n");
  113.                 socket_write($socket, "PRIVMSG $channel :.leave\r\n");
  114.             }
  115.         } elseif ($word == "commands") {
  116.             socket_write($socket, "PRIVMSG $channel :Trigger character: '$trigger_char'\r\n");
  117.             socket_write($socket, "PRIVMSG $channel :Available commands:\r\n");
  118.             socket_write($socket, "PRIVMSG $channel :".implode(", ", array_keys($commands)).".\r\n");
  119.         } elseif ($word == "quit") {
  120.             if (isOwner($sender)) {
  121.                 socket_write($socket, "QUIT \r\n");
  122.                 break;
  123.             }
  124.         } elseif ($word == "quote") {
  125.             print_r($info);
  126.             $call = $commands["quote"];
  127.             $quotes = $call($info, $quotes);
  128.         } elseif (array_key_exists($word, $commands)) {
  129.             print_r($info);
  130.             $call = $commands[$word];
  131.             $call($info);
  132.         }
  133.     }
  134.     if ($uno_mode && $sender["host"] == "play.uno.with.me") {
  135.         $call = $commands["uno"];
  136.         $state = $call($info);
  137.         if ($state) {
  138.             $uno_mode = false;
  139.         }
  140.     }
  141. }
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment