Top

PHP to IRC Bot V2.0


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Jun 09, 2008
Last Updated  Jun 19, 2008
Tags  bot  php  socket 

Introduction

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.

Grab the Code

<?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";
}
}
 
?>

Comments

  (9)  RSS
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Jun 9, 2008 4:54 pm
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

Code:
while($data = fgets($socket, 128)) {


to

Code:
while($data = fgets($socket)) {


secondly i'd do like a server function... something like

Code:

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
Soulkeeper
Comments: 80
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Jun 9, 2008 9:52 pm
Ok, Well, I changed most of it from the tut, but I've come across a problem of my own. I can't get the stupid thing to view JOIN events. Any idea how you'd set that up?
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Jun 10, 2008 8:09 am
When the bot joins the channel or when someone else joins the channel?
Soulkeeper
Comments: 80
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Jun 15, 2008 1:19 am
When someone else joins.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Jun 15, 2008 7:03 am
isn't the like raw is

Quote:
JOIN nick!identd@host #channel


???
Soulkeeper
Comments: 80
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Jun 18, 2008 3:23 am
Yah. I've got a new version I'm going to be posting, with join support, and a pre-built ial for easy banning and such.

Plus a simple user config, so it's not so messy. :D
SnapeisntEvil
Comments: 24
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Aug 17, 2008 11:14 pm
wait, how do i control it or anything
looks like it just idles on the server
Joshuaxiong1
Comments: 127
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Aug 24, 2008 8:31 am
How do you download PHP to test these script?
Nuk3d
Comments: 7
 
PHP Snippet:  PHP to IRC Bot V2.0
Posted on Sep 21, 2008 6:02 pm
how do you get it to join.. D:

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom