Skip to content

Instantly share code, notes, and snippets.

Created November 3, 2010 21:08
Show Gist options
  • Save anonymous/661718 to your computer and use it in GitHub Desktop.
Save anonymous/661718 to your computer and use it in GitHub Desktop.
OnText.php
<?php
include("OnTextObj.php");
class Irc {
var $Irc = array();
var $Client = array();
function Set($option,$value) { $this->Irc[$option] = $value; }
function Get($option) { return $this->Irc[$option]; }
function Connect() {
$this->Irc['Sock'] = fsockopen($this->Irc['Server'], 6667, $errno, $errstr, 1);
if (!empty($this->Irc['Sock'])) { echo "Connected\n"; }
else { exit("Connection Failed\n"); }
fwrite($this->Irc['Sock'],"NICK {$this->Irc['Nick']}\n");
fwrite($this->Irc['Sock'],"USER {$this->Irc['User']}\n");
}
function nick_to_wildsite($nick) {
foreach ($this->Client as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
if ($value2 == $nick) { return $key1; }
}
}
}
function Parse() {
while (!feof($this->Irc['Sock'])) {
$data = fgets($this->Irc['Sock'],128);
$data = trim($data,"\r\n");
$this->Irc['data']=$data;
$tok = explode(" ",$data);
echo "$data\n";
if ($tok[0] == "PING") { fwrite($this->Irc['Sock'],"PONG {$tok[1]}\n"); }
elseif (isset($tok[1]) && $tok[1] == "001") { fwrite($this->Irc['Sock'],"JOIN {$this->Irc['Channel']}\n"); }
elseif (isset($tok[1]) && $tok[1] == "330") {
$nick = @$tok[3]; $auth = $tok[4];
$wildsite = $this->nick_to_wildsite($nick);
$this->Client[$wildsite]['auth']=$auth;
}
elseif (isset($tok[1]) && $tok[1] == "318") {
$nick = $tok[3];
$wildsite = $this->nick_to_wildsite($nick);
if (isset($this->Client[$wildsite]['getauth'])) {
$tok = explode(" ",$this->Client[$wildsite]['getauth']);
unset($this->Client[$wildsite]['getauth']);
$text = "";
for ($i=6; $i<count($tok);$i++) {
if ($text) { $text = $text." ".$tok[$i]; }
else { $text = $tok[$i]; }
}
$OnText = new OnText;
$OnText("the message");
//$this->On_Text($tok[0],$tok[1],$tok[2],$tok[3],$tok[4],$tok[5],$text);
}
if (isset($this->Client[$nick]['getauth'])) {
$tok = explode(" ",$this->Client[$nick]['getauth']);
unset($this->Client[$nick]['getauth']);
$text = "";
for ($i=6; $i<count($tok);$i++) {
if ($text) { $text = $text." ".$tok[$i]; }
else { $text = $tok[$i]; }
}
$this->On_Text($tok[0],$tok[1],$tok[2],$tok[3],$tok[4],$tok[5],$text);
echo "\nonText\n";
unset($this->Client[$nick]['getauth']);
}
}
elseif (isset($tok[1]) && $tok[1] == "PRIVMSG") {
if (preg_match("/^:((.*)!(.*)@(.*)) PRIVMSG (#.*) :(.*)$/",$data,$match)) {
$site=$match[1]; $nick=$match[2]; $user=$match[3]; $addr=$match[4];
$wildsite="*!*@$addr"; $chan = $match[5]; $text=$match[6];
$this->Client[$wildsite]['nick'] = $nick;
if (!$this->Client[$wildsite]['auth']) {
$this->Client[$wildsite]['getauth']="$site $nick $user $addr $wildsite $chan $text";
fwrite($this->Irc['Sock'],"whois $nick\n");
}
else { $this->On_Text($site,$nick,$user,$addr,$wildsite,$chan,$text); }
}
}
}
/* If bot disconnects */
sleep(60);
$this->Connect();
$this->Parse();
}
}
Class Database {
var $Database = array();
function Set($option,$value) { $this->Database[$option] = $value; }
function Get($option) { return $this->Database[$option]; }
function Connect() {
$this->Database['Connection'] = @mysql_connect($this->Database['Server'],$this->Database['User'],$this->Database['Password']);
if (!$this->Database['Connection']) {
echo "Database Failure\n";
exit();
}
else { $this->Prepare_Users(); }
}
function Prepare_Users() {
global $Irc;
$db = "{$Irc->Get('Nick')}_{$Irc->Get('Channel')}";
if (!mysql_select_db($db,$this->Database['Connection'])) {
mysql_query("CREATE DATABASE `$db`",$this->Database['Connection']);
mysql_select_db($db,$this->Database['Connection']);
echo "Created $db Database.\n";
}
if (!mysql_query("SELECT * FROM `$db`",$this->Database['Connection'])) {
$sql = 'CREATE TABLE `users`(
`id` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`user` VARCHAR(150) NOT NULL,
`level` VARCHAR(20) NOT NULL,
PRIMARY KEY (`id`)
)';
if (mysql_query($sql,$this->Database['Connection'])) {
echo "Created Users Table\n\n";
}
}
}
}
$Database = new Database;
$Irc = new Irc;
$Irc->Set("Server","GameConnect.NL.EU.GameSurge.net");
$Irc->Set("Nick","holzbot");
$Irc->Set("User","holzbot 0 0 0");
$Irc->Set("Channel","#holzbit");
$Irc->Set("Master","holzbit.user.gamesurge");
$Database->Set("Server","127.0.0.1");
$Database->Set("User","root");
$Database->Set("Password","");
$Database->Connect();
$Irc->Connect();
$Irc->Parse();
?>
<?php
return function($site,$nick,$user,$addr,$wildsite,$chan,$text) {
global $Database;
echo "$chan->$nick: $text\n";
$text = strtolower($text);
$tok = explode(" ",$text);
$db = "{$this->Irc['Nick']}_{$this->Irc['Channel']}";
mysql_select_db($db);
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result)) {
$u = $row['user']; $l = $row['level'];
if ($u == $this->Client[$wildsite]['auth']) {
$user = $u; $level = $l;
}
}
unset ($u);
unset ($l);
if (!$level) { $level = 0; }
if ($level == '500') { $master = TRUE; }
elseif ($text == "@commands") {
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Commands: @Ping\n");
if ($master) { fwrite($this->Irc['Sock'],"PRIVMSG $chan : Master Commands: @Adduser @Access @Clvl @Deluser @Users\n"); }
}
elseif ($text == "@ping") { fwrite($this->Irc['Sock'],"PRIVMSG $chan : Pong!\n"); }
elseif ($text == "@users") {
if (!$master) { return; }
$db = "{$this->Irc['Nick']}_{$this->Irc['Channel']}";
mysql_select_db($db);
$result = mysql_query("SELECT * FROM users",$Database->Get('Connection'));
while($row = mysql_fetch_array($result)) {
$user = $row['user']; $level = $row['level'];
if ($users) { $users = "$users $user:$level"; }
else { $users = "$user:$level"; }
}
if (!$users) { $users = "None"; }
fwrite($this->Irc['Sock'],"PRIVMSG $chan : -Users- $users\n");
}
elseif ($tok[0] == "@adduser") {
if (!$master) { return; }
$tok = explode(" ",$text);
if (count($tok) != 3 || !$tok[1] || !is_numeric($tok[2])) {
fwrite($this->Irc['Sock'],"PRIVMSG $chan :Adduser: Invalid. syntax: @adduser <Authname> <Level>\n");
return;
}
elseif ($tok[2] > 500 || $tok[2] == 0) {
fwrite($this->Irc['Sock'],"PRIVMSG $chan :Adduser: Level must be between 1-500\n");
return;
}
$adduser = $tok[1]; $level = $tok[2];
$db = "{$this->Irc['Nick']}_{$this->Irc['Channel']}";
mysql_select_db($db);
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result)) {
$user = $row['user']; $level = $row['level'];
if ($adduser == $user) {
$sql = "UPDATE users SET level = '$level' WHERE user = '$user'";
mysql_query($sql,$Database->Get('Connection'));
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Adduser: $user now has $level access.\n");
return;
}
}
$sql = "INSERT INTO users (user, level) VALUES('$adduser', '$level')";
mysql_query($sql,$Database->Get('Connection'));
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Adduser: $adduser now has $level access.\n");
}
elseif ($text == "@access") {
if ($this->Client[$wildsite]['auth']) { $auth = $this->Client[$wildsite]['auth']; }
else { $auth = "Not Authed"; }
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Auth: $auth Access: $level\n");
}
elseif ($tok[0] == "@clvl") {
if (!$master) { return; }
$tok = explode(" ",$text);
if (count($tok) != 3 || !$tok[1] || !is_numeric($tok[2])) {
fwrite($this->Irc['Sock'],"PRIVMSG $chan :Adduser: Invalid. syntax: @adduser <Authname> <Level>\n");
return;
}
elseif ($tok[2] > 500 || $tok[2] == 0) {
fwrite($this->Irc['Sock'],"PRIVMSG $chan :Adduser: Level must be between 1-500\n");
return;
}
$adduser = $tok[1]; $level = $tok[2];
$db = "{$this->Irc['Nick']}_{$this->Irc['Channel']}";
mysql_select_db($db);
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result)) {
$user = $row['user']; $level = $row['level'];
if ($adduser == $user) {
$sql = "UPDATE users SET level = '$level' WHERE user = '$user'";
mysql_query($sql,$Database->Get('Connection'));
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Clvl: $user now has $level access.\n");
return;
}
}
$sql = "INSERT INTO users (user, level) VALUES('$user', '$level')";
mysql_query($sql,$Database->Get('Connection'));
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Clvl: $user now has $level access.\n");
}
elseif ($tok[0] == "@deluser") {
if (!$master) { return; }
if (count($tok) != 2 || !$tok[1]) {
fwrite($this->Irc['Sock'],"PRIVMSG $chan :Adduser: Invalid. syntax: @deluser <Authname> <Level>\n");
return;
}
$user = $tok[1];
$sql = "DELETE FROM users WHERE user='$user'";
mysql_query($sql,$Database->Get('Connection'));
fwrite($this->Irc['Sock'],"PRIVMSG $chan : Deluser: $user now has 0 access.\n");
}
elseif ($text == "@quit") {
if (!$master) { return; }
$tok = explode(" ",$text);
if ($tok[1]) {
fwrite($this->Irc['Sock'],"QUIT :{$tok[1]}\n");
}
else { fwrite($this->Irc['Sock'],"QUIT :Shutdown\n"); }
exit();
}
}
?>
<?php
class OnText {
private $reaction;
public function _construct(){
$this->reload();
}
/**
let you use the event directly from Object
@example
$OnText = new OnText;
$OnText("the message");
*/
public function __invoke($text){
return $this->reaction($text);
}
/**
reload the OnText event from OnText.php
*/
public function reaction() {
echo "test";
}
public function reload(){
$this->reaction = include("OnText.php");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment