]> ruin.nu Git - NDIRC.git/commitdiff
Authentication, autojoin, reconnect and minor fixes for common structure
authorMichael Andreen <harv@ruin.nu>
Tue, 18 Aug 2009 21:15:11 +0000 (23:15 +0200)
committerMichael Andreen <harv@ruin.nu>
Tue, 18 Aug 2009 21:15:11 +0000 (23:15 +0200)
CommonStates.pm
Dispatcher.pm
ndawn.pl

index b91cd9f1e39d235f62cb8259b0cade333c078a64..5c0ab703aefd63b66691b72f955e322a8f67df94 100644 (file)
@@ -26,9 +26,13 @@ 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 .. $#_];
@@ -56,6 +60,8 @@ sub _start {
        $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',
@@ -67,41 +73,56 @@ sub _start {
                Strip_formatting => 1,
        ));
 
-       $heap->{connector} = POE::Component::IRC::Plugin::Connector->new();
+       $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 => { } );
+       $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 = $_[KERNEL];
+       my ($kernel,$heap) = @_[KERNEL,HEAP];
+
        $kernel->yield( 'refresh' );
 }
 
 sub sig_usr2 {
        my $heap = $_[HEAP];
 
-       open COMMANDS, 'commands';
-       my @commands = split /\W+/, do { local $/; <COMMANDS> };
-       close COMMANDS;
-
-       say "Loading commands from: @commands";
        my $disp = new NDIRC::Dispatcher;
-       $disp->load(@commands);
 
-       open CHANNELS, 'channels';
-       while (<CHANNELS>){
+       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";
                $disp->add_channel($chan,\@types);
        }
-       close CHANNELS;
 
        $heap->{disp} = $disp;
 }
@@ -127,6 +148,7 @@ sub signal_handler {
        given($signal_name){
                when ('INT') {
                        exit unless $heap->{irc}->connected;
+                       $heap->{INT} = 1;
                        $heap->{irc}->yield(quit => 'Bye!');
                        $kernel->sig_handled();
                }
@@ -137,11 +159,11 @@ sub signal_handler {
 sub irc_disconnected {
        my ($sender,$heap) = @_[SENDER,HEAP];
 
-       exit;
+       exit if $heap->{INT};
 }
 
 sub irc_001 {
-       my ($sender,$heap) = @_[SENDER,HEAP];
+       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
@@ -150,6 +172,9 @@ sub irc_001 {
 
        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;
index 8d7ffe9843893ba2768cced2fd149a8becec64c5..a96c32e164843fda69c1998d5b18abad2f89fa4f 100644 (file)
@@ -54,7 +54,7 @@ sub load {
                        warn "couldn't do $file: $!"    if $!;
                        warn "couldn't run $file"       unless $return;
                }else {
-                       print "Loading $class\n";
+                       say "\nLoading $class";
                        $self->load_class($class);
 
                }
@@ -68,7 +68,7 @@ sub load_class {
        for my $c (@subs){
                my $attr = eval "$class->meta->get_method('$c')->attributes";
                if (@$attr){
-                       print "Command: $c";
+                       print "Command: $c ";
                        my %c = (channels => ['all']);
                        my @aliases;
                        for (@{$attr}){
index eecf5ee2c0d231bd8ec0d506cb1854735fd68c72..e89dc3a38cb982966c07beb93212779e23bea834 100755 (executable)
--- a/ndawn.pl
+++ b/ndawn.pl
@@ -26,18 +26,12 @@ use POE qw(Component::IRC::Qnet::State);
 
 
 use NDIRC::CommonStates;
-use NDIRC::Eos;
-
-my $nickname = 'ndbot';
-my $ircname = 'ND test bot';
-my $server = 'irc.netgamers.org';
+eval "require NDIRC::$ARGV[0]";
 
 # We create a new PoCo-IRC object
 my $irc = POE::Component::IRC::Qnet::State->spawn(
-       nick => $nickname,
-       ircname => $ircname,
-       server => $server,
-       servers => [$server, 'underworld.no.eu.netgamers.org', ]
+       nick => $ARGV[0],
+       ircname => 'ND bot',
 ) or die "Oh noooo! $!";
 
 $irc->service_bots(QBOT => 'P@cservice.netgamers.org');
@@ -49,8 +43,8 @@ $ND::memchan = '#testarmer';
 POE::Session->create(
        package_states => [
                'NDIRC::CommonStates' => [ qw(_default _start irc_001 sig_DIE sig_usr1 sig_usr2
-                       signal_handler irc_disconnected irc_invite) ],
-               'NDIRC::Eos' => [ qw(irc_public irc_msg refresh irc_join) ],
+                       signal_handler irc_disconnected irc_invite auth) ],
+               "NDIRC::$ARGV[0]" => [ qw(irc_public irc_msg refresh irc_join) ],
        ],
        heap => { irc => $irc },
 );