]> ruin.nu Git - NDIRC.git/commitdiff
Use Moose and make the bots object-oriented with Bot as base class
authorMichael Andreen <harv@ruin.nu>
Wed, 19 Aug 2009 22:47:24 +0000 (00:47 +0200)
committerMichael Andreen <harv@ruin.nu>
Wed, 19 Aug 2009 22:48:10 +0000 (00:48 +0200)
Bot.pm [new file with mode: 0644]
CommonStates.pm [deleted file]
Delling.pm
Eos.pm
ndawn.pl

diff --git a/Bot.pm b/Bot.pm
new file mode 100644 (file)
index 0000000..a4cd6e5
--- /dev/null
+++ b/Bot.pm
@@ -0,0 +1,208 @@
+#**************************************************************************
+#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
+#                                                                         *
+#   This program is free software; you can redistribute it and/or modify  *
+#   it under the terms of the GNU General Public License as published by  *
+#   the Free Software Foundation; either version 2 of the License, or     *
+#   (at your option) any later version.                                   *
+#                                                                         *
+#   This program is distributed in the hope that it will be useful,       *
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+#   GNU General Public License for more details.                          *
+#                                                                         *
+#   You should have received a copy of the GNU General Public License     *
+#   along with this program; if not, write to the                         *
+#   Free Software Foundation, Inc.,                                       *
+#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+#**************************************************************************/
+package NDIRC::Bot;
+
+use strict;
+use warnings;
+use feature ':5.10';
+
+use Moose;
+
+use POE::Session;
+use POE::Component::IRC::Plugin::Logger;
+use POE::Component::IRC::Plugin::BotTraffic;
+use POE::Component::IRC::Plugin::Connector;
+use POE::Component::IRC::Plugin::AutoJoin;
+use POE::Component::IRC::Plugin::NickReclaim;
+
+use NDIRC::Dispatcher;
+
+use IO::File;
+
+# We registered for all events, this will produce some debug info.
+sub _default {
+       my ($event, $args) = @_[ARG0 .. $#_];
+       my @output = ( "$event: " );
+
+       for my $arg (@$args) {
+               if ( ref $arg eq 'ARRAY' ) {
+                       push( @output, '[' . join(', ', @$arg ) . ']' );
+               }
+               else {
+                       push ( @output, "'$arg'" );
+               }
+       }
+       print join ' ', @output, "\n";
+       return 0;
+}
+
+sub _start {
+       my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION];
+
+       # retrieve our component's object from the heap where we stashed it
+       my $irc = $heap->{irc};
+       $kernel->sig( DIE => 'sig_DIE' );
+       $kernel->sig( USR1 => 'sig_usr1' );
+       $kernel->sig( USR2 => 'sig_usr2' );
+       $kernel->sig( INT => 'signal_handler' );
+
+       $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
+       $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
+       $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
+       $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
+               Path    => 'irclogs',
+               DCC     => 0,
+               Private => 1,
+               Public  => 1,
+               Sort_by_date => 1,
+               Strip_color => 1,
+               Strip_formatting => 1,
+       ));
+
+       $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
+               servers => ['irc.netgamers.org', 'underworld.no.eu.netgamers.org'
+                       ,'firefly.no.eu.netgamers.org', 'underworld.ca.us.netgamers.org' ]
+       );
+       $irc->plugin_add( 'Connector' => $heap->{connector} );
+
+       $kernel->signal($session => 'USR2');
+
+       $irc->yield( register => 'all' );
+       $irc->yield( connect => { server => 'irc.netgamers.org' } );
+
+       $kernel->delay( refresh => 60 );
+       return;
+}
+
+sub auth {
+       my $heap = $_[HEAP];
+
+       if (my $f =  new IO::File 'auth'){
+               my $user = <$f>;
+               chomp $user;
+               my $pass = <$f>;
+               chomp $pass;
+               $heap->{irc}->yield(qbot_auth => $user => $pass);
+       }
+}
+
+sub sig_usr1 {
+       my ($kernel,$heap) = @_[KERNEL,HEAP];
+
+       $kernel->yield( 'refresh' );
+}
+
+sub sig_usr2 {
+       my $heap = $_[HEAP];
+
+       my $disp = new NDIRC::Dispatcher;
+
+       if (my $commands = new IO::File 'commands'){
+               my @commands = split /\W+/, do{local $/; <$commands>};
+               say "Loading commands from: @commands";
+               $disp->load(@commands);
+       }
+
+       my $channels = new IO::File 'channels';
+       while (<$channels>){
+               my ($chan, @types) = split /\s+/;
+               say "$chan - @types";
+               if ($chan =~ /^(.*):(.*)$/){
+                       $chan = $1;
+                       $disp->set_target($2,$chan);
+               }
+               $disp->add_channel($chan,\@types);
+       }
+
+       $heap->{disp} = $disp;
+}
+
+sub sig_DIE {
+       my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
+       say "DIED!!!!!!!!!!!!!!";
+       # $sig is 'DIE'
+       # $ex is the exception hash
+       warn "$$: error in event: $ex->{error_str}";
+       $kernel->sig_handled();
+
+       # Send the signal to session that sent the original event.
+       #if( $ex->{source_session} ne $_[SESSION] ) {
+       #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
+       #}
+}
+
+sub signal_handler {
+       my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
+       print "First session caught SIG$signal_name\n";
+
+       given($signal_name){
+               when ('INT') {
+                       exit unless $heap->{irc}->connected;
+                       $heap->{INT} = 1;
+                       $heap->{irc}->yield(quit => 'Bye!');
+                       $kernel->sig_handled();
+               }
+       }
+       #$kernel->sig_handled();
+}
+
+sub irc_disconnected {
+       my ($sender,$heap) = @_[SENDER,HEAP];
+
+       exit if $heap->{INT};
+}
+
+sub irc_001 {
+       my ($sender,$heap,$kernel) = @_[SENDER,HEAP,KERNEL];
+
+       # 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";
+
+       $kernel->yield( 'auth' );
+       $irc->yield( mode => $irc->nick_name, '+ix');
+
+       # we join our channels
+       $irc->yield( join => $_ ) for grep /^#/, keys %{$heap->{disp}->channels};
+       return;
+}
+
+sub irc_invite {
+       my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
+       my $irc = $sender->get_heap();
+
+       $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$heap->{disp}->channels}
+}
+
+sub irc_public {
+}
+
+sub irc_msg {
+}
+
+sub  refresh {
+}
+
+sub irc_join {
+}
+
+1;
diff --git a/CommonStates.pm b/CommonStates.pm
deleted file mode 100644 (file)
index e46481c..0000000
+++ /dev/null
@@ -1,194 +0,0 @@
-#**************************************************************************
-#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
-#                                                                         *
-#   This program is free software; you can redistribute it and/or modify  *
-#   it under the terms of the GNU General Public License as published by  *
-#   the Free Software Foundation; either version 2 of the License, or     *
-#   (at your option) any later version.                                   *
-#                                                                         *
-#   This program is distributed in the hope that it will be useful,       *
-#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
-#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
-#   GNU General Public License for more details.                          *
-#                                                                         *
-#   You should have received a copy of the GNU General Public License     *
-#   along with this program; if not, write to the                         *
-#   Free Software Foundation, Inc.,                                       *
-#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
-#**************************************************************************/
-package NDIRC::CommonStates;
-
-use strict;
-use warnings;
-use feature ':5.10';
-
-use POE::Session;
-use POE::Component::IRC::Plugin::Logger;
-use POE::Component::IRC::Plugin::BotTraffic;
-use POE::Component::IRC::Plugin::Connector;
-use POE::Component::IRC::Plugin::AutoJoin;
-use POE::Component::IRC::Plugin::NickReclaim;
-
-use NDIRC::Dispatcher;
-
-use IO::File;
-
-# We registered for all events, this will produce some debug info.
-sub _default {
-       my ($event, $args) = @_[ARG0 .. $#_];
-       my @output = ( "$event: " );
-
-       for my $arg (@$args) {
-               if ( ref $arg eq 'ARRAY' ) {
-                       push( @output, '[' . join(', ', @$arg ) . ']' );
-               }
-               else {
-                       push ( @output, "'$arg'" );
-               }
-       }
-       print join ' ', @output, "\n";
-       return 0;
-}
-
-sub _start {
-       my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION];
-
-       # retrieve our component's object from the heap where we stashed it
-       my $irc = $heap->{irc};
-       $kernel->sig( DIE => 'sig_DIE' );
-       $kernel->sig( USR1 => 'sig_usr1' );
-       $kernel->sig( USR2 => 'sig_usr2' );
-       $kernel->sig( INT => 'signal_handler' );
-
-       $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
-       $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
-       $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
-       $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
-               Path    => 'irclogs',
-               DCC     => 0,
-               Private => 1,
-               Public  => 1,
-               Sort_by_date => 1,
-               Strip_color => 1,
-               Strip_formatting => 1,
-       ));
-
-       $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
-               servers => ['irc.netgamers.org', 'underworld.no.eu.netgamers.org'
-                       ,'firefly.no.eu.netgamers.org', 'underworld.ca.us.netgamers.org' ]
-       );
-       $irc->plugin_add( 'Connector' => $heap->{connector} );
-
-       $kernel->signal($session => 'USR2');
-
-       $irc->yield( register => 'all' );
-       $irc->yield( connect => { server => 'irc.netgamers.org' } );
-
-       $kernel->delay( refresh => 60 );
-       return;
-}
-
-sub auth {
-       my $heap = $_[HEAP];
-
-       if (my $f =  new IO::File 'auth'){
-               my $user = <$f>;
-               chomp $user;
-               my $pass = <$f>;
-               chomp $pass;
-               $heap->{irc}->yield(qbot_auth => $user => $pass);
-       }
-}
-
-sub sig_usr1 {
-       my ($kernel,$heap) = @_[KERNEL,HEAP];
-
-       $kernel->yield( 'refresh' );
-}
-
-sub sig_usr2 {
-       my $heap = $_[HEAP];
-
-       my $disp = new NDIRC::Dispatcher;
-
-       if (my $commands = new IO::File 'commands'){
-               my @commands = split /\W+/, do{local $/; <$commands>};
-               say "Loading commands from: @commands";
-               $disp->load(@commands);
-       }
-
-       my $channels = new IO::File 'channels';
-       while (<$channels>){
-               my ($chan, @types) = split /\s+/;
-               say "$chan - @types";
-               if ($chan =~ /^(.*):(.*)$/){
-                       $chan = $1;
-                       $disp->set_target($2,$chan);
-               }
-               $disp->add_channel($chan,\@types);
-       }
-
-       $heap->{disp} = $disp;
-}
-
-sub sig_DIE {
-       my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
-       say "DIED!!!!!!!!!!!!!!";
-       # $sig is 'DIE'
-       # $ex is the exception hash
-       warn "$$: error in event: $ex->{error_str}";
-       $kernel->sig_handled();
-
-       # Send the signal to session that sent the original event.
-       #if( $ex->{source_session} ne $_[SESSION] ) {
-       #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
-       #}
-}
-
-sub signal_handler {
-       my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
-       print "First session caught SIG$signal_name\n";
-
-       given($signal_name){
-               when ('INT') {
-                       exit unless $heap->{irc}->connected;
-                       $heap->{INT} = 1;
-                       $heap->{irc}->yield(quit => 'Bye!');
-                       $kernel->sig_handled();
-               }
-       }
-       #$kernel->sig_handled();
-}
-
-sub irc_disconnected {
-       my ($sender,$heap) = @_[SENDER,HEAP];
-
-       exit if $heap->{INT};
-}
-
-sub irc_001 {
-       my ($sender,$heap,$kernel) = @_[SENDER,HEAP,KERNEL];
-
-       # 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";
-
-       $kernel->yield( 'auth' );
-       $irc->yield( mode => $irc->nick_name, '+ix');
-
-       # we join our channels
-       $irc->yield( join => $_ ) for grep /^#/, keys %{$heap->{disp}->channels};
-       return;
-}
-
-sub irc_invite {
-       my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
-       my $irc = $sender->get_heap();
-
-       $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$heap->{disp}->channels}
-}
-
-1;
index 362e7aac79b93846ebee164e0a27071e77e7175f..33141596b3f6d82c29ceebe6ec6e4d1be398dbcd 100644 (file)
@@ -1,5 +1,5 @@
 #**************************************************************************
-#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
+#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
 #                                                                         *
 #   This program is free software; you can redistribute it and/or modify  *
 #   it under the terms of the GNU General Public License as published by  *
@@ -22,6 +22,9 @@ use strict;
 use warnings;
 use feature ':5.10';
 
+use Moose;
+extends 'NDIRC::Bot';
+
 use POE::Session;
 use NDIRC::Misc;
 use ND::DB;
diff --git a/Eos.pm b/Eos.pm
index 079b7c9012ddc7acdc00a6f3c54bb369669b8bf5..258dd729cd6502b16e427f9ae3d529cbf073f95b 100644 (file)
--- a/Eos.pm
+++ b/Eos.pm
@@ -1,5 +1,5 @@
 #**************************************************************************
-#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
+#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
 #                                                                         *
 #   This program is free software; you can redistribute it and/or modify  *
 #   it under the terms of the GNU General Public License as published by  *
@@ -22,6 +22,9 @@ use strict;
 use warnings;
 use feature ':5.10';
 
+use Moose;
+extends 'NDIRC::Bot';
+
 use POE::Session;
 use NDIRC::Misc;
 use ND::DB;
index ca60ea24e1c6593a29fe7339661c61290e15eafa..a14ee36fe319bef16eaaeb34b7eda5d8c3e5acf3 100755 (executable)
--- a/ndawn.pl
+++ b/ndawn.pl
@@ -24,8 +24,6 @@ use warnings;
 use feature ':5.10';
 use POE qw(Component::IRC::Qnet::State);
 
-
-use NDIRC::CommonStates;
 eval "require NDIRC::$ARGV[0]";
 
 die $@ if $@;
@@ -38,11 +36,13 @@ my $irc = POE::Component::IRC::Qnet::State->spawn(
 
 $irc->service_bots(QBOT => 'P@cservice.netgamers.org');
 
+my $bot = eval "new NDIRC::$ARGV[0]";
+
 POE::Session->create(
-       package_states => [
-               'NDIRC::CommonStates' => [ qw(_default _start irc_001 sig_DIE sig_usr1 sig_usr2
-                       signal_handler irc_disconnected irc_invite auth) ],
-               "NDIRC::$ARGV[0]" => [ qw(irc_public irc_msg refresh irc_join) ],
+       object_states => [
+               $bot => [ qw(_default _start irc_001 sig_DIE sig_usr1 sig_usr2
+                       signal_handler irc_disconnected irc_invite auth
+                       irc_public irc_msg refresh irc_join) ],
        ],
        heap => { irc => $irc },
 );