#!/usr/bin/perl
use strict;
use warnings;
use POE qw(Component::IRC);
use Geo::IPfree;
use Cwd;
use Switch;
my $cwd = getcwd;
my $fileconf = $cwd . '/crawli.conf';
my $nickname;
my $ircserver;
my $port;
my $ircname;
my $username;
my @channels = ( '#Blah', '#Foo', '#Bar' );
sub load_conf {
open (FILE, $fileconf) or die "Can't open $fileconf, $!";
my @data=<FILE>;
close(FILE);
foreach my $conf (@data) {
next if $conf !~ /^set/;
$conf =~ s/^set //g;
my ($param, $setted) = split(/: /, $conf);
switch($param) {
case 'nickname' { $nickname = $setted; }
case 'ircserver' { $ircserver = $setted; }
case 'port' { $port = $setted; }
case 'ircname' { $ircname = $setted; }
case 'username' { $username = $setted; }
}
}
}
&load_conf();
# We create a new PoCo-IRC object and component.
my $irc = POE::Component::IRC->spawn(
nick => $nickname,
server => $ircserver,
port => $port,
ircname => $ircname,
username => $username,
LocalAddr => "192.168.1.6",
) or die "Couldn't start: $!";
POE::Session->create(
package_states => [
'main' => [ qw(_default _start irc_001 irc_public) ],
],
heap => { irc => $irc },
);
$poe_kernel->run();
exit 0;
sub _start {
my ($kernel,$heap) = @_[KERNEL,HEAP];
# We get the session ID of the component from the object
# and register and connect to the specified server.
my $irc_session = $heap->{irc}->session_id();
$kernel->post( $irc_session => register => 'all' );
$kernel->post( $irc_session => connect => { } );
undef;
}
sub irc_001 {
my ($kernel,$sender) = @_[KERNEL,SENDER];
# Get the component's object at any time by accessing the heap of
# the SENDER
my $poco_object = $sender->get_heap();
print "Connected to ", $poco_object->server_name(), "\n";
# In any irc_* events SENDER will be the PoCo-IRC session
$kernel->post( $sender => join => $_ ) for @channels;
undef;
}
sub irc_public {
my $rot13;
my ($kernel,$sender,$who,$where,$what) =
@_[KERNEL,SENDER,ARG0,ARG1,ARG2];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
if ($rot13 = $what =~ /^rot13 (.+)/ ) {
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];
$kernel->post( $sender => privmsg => $channel => "$nick: $rot13" );
}
my $counter;
my @wlist = map {split ' '} $what;
if ($wlist[0] eq "!geo" && defined($wlist[1])){
my $geo = Geo::IPfree::new();
$geo->Clean_Cache;
my ($country_ab,$country_name) = $geo->LookUp($wlist[1]);
$kernel->post( $sender => privmsg => $channel => "$wlist[1]'s server located in $country_name ($country_ab)." );
}
undef;
}
# We registered for all events, this will produce some debug info.
sub _default {
my ($event, $args) = @_[ARG0 .. $#_];
my @output = ( "$event: " );
foreach my $arg ( @$args ) {
if ( ref($arg) eq 'ARRAY' ) {
push( @output, "[" . join(" ,", @$arg ) . "]" );
} else {
push ( @output, "'$arg'" );
}
}
print STDOUT join ' ', @output, "\n";
return 0;
}