From a4667cce6415476312d6932f92f89fb28101ab88 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 18 Aug 2009 23:15:11 +0200 Subject: [PATCH] Authentication, autojoin, reconnect and minor fixes for common structure --- CommonStates.pm | 53 ++++++++++++++++++++++++++++++++++++------------- Dispatcher.pm | 4 ++-- ndawn.pl | 16 +++++---------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/CommonStates.pm b/CommonStates.pm index b91cd9f..5c0ab70 100644 --- a/CommonStates.pm +++ b/CommonStates.pm @@ -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 $/; }; - close COMMANDS; - - say "Loading commands from: @commands"; my $disp = new NDIRC::Dispatcher; - $disp->load(@commands); - open CHANNELS, 'channels'; - while (){ + 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; diff --git a/Dispatcher.pm b/Dispatcher.pm index 8d7ffe9..a96c32e 100644 --- a/Dispatcher.pm +++ b/Dispatcher.pm @@ -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}){ diff --git a/ndawn.pl b/ndawn.pl index eecf5ee..e89dc3a 100755 --- 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 }, ); -- 2.39.2