Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- - Author: Om (at0mic)
- - Date: 3/12/2016
- - Project: None
- - Description: Single script based IRC Bot.
- - Do not remove the copyrights. you are free to use it.
- -->
- #!/usr/bin/php
- <?php
- set_time_limit(0);
- date_default_timezone_set("Asia/Kolkata");
- ini_set('display_errors', 'on');
- $conn = mysqli_connect("localhost", "irc", "password", "irc");
- if (!$conn) {
- die("MySQL Error");
- }
- $query = mysqli_query($conn, "SELECT * FROM irc_config");
- $row = mysqli_fetch_assoc($query);
- $server = $row['server'];
- $port = $row['port'];
- $nick = $row['nick'];
- $password = $row['password'];
- $channel = $row['channel'];
- $configure = array(
- 'server' => $server,
- 'port' => $port,
- 'nick' => $nick,
- 'name' => $nick,
- 'channel' => $channel,
- 'pass' => $password
- );
- class DragonPHP{
- public $socket;
- public $msg = array();
- function __construct($configure){
- $this->socket = fsockopen($configure['server'], $configure['port']);
- $this->login($configure);
- $this->main();
- $this->send_data('JOIN', $this->configure['channel']);
- }
- function login($configure){
- $this->send_data('USER', $configure['nick'] . ' ombhende.com ' . $configure['nick'] . ' :' . $configure['name']);
- $this->send_data('NICK', $configure['nick']);
- }
- function main(){
- while (true):
- $data = fgets($this->socket, 128);
- echo nl2br($data);
- flush();
- $this->ex = explode(' ', $data);
- if($this->ex[0] == 'PING'){
- $this->send_data('PONG', $this->ex[1]);
- }
- // Channel which bot joins when script is started.
- $queryCon = mysqli_connect("localhost", "irc", "password", "irc");
- $irc_chans = mysqli_query($queryCon, "SELECT * FROM irc_chans");
- while ($chan = mysqli_fetch_assoc($irc_chans)) {
- $joinList = $chan['channel'];
- $this->joinChannelOnStart($joinList);
- }
- $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
- $channel = $this->ex[2];
- switch($command){
- case ':!server':
- $this->getServerInfo();
- break;
- case ':!status':
- $this->serverStatus();
- break;
- case ':!join':
- $this->join_channel($this->ex[4]);
- break;
- case ':!part':
- $this->part_channel($this->ex[4]);
- break;
- case ':!time':
- $this->send_data("PRIVMSG", "$channel " . date("D, d M Y H:i"));
- break;
- case ':!quit':
- $this->quit();
- break;
- case ':!updates':
- $this->showUpdates();
- break;
- case ':!uptime':
- $this->showIRCUptime();
- break;
- case ':!addupdate':
- $this->addUpdate();
- break;
- case ':!identify':
- $this->identify();
- break;
- case ':!say':
- $this->say();
- break;
- }
- endwhile;
- }
- function identify() {
- global $password;
- $this->send_data("NS IDENTIFY $password");
- $this->send_data("PRIVMSG " . $this->ex[2] . " :Successfully Identified!");
- }
- function serverStatus() {
- // Memory Usage
- $free = shell_exec('free');
- $free = (string)trim($free);
- $free_arr = explode("\n", $free);
- $mem = explode(" ", $free_arr[1]);
- $mem = array_filter($mem);
- $mem = array_merge($mem);
- $memory_usage = $mem[2]/$mem[1]*100;
- // CPU Usage
- $load = sys_getloadavg();
- $this->notice("Server Status | CPU: " . $load[0] . " -|- Memory: " . round($memory_usage, 2), str_replace(":", "", strstr($this->ex[0], '!', true)));
- }
- function getServerInfo() {
- $conn = mysqli_connect("localhost", "irc", "password", "irc");
- if (!$conn) {
- die("MySQL Error");
- }
- $query = mysqli_query($conn, "SELECT * FROM irc_config");
- $row = mysqli_fetch_assoc($query);
- $server = $row['server'];
- $port = $row['port'];
- $this->notice("Server: $server:$port | Connected To Network!", str_replace(":", "", strstr($this->ex[0], '!', true)));
- }
- function say(){
- $arraysize = sizeof($this->ex);
- $count = 4;
- while($count <= $arraysize) {
- $text = $text . " " . $this->ex[$count];
- $count++;
- }
- if (strstr($this->ex[0], '!', true) == ":Om") {
- $this->privmsg($text, $this->ex[2]);
- unset($text);
- } else {
- $this->privmsg("Access Denied.", $this->ex[2]);
- }
- }
- function privmsg($message, $to){
- fputs($this->socket,"PRIVMSG " . $to . " :" . $message . "\n");
- }
- function notice($message, $to) {
- fputs($this->socket, "NOTICE " . $to . " :" . $message . "\n");
- }
- /*
- * Sends data to the server.
- */
- function send_data($cmd, $msg = null){
- if($msg == null){
- fputs($this->socket, $cmd . "\n");
- echo "<b>$cmd</b>";
- }else{
- fputs($this->socket, $cmd.' '.$msg."\n");
- echo "<b>$cmd $msg</b>";
- }
- }
- function joinChannelOnStart($channel) {
- $this->send_data('JOIN', $channel);
- }
- function join_channel($channel){
- if (empty($channel)) {
- $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Parameter: !join #<channel>");
- } elseif (strstr($this->ex[0], '!', true) == ':Om') {
- $this->send_data('JOIN', $channel);
- $this->send_data("PRIVMSG #om :Successfully Joined $channel!");
- } else {
- $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Access Denied");
- $this->send_data("PRIVMSG #om :User: " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " Used command: !join");
- }
- }
- function part_channel($channel) {
- if (empty($channel)) {
- $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Parameter: !part #<channel>");
- } elseif (strstr($this->ex[0], '!', true) == ':Om') {
- $this->send_data('PART', $channel);
- $this->send_data("PRIVMSG #om :Successfully Left $channel");
- } else {
- $this->send_data("PRIVMSG #om :User: " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " Used command: !part");
- $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Access Denied");
- }
- }
- function showUpdates() {
- $conn = mysqli_connect("localhost", "irc", "password", "irc");
- mysqli_select_db($conn, "gameserver");
- if (mysqli_connect_errno($conn)) {
- $this->send_data("PRIVMSG " . $this->ex[2] . " :Failed to connect to MySQL: " . mysqli_connect_error);
- } else {
- echo "Successfully Connected to MySQL Database.";
- }
- $query = mysqli_query($conn,"SELECT * FROM (
- SELECT * FROM `irc_updates` ORDER BY id DESC LIMIT 10
- ) sub
- ORDER BY id DESC");
- $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :Last 10 Updates:");
- while ($row = mysqli_fetch_assoc($query)) {
- $id = $row['id'];
- $update = $row['updates'];
- $date = $row['dat'];
- $dev = $row['developer'];
- $this->send_data("NOTICE " . str_replace(":", "", strstr($this->ex[0], '!', true)) . " :$update");
- }
- }
- function quit() {
- if (strstr($this->ex[0], '!', true) == ':Om') {
- $this->send_data('QUIT Quit message by ' . strstr($this->ex[0], '!', true));
- exit;
- echo "\n";
- } else {
- $this->send_data("PRIVMSG " . $this->ex[2] . " :Access Denied");
- }
- }
- function addUpdate() {
- $conn = mysqli_connect("localhost", "irc", "password", "irc");
- mysqli_select_db($conn, "gameserver");
- if (mysqli_connect_errno($conn)) {
- $this->send_data("PRIVMSG " . $this->ex[2] . " :Failed to connect to MySQL: " . mysqli_connect_error);
- } else {
- echo "Querying...";
- }
- $arraysize = sizeof($this->ex);
- $count = 4;
- while($count <= $arraysize) {
- $text = $text . " " . $this->ex[$count];
- $count++;
- }
- if (strstr($this->ex[0], '!', true) == ":Om") {
- $date = date("D, d M Y H:i");
- $query = mysqli_query($conn, "INSERT INTO irc_updates (updates, dat, developer) VALUES ('$text','$date','" . str_replace(":", "", strstr($this->ex[0], '!', true)) . "');");
- if (!$query) {
- $this->privmsg("Failed to insert update | Error: " . mysqli_error($query), $this->ex[2]);
- }
- $this->privmsg("Successfull Query.", $this->ex[2]);
- $this->notice("Update has been successfully added!", str_replace(":", "", strstr($this->ex[0], '!', true)));
- unset($text);
- } else {
- $this->privmsg("Access Denied", $this->ex[2]);
- }
- }
- function showIRCUptime() {
- $exec = exec('uptime');
- $this->send_data("PRIVMSG " . $this->ex[2] . " :Uptime: $exec");
- }
- }
- $bot = new DragonPHP($configure);
- ?>
Add Comment
Please, Sign In to add comment