]> ruin.nu Git - NDIRC.git/blobdiff - Bot.pm
Make disp an instance variable, instead of heap, and move parseCommand to Bot
[NDIRC.git] / Bot.pm
diff --git a/Bot.pm b/Bot.pm
index a4cd6e51c522819f3d73afa3d3a2e6c05fd7be0a..c90f8339d0d061e6009c10c5dd88ac6729ed8860 100644 (file)
--- a/Bot.pm
+++ b/Bot.pm
@@ -35,6 +35,12 @@ use NDIRC::Dispatcher;
 
 use IO::File;
 
+has disp => (
+       is => 'rw',
+       isa => 'Object',
+       lazy_build => 1
+);
+
 # We registered for all events, this will produce some debug info.
 sub _default {
        my ($event, $args) = @_[ARG0 .. $#_];
@@ -81,8 +87,6 @@ sub _start {
        );
        $irc->plugin_add( 'Connector' => $heap->{connector} );
 
-       $kernel->signal($session => 'USR2');
-
        $irc->yield( register => 'all' );
        $irc->yield( connect => { server => 'irc.netgamers.org' } );
 
@@ -109,8 +113,13 @@ sub sig_usr1 {
 }
 
 sub sig_usr2 {
-       my $heap = $_[HEAP];
+       my $self = shift @_;
+
+       $self->disp($self->_build_disp);
+}
 
+sub _build_disp {
+       my ($self) = @_;
        my $disp = new NDIRC::Dispatcher;
 
        if (my $commands = new IO::File 'commands'){
@@ -130,7 +139,7 @@ sub sig_usr2 {
                $disp->add_channel($chan,\@types);
        }
 
-       $heap->{disp} = $disp;
+       return $disp;
 }
 
 sub sig_DIE {
@@ -169,7 +178,7 @@ sub irc_disconnected {
 }
 
 sub irc_001 {
-       my ($sender,$heap,$kernel) = @_[SENDER,HEAP,KERNEL];
+       my ($self,$sender,$kernel) = @_[OBJECT,SENDER,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
@@ -182,15 +191,15 @@ sub irc_001 {
        $irc->yield( mode => $irc->nick_name, '+ix');
 
        # we join our channels
-       $irc->yield( join => $_ ) for grep /^#/, keys %{$heap->{disp}->channels};
+       $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
        return;
 }
 
 sub irc_invite {
-       my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
+       my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, HEAP, ARG0 .. ARG1];
        my $irc = $sender->get_heap();
 
-       $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$heap->{disp}->channels}
+       $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
 }
 
 sub irc_public {
@@ -205,4 +214,49 @@ sub  refresh {
 sub irc_join {
 }
 
+sub parseCommand {
+       my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
+
+       return if $channel !~ /^#/ && $msg =~ /^~/;
+       $msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;
+
+       my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
+
+       if ($msg =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
+               if (!$command || $command =~ m{^http://}){
+                       ($p,$command,$args) = ('.','addscan',$msg);
+               }elsif($command ne 'addscan'){
+                       $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
+               }
+       }
+
+       return 0 unless $self->disp->has_command($command,$channel);
+
+       my $reply_string;
+       given ($p){
+               when ('!'){
+                       $reply_string = "privmsg $nick";
+               }
+               when ('~'){
+                       $reply_string = "privmsg $channel";
+               }
+               default {
+                       $reply_string = "notice $nick";
+               }
+       }
+
+       $address =~ s/.*@(.*)/$1/;
+       my $c = NDIRC::Context->new({
+                       host => $address,
+                       nick => $nick,
+                       channel => $channel,
+                       disp => $self->disp,
+                       model => $model,
+                       server => $server,
+                       reply_string => $reply_string,
+               });
+
+       return $self->disp->run_command($c,$command,$args);
+}
+
 1;