What in the name of Zeus is fsockopen?

If you didn’t already know, fsockopen is a function in enables us to connect to remote hosts by opening sockets to them. Have you heard some Linux user say everything in Linux is a file? Apparently fsockopen treats sockets like files too. You know what this means don’t you, We can use it like fopen to read and write from those open socket using functions like fwrite and fread. I think you can think of some cool uses for it already :P.

Things you need to know

The only thing I assume you know to completely understand this article is the basics of PHP.

Alright, so how do I create an IRC Bot?

Have you seen those fancy IRC bots and wondered how you can make them? Maybe not. But if you have, you’re in luck, because that’s what we’re gonna do in this article. Build an IRC bot, and its easy too. Woohoo :D.

But first some basic IRC commands

NICK: Sets the nick or display name you want to use in an IRC server or channel.

USER: I think its the same as nick, I’m not sure because, IRC clients like mIRC often set this variable as username on local computer. You can use the same value for for nick and user.

JOIN: Join a channel.

PART: Leave a channel

MSG: Send message to a user.

PRIVMSG: Like MSG but you can also use to send messages to channels.

QUIT: Quit IRC all together.

These are the commands we’re going to be using here. You can find more here.

How it works

Like I said fsockopen’s pretty much like fopen. So all we have to do is open a connection to the IRC server using.

$nick=”Bot”

$host=”irc.dal.net”;

$port=6667;

$chan=””;

$fp=fsockopen($host,$port);

Now that the connection’s been opened we can send data to the server using fwrite or fputs. Its as easy as writing to files.

Heres how we login to the server.

fwrite($fp,”NICK $nick\r

”);

fwrite($fp,”USER localuser $host erl: luser \r

”);

fwrite($fp, “JOIN #chan \r

”);

fwrite($fp,”PRIVMSG #chan Hi, I’m an IRC bot\r

”);

Its important that each command we send ends with “\r

” because it simulates the return or enter key press that we normally do on terminal, it helps the server know that we’re issuing a command. And in order to keep it running we’ll need to implement a while loop so that it doesn’t end till we want it to.

while($msg=fgets($fp,200)){

$msg=explode(“:ping”,$msg);

$msg[0]==’ping’?fwrite($fp,”PONG “.$msg[1].”\r

”):””;

//This checks for any line with JOIN in them, gets the nick of the person who joined the chat or channel and greets them

preg_match(“/:(.*)\!.* JOIN :.*/Ui”,$msg,$nick)?fwrite($fp,”PRIVMSG #php hello “.$nick[1].”\r

”):””;

echo $msg.”

”;

}

Everything in the code above’s pretty straight forward except for the line that checks ping requests and preg_match thats checks for new people joining the channels and greets them. The IRC’s protocol requires the server to check if the user has timed out or gone offline so that it can close the connection, that’s the reason why it sends :ping requests all the time, if we don’t respond with pong and the message to it the connection gets close.

The whole code would look like this.

Running it from the terminal

Its better to run it from the terminal because its easy to close using the Ctrl+c or z buttons rather than closing the browser tab. In order to run it from the terminal do this.

cd /to/the/script’s/location

php scriptname.php

Teaching it more tricks

There are a lot of things you can make it do like trivia games, or output random quotes or jokes, display google, and stuff like that. But its outside the scope of this article. The functions you need to make it do all those things are fsockopen, cURL and regexp.