X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Bot.pm;h=e25918221d2dcb02af98c76f5883dffa7c718714;hb=b8bf12b07ac296bd878c20951ee6435515bae169;hp=89ec47d7c1e75ae9d7c3cb397b07e0b92b6a2026;hpb=0822847cd53af14ada68762a2f9d152a274a4d9b;p=NDIRC.git diff --git a/Bot.pm b/Bot.pm index 89ec47d..e259182 100644 --- a/Bot.pm +++ b/Bot.pm @@ -26,6 +26,7 @@ use Moose; use POE::Component::IRC::Common qw/irc_to_utf8/; use POE::Session; +use POE::Kernel; use POE::Component::IRC::Plugin::Logger; use POE::Component::IRC::Plugin::BotTraffic; use POE::Component::IRC::Plugin::Connector; @@ -36,7 +37,7 @@ use Mojo::Discord; use Mojo::IOLoop; use NDIRC::Dispatcher; -use NDIRC::Context; +use NDIRC::IrcContext; use IO::File; @@ -61,6 +62,18 @@ has discord_id => ( isa => 'Str' ); +has discord_channels => ( + is => 'rw', + isa => 'HashRef', + default => sub { {} } +); + +has targets => ( + is => 'ro', + isa => 'HashRef[ArrayRef[Str]]', + default => sub{ {} }, +); + # We registered for all events, this will produce some debug info. sub _default { my ($event, $args) = @_[ARG0 .. $#_]; @@ -78,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' ); @@ -103,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 ); @@ -124,14 +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(@_) }, - }, '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; @@ -220,21 +239,24 @@ 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); } - my $channels = new IO::File 'channels' or die $!;; + %{$self->targets} = (); + my $channels = IO::File->new('channels') or die $!;; while (<$channels>){ my ($chan, @types) = split /\s+/; say "$chan - @types"; if ($chan =~ /^(.*):(.*)$/){ $chan = $1; - $disp->set_target($2,$chan); + $self->targets->{$2} = [] unless exists $self->targets->{$2}; + push @{$self->targets->{$2}},lc $chan; + say "$2 - @{$self->targets->{$2}}"; } $disp->add_channel($chan,\@types); } @@ -317,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}); @@ -329,6 +351,43 @@ sub discord_message_create { } sub discord_guild_create { + my ($self, $gw, $hash) = @_; + + for my $chan (@{$hash->{channels}}) { + say localtime(time) . " - $chan->{id} - $chan->{name}"; + $self->discord_channels->{$chan->{id}} = $chan; + } +} + +sub discord_channel_create { + my ($self, $gw, $chan) = @_; + + for my $key (keys %{$chan}) { + say localtime(time) . " - $key - $chan->{$key}"; + } + $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 { @@ -363,17 +422,58 @@ sub parseCommand { } $address =~ s/.*@(.*)/$1/; - my $c = NDIRC::Context->new({ + my $c = NDIRC::IrcContext->new({ host => $address, nick => $nick, channel => $channel, disp => $self->disp, model => $model, server => $server, + bot => $self, reply_string => $reply_string, }); return $self->disp->run_command($c,$command,$args); } +sub toTarget { + my ($self, $target, $msg) = @_; + + return unless exists $self->targets->{$target}; + + $self->message($msg, @{$self->targets->{$target}}); + +} + +sub message { + my ($self, $msg, @targets) = @_; + + for (@targets) { + when (/^D-(\d+)$/i) { + $self->discordMessage($1, $msg); + } + default { + $self->ircMessage(privmsg => $_, $msg); + } + } +} + +sub ircMessage { + my ($self, $command, $target, $msg) = @_; + + $msg =~ s`(.*?)`${\(chr(2))}$1${\(chr(15))}`gi; + $msg =~ s`(.*?)`${\(chr(3))}$1$2${\(chr(15))}`gi; + + $self->irc->yield($command, $target, $msg); +} + +sub discordMessage { + my ($self, $target, $msg) = @_; + + $msg =~ s`(.*?)`**$1**`gi; + $msg =~ s`(.*?)`*$2*`gi; + + $self->discord->send_message($target, $msg ); +} + 1;