Permalink
Browse files

bot version 2, changed conf format, supports multiple servers

  • Loading branch information...
1 parent c4adef7 commit 1e78396f2ac5fdab1b32f3404427edda7b769668 @rbuels rbuels committed Mar 19, 2010
Showing with 77 additions and 48 deletions.
  1. +29 −2 radicalweed.conf
  2. +48 −46 radicalweed.pl
View
@@ -1,7 +1,11 @@
# each section is a channel for the bot to join
# and in that section, just list the nicks that the bot
# should give op to (set +o)
- <cxgn>
+<server irc.perl.org>
+ nick radicalweed2
+ description Solanum carolinense - the SGN Op Bot
+ <channel cxgn>
+ <ops>
adri
aure
isaak
@@ -10,4 +14,27 @@
mueller
naama
rbuels
- </cxgn>
+ </ops>
+ </channel>
+ <channel toast>
+ </channel>
+</server>
+
+<server irc.freenode.net>
+ nick buelsbot
+ description Rob's little bot
+ <channel bioperl>
+ <ops>
+ rbuels
+ deafferret
+ pyrimidine
+ </ops>
+ </channel>
+ <channel obf-soc>
+ <ops>
+ rbuels
+ deafferret
+ pyrimidine
+ </ops>
+ </channel>
+</server>
View
@@ -7,23 +7,52 @@
use local::lib './local_lib';
use POE qw(Component::IRC);
use Config::General;
-
-my @channels = do {
- my %c = read_config();
- map "#$_", keys %c
- };
-
-# We create a new PoCo-IRC object
-my $irc = POE::Component::IRC->spawn(
- nick => 'radicalweed',
- ircname => 'the SGN Op Bot - Solanum carolinense',
- server => 'irc.perl.org',
- ) or die "Oh noooo! $!";
-
-POE::Session->create(
- package_states => [ main => [qw( _start irc_001 irc_join )], ],
- heap => { irc => $irc },
-);
+use Hash::Util qw/ lock_hash /;
+#use DB_File::Lock;
+
+my %config = read_config();
+lock_hash %config;while( my ($server_addr,$server) = each %{$config{server}} ) {
+
+ print "setting up $server_addr...\n";
+
+ # We create a new PoCo-IRC object
+ my $irc = POE::Component::IRC->spawn(
+ nick => $server->{nick},
+ ircname => $server->{description},
+ server => $server_addr,
+ ) or die "Oh noooo! $!";
+
+ POE::Session->create(
+ package_states => [main => [qw[ _start _default ]]],
+ inline_states => {
+ irc_001 => sub {
+ 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 keys %{ $server->{channel} };
+ },
+ irc_join => sub {
+ my ( $sender, $who, $where, $what ) = @_[ SENDER, ARG0 .. ARG2 ];
+ my $nick = ( split /!/, $who )[0];
+ my $channel = ref $where ? $where->[0] : $where;
+ $channel =~ s/^#//;
+
+ my %config = read_config();
+ if( exists $config{server}{$server_addr}{channel}{$channel}{ops}{$nick} ) {
+ $irc->yield( mode => "#$channel" => '+o' => $nick );
+ }
+ },
+ },
+ heap => { irc => $irc },
+ );
+}
$poe_kernel->run();
@@ -32,41 +61,14 @@ sub _start {
# retrieve our component's object from the heap where we stashed it
my $irc = $heap->{irc};
-
$irc->yield( register => 'all' );
$irc->yield( connect => {} );
- 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_join {
- my ( $sender, $who, $where, $what ) = @_[ SENDER, ARG0 .. ARG2 ];
- my $nick = ( split /!/, $who )[0];
- my $channel = ref $where ? $where->[0] : $where;
- $channel =~ s/^#//;
-
- my %config = read_config();
- if( exists $config{$channel}{$nick} ) {
- $irc->yield( mode => "#$channel" => '+o' => $nick );
- }
-}
-
sub read_config {
- return Config::General->new('./radicalweed.conf')->getall;
+ return Config::General->new('./radicalweed2.conf')->getall;
}
# sub irc_public {
@@ -85,7 +87,7 @@ sub read_config {
sub _default {
my ( $event, $args ) = @_[ ARG0 .. $#_ ];
my @output = ("$event: ");
-
+
for my $arg (@$args) {
if ( ref $arg eq 'ARRAY' ) {
push( @output, '[' . join( ', ', @$arg ) . ']' );

0 comments on commit 1e78396

Please sign in to comment.