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::Context;
+use NDIRC::IrcContext;
use IO::File;
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 .. $#_];
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' );
));
$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 );
+
+ 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',
+ 'reconnect' => 1,
+ 'verbose' => 1,
+ 'logdir' => '/tmp',
+ ));
+
+ $self->discord->gw->on('MESSAGE_CREATE' => sub { $self->discord_message_create(@_) });
+ $self->discord->gw->on('MESSAGE_UPDATE' => 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;
}
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);
}
sub irc_join {
}
+sub discord_ready {
+ my ($self, $gw, $hash) = @_;
+
+ $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, $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 {
my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
- if ($msg =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
- if (!$command || $command =~ m{^http://}){
+ 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)
}
$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`<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;