I had always been intrigued about IRC Bots and always thought it was quite cool to be able to write one, esp when i would see loads of script kiddies downloading stuff and setting it up on free shell accounts. recently at work it was suddenly a season for writing bots, it seemed like everyone was writing one, for a useful purpose ofcourse so i though i would jump it and give it a shot.
So as it turned out to be a fairly straight forward job to do. Since I was happy writing stuff in Perl i decided to write the bot in perl like most of my hack are.
Setting up the development/deployment environment:
Install the Bot::BasicBot from cpan
#sudo perl -MCPAN -e ‘install Bot::BasicBot’
This would go out for you and get all the required modules and its dependencies.
Writing the actual bot:
The API for the pm is pretty well documented and has a good enough example to get you started. Writing the bot using this PERL module was pretty much like writing a SAX parser where you would override methods.
So lets get started with the basic bot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #!/usr/bin/perl
use warnings;
use strict;
package MyBot;
use base qw(Bot::BasicBot);
# Create an instance of the bot and start it running. Connect
# to the main perl IRC server, and join some channels.
MyBot->new(
server => 'irc.freenode.net',
channels => ['#testbot'],
nick => 'blabot'
)->run(); |
Now do a chmod a+x on this bot and run it, this will cause the bot to come to life, connect to the server and join the channel as specified in the MyBot’s constructor. You can find other interesting attributes to assign in the constructor here
This is a pretty boring bot, it doesnt do much it just joins the channel and sits there doing nothing. To make it do something, you need to override interesting ‘events’ for the bot to do something. Some of the interesting events are
- chanjoin: This is called everytime someone joins the channel
- said: this function is called everytime someone says something
- help: this is called when someone calls the bot with the message help
- Other interesting events can be found on the API page
So continuing to write something more useful we go ahead and update the code with some new overridden methods.
1
2
3
4
5
6
| sub chanjoin {
my ($self, $message) = @_;
if ($message->{who} eq 'rajat') {
return 'Hello Rajat, how are you doing!!';
}
} |
This is a clever one, it greets me everytime I join the channel, ok maybe not so much.
Ok so to make the bot do something useful, I get to make a search query on yahoo.com everytime one calls it with a search term. So i go ahead and install the Yahoo::Search module as well.
1
2
3
4
5
6
7
8
9
10
| #sudo perl -MCPAN -e 'install Yahoo::Search'
</code>
Which does all the installation magic for you.
Loads the Module in the perl script we are writing.
<pre lang="perl" line="1">
#!/usr/bin/perl
use warnings;
use strict;
use Yahoo::Search; |
and then override the said method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| sub said {
my ($self, $message) = @_;
my $out;
my $address;
$address = $message->{address};
if (!$address) {
$address = "";
}
if ($address eq 'blabot') {
my @results = Yahoo::Search->Results(
Doc => $message->{body},
AppId => "YahooDemo",
Mode =>'all',
Start => 0,
Count =>5,
Type =>'any',
AllowAdult => 0,
AllowSimilar => 0,
Language => undef);
for my $result(@results) {
$out = $out . ' ' . $result->Url . "\n";
}
}
return $out;
} |
and that’s all that is required. Running the script will get it to connect to the irc server and join the channel, to test the bot you can either type to message in the mains on in private by send the command.
blabot, yahoo developer
it will cause the bot to return the top five search results for the term ‘yahoo developer’. You can download the script here.
A couple of useful links:
http://search.cpan.org/dist/Bot-BasicBot/lib/Bot/BasicBot.pm
Writing an IRC Bot in Perl