X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Bot.pm;h=363531217864018f1faf26ab54ec00a37cbeae9d;hb=refs%2Fheads%2Fdiscord;hp=a4cd6e51c522819f3d73afa3d3a2e6c05fd7be0a;hpb=05629ec258dcfc359decc3cbc44c3345dd0eb62e;p=NDIRC.git diff --git a/Bot.pm b/Bot.pm index a4cd6e5..3635312 100644 --- a/Bot.pm +++ b/Bot.pm @@ -24,17 +24,56 @@ use feature ':5.10'; 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; use POE::Component::IRC::Plugin::AutoJoin; use POE::Component::IRC::Plugin::NickReclaim; +use Mojo::Discord; +use Mojo::IOLoop; + use NDIRC::Dispatcher; +use NDIRC::IrcContext; use IO::File; +has disp => ( + is => 'rw', + isa => 'Object', + lazy_build => 1 +); + +has discord => ( + is => 'rw', + isa => 'Object' +); + +has discord_name => ( + is => 'rw', + isa => 'Str' +); + +has discord_id => ( + is => 'rw', + 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 .. $#_]; @@ -52,11 +91,16 @@ sub _default { return 0; } +my $irc; +sub irc { + return $irc; +} + sub _start { - my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION]; + 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' ); @@ -73,20 +117,42 @@ sub _start { Sort_by_date => 1, Strip_color => 1, Strip_formatting => 1, + Notices => 1, )); $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 => [['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 => { server => 'irc.netgamers.org' } ); $kernel->delay( refresh => 60 ); + + if (my $f = new IO::File 'discord'){ + my $user = <$f>; + chomp $user; + my $token = <$f>; + chomp $token; + + $self->discord(Mojo::Discord->new( + 'token' => $token, + '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, + )); + $self->discord->init(); + } return; } @@ -108,9 +174,71 @@ sub sig_usr1 { $kernel->yield( 'refresh' ); } +sub clear_constraint { + my $tc = shift; + + while (1) { + if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){ + for my $t (@{$tc->{type_constraints}}){ + clear_constraint($t); + } + + } + if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){ + clear_constraint($tc->{type_parameter}); + } + last if ref $tc eq 'HASH'; + last if ref $tc eq ''; + if (defined $tc->{_type_constraint}){ + $tc = $tc->{_type_constraint}; + }elsif(defined $tc->{__type_constraint}){ + $tc = $tc->{__type_constraint}; + }else{ + last; + } + } +} + +sub clear_metains { + my $ins = shift; + + for my $a (@{$ins->{attributes}}){ + for my $m (@{$a->{associated_methods}}){ + $m->{body} = undef; + } + clear_constraint($a->{isa}); + } +} + +sub clear_cycles { + my $c = shift; + + for my $m (values %{$c->meta->{methods}}){ + clear_constraint($m->{type_constraint}); + + my $ps = $m->{parsed_signature}; + for my $p (@{$ps->{_positional_params}->{params}}){ + clear_metains($p->{__MOP__}->{_meta_instance}); + } + + $m->{body} = undef; + } + clear_metains($c->meta->{_meta_instance}); +} + + sub sig_usr2 { - my $heap = $_[HEAP]; + my $self = shift @_; + + for my $c (values %{$self->disp->commands}){ + clear_cycles($c); + } + + $self->disp($self->_build_disp); +} +sub _build_disp { + my ($self) = @_; my $disp = new NDIRC::Dispatcher; if (my $commands = new IO::File 'commands'){ @@ -119,18 +247,21 @@ sub sig_usr2 { $disp->load(@commands); } - my $channels = new IO::File 'channels'; + %{$self->targets} = (); + my $channels = new IO::File '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}},$chan; + say "$2 - @{$self->targets->{$2}}"; } $disp->add_channel($chan,\@types); } - $heap->{disp} = $disp; + return $disp; } sub sig_DIE { @@ -169,7 +300,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,21 +313,23 @@ 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, 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 { + $_[ARG2] = irc_to_utf8 $_[ARG2]; } sub irc_msg { + $_[ARG2] = irc_to_utf8 $_[ARG2]; } sub refresh { @@ -205,4 +338,142 @@ sub refresh { sub irc_join { } +sub discord_ready { + my $self = shift; + my $hash = shift; + $self->discord_id($hash->{user}{id}); + $self->discord_name($hash->{user}{username}); + + say localtime(time) . " - Connected to Discord. $self->{discord_id}"; +} + +sub discord_message_create { +} + +sub discord_guild_create { + my $self = shift; + my $hash = shift; + + for my $chan (@{$hash->{channels}}) { + say localtime(time) . " - $chan->{id} - $chan->{name}"; + $self->discord_channels->{$chan->{id}} = $chan; + } +} + +sub discord_channel_create { + my $self = shift; + my $chan = shift; + + 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"; + + return $self->disp->run_command($c,$command,$args); +} + +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{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){ + if (!$command || $command =~ m{^https?://}){ + ($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::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+)$/) { + $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;