This is just a simple PHP to IRC socket bot. I've got MySQL installed on mine, and it's pretty simple to do. This is just an extremely basic example, though.
EDIT:
This is v2.0. It's rewritten, and has a lot of new features readily available. :D
Also, Please note, This bot is a CLI bot.
<?php
##################################
### PHP bot base by SoulKeeper ###
##################################
$ip = "irc.webchat.org";
$port = 6667;
$nickname = "php_bot";
$ident = "Megazord";
$realname = "Megazord PHP Bot";
$home = "#hmtest";
$backslash = chr(92);
$userbase['soulkeeper'] = "password";
$userbase['soulkeeper'] = "password";
error_reporting(1);
function wideban($address) {
$this_ban = "*";
$dot = explode(".", $address);
foreach ($dot as $yah_dot_yah) {
$numbers = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
foreach ($numbers as $this_num) {
$pos = strpos($yah_dot_yah, $this_num);
if ($pos === false) { $num = FALSE; } else { $num = TRUE; $never_set = TRUE; }
}
if (!$num) { $this_ban .= ".$yah_dot_yah"; $num = FALSE; }
}
if (!$never_set) {
$this_ban = "*";
foreach ($dot as $this_dot) {
if ($first) { $this_ban .= ".$this_dot"; }
$first = TRUE;
}
}
return $this_ban;
}
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $ip, $port);
sleep(1);
socket_write($socket, "USER $ident $ident $ident :$realname\nNICK $nickname\n");
while ($read = socket_read($socket, 2048)) {
$hack = explode("\n", $read);
foreach ($hack as $this_hack) {
$token = explode(" ", $this_hack);
$id = explode("!", strtolower($token[0]));
$nick = str_replace(":", "", $id[0]);
$ident = explode("@", $id[1]);
$address = $ident[1];
$ident = $ident[0];
$channel = str_replace(":", "", trim(strtolower($token[2])));
if ($token[0] == "PING") { socket_write($socket, "PONG $token[1]\n"); }
if ($token[1] == "376") { socket_write($socket, "JOIN $home\n"); socket_write($socket, "UMODE -Mm\n"); }
if ($token[1] == "353") {
$channel = strtolower($token[4]);
$tmpnames = explode(":", trim(strtolower($this_hack)));
$tmpnames = explode(" ", $tmpnames[2]);
foreach ($tmpnames as $this_name) {
$this_name = str_replace("@", "", $this_name);
$this_name = str_replace("+", "", $this_name);
$ial[$channel][$this_name]['h'] = TRUE;
socket_write($socket, "WHO $this_name\n");
}
}
if ($token[1] == "352") {
$ident = strtolower($token[4]);
$address = strtolower($token[5]);
$nick = strtolower($token[7]);
foreach ($ial as $this_chan=>$this_nick) {
if ($ial[$this_chan][$nick]['h']) { $ial[$this_chan][$nick]['h'] = $address; $ial[$this_chan][$nick]['i'] = $ident; }
}
}
if ($token[1] == "JOIN") {
$ial[$channel][$nick]['h'] = $address;
$ial[$channel][$nick]['i'] = $ident;
if (isset($banlist[$channel][$nick])) { socket_write($socket, "KICK $channel $nick :Banned.\n"); }
if ($token[1] == "MODE") {
if($token[4] == "Megazord") {
if($token[3] == "-o") { sockwrite($socket, "PRIVMSG ChanServ op $channel\n"); }
}
}
foreach ($banlist[$channel] as $this_nick=>$banmask) {
if ($banmask == $ial[$channel][$nick]['h']) { socket_write($socket, "KICK $channel $nick :Nice try. ;)\n"); }
}
}
if ($token[1] == "PRIVMSG") {
$message = explode(":", trim(strtolower($this_hack)));
$priv_token = explode(" ", $message[2]);
$this_nick = $priv_token[1];
$this_ban = wideban($ial[$channel][$this_nick]['h']);
if ($priv_token[0] == "login") {
if ($userbase[$nick] == $priv_token[1]) { $logged_in[$nick] = TRUE; socket_write($socket, "NOTICE $nick :Successfully logged in.\n"); } else { socket_write($socket, "NOTICE $nick :Access \002denied\002\n"); }
}
if (!$logged_in[$nick]) { unset($priv_token); }
if ($priv_token[0] == ".ban") {
if (!isset($ial[$channel][$this_nick]['h'])) { socket_write($socket, "PRIVMSG $channel :Take a look at the nick list, pal.\n"); } else {
$banlist[$channel][$this_nick] = $ial[$channel][$this_nick]['h'];
echo "Banning $channel : $this_ban\n";
socket_write($socket, "MODE $channel +b *!*@$this_ban\n");
socket_write($socket, "KICK $channel $this_nick :Requested by $nick\n");
}
}
if ($priv_token[0] == ".unban") {
if (isset($banlist[$channel][$this_nick])) {
$this_ban = wideban($banlist[$channel][$this_nick]);
unset($banlist[$channel][$this_nick]);
socket_write($socket, "MODE $channel -b *!*@$this_ban\n");
socket_write($socket, "PRIVMSG $channel :Done.\n");
}
}
if ($priv_token[0] == ".banlist") {
$count = 0;
if (isset($banlist[$channel])) { socket_write($socket, "PRIVMSG $channel :Listing banlist entries for $channel.\n"); foreach ($banlist[$channel] as $whateva=>$banmask) { $count++; socket_write($socket, "PRIVMSG $channel :#$count $whateva [\002$banmask\002]\n"); } } else { socket_write($socket, "PRIVMSG $channel :No entries in banlist for $channel\n"); }
}
if ($priv_token[0] == ".join") {
socket_write($socket, "JOIN $priv_token[1]\n");
socket_write($socket, "NOTICE $nick :Succesfully joined $priv_token[1]\n");
}
if ($priv_token[0] == ".part") {
socket_write($socket, "PART $priv_token[1]\n");
socket_write($socket, "NOTICE $nick :Succesfully parted $priv_token[1]\n");
}
if ($priv_token[0] == ".die") {
socket_write($socket, "PRIVMSG $chan :Killing Myself. D=\n");
die("Dead");
}
if ($priv_token[0] == ".identify") {
socket_write($socket, "NICK Megazord [Password]\n");
socket_write($socket, "NOTICE $nick :Successfully Authed!\n");
}
}
echo "$this_hack\n";
}
}
?>
First thing i notice, you got this off of a tutorial, as i got my first bot of there, and the problem is it\'ll only get the first 128 characters, to fix it change
while($data = fgets($socket, 128)) {
to
while($data = fgets($socket)) {
secondly i\'d do like a server function... something like
function send ($action, $channel, $extra, $message) {
if (isset($extra)) { fputs($socket, $action $channel $extra .\":\".$message.\"\\n\"); }
else { fputs($socket, $action $channel .\":\".$message.\"\\n\"); }
}
then that way all you have to do is send(\"MODE\",$ex[2],\"+o\",$ex[4]); or stuff like that :)
Thirdly, you have an extra closing bracket on the end :)
Fourthly, It isn\'t exactly a \"PHP to IRC\" it is a \"PHP IRC Bot\". When i saw \"PHP to IRC\" i thought it was a IRCd Scripted in PHP. But its only a PHP IRC Bot :) so perhaps you\'d like to change the title?
Besides that, good job.
Furby