Jump to content
Gonzalez

IRC Bots in PHP

Recommended Posts

Funny_Robot.jpg

Hi there people, welcome back to another episode of coding in php with mrwutang :P. How was Xmas? Hope yall had fun. Today we'll be learning how to build IRC bots in PHP.

Whats and IRC bot and why would anyone use it?

IRC bots are scripts or programs that connects to an IRC server and acts like a client, the only difference is that it doesnt let the user interact with the server, it generates automated response or none at all according to what its builder, you want it to do. And ytf would anyone use it? Think about this scenario, you own an IRC server which is busy as shit, but you dont have the time to manage it or greet every user that visits it. Thats when our bot comes in. If you code it properly, it can do anything from greeeting each and every user, hosting text based games, quote jokes and facts, to moderating the server. A bot can also be used for malicious intents like flooding a channel. You could also make one cos it fun Cheesy. So lets get crackin, damn I like saying that

IF-robot-open.jpg

General purpose IRC commands:

NICK- Used to set your nick :P. Usage- NICK <YourNickHere> \r\n

PASS-Sets the password for your accound. Incase your IRC host needs it. For eg, NickServ needs you to provide a password for the nick you registered in. Usage- PASS <YourPassword> \r\n

USERNAME-Lets you set parameters like your real name domain name and things like that. I dont know much about this command. Usage- USERNAME <username> <domain> <name> <realname>. Just type your real name or nick for all these parameters. Sorry I cant give much info about this.

JOIN- You guessed it, lets you join a #channel. Usage- JOIN <#channelName>

PRIVMSG-Sends a message to a channel or user, Usage-PRIVMSG <#channelName/userName> <Message>

For more info visit RFC 1459

You gotta note that you should use the commands in the order when logging in PASS, USER and NICK. Because the protocol wants you to send it the PASS before username. You disobey it and the IRC police will be at your doorsteps before you can say Hamburger. Dont worry if you didnt understand anything I just said. Its all gonna make sense soon.

Functions we're gonna be using:

fsockopen()- Name says it all, opens a socket to read, write or both. Like fopen.

fwrite()-Writes to the fsockopen-ed socket. Some wise guy said everything in 'Nix is a file.

fgets()-Reads from the fsockopen-ed socket.

preg_match()-Regular expression. Matches text or patterns. We use it to get commands or messages from the server.

As usual Im gonna be posting the script and doing the explaining later.

<?php
$nick="Bot";
$name="Bot";
$pass="Password";
$irc="irc.evilzone.org";
$port=6667;
#chan="#evilzone";
$fp=fsockopen($irc,$port);
if($fp){
die("Couldnt connect to the server");
}
fwrite($fp,"PASS $pass\r\nUSER $name\r\nNICK $nick\r\nJOIN$chan\r\n");
while($msg=fgets($fp)){
if(preg_match("/:(.*)\!.*JOIN.*:#.*/",$msg,$usr)){
$usr=$usr[1];
fwrite($fp,"PRIVMSG $chan Hi $usr\r\n");
}
if(preg_match("/:(.*)\!.*PART|QUIT.*:.*/",$msg,$usr)){
$usr=$usr[1];
fwrite($fp,"PRIVMSG $chan Bye $usr\r\n");
}
if(preg_match("/:(.*)\!.*Hi $nick.*/",$msg,$usr)){
$usr=$usr[1];
fwrite($fp,"PRIVMSG $chan Hi $usr\r\n");
}
}
?>

The bot in our example is pretty basic. It visits an IRC server specified in the variable $irc using password, nick and username stored in $pass,$user, and $nick and joins the channel specified in $chan. We use fsockopen, fwrite and fgets to open a socket to the server, send commands and read messages from the server. Our bot greets any user that greets the bot or joins or leaves the channel. See? Easy-Peasy.

We have the basic bot up and running now we can get it to do pretty much anything we want it to do.Use your new found powers wisely. Like that Spiderman's uncle said. With great power comes great responsibilities, Dont ask it to rob banks or take control of the world Tongue. Yea I got carried away I'll stop making lame jokes now Lips Sealed. Hope this article made s ense and you learned something. Message me if you didnt. Thanx for reading. Take care and have fun.

By mrwutang

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...