#!/usr/bin/perl #obot #James Stanley 2010 use strict; use warnings; use LWP; use POE qw(Component::IRC Component::IRC::Plugin::FollowTail Component::IRC::Plugin::Connector Component::IRC::Plugin::NickServID Component::IRC::Plugin::CTCP); my $nickname = 'obot'; my $ircname = 'obot'; my $server = 'irc.freenode.net'; my @channels = ('#maximilian'); # We create a new PoCo-IRC object my $irc = POE::Component::IRC->spawn( nick => $nickname, ircname => $ircname, server => $server, ) or die "Oh noooo! $!"; POE::Session->create( package_states => [ main => [ qw(irc_raw _start irc_001 irc_public irc_tail_input lag_o_meter) ], ], heap => { irc => $irc }, ); $poe_kernel->run(); sub _start { my ($kernel,$heap) = @_[KERNEL, HEAP]; # retrieve our component's object from the heap where we stashed it my $irc = $heap->{irc}; $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(); $irc->plugin_add('Connector' => $heap->{connector}); $irc->plugin_add('FollowTail' => POE::Component::IRC::Plugin::FollowTail->new( filename => "/var/games/nethack/logfile" )); $irc->plugin_add('FollowTail2' => POE::Component::IRC::Plugin::FollowTail->new( filename => "/var/spool/irc" )); $irc->plugin_add('NickServID' => POE::Component::IRC::Plugin::NickServID->new( Password => 'SECRET MOTHERFUCKER' )); $irc->plugin_add('CTCP' => POE::Component::IRC::Plugin::CTCP->new( version => "obot", userinfo => "obot" )); $irc->yield(register => 'all'); $irc->yield(connect => { Nick => "obot", Server => "irc.freenode.net" } ); $kernel->delay('lag_o_meter' => 60); return; } sub irc_001 { my $sender = $_[SENDER]; # Since this is an irc_* event, we can get the component's object by # accessing the heap of the sender. Then we register and connect to the # specified server. my $irc = $sender->get_heap(); print "Connected to ", $irc->server_name(), "\n"; # we join our channels $irc->yield( join => $_ ) for @channels; return; } sub irc_public { my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2]; my $nick = ( split /!/, $who )[0]; my $channel = $where->[0]; print "$nick: $what\n"; #Match a URL, get the title if($what =~ /(https?:\/\/[^\s\)]+)/) { my $ua = LWP::UserAgent->new; $ua->max_size(1024); #Strip trailing dots my $url = $1; $url =~ s/[,.;"'!?]*$//; print "url: $url\n"; my $response = $ua->get($url); $irc->yield(privmsg => $channel => "Content-Type: " . $response->content_type) unless $response->content_type eq "text/html"; my ($title) = $response->content() =~ /(.*?)<\/title>/si; $title =~ tr/\t\r\n\f/ /; $irc->yield(privmsg => $channel => $title); } elsif($what =~ /!roll (\d?\d?)d(\d\d?)/) { my $count = $1; my $dice = $2; my $total = 0; if($count < 1) { $count = 1; } if($dice < 1) { $dice = 1; } my $output = "$nick rolls ${count}d$dice:"; for(my $i = 0; $i < $count; $i++) { my $n = int(rand($dice)) + 1; $total = $total + $n; $output .= " $n"; } if($count > 1) { $output .= " = $total"; } $irc->yield(privmsg => $channel => $output); } return; } #Handle nethack deaths/quits sub irc_tail_input { my ($kernel, $sender, $filename, $input) = @_[KERNEL, SENDER, ARG0, ARG1]; if($filename eq "/var/games/nethack/logfile") { #Match a nethack log line, print out the info if($input =~ /^3\.4\.3 (\d+) \d+ \d+ \d+ -?\d+ \d+ \d+ \d{8} \d{8} \d+ (\w+ \w+ \w+ \w+) (\w+),(.+)$/) { $kernel->post($sender, 'privmsg', $_, "$3 ($2), $1 points, $4.") for @channels; } else { $kernel->post($sender, 'privmsg', $_, $input) for @channels; } } elsif($filename eq "/var/spool/irc") { #Print something from the fifo $kernel->post($sender, 'privmsg', $_, $input) for @channels; } return; } sub lag_o_meter { my ($kernel,$heap) = @_[KERNEL,HEAP]; print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n"; $kernel->delay( 'lag_o_meter' => 60 ); return; } sub irc_raw { print $_[ARG0],"\n"; }