From 445fc8eeeb37b4728b1d7608862e772856eebb19 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sat, 23 Feb 2019 23:04:08 +0100 Subject: [PATCH] Webbie messages --- Bot.pm | 32 +++++++++++++++++++++++++++++++- Delling.pm | 22 +++++++++++----------- Dispatcher.pm | 10 ---------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Bot.pm b/Bot.pm index 0a65f8f..0d51786 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; @@ -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 .. $#_]; @@ -235,13 +242,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}},$chan; + say "$2 - @{$self->targets->{$2}}"; } $disp->add_channel($chan,\@types); } @@ -400,4 +410,24 @@ sub parseCommand { return $self->disp->run_command($c,$command,$args); } +sub toTarget { + my ($self, $target, $msg) = @_; + + return unless exists $self->targets->{$target}; + + my $session = $poe_kernel->get_active_session(); + my $heap = $session->get_heap(); + my $irc = $heap->{irc}; + + + for (@{$self->targets->{$target}}) { + when (/^#/) { + $irc->yield(privmsg => $_, $msg); + } + when (/^d-(\d+)/i) { + $self->discord->send_message($1, $msg ); + } + } +} + 1; diff --git a/Delling.pm b/Delling.pm index 1eb4824..96ff5ba 100644 --- a/Delling.pm +++ b/Delling.pm @@ -103,7 +103,7 @@ WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v'); $irc->yield(mode => $channel, $mode, @who) if $mode; } - if (lc $channel ~~ lc $disp->targets->{members}){ + if (lc $channel ~~ lc $self->targets->{members}){ if (time - $last_announcement < 1){ $last_announcement = time; return; @@ -115,14 +115,14 @@ FROM users u WHERE hostmask = ? }, undef, $address); if ($user){ unless ($user->{password}) { - $irc->yield(privmsg => $disp->targets->{members}, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points"); + $irc->yield(privmsg => $self->targets->{members}, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points"); } if ($tick > 12 && not defined $user->{pid}){ - $irc->yield(privmsg => $disp->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords."); + $irc->yield(privmsg => $self->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords."); } if (not defined $user->{last_forum_visit}){ - $irc->yield(privmsg => $disp->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum"); + $irc->yield(privmsg => $self->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum"); }else { my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid}); if ($unread && $unread->{new}){ @@ -170,21 +170,21 @@ sub refresh { my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats}); if ($tick != $row[0]){ $tick = $row[0]; - $irc->yield(privmsg => $disp->targets->{def}, "New tick: $tick"); + $self->toTarget(def => "New tick: $tick"); } if (defined $row[1] && $stattick != $row[1]){ $stattick = $row[1]; - $irc->yield(privmsg => $disp->targets->{members}, "New tick: $stattick"); + $self->toTarget(members => "New tick: $stattick"); } my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent}); my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?}); $ircreqs->execute; while (my $req = $ircreqs->fetchrow_hashref){ if ($req->{channel} eq 'def'){ - $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}"); - $self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh); - }elsif(exists $disp->targets->{$req->{channel}}){ - $irc->yield(privmsg => $disp->targets->{$req->{channel}}, "<$req->{username} via webbie> $req->{message}"); + $self->toTarget(def => chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}"); + #$self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh); + }else{ + $self->toTarget($req->{channel} => "<$req->{username} via webbie> $req->{message}"); } $upircreq->execute($req->{id}); } @@ -204,7 +204,7 @@ GROUP BY username,call,tick,dm.fleet,p.value my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?}); $defmissions->execute(); while (my $mission = $defmissions->fetchrow_hashref){ - $irc->yield(privmsg => $disp->targets->{def}, chr(3)."06 $mission->{username} sent def to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}"); + $self->toTarget(def => chr(3)."06 $mission->{username} sent def to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}"); $updefmis->execute($mission->{fleet}); } diff --git a/Dispatcher.pm b/Dispatcher.pm index 6705349..76beef5 100644 --- a/Dispatcher.pm +++ b/Dispatcher.pm @@ -46,11 +46,6 @@ has channels => ( default => sub{ {} }, ); -has targets => ( - is => 'ro', - isa => 'HashRef[Str]', - default => sub{ {} }, -); my $DISP; @@ -107,11 +102,6 @@ sub has_command { return $self->channels->{$channel}->has($command->type); } -sub set_target { - my ($self,$label,$target) = @_; - $self->targets->{$label} = $target; -} - sub run_command { my ($self,$c,$command,$args) = @_; -- 2.39.2