]> ruin.nu Git - NDIRC.git/blobdiff - Bot.pm
Remove indirect object notation
[NDIRC.git] / Bot.pm
diff --git a/Bot.pm b/Bot.pm
index 0d517867f3dd39869ec9a4e542ca0afee753b0c6..e25918221d2dcb02af98c76f5883dffa7c718714 100644 (file)
--- a/Bot.pm
+++ b/Bot.pm
@@ -91,11 +91,16 @@ sub _default {
        return 0;
 }
 
+my $irc;
+sub irc {
+       return $irc;
+}
+
 sub _start {
        my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
 
        # retrieve our component's object from the heap where we stashed it
-       my $irc = $heap->{irc};
+       $irc = $heap->{irc};
        $kernel->sig( DIE => 'sig_DIE' );
        $kernel->sig( USR1 => 'sig_usr1' );
        $kernel->sig( USR2 => 'sig_usr2' );
@@ -116,13 +121,12 @@ sub _start {
        ));
 
        $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'] ]
+               servers => [['chronos.fr.eu.netgamers.org'], ['underworld.no.eu.netgamers.org']]
        );
        $irc->plugin_add( 'Connector' => $heap->{connector} );
 
        $irc->yield( register => 'all' );
-       $irc->yield( connect => { server => 'irc.netgamers.org' } );
+       #$irc->yield( connect => { server => 'chronos.fr.eu.netgamers.org' } );
 
        $kernel->delay( refresh => 60 );
 
@@ -137,15 +141,16 @@ sub _start {
                                'name'      => $user,
                                'url'       => 'https://nd.ruin.nu',
                                'version'   => '1.0',
-                               'callbacks' => {
-                                       'READY'          => sub { $self->discord_ready(@_) },
-                                       'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
-                                       'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
-                                       'CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) },
-                               },
                                'reconnect' => 1,
                                'verbose'   => 1,
+                               'logdir'   => '/tmp',
                        ));
+
+               $self->discord->gw->on('MESSAGE_CREATE' => sub { $self->discord_message_create(@_) });
+               $self->discord->gw->on('READY'          => sub { $self->discord_ready(@_) });
+               $self->discord->gw->on('GUILD_CREATE'   => sub { $self->discord_guild_create(@_) });
+               $self->discord->gw->on('CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) });
+
                $self->discord->init();
        }
        return;
@@ -234,23 +239,23 @@ sub sig_usr2 {
 
 sub _build_disp {
        my ($self) = @_;
-       my $disp = new NDIRC::Dispatcher;
+       my $disp = NDIRC::Dispatcher->new;
 
-       if (my $commands = new IO::File 'commands'){
+       if (my $commands = IO::File->new('commands')){
                my @commands = split /\W+/, do{local $/; <$commands>};
                say "Loading commands from: @commands";
                $disp->load(@commands);
        }
 
        %{$self->targets} = ();
-       my $channels = new IO::File 'channels' or die $!;;
+       my $channels = IO::File->new('channels') or die $!;;
        while (<$channels>){
                my ($chan, @types) = split /\s+/;
                say "$chan - @types";
                if ($chan =~ /^(.*):(.*)$/){
                        $chan = $1;
                        $self->targets->{$2} = [] unless exists $self->targets->{$2};
-                       push @{$self->targets->{$2}},$chan;
+                       push @{$self->targets->{$2}},lc $chan;
                        say "$2 - @{$self->targets->{$2}}";
                }
                $disp->add_channel($chan,\@types);
@@ -334,8 +339,8 @@ sub irc_join {
 }
 
 sub discord_ready {
-       my $self = shift;
-       my $hash = shift;
+       my ($self, $gw, $hash) = @_;
+
        $self->discord_id($hash->{user}{id});
        $self->discord_name($hash->{user}{username});
 
@@ -346,8 +351,7 @@ sub discord_message_create {
 }
 
 sub discord_guild_create {
-       my $self = shift;
-       my $hash = shift;
+       my ($self, $gw, $hash) = @_;
 
        for my $chan (@{$hash->{channels}}) {
                say localtime(time) . " - $chan->{id} - $chan->{name}";
@@ -356,8 +360,7 @@ sub discord_guild_create {
 }
 
 sub discord_channel_create {
-       my $self = shift;
-       my $chan = shift;
+       my ($self, $gw, $chan) = @_;
 
        for my $key (keys %{$chan}) {
                say localtime(time) . " - $key - $chan->{$key}";
@@ -365,6 +368,28 @@ sub discord_channel_create {
        $self->discord_channels->{$chan->{id}} = $chan;
 }
 
+sub handleCommand {
+       my ($self, $c, $msg) = @_;
+
+       my ($p,$command,$args) = ($msg =~ /^([.!])(\S+)(?: (.+))?/);
+
+       if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
+               if (!$command || $command =~ m{^https?://}){
+                       ($p,$command,$args) = ('.','addscan',$msg);
+               }elsif($command ne 'addscan'){
+                       $self->handleCommand ($c, ".addscan $msg")
+               }
+       }
+
+       return 0 unless $self->disp->has_command($command,$c->channel);
+
+       say localtime(time) . " - $msg";
+
+       $c->dm_reply(1) if $p eq '!';
+
+       return $self->disp->run_command($c,$command,$args);
+}
+
 sub parseCommand {
        my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
 
@@ -404,6 +429,7 @@ sub parseCommand {
                        disp => $self->disp,
                        model => $model,
                        server => $server,
+                       bot => $self,
                        reply_string => $reply_string,
                });
 
@@ -415,19 +441,39 @@ sub toTarget {
 
        return unless exists $self->targets->{$target};
 
-       my $session = $poe_kernel->get_active_session();
-       my $heap = $session->get_heap();
-       my $irc = $heap->{irc};
+       $self->message($msg, @{$self->targets->{$target}});
 
+}
+
+sub message {
+       my ($self, $msg, @targets) = @_;
 
-       for (@{$self->targets->{$target}}) {
-               when (/^#/) {
-                       $irc->yield(privmsg => $_, $msg);
+       for (@targets) {
+               when (/^D-(\d+)$/i) {
+                       $self->discordMessage($1, $msg);
                }
-               when (/^d-(\d+)/i) {
-                       $self->discord->send_message($1, $msg );
+               default {
+                       $self->ircMessage(privmsg => $_, $msg);
                }
        }
 }
 
+sub ircMessage {
+       my ($self, $command, $target, $msg) = @_;
+
+       $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
+       $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
+
+       $self->irc->yield($command, $target, $msg);
+}
+
+sub discordMessage {
+       my ($self, $target, $msg) = @_;
+
+       $msg =~ s`<b>(.*?)</b>`**$1**`gi;
+       $msg =~ s`<c(\d+)>(.*?)</c>`*$2*`gi;
+
+       $self->discord->send_message($target, $msg );
+}
+
 1;