X-Git-Url: https://ruin.nu/git/%3CTMPL_VAR%20NAME=PAGE%3E?a=blobdiff_plain;f=Bot.pm;h=1c54701f3251829af9c333e29a12b81d001ed031;hb=3b7dbc35e153a4a3922f98daf365fe76245e35e4;hp=9bf39f9ae09c612b88bdca9100558b1a4f505955;hpb=036bfadee395d5ec8bfa4e00b05077a90a8d232a;p=NDIRC.git
diff --git a/Bot.pm b/Bot.pm
index 9bf39f9..1c54701 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;
@@ -67,6 +68,12 @@ has discord_channels => (
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 .. $#_];
@@ -84,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' );
@@ -109,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 );
@@ -130,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;
@@ -234,13 +247,16 @@ sub _build_disp {
$disp->load(@commands);
}
+ %{$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}},lc $chan;
+ say "$2 - @{$self->targets->{$2}}";
}
$disp->add_channel($chan,\@types);
}
@@ -323,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});
@@ -335,13 +351,43 @@ 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}";
- $self->discord_channels->{$chan->{id}} = $chan->{name};
+ 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")
+ }
+ }
+
+ $c->dm_reply(1) if $p eq '!';
+
+ return 0 unless $self->disp->has_command($command,$c->channel);
+
+ say localtime(time) . " - $msg";
+
+ return $self->disp->run_command($c,$command,$args);
}
sub parseCommand {
@@ -376,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;