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/addUser whois flags flag laston addPoints chattrG setHost setPNick deactivateUser getShips/;
31 my ($msg, $command) = @_;
34 if(defined $msg && $msg =~ /^(\w+)(?: ([^.\s]+))?$/){
38 $ND::server->command("notice $ND::nick syntax: .$command nick [pnick] | nick must be alphanum characters, if no pnick is given then it will be set to nick");
42 $pnick = $nick unless $pnick;
43 my $host = "$pnick.users.netgamers.org";
44 my ($username,$hostname,$p_nick) = $ND::DBH->selectrow_array(q{SELECT username, hostmask,pnick
45 FROM users WHERE username ILIKE ? OR hostmask ILIKE ? OR pnick ILIKE ?}
46 ,undef,$nick,$host,$pnick);
47 if ((not defined $username) && $ND::DBH->do("INSERT INTO users (username,hostmask,pnick,password) VALUES(?,?,?,'')"
48 ,undef,$nick,$host,$pnick)){
49 $ND::server->command("msg $ND::target Added $ND::B$nick(/$pnick)$ND::B with host: $ND::B$host$ND::B");
50 }elsif(defined $username){
51 $ND::server->command("msg $ND::target $ND::B$username ($p_nick)$ND::B already exists with host: $ND::B$hostname$ND::B.");
54 $ND::server->command("msg $ND::target Something went wrong when trying to add $ND::B$nick ($pnick)$ND::B with host: $ND::B$host$ND::B, ".$ND::DBH->errstr);
57 $ND::server->command("msg $ND::target Only HCs are allowed to add users");
61 my ($msg, $command) = @_;
64 if(defined $msg && $msg =~ /^(\S+)$/){
67 $ND::server->command("notice $ND::nick syntax: .$command nick | % can be used for wildcards \%arro% will match barrow");
70 if (officer() || ia()){
71 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");
73 while (my @row = $f->fetchrow()){
74 $ND::server->command("msg $ND::target $row[0] flags: ($row[2]) host: $row[1]");
77 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
80 $ND::server->command("msg $ND::target Only officers are allowed to check that");
85 my ($msg, $command) = @_;
88 if(defined $msg && $msg =~ /^(\S+)$/){
91 my ($flags) = $ND::DBH->selectrow_array("SELECT TRIM(', ' FROM concat(flag||':'||groupname||', ')) FROM groups");
92 $ND::server->command("notice $ND::nick syntax: .$command nick | % can be used for wildcards \%arro% will match barrow | Current flags: $flags");
96 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");
98 while (my @row = $f->fetchrow()){
99 $ND::server->command("msg $ND::target Flags for $row[0] on: $ND::target: $row[1]| (Global: $row[2])");
102 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
108 my ($msg, $command) = @_;
111 if(defined $msg && $msg =~ /^(\w)$/){
114 $ND::server->command("notice $ND::nick syntax: .$command flag");
118 if (officer() || ia() || ($ND::target eq $ND::scanchan && $flag eq 'S')){
119 my $f = $ND::DBH->prepare(qq{
120 SELECT TRIM(', ' FROM concat(username||', ')),count(username) FROM
121 (SELECT uid, username FROM users ORDER BY username) u NATURAL JOIN groupmembers gm
122 JOIN groups g ON g.gid = gm.gid
125 if (my ($users,$count) = $ND::DBH->selectrow_array($f,undef,$flag)){
126 $ND::server->command("msg $ND::target $ND::B$count$ND::B Users with flag $ND::B$flag$ND::B: $users");
129 $ND::server->command("msg $ND::target Only officers are allowed to check that");
134 my ($msg, $command) = @_;
138 if (officer() || ia()){
139 if(defined $msg && $msg =~ /^(\w)(?: (\d+))?$/){
142 $min = 0 unless defined $min;
143 $f = $ND::DBH->prepare(qq{SELECT username,last,lastforum
144 FROM (SELECT uid,username, date_part('day',now() - laston)::int AS last
145 ,date_part('day',now() - (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid))::int AS lastforum
146 ,laston FROM users u) u
147 NATURAL JOIN groupmembers
149 WHERE flag = ? AND (last >= ? OR last IS NULL)
152 $f->execute($flag,$min);
153 }elsif(defined $msg && $msg =~ /^(\S+)$/){
154 $f = $ND::DBH->prepare(qq{SELECT username,last,lastforum
155 FROM (SELECT uid,username, date_part('day',now() - laston)::int AS last
156 ,date_part('day',now() - (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid))::int AS lastforum
157 , laston FROM users u) u
158 WHERE username ILIKE ? ORDER BY lower(username)
162 $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). If days is specified it will only list users with at least that amount of idle time.");
168 while (my $user = $f->fetchrow_hashref){
169 $user->{last} = '?' unless defined $user->{last};
170 $user->{lastforum} = '?' unless defined $user->{lastforum};
171 $text .= "$user->{username}($user->{last}|$user->{lastforum}) ";
174 $ND::server->command("msg $ND::target $ND::B$i$ND::B Users(days)".(defined $flag ? " with flag $ND::B$flag$ND::B" : "").": $text");
176 $ND::server->command("msg $ND::target Only officers are allowed to check that");
182 my ($ship,$command) = @_;
184 unless (defined $ship){
185 $ND::server->command("notice $ND::nick Usage: .$command ship | % can be used as wildcard, e.g. beet%");
188 if (officer() || dc()){
189 my $f = $ND::DBH->prepare(qq{SELECT username,SUM(fs.amount) AS amount
191 JOIN (SELECT DISTINCT ON (sender) sender AS planet,id FROM fleets
192 WHERE mission = 'Full fleet' AND name <> 'Unit'
193 ORDER BY sender,tick DESC,id DESC) f USING (planet)
194 JOIN fleet_ships fs ON f.id = fs.id
196 GROUP BY username ORDER BY amount DESC
202 while (my $user = $f->fetchrow_hashref){
203 $user->{last} = '?' unless defined $user->{last};
204 $text .= "$user->{username}: $user->{amount} ";
206 $total += $user->{amount};
209 $ND::server->command("notice $ND::nick $ND::B$i$ND::B Users with $ND::B$total $ship$ND::B: $text");
211 $ND::server->command("msg $ND::target $ND::B$i$ND::B Couldn't find any user with $ND::B$ship$ND::B:");
220 if(defined $msg && $msg =~ /^(\S+)(?: (-?(\d+)?(\.\d+)?))?$/){
224 $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.");
227 if ( ($t eq "d" && dc())
228 || ($t eq "a" && bc())
229 || ($t eq "h" && officer())
230 || ($t eq "s" && scanner())){
231 $points = 1 unless (defined $points);
232 if ($points*$points > 400){
233 $ND::server->command("msg $ND::target Values between -20 and 20 please");
236 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
238 my @row = $f->fetchrow();
240 my $type = "defense";
241 $type = "attack" if $t eq "a";
242 $type = "humor" if $t eq "h";
243 $type = "scan" if $t eq "s";
244 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]);
245 if ($t eq 'a' && $fleets > 0 && $points > 0){
246 $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.");
250 $ND::DBH->do("UPDATE users SET $type = $type + ? WHERE uid = ?",undef,$points,$row[0]);
251 $ND::server->command("msg $ND::target $row[1] has been given $points $type");
252 }elsif ($f->rows == 0){
253 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
255 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
260 $ND::server->command("msg $ND::target You don't have access for that");
265 my ($msg, $command) = @_;
268 if(defined $msg && $msg =~ /^(\S+) ((\+|-)?\w+)$/){
272 $ND::server->command("notice $ND::nick syntax: .chattr nick [-]flags | % can be used for wildcards \%arro% will match barrow, if a - is given then flags will be removed, otherwise added");
275 if (hc()){# || ($flags =~ /^(\+|-)?x$/ && $ND::address eq 'Assassin.users.netgamers.org')){
276 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
278 my @user = $f->fetchrow();
284 $update = $ND::DBH->prepare("DELETE FROM groupmembers WHERE uid = ? AND gid = (SELECT gid FROM groups WHERE flag = ?)");
286 $update = $ND::DBH->prepare("INSERT INTO groupmembers (uid,gid) VALUES(?,(SELECT gid FROM groups WHERE flag = ?))");
288 while ($flags =~ m/(\w)/g){
289 $update->execute($user[0],$1);
291 $update = $ND::DBH->prepare("SELECT concat(flag) FROM (SELECT uid,flag FROM groupmembers NATURAL JOIN groups ORDER BY uid,flag ) g WHERE uid = ? ");
292 my @flags = $ND::DBH->selectrow_array($update,undef,$user[0]);
293 $ND::server->command("msg $ND::target Global flags for $user[1] are now: $flags[0]");
294 }elsif ($f->rows == 0){
295 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
297 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
304 my ($msg, $command) = @_;
307 if(defined $msg && $msg =~ /^(\S+) (\S+)$/){
311 $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow");
315 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
317 my ($uid,$nick) = $f->fetchrow();
319 my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE hostmask ILIKE ? AND NOT (username ILIKE ?)",undef,$host,$nick);
320 if ((not defined $username) && $ND::DBH->do("UPDATE users SET hostmask = ? WHERE uid = ?",undef,$host,$uid) > 0){
321 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s host to: $ND::B$host$ND::B");
322 }elsif(defined $username){
323 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B.");
325 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
327 }elsif ($f->rows == 0){
328 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
330 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
337 my ($msg, $command) = @_;
340 if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){
344 $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow");
348 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
350 my ($uid,$nick) = $f->fetchrow();
352 my ($username,$p_nick) = $ND::DBH->selectrow_array("SELECT username, pnick FROM users WHERE pnick ILIKE ? AND NOT (username ILIKE ?)",undef,$pnick,$nick);
353 my $hostname = "$pnick.users.netgamers.org";
354 if ((not defined $username) && $ND::DBH->do("UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?",undef,$pnick,$hostname,$uid) > 0){
355 $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");
356 }elsif(defined $username){
357 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with pnick $ND::B$p_nick$ND::B.");
359 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
361 }elsif ($f->rows == 0){
362 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
364 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
371 my ($msg, $command) = @_;
374 if(defined $msg && $msg =~ /^(\S+)$/){
377 $ND::server->command("notice $ND::nick syntax: .$command user");
381 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
383 my ($uid,$username) = $f->fetchrow();
385 my $updated = $ND::DBH->do("UPDATE users SET hostmask = ?, password = '' WHERE uid = ?",undef,$username,$uid);
387 my $groups = $ND::DBH->do("DELETE FROM groupmembers WHERE uid = ?",undef,$uid);
388 $ND::server->command("msg $ND::target $ND::B$username$ND::B has been deactivated.");
390 $ND::server->command("msg $ND::target Something went wrong when trying to modify $ND::B$username$ND::B");
392 }elsif ($f->rows == 0){
393 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
395 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");