From 0d63bf9da52cdfe2c9d8850b690032ff539b6398 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Thu, 20 Aug 2009 00:28:42 +0200 Subject: [PATCH] Removed the global variables for special chans, using targets hashref in Dispatcher instead --- Commands/Def.pm | 6 +++--- Commands/Scans.pm | 2 +- CommonStates.pm | 4 ++++ Dispatcher.pm | 15 +++++++++++++-- Eos.pm | 31 ++++++++++++++++--------------- ndawn.pl | 6 ++---- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/Commands/Def.pm b/Commands/Def.pm index b115dd4..d7862a3 100644 --- a/Commands/Def.pm +++ b/Commands/Def.pm @@ -75,9 +75,9 @@ ORDER BY c.landing_tick; return; } } - $c->message(notice => $ND::memchan, "DEFENSE REQUIRED!! WAKE UP!!"); - $c->message(privmsg => $ND::memchan, "DEFENSE REQUIRED $mess $callinfo MSG " - .$c->nick." TO RESPOND"); + $c->message(notice => $c->disp->targets->{members}, "DEFENSE REQUIRED!! WAKE UP!!"); + $c->message(privmsg => $c->disp->targets->{members}, "DEFENSE REQUIRED " + ."$mess $callinfo MSG ".$c->nick." TO RESPOND"); } sub settype diff --git a/Commands/Scans.pm b/Commands/Scans.pm index acfe9a9..3159031 100644 --- a/Commands/Scans.pm +++ b/Commands/Scans.pm @@ -93,7 +93,7 @@ VALUES((SELECT uid FROM users WHERE hostmask ILIKE $1),$2,$3,$4) RETURNING (id) } if ($id){ - $c->message(privmsg => $ND::scanchan + $c->message(privmsg =>$c->disp->targets->{scans} ,"$id http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z" . " ($x:$y:$z $type) | <".$c->nick."> $msg" ); diff --git a/CommonStates.pm b/CommonStates.pm index 5c0ab70..e46481c 100644 --- a/CommonStates.pm +++ b/CommonStates.pm @@ -121,6 +121,10 @@ sub sig_usr2 { while (<$channels>){ my ($chan, @types) = split /\s+/; say "$chan - @types"; + if ($chan =~ /^(.*):(.*)$/){ + $chan = $1; + $disp->set_target($2,$chan); + } $disp->add_channel($chan,\@types); } diff --git a/Dispatcher.pm b/Dispatcher.pm index a96c32e..7c4b0a6 100644 --- a/Dispatcher.pm +++ b/Dispatcher.pm @@ -30,17 +30,23 @@ use NDIRC::Command; has commands => ( - is => 'rw', + is => 'ro', isa => 'HashRef[Object]', default => sub{ {} }, ); has channels => ( - is => 'rw', + is => 'ro', isa => 'HashRef[ArrayRef[Str]]', default => sub{ {} }, ); +has targets => ( + is => 'ro', + isa => 'HashRef[Str]', + default => sub{ {} }, +); + sub load { my $self = shift; @@ -122,6 +128,11 @@ sub has_command { return 0; } +sub set_target { + my ($self,$label,$target) = @_; + $self->targets->{$label} = $target; +} + sub run_command { my ($self,$c,$command,$args) = @_; diff --git a/Eos.pm b/Eos.pm index 325f695..079b7c9 100644 --- a/Eos.pm +++ b/Eos.pm @@ -58,8 +58,9 @@ sub irc_msg { if (parseCommand($msg,$irc,$nick,$address,'pm',$heap->{disp},DB())){ #Command parsed and run successfully }else{ - $irc->yield(privmsg => $ND::defchan, chr(3)."04 $nick >> $msg"); - parseCommand("~report_incs $msg",$irc,$nick,' BATCH ',$ND::defchan,$heap->{disp},DB()); + my $disp = $heap->{disp}; + $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 $nick >> $msg"); + parseCommand("~report_incs $msg",$irc,$nick,' BATCH ',$disp->targets->{def},$heap->{disp},DB()); } } @@ -67,10 +68,11 @@ sub irc_join { my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1]; my ($nick,$username,$address) = ( split /[!@]/, $who ); my $irc = $sender->get_heap(); + my $disp = $heap->{disp}; my $dbh = DB(); - if ($channel =~ /^$ND::memchan$/i){ + if (lc $channel ~~ lc $disp->targets->{members}){ if (time - $last_announcement < 1){ $last_announcement = time; return; @@ -82,14 +84,14 @@ FROM users u WHERE hostmask = ? }, undef, $address); if ($user){ if ($user->{password} eq ''){ - $irc->yield(privmsg => $ND::memchan, "$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 => $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"); } if ($tick > 36 && not defined $user->{pid}){ - $irc->yield(privmsg => $ND::memchan, "$nick: go to https://nd.ruin.nu/ and enter your coords."); + $irc->yield(privmsg => $disp->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords."); } if (not defined $user->{last_forum_visit}){ - $irc->yield(privmsg => $ND::memchan, "$nick: Go read the forum! https://nd.ruin.nu/forum"); + $irc->yield(privmsg => $disp->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}){ @@ -111,28 +113,27 @@ sub refresh { print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n"; my $irc = $heap->{irc}; + my $disp = $heap->{disp}; my $dbh = DB(); my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats}); if ($tick != $row[0]){ $tick = $row[0]; - $irc->yield(privmsg => $ND::defchan, "New tick: $tick"); + $irc->yield(privmsg => $disp->targets->{def}, "New tick: $tick"); } if (defined $row[1] && $stattick != $row[1]){ $stattick = $row[1]; - $irc->yield(privmsg => $ND::memchan, "New tick: $stattick"); + $irc->yield(privmsg => $disp->targets->{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 => $ND::defchan, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}"); - parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$ND::defchan,$heap->{disp},$dbh); - }elsif($req->{channel} eq 'scan'){ - $irc->yield(privmsg => $ND::scanchan, "<$req->{username} via webbie> $req->{message}"); - }elsif($req->{channel} eq 'members'){ - $irc->yield(privmsg => $ND::memchan, "<$req->{username} via webbie> $req->{message}"); + $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}"); + parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$heap->{disp},$dbh); + }elsif(exists $disp->targets->{$req->{channel}}){ + $irc->yield(privmsg => $disp->targets->{$req->{channel}}, "<$req->{username} via webbie> $req->{message}"); } $upircreq->execute($req->{id}); } @@ -152,7 +153,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 => $ND::defchan, 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}"); + $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}"); $updefmis->execute($mission->{fleet}); } } diff --git a/ndawn.pl b/ndawn.pl index e89dc3a..ca60ea2 100755 --- a/ndawn.pl +++ b/ndawn.pl @@ -28,6 +28,8 @@ use POE qw(Component::IRC::Qnet::State); use NDIRC::CommonStates; eval "require NDIRC::$ARGV[0]"; +die $@ if $@; + # We create a new PoCo-IRC object my $irc = POE::Component::IRC::Qnet::State->spawn( nick => $ARGV[0], @@ -36,10 +38,6 @@ my $irc = POE::Component::IRC::Qnet::State->spawn( $irc->service_bots(QBOT => 'P@cservice.netgamers.org'); -$ND::scanchan = '#testarmer'; -$ND::defchan = '#testarlite'; -$ND::memchan = '#testarmer'; - POE::Session->create( package_states => [ 'NDIRC::CommonStates' => [ qw(_default _start irc_001 sig_DIE sig_usr1 sig_usr2 -- 2.39.2