Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use Net::IRC;
- use strict;
- use Data::Dumper;
- # create a WeatherGet object (downloads and parses weather from
- # weatherunderground.com)
- my $irc = new Net::IRC;
- my $conn = $irc->newconn(
- Server => shift || 'irc.freenode.com',
- Port => shift || '6667',
- Nick => 'Upper',
- Ircname => '!uptime',
- Username => 'upper'
- );
- $conn->{channel} = shift || '#uptime-demo';
- sub on_connect {
- my $conn = shift;
- # when we connect, join our channel and greet it
- $conn->join($conn->{channel});
- $conn->privmsg($conn->{channel}, 'booya! it\'s Upper time!');
- }
- sub on_public {
- # on an event, we get connection object and event hash
- my ($conn, $event) = @_;
- # this is what was said in the event
- my $text = $event->{args}[0];
- # regex the text to see if it begins with !roker
- if ($text =~ /^\!uptime/) {
- # if so, pass the text and the nick off to the weather method
- #my $weather_text = weather($1, $event->{nick});
- my $uptime_text = qx{ uptime };
- $conn->privmsg($event->{to}[0], $uptime_text);
- }
- }
- sub on_msg {
- my ($conn, $event) = @_;
- # This is a *really* hacky part. I usually use this bot on an IRC
- # server that doesn't send the end of MOTD that signifies connect, so
- # I use this to simulate the connect.
- #
- # It basically says "if someone with my nick /msg you with 'join',
- # then you've connected"
- if (
- ($event->{nick} eq 'wholok') and
- ($event->{args}[0] eq 'join')){
- on_connect($conn);
- }
- # Under normal circumstances, simply reply to the nick that there's
- # no reason to /msg RokerBot.
- else {
- $conn->privmsg($event->{nick}, "Don't nobody /msg Upper.");
- }
- }
- sub on_notice {
- my ($conn, $event) = @_;
- # This handles nick registration. On some IRC networks, you can
- # password-protect you nick. The IRC server will send you a "notice"
- # as NickServ that you have to identify with your passowrd.
- if (
- ($event->{nick} eq 'NickServ') and
- ($event->{args}[0] eq 'If you do not change within one minute, I will change your nick.')
- ) {
- # send an /msg to NickServ with the password
- $conn->privmsg('NickServ', 'identify rokerman');
- # This is another hack, based on the fact that I've registered
- # the nick "RokerBot" on the IRC server which doesn't
- # send the end of MOTD message. So when I'm asked to
- # send my password, I know I've connected.
- #
- # This is redundant with the behavior covered in on_msg
- on_connect($conn);
- }
- }
- sub default {
- # This is helpful to see what an event returns. Data::Dumper will
- # recursively reveal the structure of any value
- my ($conn, $event) = @_;
- print Dumper($event);
- }
- # add handlers for our standard events
- $conn->add_handler('public', \&on_public);
- $conn->add_handler('msg', \&on_msg);
- $conn->add_handler('notice', \&on_notice);
- $conn->add_handler('376', \&on_connect);
- # experiment with the cping event, printing out to standard output
- $conn->add_handler('cping', \&default);
- # start IRC
- $irc->start;
Add Comment
Please, Sign In to add comment