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/flag laston addPoints setHost setPNick getShips getFleet/;
31 my ($msg, $command) = @_;
34 if(defined $msg && $msg =~ /^(\w)$/){
37 $ND::server->command("notice $ND::nick syntax: .$command flag");
41 if (officer() || ia() || ($ND::target eq $ND::scanchan && $flag eq 'S')){
42 my $f = $ND::DBH->prepare(qq{
43 SELECT TRIM(', ' FROM concat(username||', ')),count(username) FROM
44 (SELECT uid, username FROM users ORDER BY username) u NATURAL JOIN groupmembers gm
45 JOIN groups g ON g.gid = gm.gid
48 if (my ($users,$count) = $ND::DBH->selectrow_array($f,undef,$flag)){
49 $ND::server->command("msg $ND::target $ND::B$count$ND::B Users with flag $ND::B$flag$ND::B: $users");
52 $ND::server->command("msg $ND::target Only officers are allowed to check that");
57 my ($msg, $command) = @_;
59 if (officer() || ia()){
62 if(defined $msg && $msg =~ /^(\w)(?: (\d+)(?:\|(\d+)\|(\d+))?)?$/){
67 $min = 0 unless defined $min;
68 $f = $ND::DBH->prepare(q{SELECT username, last
69 ,COALESCE(lastforum::text,'?') AS lastforum
70 ,COALESCE(lastclaim::text,'?') AS lastclaim
72 ,date_part('day',now() - laston)::int AS last
73 ,date_part('day',now() - (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid)) AS lastforum
74 ,date_part('day',now() - (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid)) AS lastclaim
76 NATURAL JOIN groupmembers
80 WHERE COALESCE(last >= $2,TRUE) AND COALESCE(lastforum >= $3,TRUE) AND COALESCE(lastclaim >= $4,TRUE)
81 ORDER BY last DESC, lastforum DESC, lastclaim DESC
83 $f->execute($flag,$min,$forum,$claim);
84 }elsif(defined $msg && $msg =~ /^(\S+)$/){
85 $f = $ND::DBH->prepare(q{SELECT username
86 ,date_part('day',now() - laston)::int AS last
87 ,COALESCE(date_part('day',now() - (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid))::text,'?')
89 ,COALESCE(date_part('day',now() - (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid))::text,'?') AS lastclaim
91 WHERE username ILIKE $1 ORDER BY lower(username)
95 $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.");
101 while (my $user = $f->fetchrow_hashref){
102 $user->{last} = '?' unless defined $user->{last};
103 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
106 $ND::server->command("msg $ND::target $ND::B$i$ND::B Users(days)".(defined $flag ? " with flag $ND::B$flag$ND::B" : "").": $text");
108 $ND::server->command("msg $ND::target Only officers are allowed to check that");
114 my ($ship,$command) = @_;
116 unless (defined $ship){
117 $ND::server->command("notice $ND::nick Usage: .$command ship | % can be used as wildcard, e.g. beet%");
120 if (officer() || dc()){
121 my $f = $ND::DBH->prepare(qq{SELECT username,SUM(fs.amount) AS amount
123 JOIN (SELECT DISTINCT ON (planet) planet,fid FROM fleets
124 WHERE mission = 'Full fleet' AND name <> 'Unit'
125 ORDER BY planet,tick DESC,fid DESC) f USING (planet)
126 JOIN fleet_ships fs USING (fid)
127 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
128 GROUP BY username ORDER BY amount DESC
130 if ($command eq 'shipshome'){
131 $f = $ND::DBH->prepare(q{
132 SELECT username,SUM(amount) AS amount
134 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
135 GROUP BY username ORDER BY amount DESC
142 while (my $user = $f->fetchrow_hashref){
143 $user->{last} = '?' unless defined $user->{last};
144 $text .= "$user->{username}: $user->{amount} ";
146 $total += $user->{amount};
149 $ND::server->command("notice $ND::nick $ND::B$i$ND::B Users with $ND::B$total $ship$ND::B: $text");
151 $ND::server->command("msg $ND::target $ND::B$i$ND::B Couldn't find any user with $ND::B$ship$ND::B:");
157 my ($nick,$command) = @_;
159 unless (defined $nick){
160 $ND::server->command("notice $ND::nick Usage: $command nick | % can be used as wildcard, e.g. barr%");
163 if (officer() || dc()){
164 my $f = $ND::DBH->prepare(q{SELECT fs.ship, fs.amount, username
166 JOIN (SELECT fid,username
168 JOIN users u USING (planet)
169 WHERE mission = 'Full fleet' AND name <> 'Unit'
170 AND username ILIKE $1
171 ORDER BY planet,tick DESC,fid DESC
179 while (my $ship = $f->fetchrow_hashref){
180 unless (defined $username) {
181 $username = $ship->{username};
182 $text = "$ND::B$username$ND::O has: "
184 $text .= "$ship->{ship} $ship->{amount} ";
187 $ND::server->command("notice $ND::nick $text");
189 $ND::server->command("notice $ND::nick Couldn't find any fleet for $nick");
198 if(defined $msg && $msg =~ /^(\S+)(?: (-?(\d+)?(\.\d+)?))?$/){
202 $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.");
205 if ( ($t eq "d" && dc())
206 || ($t eq "a" && bc())
207 || ($t eq "h" && officer())
208 || ($t eq "s" && scanner())){
209 $points = 1 unless $points;
210 if ($points*$points > 400){
211 $ND::server->command("msg $ND::target Values between -20 and 20 please");
214 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
216 my @row = $f->fetchrow();
218 my $type = "defense";
219 $type = "attack" if $t eq "a";
220 $type = "humor" if $t eq "h";
221 $type = "scan" if $t eq "s";
222 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]);
223 if ($t eq 'a' && $fleets > 0 && $points > 0){
224 $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.");
228 $ND::DBH->do("UPDATE users SET $type = $type + ? WHERE uid = ?",undef,$points,$row[0]);
229 $ND::server->command("msg $ND::target $row[1] has been given $points $type");
230 }elsif ($f->rows == 0){
231 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
233 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
238 $ND::server->command("msg $ND::target You don't have access for that");
243 my ($msg, $command) = @_;
246 if(defined $msg && $msg =~ /^(\S+) (\S+)$/){
250 $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow");
254 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
256 my ($uid,$nick) = $f->fetchrow();
258 my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE hostmask ILIKE ? AND NOT (username ILIKE ?)",undef,$host,$nick);
259 if ((not defined $username) && $ND::DBH->do("UPDATE users SET hostmask = ? WHERE uid = ?",undef,$host,$uid) > 0){
260 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s host to: $ND::B$host$ND::B");
261 }elsif(defined $username){
262 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B.");
264 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
266 }elsif ($f->rows == 0){
267 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
269 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
276 my ($msg, $command) = @_;
279 if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){
283 $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow");
287 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
289 my ($uid,$nick) = $f->fetchrow();
291 my ($username,$p_nick) = $ND::DBH->selectrow_array("SELECT username, pnick FROM users WHERE pnick ILIKE ? AND NOT (username ILIKE ?)",undef,$pnick,$nick);
292 my $hostname = "$pnick.users.netgamers.org";
293 if ((not defined $username) && $ND::DBH->do("UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?",undef,$pnick,$hostname,$uid) > 0){
294 $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");
295 }elsif(defined $username){
296 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with pnick $ND::B$p_nick$ND::B.");
298 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
300 }elsif ($f->rows == 0){
301 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
303 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");