1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
19 package NDIRC::Usermgm;
26 our @ISA = qw/Exporter/;
28 our @EXPORT = qw/laston addPoints setHost setPNick getShips getFleet/;
31 my ($msg, $command) = @_;
33 if (officer() || ia()){
36 if(defined $msg && $msg =~ /^(\w)(?: (\d+)(?:\|(\d+)\|(\d+))?)?$/){
41 $min = 0 unless defined $min;
42 $f = $ND::DBH->prepare(q{SELECT username, last
43 ,COALESCE(lastforum::text,'?') AS lastforum
44 ,COALESCE(lastclaim::text,'?') AS lastclaim
46 ,date_part('day',now() - laston)::int AS last
47 ,date_part('day',now() - (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid)) AS lastforum
48 ,date_part('day',now() - (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid)) AS lastclaim
50 NATURAL JOIN groupmembers
54 WHERE COALESCE(last >= $2,TRUE) AND COALESCE(lastforum >= $3,TRUE) AND COALESCE(lastclaim >= $4,TRUE)
55 ORDER BY last DESC, lastforum DESC, lastclaim DESC
57 $f->execute($flag,$min,$forum,$claim);
58 }elsif(defined $msg && $msg =~ /^(\S+)$/){
59 $f = $ND::DBH->prepare(q{SELECT username
60 ,date_part('day',now() - laston)::int AS last
61 ,COALESCE(date_part('day',now() - (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid))::text,'?')
63 ,COALESCE(date_part('day',now() - (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid))::text,'?') AS lastclaim
65 WHERE username ILIKE $1 ORDER BY lower(username)
69 $ND::server->command("notice $ND::nick syntax: .$command <flag [days]|nick> | lists users and the number of days since they were last seen (irc|forum|claim). If days is specified it will only list users with at least that amount of idle time.");
75 while (my $user = $f->fetchrow_hashref){
76 $user->{last} = '?' unless defined $user->{last};
77 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
80 $ND::server->command("msg $ND::target $ND::B$i$ND::B Users(days)".(defined $flag ? " with flag $ND::B$flag$ND::B" : "").": $text");
82 $ND::server->command("msg $ND::target Only officers are allowed to check that");
88 my ($ship,$command) = @_;
90 unless (defined $ship){
91 $ND::server->command("notice $ND::nick Usage: .$command ship | % can be used as wildcard, e.g. beet%");
94 if (officer() || dc()){
95 my $f = $ND::DBH->prepare(qq{SELECT username,SUM(fs.amount) AS amount
97 JOIN (SELECT DISTINCT ON (planet) planet,fid FROM fleets
98 WHERE mission = 'Full fleet' AND name <> 'Unit'
99 ORDER BY planet,tick DESC,fid DESC) f USING (planet)
100 JOIN fleet_ships fs USING (fid)
101 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
102 GROUP BY username ORDER BY amount DESC
104 if ($command eq 'shipshome'){
105 $f = $ND::DBH->prepare(q{
106 SELECT username,SUM(amount) AS amount
108 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
109 GROUP BY username ORDER BY amount DESC
116 while (my $user = $f->fetchrow_hashref){
117 $user->{last} = '?' unless defined $user->{last};
118 $text .= "$user->{username}: $user->{amount} ";
120 $total += $user->{amount};
123 $ND::server->command("notice $ND::nick $ND::B$i$ND::B Users with $ND::B$total $ship$ND::B: $text");
125 $ND::server->command("msg $ND::target $ND::B$i$ND::B Couldn't find any user with $ND::B$ship$ND::B:");
131 my ($nick,$command) = @_;
133 unless (defined $nick){
134 $ND::server->command("notice $ND::nick Usage: $command nick | % can be used as wildcard, e.g. barr%");
137 if (officer() || dc()){
138 my $f = $ND::DBH->prepare(q{SELECT fs.ship, fs.amount, username
140 JOIN (SELECT fid,username
142 JOIN users u USING (planet)
143 WHERE mission = 'Full fleet' AND name <> 'Unit'
144 AND username ILIKE $1
145 ORDER BY planet,tick DESC,fid DESC
153 while (my $ship = $f->fetchrow_hashref){
154 unless (defined $username) {
155 $username = $ship->{username};
156 $text = "$ND::B$username$ND::O has: "
158 $text .= "$ship->{ship} $ship->{amount} ";
161 $ND::server->command("notice $ND::nick $text");
163 $ND::server->command("notice $ND::nick Couldn't find any fleet for $nick");
172 if(defined $msg && $msg =~ /^(\S+)(?: (-?(\d+)?(\.\d+)?))?$/){
176 $ND::server->command("notice $ND::nick syntax: .$t nick [points] | % can be used for wildcards \%arro\% will match barrow, if the number of points isn't specified, then 1 will be assumed.");
179 if ( ($t eq "d" && dc())
180 || ($t eq "a" && bc())
181 || ($t eq "h" && officer())
182 || ($t eq "s" && scanner())){
183 $points = 1 unless $points;
184 if ($points*$points > 400){
185 $ND::server->command("msg $ND::target Values between -20 and 20 please");
188 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
190 my @row = $f->fetchrow();
192 my $type = "defense";
193 $type = "attack" if $t eq "a";
194 $type = "humor" if $t eq "h";
195 $type = "scan" if $t eq "s";
196 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]);
197 if ($t eq 'a' && $fleets > 0 && $points > 0){
198 $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.");
202 $ND::DBH->do("UPDATE users SET $type = $type + ? WHERE uid = ?",undef,$points,$row[0]);
203 $ND::server->command("msg $ND::target $row[1] has been given $points $type");
204 }elsif ($f->rows == 0){
205 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
207 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
212 $ND::server->command("msg $ND::target You don't have access for that");
217 my ($msg, $command) = @_;
220 if(defined $msg && $msg =~ /^(\S+) (\S+)$/){
224 $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow");
228 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
230 my ($uid,$nick) = $f->fetchrow();
232 my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE hostmask ILIKE ? AND NOT (username ILIKE ?)",undef,$host,$nick);
233 if ((not defined $username) && $ND::DBH->do("UPDATE users SET hostmask = ? WHERE uid = ?",undef,$host,$uid) > 0){
234 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s host to: $ND::B$host$ND::B");
235 }elsif(defined $username){
236 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B.");
238 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
240 }elsif ($f->rows == 0){
241 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
243 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
250 my ($msg, $command) = @_;
253 if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){
257 $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow");
261 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
263 my ($uid,$nick) = $f->fetchrow();
265 my ($username,$p_nick) = $ND::DBH->selectrow_array("SELECT username, pnick FROM users WHERE pnick ILIKE ? AND NOT (username ILIKE ?)",undef,$pnick,$nick);
266 my $hostname = "$pnick.users.netgamers.org";
267 if ((not defined $username) && $ND::DBH->do("UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?",undef,$pnick,$hostname,$uid) > 0){
268 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s pnick to: $ND::B$pnick$ND::B and hostname to $ND::B$hostname$ND::B");
269 }elsif(defined $username){
270 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with pnick $ND::B$p_nick$ND::B.");
272 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
274 }elsif ($f->rows == 0){
275 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
277 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");