From: Michael Andreen Date: Thu, 4 Jan 2007 21:04:24 +0000 (+0000) Subject: modules X-Git-Url: https://ruin.nu/git/?p=NDIRC.git;a=commitdiff_plain;h=9e84f393649d1ecbc24a94476352a28f49afa458 modules --- 9e84f393649d1ecbc24a94476352a28f49afa458 diff --git a/Access.pm b/Access.pm new file mode 100644 index 0000000..91b8002 --- /dev/null +++ b/Access.pm @@ -0,0 +1,65 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Access; +use strict; +use warnings; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/officer dc bc hc scanner intel masterop masterinvite/; + +sub officer { + return groupmember("HO"); +}; +sub dc { + return groupmember("HD"); +}; +sub bc { + return groupmember("HB"); +}; +sub hc { + return groupmember("H"); +}; +sub scanner { + return groupmember("HS"); +}; +sub intel { + return groupmember("HI"); +}; + +sub masterop { + return groupmember("HO"); +}; +sub masterinvite { + return groupmember("H"); +}; + +sub groupmember { + my ($groups) = @_; + $groups = join ",", map {"'$_'"} split //, $groups; + my $f = $ND::DBH->prepare("SELECT username FROM users NATURAL JOIN groupmembers NATURAL JOIN groups WHERE flag IN ('T',$groups) AND hostmask ILIKE ?"); + $f->execute($ND::address); + if ($f->fetchrow()){ + return 1; + } + return 0; +}; + +1; diff --git a/Channel.pm b/Channel.pm new file mode 100644 index 0000000..4f18a16 --- /dev/null +++ b/Channel.pm @@ -0,0 +1,74 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Channel; +use strict; +use warnings; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/op deop voice devoice/; + +sub op { + my ($nick) = @_; + umode("op","op",$nick); +} + +sub deop { + my ($nick) = @_; + umode("deop","op",$nick); +} + +sub voice { + my ($nick) = @_; + umode("voice","voice",$nick); +} + +sub devoice { + my ($nick) = @_; + umode("devoice","voice",$nick); +} + +sub umode { + my ($command,$access,$nick) = @_; + my $where = ""; + unless (defined $nick){ + $nick = $ND::nick; + $where = "OR f.name = 'auto_$access'"; + } + + my $mode = qq{ +SELECT DISTINCT c.name FROM users u + JOIN groupmembers g ON g.uid = u.uid + JOIN channel_group_flags gf ON g.gid = gf.group + JOIN channels c ON gf.channel = c.id + JOIN channel_flags f ON f.id = gf.flag +WHERE u.hostmask ILIKE ? AND c.name = ? AND (f.name = '$access' $where); + }; + if (masterop()){ + $mode = 1; + }else{ + ($mode) = $ND::DBH->selectrow_array($mode,undef,$ND::address,$ND::target); + } + if ($mode){ + $ND::server->command("$command $ND::target $nick"); + } +} + +1; diff --git a/Def.pm b/Def.pm new file mode 100644 index 0000000..8f0fca9 --- /dev/null +++ b/Def.pm @@ -0,0 +1,176 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Def; +use strict; +use warnings; +use ND::DB; +use ND::IRC::Access; +use ND::IRC::Misc; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/showCall setType takeCall covCall ignoreCall defcall anon setDefPrio/; + +sub showCall { + my ($id) = @_; + DB(); + if (dc()){ + my $f = $ND::DBH->prepare(<execute($id); + while (my @row = $f->fetchrow()){ + @row = map (valuecolor(0),@row); + $ND::server->command("msg $ND::target (CALL $id) $row[0]: $row[1], $row[3] ($row[2]), $row[4] ($row[10]), $row[5], ETA: $row[11](/$row[6]), Amount: $row[7], $row[8], Type: $row[9]"); + } + } +} + +sub setType { + my ($type,$id,$x,$y,$z) = @_; + DB(); + if (dc()){ + my $fleet; + my $query = qq{ + SELECT i.id,call,shiptype, coords(x,y,z),c.landing_tick - tick() FROM incomings i + JOIN current_planet_stats p ON i.sender = p.id + JOIN calls c ON i.call = c.id + }; + if (defined $x && $x eq 'call'){ + $fleet = $ND::DBH->prepare(qq{ + $query + WHERE i.call = ? + }); + $fleet->execute($id); + }elsif (defined $x){ + $fleet = $ND::DBH->prepare(qq{ + $query + WHERE i.call = ? AND p.id = planetid(?,?,?,0) + }); + $fleet->execute($id,$x,$y,$z); + }else{ + $fleet = $ND::DBH->prepare(qq{ + $query + WHERE i.id = ? + }); + $fleet->execute($id); + } + while (my ($id,$call,$oldtype,$coords,$tick) = $fleet->fetchrow()){ + if($ND::DBH->do(q{UPDATE incomings SET shiptype = ? WHERE id = ?},undef,$type,$id) == 1){ + $ND::DBH->do(q{INSERT INTO log (uid,text) VALUES ((SELECT uid FROM + users WHERE hostmask ILIKE ?),?)},undef,$ND::address,"DC set fleet: $id to: $type"); + $ND::server->command("msg $ND::target Set fleet from $coords on call $call to $type (previosly $oldtype)"); + if ($tick < 0 && not (defined $x && $x eq 'call')){ + $ND::server->command("msg $ND::target This call is old, did you use the call id, instead of inc id by accident? You can use .settypeall callid to set the type on all incs in a call."); + } + } + } + } +} +sub takeCall { + my ($id) = @_; + DB(); + if (dc()){ + if ($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?) WHERE id = ?} + ,undef,$ND::address,$id) == 1){ + $ND::server->command("msg $ND::target Updated the DC for call $id"); + } + } +} + +sub covCall { + my ($id) = @_; + DB(); + if (dc()){ + if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = TRUE, open = FALSE WHERE id = ?} + ,undef,$ND::address,$id) == 1){ + $ND::server->command("msg $ND::target Marked call $id as covered"); + } + } +} + +sub ignoreCall { + my ($id) = @_; + DB(); + if (dc()){ + if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = FALSE, open = FALSE WHERE id = ?} + ,undef,$ND::address,$id) == 1){ + $ND::server->command("msg $ND::target Marked call $id as ignored"); + } + } +} + +sub defcall { + my ($msg,$nick,$callnr) = @_; + DB(); + if (dc()){ + my $call = ""; + if ($callnr){ + my $st = $ND::DBH->prepare(q{ + SELECT c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK'), concat(i.shiptype||'/') AS shiptype, dc.username + FROM calls c + JOIN incomings i ON i.call = c.id + LEFT OUTER JOIN users dc ON dc.uid = c.dc + WHERE not covered AND c.id = ? + GROUP BY c.id,c.landing_tick,dc.username + ORDER BY c.landing_tick; + }); + if (my @row = $ND::DBH->selectrow_array($st,undef,$callnr)){ + chop($row[1]); + $call = "(Anti $row[1] ETA: $row[0])" + } + } + $ND::server->command("notice $ND::memchan DEFENSE REQUIRED!! WAKE UP!!"); + $ND::server->command("msg $ND::memchan DEFENSE REQUIRED $msg $call MSG $nick TO RESPOND"); + } +} + +sub anon { + my ($target,$msg) = @_; + if (dc()){ + $ND::server->command("msg $target ".chr(2).$msg); + $ND::server->command("msg $ND::target ".chr(3)."3$1 << $2"); + } +} + + +sub setDefPrio { + my ($min,$max) = @_; + DB(); + if (hc()){ + $ND::DBH->begin_work; + my $update = $ND::DBH->prepare('UPDATE misc SET value = ? :: int WHERE id = ?'); + $update->execute($min,'DEFMIN'); + $update->execute($max,'DEFMAX'); + if ($ND::DBH->commit){ + $ND::server->command("msg $ND::target min def prio set to $min and max set to $max"); + }else{ + $ND::server->command("msg $ND::target something went wrong"); + } + } +} + +1; diff --git a/Intel.pm b/Intel.pm new file mode 100644 index 0000000..6012491 --- /dev/null +++ b/Intel.pm @@ -0,0 +1,109 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Intel; +use strict; +use warnings; +use ND::DB; +use ND::IRC::Access; +use ND::IRC::Misc; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/checkIntel setHostile findNick setNick setAlly setChannel/; + +sub checkIntel { + my ($x,$y,$z) = @_; + DB(); + if (officer() || dc()){ + my $f = $ND::DBH->prepare("SELECT nick,alliance,coords(x,y,z),ruler,planet,hit_us,race,score,size,value,planet_status,relationship FROM current_planet_stats WHERE x = ? AND y = ? and z = ?"); + $f->execute($x,$y,$z); + while (my @row = $f->fetchrow()){ + @row = map (valuecolor(1),@row); + $ND::server->command("notice $ND::target $row[2] - $row[3] OF $row[4], Alliance=$row[1] ($row[11]), Nick=$row[0] ($row[10]), Hostile Count=$row[5], Race=$row[6], Score=$row[7], Size=$row[8], Value=$row[9] "); + } + }else{ + $ND::server->command("msg $ND::target Only officers are allowed to check that"); + } +} + +sub setHostile { + my ($x,$y,$z) = @_; + DB(); + if(dc()){ + my $rv = $ND::DBH->do("UPDATE planets SET planet_status = 'Hostile' WHERE id = (SELECT id FROM current_planet_stats WHERE x = ? AND y = ? and z = ?)",undef,$x,$y,$z); + if ($rv == 1){ + $ND::server->command("msg $ND::target $x:$y:$z is now marked s hostile"); + } + } +} + +sub findNick { + my ($nick) = @_; + DB(); + if(officer()){ + my $f = $ND::DBH->prepare("SELECT coords(x,y,z), ruler,planet,nick FROM current_planet_stats WHERE nick ILIKE ? ORDER BY x,y,z"); + $f->execute($nick); + $ND::server->command("notice $ND::target No such nick") if $f->rows == 0; + while (my @row = $f->fetchrow()){ + $ND::server->command("notice $ND::target $row[0] $row[1] OF $row[2] is $row[3]"); + } + } +} +sub setNick { + my ($x,$y,$z,$nick) = @_; + DB(); + if (officer()){ + if ($ND::DBH->do("UPDATE planets SET nick = ? WHERE id = planetid(?,?,?,0)" + ,undef,$nick,$x,$y,$z)){ + $ND::server->command("msg $ND::target $x:$y:$z has been updated"); + } + } +} + +sub setAlly { + my ($x,$y,$z,$ally) = @_; + DB(); + if (officer()){ + my $aid; + if ($ally ne 'unknown'){ + ($aid,$ally) = $ND::DBH->selectrow_array("SELECT id,name FROM alliances WHERE name ILIKE ?",undef,$ally); + } + if ($ally){ + $ND::DBH->do("UPDATE planets SET alliance_id = ? WHERE id = planetid(?,?,?,0)" + ,undef,$aid,$x,$y,$z); + $ND::server->command("msg $ND::target Setting $x:$y:$z as $ally"); + }else{ + $ND::server->command("msg $ND::target Couldn't find such an alliance"); + } + } +} + +sub setChannel { + my ($x,$y,$z,$channel) = @_; + DB(); + if (officer()){ + if ($ND::DBH->do("UPDATE planets SET channel = ? WHERE id = planetid(?,?,?,0)" + ,undef,$channel,$x,$y,$z)){ + $ND::server->command("msg $ND::target $x:$y:$z relay channel has been set to: $channel"); + } + } +} + +1; diff --git a/Members.pm b/Members.pm new file mode 100644 index 0000000..49485f9 --- /dev/null +++ b/Members.pm @@ -0,0 +1,116 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Members; +use strict; +use warnings; +use ND::IRC::Access; +use ND::DB; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/currentCalls showraids checkPoints findSMS/; + +sub currentCalls { + my ($verbose) = @_; + DB(); + if (1){ #TODO: add check for member + my $f = $ND::DBH->prepare(<= 7 + GROUP BY c.id,c.landing_tick,dc.username + ORDER BY c.landing_tick; +SQL +); + $f->execute(); + my $calls = ""; + while (my @row = $f->fetchrow()){ + chop($row[1]); + my $dc = defined $row[2] ? $row[2] : ''; + $calls .= " (Anti $row[1] ETA: $row[0] DC: $dc) |" + } + chop($calls); + if (defined $verbose || length $calls > 0){ + $ND::server->command("msg $ND::target Current calls: $calls"); + } + } +} + +sub showraids { + DB(); + if (1){ #TODO: add check for member + my $f = $ND::DBH->prepare(< tick() + AND id IN (SELECT raid FROM raid_access WHERE gid = 2) +SQL +); + $f->execute(); + my $calls = ""; + while (my ($raid) = $f->fetchrow()){ + $calls .= " https://nd.ruin.nu/raids?raid=$raid |" + } + $calls = "No open future raids" if ($f->rows == 0); + chop($calls); + $ND::server->command("msg $ND::target $calls"); + } +} + +sub checkPoints { + my ($nick) = @_; + DB(); + my $f; + if ($nick){ + if (officer() || dc() || bc()){ + $f = $ND::DBH->prepare("SELECT username, attack_points, defense_points, scan_points, humor_points FROM users WHERE username ILIKE ?"); + }else{ + $ND::server->command("msg $ND::target Only officers are allowed to check for others"); + } + } else{ + $f = $ND::DBH->prepare("SELECT username, attack_points, defense_points, scan_points, humor_points FROM users WHERE hostmask ILIKE ?"); + $nick = $ND::address; + } + if ($f){ + $f->execute($nick); + while (my @row = $f->fetchrow()){ + $ND::server->command("msg $ND::target $row[0] has $row[1] Attack, $row[2] Defense, $row[3] Scan, $row[4] Humor points"); + } + } +} + +sub findSMS { + my ($nick) = @_; + DB(); + my $f; + if (officer() || dc()){ + $f = $ND::DBH->prepare("SELECT username,COALESCE(sms,'nothing added') FROM users WHERE username ILIKE ?"); + if (my ($username,$sms) = $ND::DBH->selectrow_array($f,undef,$nick)){ + $ND::server->command("notice $ND::target $ND::B$username$ND::B has sms $ND::B$sms$ND::B"); + }else{ + $ND::server->command("notice $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + } + }else{ + $ND::server->command("notice $ND::target Only dcs and above are allowed to check for others"); + } +} + +1; diff --git a/Misc.pm b/Misc.pm new file mode 100644 index 0000000..2b479dd --- /dev/null +++ b/Misc.pm @@ -0,0 +1,49 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Misc; +use strict; +use warnings; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/valuecolor/; + +$ND::defchan = "#def-ndawn"; +$ND::memchan = "#nd"; +$ND::scanchan = "#ndef"; +$ND::bcchan = "#nd-day"; +$ND::intelchan = "#ndintel"; +$ND::officerchan = "#nd-officers"; +$ND::communitychan = "#ndawn"; +$ND::pubchan = "#newdawn"; +$ND::xanchan = "#ViolatorS"; + +sub valuecolor { + my $s = $_; + $s = $_[1] if defined $_[1]; + $s = "" unless defined $s; + return chr(3)."5$s".chr(3) if $s eq 'Hostile'; + return chr(3)."3$s".chr(3) if $s eq 'Friendly'; + return chr(3)."3$s".chr(3) if $s eq 'Nap' or $s eq 'NAP'; + return chr(2)."$s".chr(2) if $_[0]; + return $s; +} + +1; diff --git a/PA.pm b/PA.pm new file mode 100644 index 0000000..fbe2a84 --- /dev/null +++ b/PA.pm @@ -0,0 +1,198 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::PA; +use strict; +use warnings; +use ND::DB; +use ND::IRC::Access; +use ND::IRC::Misc; +use POSIX; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/checkPlanet checkGal shipEff shipStop parseValue prettyValue/; + +sub checkPlanet { + my ($x,$y,$z,$intel) = @_; + DB(); + my $f = $ND::DBH->prepare("SELECT ruler,planet,race,score,size,value,scorerank,sizerank,valuerank, xp, xprank, alliance FROM current_planet_stats WHERE x = ? AND y = ? and z = ?"); + $f->execute($x,$y,$z); + while (my @row = $f->fetchrow()){ + @row = map (valuecolor(1),@row); + my $ally = ""; + $ally = " Alliance=$row[11]," if $intel; + $ND::server->command("notice $ND::target $x:$y:$z $row[0] OF $row[1],$ally Race=$row[2], Score=$row[3] ($row[6]), Size=$row[4] ($row[7]), Value=$row[5] ($row[8]), XP=$row[9] ($row[10])"); + } +} +sub checkGal { + my ($x,$y) = @_; + DB(); + my $f = $ND::DBH->prepare("SELECT name,score,size,value FROM galaxies WHERE x = ? AND y = ? and tick = (SELECT max(tick) from galaxies)"); + $f->execute($x,$y); + while (my @row = $f->fetchrow()){ + @row = map (valuecolor(1),@row); + $ND::server->command("notice $ND::target $x:$y $row[0], Score=$row[1], Size=$row[2], Value=$row[3]"); + } +} + +sub shipEff { + my ($amount,$ship,$value) = @_; + $ship = "\%$ship\%"; + $amount = parseValue($amount); + $value = parseValue($value); + $value *= -1.5 if defined $value and $value < 0; + + my @ship = $ND::DBH->selectrow_array(q{ +SELECT name,target,"type",damage,metal+crystal+eonium,init,"class",guns,race +FROM ship_stats WHERE name ILIKE ? + }, undef, $ship); + if (@ship){ + my $type = "kill"; + $type = "stun" if $ship[2] eq 'Emp'; + $type = "steal" if ($ship[2] eq 'Steal') or ($ship[2] eq 'Pod'); + + $amount = int(($value*100/$ship[4])) if $amount eq 'value'; + $value = prettyValue(($amount*$ship[4]/100)); + my $text = "$amount $ship[0] ($ship[5]:$value) will $type:"; + my $st = $ND::DBH->prepare(q{ + SELECT name,"class","type",armor,metal+crystal+eonium,init,target,eres,race + FROM ship_stats WHERE "class" = ? + }); + $st->execute($ship[1]); + while (my @target = $st->fetchrow()){ + my $dead = $ship[2] eq 'Emp' ? int($amount*$ship[7]*(100-$target[7])/100) : int($amount*$ship[3]/$target[3]); + $value = prettyValue($dead*$target[4]/100); + if (($target[6] eq $ship[6]) and ($target[5] <= $ship[5])){ + $target[5] = "${ND::C}04$target[5]$ND::C"; + }elsif(($target[6] eq $ship[6]) and ($target[5] > $ship[5])){ + $target[5] = "${ND::C}12$target[5]$ND::C"; + } + $target[0] = "${ND::C}04$target[0]$ND::C" if $target[2] eq 'Norm' || $target[2] eq 'Cloak'; + $target[0] = "${ND::C}12$target[0]$ND::C" if $target[2] eq 'Emp'; + $target[0] = "${ND::C}13$target[0]$ND::C" if $target[2] eq 'Steal'; + $text .= " $ND::B$dead$ND::B $target[0] ($target[5]:$value),"; + } + chop $text; + $ND::server->command("notice $ND::target $text"); + } + #print $text; +} + +sub shipStop { + my ($amount,$ship,$value) = @_; + $ship = "\%$ship\%"; + $amount = parseValue($amount); + $value = parseValue($value); + $value *= -1.5 if defined $value and $value < 0; + + my @ship = $ND::DBH->selectrow_array(q{ +SELECT name,target,"type",armor,metal+crystal+eonium,init,"class",eres,race +FROM ship_stats WHERE name ILIKE ? + }, undef, $ship); + if (@ship){ + $ship[0] = "${ND::C}04$ship[0]$ND::C" if $ship[2] eq 'Norm'; + $ship[0] = "${ND::C}12$ship[0]$ND::C" if $ship[2] eq 'Emp'; + $ship[0] = "${ND::C}13$ship[0]$ND::C" if $ship[2] eq 'Steal'; + + $amount = int(($value*100/$ship[4])) if $amount eq 'value'; + $value = prettyValue(($amount*$ship[4]/100)); + my $text = "To stop $amount $ship[0] ($ship[5]:$value) you need:"; + my $st = $ND::DBH->prepare(q{ + SELECT name,"class","type",damage,metal+crystal+eonium,init,target,guns,race + FROM ship_stats WHERE "target" = ? + }); + $st->execute($ship[6]); + while (my @stopper = $st->fetchrow()){ + my $needed = $stopper[2] eq 'Emp' ? ceil($amount*100/(100-$ship[7])/$stopper[7]) : ceil($amount*$ship[3]/$stopper[3]); + $value = prettyValue($needed*$stopper[4]/100); + if (($stopper[1] eq $ship[1]) and ($ship[5] <= $stopper[5])){ + $stopper[5] = "${ND::C}04$stopper[5]$ND::C"; + }elsif(($stopper[1] eq $ship[1]) and ($ship[5] > $stopper[5])){ + $stopper[5] = "${ND::C}12$stopper[5]$ND::C"; + } + $stopper[0] = "${ND::C}04$stopper[0]$ND::C" if $stopper[2] eq 'Norm' || $stopper[2] eq 'Cloak'; + $stopper[0] = "${ND::C}12$stopper[0]$ND::C" if $stopper[2] eq 'Emp'; + $stopper[0] = "${ND::C}13$stopper[0]$ND::C" if $stopper[2] eq 'Steal'; + $text .= " $ND::B$needed$ND::B $stopper[0] ($stopper[5]:$value),"; + } + chop $text; + $ND::server->command("notice $ND::target $text"); + } + #print $text; +} + +sub parseValue { + if (defined $_[0] && $_[0] =~ /^(-?\d+(?:\.\d+)?)([khMG])?$/){ + return $1 unless defined $2; + return $1*100 if $2 eq 'h'; + return $1*1000 if $2 eq 'k'; + return $1*1000000 if $2 eq 'M'; + return $1*1000000000 if $2 eq 'G'; + } + return $_[0]; +} + +sub prettyValue { + my ($value) = @_; + my $unit = ''; + my @units = ('k','M','G','T'); + for (my $i = 0; $value >= 1000;$i++){ + $value /= 1000; + $unit = $units[$i]; + } + return sprintf('%.2f%s', $value,$unit); +} + + +sub min { + my ($x,$y) = @_; + return ($x > $y ? $y : $x); +} + +sub max { + my ($x,$y) = @_; + return ($x < $y ? $y : $x); +} + +sub calcXp { + my ($x,$y,$z,$roids) = @_; + + my ($avalue,$ascore) = $ND::DBH->selectrow_array(q{ + SELECT value,score FROM current_planet_stats WHERE + id = (SELECT planet FROM users WHERE hostmask ILIKE ?); + }, undef, $ND::address); + my ($tvalue,$tscore) = $ND::DBH->selectrow_array(q{ + SELECT value,score FROM current_planet_stats WHERE + x = ? AND y = ? and z = ?; + }, undef, $x,$y,$z); + unless (defined $avalue && defined $ascore){ + $ND::server->command("notice $ND::target You don't have a planet specified"); + return; + } + unless (defined $tvalue && defined $tscore){ + $ND::server->command("notice $ND::target Doesn't seem to be a planet at $x:$y:$z"); + return; + } + my $xp = int(max($roids * 10 * (min(2,$tscore/$ascore) + min(2,$tvalue/$avalue) - 1),0)); + my $score = 60 * $xp; + $ND::server->command("notice $ND::target You will gain $ND::B$xp$ND::B XP, $ND::B$score$ND::B score, if you steal $roids roids from $x:$y:$z"); +} + +1; diff --git a/Quotes.pm b/Quotes.pm new file mode 100644 index 0000000..cf2750b --- /dev/null +++ b/Quotes.pm @@ -0,0 +1,113 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Quotes; +use strict; +use warnings; +use ND::IRC::Access; +use Tie::File; +use File::Temp (); +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/quote addQuote lastQuote findQuote delQuote/; + +tie our @FILE, 'Tie::File', "/home/ndawn/.eos/scripts/quote.txt"; +tie our @OLDFILE, 'Tie::File',"/home/ndawn/.eos/scripts/oldquotes.txt" or die "test"; + +sub quote { + my ($n) = @_; + $n = $n-1 if defined $n; + $n = int(rand($#FILE)) unless defined $n; + my $text = $FILE[$n]; + $text =~ s/(.*?)[\r\n]*$/$1/; + $n++; + my $num = $#FILE+1; + $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$num:$ND::B $text"); +} + +sub addQuote { + my ($quote) = @_; + push @FILE, $quote; + my $num = $#FILE+1; + $ND::server->command("msg $ND::target Quote $ND::B$num$ND::B added"); +} +sub lastQuote { + my $n = $#FILE; + my $text = $FILE[$n]; + $text =~ s/(.*?)[\r\n]*$/$1/; + $n++; + $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$n:$ND::B $text"); +} +sub findQuote { + my ($type,$pattern) = @_; + my $matcher; + if ($type eq 'qre'){ + if (defined (eval 'm/$pattern/ix')){ + $matcher = 'm/$pattern/ix'; + }else { + $ND::server->command("msg $ND::target bad regexp"); + close FILE; + return; + } + }else{ + $matcher = '(index uc($_), uc($pattern)) != -1'; + } + #mkdir "/tmp/quotes"; + #my $file = "/tmp/quotes/$ND::address.txt"; + #open(FILE,'>',"$file"); + my $file = new File::Temp( SUFFIX => '.txt' ); + my $n = 1; + my $match = 0; + for $_ (@FILE){ + chomp; + if (eval $matcher){ + $match = 1; + print $file "$n: $_\n"; + } + $n++; + } + if ($match){ + $ND::server->command("dcc send $ND::nick $file"); + }else{ + $ND::server->command("msg $ND::target $ND::nick: No quotes matching that."); + } +} +sub delQuote { + my ($n) = @_; + if (hc){ + $n = $n-1; + if (exists $FILE[$n]){ + my ($uid,$username) = $ND::DBH->selectrow_array(q{SELECT uid,username FROM users where hostmask ILIKE ?} + ,undef,$ND::address); + my $text = $FILE[$n]; + push @OLDFILE,"Removed by $username ($uid): $text"; + splice @FILE,$n,1; + $n++; + my $num = $#FILE+1; + $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B {$text} removed, number of quotes now: $ND::B$num$ND::B"); + }else{ + $ND::server->command("msg $ND::target No such quote."); + } + }else{ + $ND::server->command("msg $ND::target You don't have access to that!"); + } +} + +1; diff --git a/Scans.pm b/Scans.pm new file mode 100644 index 0000000..f4e0e5b --- /dev/null +++ b/Scans.pm @@ -0,0 +1,61 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Scans; +use strict; +use warnings; +use ND::DB; +use ND::IRC::Access; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/addScan sendScan/; + +sub addScan { + my ($id,$verbose) = @_; + DB(); + if (1){ + unless ($ND::DBH->selectrow_array("SELECT scan_id FROM scans WHERE scan_id = ? AND tick >= tick() - 48",undef,$id)){ + my @user = $ND::DBH->selectrow_array(q{SELECT uid,username, scan_points, tick() + FROM users WHERE hostmask ILIKE ? },undef,$ND::address); + if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,"type") VALUES (?,tick(),COALESCE(?,'-1'))}, + undef,$id,$user[0]) == 1){ + if (@user){ + $ND::DBH->do('UPDATE users SET scan_points = scan_points + 1 WHERE uid = ? ',undef,$user[0]); + $user[2] += 1; + $ND::server->command("msg $ND::target Added scan, at tick $user[3]. $user[1] points now $user[2]"); + }elsif ($verbose){ + $ND::server->command("msg $ND::target Added scan, but unknown user, no points"); + } + } + }elsif ($verbose){ + $ND::server->command("msg $ND::target a scan with that id has already been added within the last 48 ticks"); + } + } +} +sub sendScan { + my ($target,$msg) = @_; + DB(); + if (scanner()){ + $ND::server->command("msg $target ".chr(2).$msg.chr(3)."4 (reply with /msg $ND::scanchan)"); + $ND::server->command("msg $ND::target ${ND::C}3$1 << $2"); + } +} + +1; diff --git a/Usermgm.pm b/Usermgm.pm new file mode 100644 index 0000000..99f3ec9 --- /dev/null +++ b/Usermgm.pm @@ -0,0 +1,255 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package ND::IRC::Usermgm; +use strict; +use warnings; +use ND::DB; +use ND::IRC::Access; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/addUser whois flags flag laston addPoints chattrG setHost deactivateUser/; + +sub addUser { + my ($nick,$host) = @_; + DB(); + if (hc()){ + unless (defined $host){ + my @nicks = $ND::server->nicks_get_same($nick); + if (@nicks){ + $nicks[1]->{host} =~ /.*@(.*)/; + $host = $1; + }else{ + $host = "$nick.users.netgamers.org"; + } + } + my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE username ILIKE ? OR hostmask ILIKE ?",undef,$nick,$host); + if ((not defined $username) && $ND::DBH->do("INSERT INTO users (username,hostmask,password) VALUES(?,?,'')" + ,undef,$nick,$host)){ + $ND::server->command("msg $ND::target Added $ND::B$nick$ND::B with host: $ND::B$host$ND::B"); + }elsif(defined $username){ + $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B."); + + }else{ + $ND::server->command("msg $ND::target Something went wrong when trying to add $ND::B$nick$ND::B with host: $ND::B$host$ND::B, most likely duplicate user name"); + } + }else{ + $ND::server->command("msg $ND::target Only HCs are allowed to check that"); + } +} +sub whois { + my ($nick) = @_; + DB(); + if (officer()){ + my $f = $ND::DBH->prepare("SELECT username, hostmask, concat(flag) FROM users u LEFT OUTER JOIN (SELECT uid,flag FROM groupmembers NATURAL JOIN groups ORDER BY uid,flag ) g ON g.uid = u.uid WHERE username ILIKE ? GROUP BY username,hostmask"); + $f->execute($nick); + while (my @row = $f->fetchrow()){ + $ND::server->command("msg $ND::target $row[0] flags: ($row[2]) host: $row[1]"); + } + if ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + } + }else{ + $ND::server->command("msg $ND::target Only officers are allowed to check that"); + } +} + +sub flags { + my ($nick) = @_; + DB(); + unless ($1){ + my ($flags) = $ND::DBH->selectrow_array("SELECT TRIM(', ' FROM concat(flag||':'||groupname||', ')) FROM groups"); + $ND::server->command("msg $ND::target $flags"); + }elsif (hc()){ + my $f = $ND::DBH->prepare("SELECT username, concat(flag), TRIM(', ' FROM concat(groupname||', ')) FROM users u LEFT OUTER JOIN (SELECT uid,flag,groupname FROM groupmembers NATURAL JOIN groups ORDER BY uid,flag ) g ON g.uid = u.uid WHERE username ILIKE ? GROUP BY username,hostmask"); + $f->execute($nick); + while (my @row = $f->fetchrow()){ + $ND::server->command("msg $ND::target Flags for $row[0] on: $ND::target: $row[1]| (Global: $row[2])"); + } + if ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + } + }else{ + $ND::server->command("msg $ND::target Only HCs are allowed to check that"); + } +} + +sub flag { + my ($flag) = @_; + + if (officer()|| ($ND::target eq $ND::scanchan && $flag eq 'S')){ + my $f = $ND::DBH->prepare(qq{ +SELECT TRIM(', ' FROM concat(username||', ')) FROM + (SELECT uid, username FROM users ORDER BY username) u NATURAL JOIN groupmembers gm + JOIN groups g ON g.gid = gm.gid +WHERE flag = ?; + }); + if (my ($users) = $ND::DBH->selectrow_array($f,undef,$flag)){ + $ND::server->command("msg $ND::target Users with flag $ND::B$flag$ND::B: $users"); + } + }else{ + $ND::server->command("msg $ND::target Only officers are allowed to check that"); + } +} + +sub laston { + my ($flag,$min) = @_; + + if (officer()){ + my $f = $ND::DBH->prepare(qq{SELECT username,last + FROM (SELECT uid,username, date_part('day',now() - laston)::int AS last FROM users) u NATURAL JOIN groupmembers NATURAL JOIN groups WHERE flag = ? AND (last >= ? OR last IS NULL) ORDER BY last DESC + }); + $min = 0 unless defined $min; + $f->execute($flag,$min); + my $text; + while (my $user = $f->fetchrow_hashref){ + $user->{last} = '?' unless defined $user->{last}; + $text .= "$user->{username}($user->{last}) "; + } + $ND::server->command("msg $ND::target Users(days) with flag $ND::B$flag$ND::B: $text"); + }else{ + $ND::server->command("msg $ND::target Only officers are allowed to check that"); + } +} + +sub addPoints { + my ($t,$nick,$p) = @_; + DB(); + if ( ($t eq "d" && dc()) + || ($t eq "a" && bc()) + || ($t eq "h" && officer()) + || ($t eq "s" && scanner())){ + my $points = 1; + if ($p){ + $points = $p; + } + if ($points*$points > 400){ + $ND::server->command("msg $ND::target Values between -20 and 20 please"); + return; + } + my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); + $f->execute($nick); + my @row = $f->fetchrow(); + if ($f->rows == 1){ + my $type = "defense"; + $type = "attack" if $t eq "a"; + $type = "humor" if $t eq "h"; + $type = "scan" if $t eq "s"; + my ($fleets) = $ND::DBH->selectrow_array('SELECT count(*) FROM raids r JOIN raid_targets rt ON r.id = rt.raid JOIN raid_claims rc ON rt.id = rc.target WHERE not launched AND uid = ? AND tick + 24 > tick();',undef,$row[0]); + if ($t eq 'a' && $fleets > 0 && $points > 0){ + $ND::server->command("msg $ND::target $row[1] has $fleets claimed waves the last 24 ticks that aren't marked as launched, so no points."); + return; + } + $type .= "_points"; + $ND::DBH->do("UPDATE users SET $type = $type + ? WHERE uid = ?",undef,$points,$row[0]); + $ND::server->command("msg $ND::target $row[1] has been given $points $type"); + }elsif ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + }else{ + $ND::server->command("msg $ND::target More than 1 user matched, please refine the search"); + } + $f->finish; + + }else{ + $ND::server->command("msg $ND::target You don't have access for that"); + } +} + +sub chattrG { + my ($nick, $flags) = @_; + DB(); + if (hc() || ($flags =~ /^(\+|-)?x$/ && $ND::address eq 'Assassin.users.netgamers.org')){ + my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); + $f->execute($nick); + my @user = $f->fetchrow(); + if ($f->rows == 1){ + my $add = 1; + $flags =~ /^(-)/; + my $update; + if ($1 eq "-"){ + $update = $ND::DBH->prepare("DELETE FROM groupmembers WHERE uid = ? AND gid = (SELECT gid FROM groups WHERE flag = ?)"); + }else{ + $update = $ND::DBH->prepare("INSERT INTO groupmembers (uid,gid) VALUES(?,(SELECT gid FROM groups WHERE flag = ?))"); + } + while ($flags =~ m/(\w)/g){ + $update->execute($user[0],$1); + } + $update = $ND::DBH->prepare("SELECT concat(flag) FROM (SELECT uid,flag FROM groupmembers NATURAL JOIN groups ORDER BY uid,flag ) g WHERE uid = ? "); + my @flags = $ND::DBH->selectrow_array($update,undef,$user[0]); + $ND::server->command("msg $ND::target Global flags for $user[1] are now: $flags[0]"); + }elsif ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + }else{ + $ND::server->command("msg $ND::target More than 1 user matched, please refine the search"); + } + $f->finish; + } +} + +sub setHost { + my ($nick, $host) = @_; + DB(); + if (hc()){ + my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); + $f->execute($nick); + my ($uid,$nick) = $f->fetchrow(); + if ($f->rows == 1){ + my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE hostmask ILIKE ? AND NOT (username ILIKE ?)",undef,$host,$nick); + if ((not defined $username) && $ND::DBH->do("UPDATE users SET hostmask = ? WHERE uid = ?",undef,$host,$uid) > 0){ + $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s host to: $ND::B$host$ND::B"); + }elsif(defined $username){ + $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B."); + }else{ + $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host"); + } + }elsif ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + }else{ + $ND::server->command("msg $ND::target More than 1 user matched, please refine the search"); + } + $f->finish; + } +} + +sub deactivateUser { + my ($nick) = @_; + DB(); + if (hc()){ + my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); + $f->execute($nick); + my ($uid,$username) = $f->fetchrow(); + if ($f->rows == 1){ + my $updated = $ND::DBH->do("UPDATE users SET hostmask = ?, password = '' WHERE uid = ?",undef,$username,$uid); + if ($updated > 0){ + my $groups = $ND::DBH->do("DELETE FROM groupmembers WHERE uid = ?",undef,$uid); + $ND::server->command("msg $ND::target $ND::B$username$ND::B has been deactivated."); + }else{ + $ND::server->command("msg $ND::target Something went wrong when trying to modify $ND::B$username$ND::B"); + } + }elsif ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + }else{ + $ND::server->command("msg $ND::target More than 1 user matched, please refine the search"); + } + $f->finish; + } +} + +1;