Words from Alejandro U. Alvarez
Creating an IRC bot in PHP from scratch
I came across several articles about this topic, and I decided to take a look at it, combine them, and actually have the bot work.
The way this bot works is it will open up a server, join an IRC chat channel and post a message, going into idle status after that, waiting to receive a ping in response. If you don't want to wait, once the message is posted you can cancel page loading and enter the page again (Not reload it, since that would send the message again), and you would have the message posted.
A good use for this bot (Although not recommended because of the high traffic) would be to send messages to hundreds of different IRC channels, or to have the program refresh the page every minute or so and send a message to certain IRC channels every x time.
We will create a new file, named "irc_bot.php" with the following function:
< ?php function writeIRC($msg){ // define your variables $host = "irc.dal.net"; $port=6666; $nick="demoTester"; $ident="DemoTester"; $chan="#frih"; $readbuffer=""; $realname = "Demo Tester"; // open a socket connection to the IRC server $fp = fsockopen($host, $port, $erno, $errstr, 30); // print the error if ther eis no connection if (!$fp) { echo $errstr." (".$errno.")\n"; } else { // write data through the socket to join the channel fwrite($fp, "NICK ".$nick."\r\n"); fwrite($fp, "USER ".$ident." ".$host." bla :".$realname."\r\n"); fwrite($fp, "JOIN :".$chan."\r\n"); // write data through the socket to print text to the channel fwrite($fp, "PRIVMSG ".$chan." :$msg\r\n"); $timep = 0; //set up timer $timep = round(microtime(), 3); //start microtime // loop through each line to look for ping while (!feof($fp)) { $line = fgets($fp, 128); echo $line."\n"; $line = explode(":ping ", $line); echo $line[0]."\n"; if ($line[1]) { fwrite($fp, "PONG ".$line[1]."\r\n"); } $time2 = round(microtime(), 3); //set up second timer $gen = $time2 - $timep; //find the difference if($gen > 30){ //timeout - break loop echo ' Operation finished without receiving ping. Check out the stats iframe to see your message. '; break; } } fclose($fp); } } ?>
Using it:
So now you should join a channel in the server you like, for this example I will be using one of frihost.com's IRC channels (Server: irc.dal.net Channel: #frih)
The way you would use this bot would be to send messages every set period of time for example, with loop functions, or to manage IRC channels, if you leave the last loop of code and you examine every single line posted on the channel.
You could also use it to warn users, or to post to several channels at the same time... The uses are varied, and here you have a base code to start from...
Have fun, and if you use it, post a link in the comments and I'll include it here in the article, as examples of usage.
Example of Bot working
Well I have created a simple page that will use POST to send data to the IRC server, and then I included the channel stats in an iframe below, so you can see the results live...
It's pretty cool, so try it, and if you wonder how it is done download the demo page.
For any questions/suggestions please comment
| Print article | This entry was posted by alex on March 18, 2008 at 2:15 pm, and is filed under PHP, Programming, Web-related. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
Display timezone-specific dates in PHP
about 3 months ago - No comments
It’s common to have a website designed for one country (For example Spain or the UK) and have it in a server elsewhere (In the US for example). You will notice that sometimes when displaying a date this way, it shows the local time at the server!
Instead of manually correcting this time difference there is More >
Developing more intelligent algorithms
about 8 months ago - No comments
First of all, what do we call intelligence when talking about algorithms? Well, if we have an algorithm that is able to forecast when it is going to rain and when it will be sunny, we might call that intelligent, but it depends on some other variable, the time and effort it took for it More >
New threat for all Joomla and WordPress installations
about 10 months ago - 3 comments
There is a new BOT out there, and one of the bad ones. I have started receiving traffic from it in my servers over the past week, and after some investigation it turns out it is quite a powerful bot, and so simple to use even a kid with a computer could use it.
The bot More >
Parse links in user comments
about 10 months ago - No comments
When you allow users to comment and post stuff to your website, it is interesting and useful allowing them to post links and other stuff. But how can we do so easily?
Surely there is BBCode, phpBB, allowing only some HTML tags… etc but how easy is this approach for the end user? Of course some More >
Calculate age in PHP from timestamp
about 10 months ago - No comments
If you ever wanted to calculate someone’s age in PHP from a birth timestamp, you must take into account that the age is more than the number of years, since days and months are also important, so I wrote a simple function that will return the exact age for a given timestamp:
function getAge($birth){
$t = time();
$age More >
Easiest PHP file upload
about 11 months ago - No comments
Hello people,
I want to share with all of you a file upload class I have developed, that makes it stupid simple to upload files haha
The PHP class:
First of all, here is the PHP class you will need:
< ?php
//Uploader class, by Alex
// This class is meant to handle all kinds of file uploads for DJs Music
// More >
Easily assign an image to a post in WordPress
about 1 year ago - 1 comment
Have you ever wondered how to assign an image to a certain post using WordPress? Surely there are some plugins that try to do this, and maybe they accomplish it, but probably slowing down your blog.
Well, here is a way of doing it without slowing the blog or installing any sort of additional plugins. When More >
Did you mean… ? In php
about 2 years ago - 13 comments
In a new website I am developing for a client I had to add the usual “Did you mean… ?” in the search results for her. Si I started thinking for the easiest way to do this.
There are actually a lot of php functions out there to look for similar text. The most obvious one?
similar_text()
You More >
How we became web developers…
about 2 years ago - 3 comments
I normally write to those web developers/programmers who are already good, experienced, and thus the articles are somewhat advanced.
But today I got up feeling nostalgic I guess, or I just felt like remembering back on my www birth, on my first impression of the Internet, my first site online, my first steps in w3c standards, More >
Using Gravatars in your blog comments
about 2 years ago - No comments
I’m sure you have seen in some blogs or websites that as soon as you enter your email, or other people do, an avatar is displayed for them. If you haven’t still set your own you should do that now.
A Gravatar is a “Globally Recognized Avatar”, meaning that anyone can display yours if they know More >
about 2 years ago
yo very cool article, i love the demo. I think I’ll use it to post to channels automatically or whatever
about 2 years ago
Your posts are always a good source of information. I love improving my php skills with stuff like this, Thumbs Up!
about 2 years ago
Nice bot, but how can i tell it to send a /quote pass command, or how can we make it IDENT? because i can’t get on any main servers, and the DNS of the server i use just goes from the IP to a NAME123, not a .COM
about 2 years ago
To use the command IDENT simply add it to the message list, same as with the command JOIN used at the beginning.
If you want it to be more complex, or you need a complex bot for an IRC Channel I recommend you download one already coded, since this tutorial simply aimed to teach how it could be done.
Thanks for the interest
about 2 years ago
I’ve found out, that the simplest way to communicate with im networks is to use the PHPurple extension. With this u got much more
about 2 years ago
Thank you! I will indeed try it, if I find it easy I’ll make a new bot with more features to connect to IRC boards.