1 #**************************************************************************
2 # Copyright (C) 2009 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 #**************************************************************************/
20 package NDIRC::Commands::Usermgm;
27 use MooseX::MethodAttributes;
30 : Help(syntax: .+user username [pnick] | username must be alphanum characters, if no pnick is given then it will be set to the same as username)
34 my ($self,$c,$msg) = @_;
35 my ($nick,$pnick) = $msg =~ /^(\w+)(?: ([^.\s]+))?$/ or die 'ARGS';
40 my $host = "$pnick.users.netgamers.org";
41 my ($username,$hostname,$p_nick) = $dbh->selectrow_array(q{
42 SELECT username, hostmask,pnick
43 FROM users WHERE username = $1 OR hostmask = $2 OR pnick = $3
44 },undef,$nick,$host,$pnick);
46 if (defined $username){
47 $c->reply("<b>$username ($p_nick)</b> already exists with host: <b>$hostname</b>");
50 INSERT INTO users (username,hostmask,pnick,password) VALUES(?,?,?,'')
51 },undef,$nick,$host,$pnick);
52 $c->reply("Added <b>$nick(/$pnick)</b> with host: <b>$host</b>");
57 : Help(syntax: .-user nick | nick must be alphanum characters, if no pnick is given then it will be set to nick)
59 : ACL(irc_deactivateuser)
61 my ($self,$c,$msg) = @_;
63 my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
66 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
68 my ($uid,$username) = $f->fetchrow();
71 my $updated = $dbh->do(q{
72 UPDATE users SET hostmask = ?, password = '' WHERE uid = ?
73 },undef,$username,$uid);
75 my $groups = $dbh->do(q{DELETE FROM groupmembers WHERE uid = ?},undef,$uid);
77 $c->reply("<b>$username</b> has been deactivated. Removed from $groups groups.");
79 $c->reply("Something went wrong when trying to modify <b>$username</b>");
81 }elsif ($f->rows == 0){
82 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
84 $c->reply("More than 1 user matched, please refine the search");
90 : Help(syntax: .chattr username [-]flags | % can be used for wildcards \%arro% will match barrow, if a - is given then flags will be removed, otherwise added)
93 my ($self,$c,$msg) = @_;
95 my ($nick, $mode, $flags) = $msg =~ /^(\S+) (\+|-)?(\w+)$/ or die 'ARGS';
98 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
100 my $user = $f->fetchrow_hashref;
104 $update = $dbh->prepare(q{
105 DELETE FROM groupmembers WHERE uid = $1 AND gid = ANY($2)
108 $update = $dbh->prepare(q{
109 INSERT INTO groupmembers (uid,gid)
110 (SELECT $1,gid FROM unnest($2::text[]) gid WHERE
111 gid NOT IN (SELECT gid FROM groupmembers WHERE uid = $1)
112 AND gid IN (SELECT gid FROM groups))
115 my @flags = split /\W*/,$flags;
116 $update->execute($user->{uid},\@flags);
117 $update = $dbh->prepare(q{
118 SELECT array_to_string(array_agg(gid),'')
119 FROM (SELECT uid,gid FROM groupmembers ORDER BY uid,gid ) g
122 $flags = $dbh->selectrow_array($update,undef,$user->{uid});
123 $c->reply("Flags for <b>$user->{username}</b> are now: $flags");
124 }elsif ($f->rows == 0){
125 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
127 $c->reply("More than 1 user matched, please refine the search");
133 : Help(syntax: .whois [username] | % can be used for wildcards \%arro% will match barrow. If no username is given then all flags will be listed)
137 my ($self,$c,$msg) = @_;
138 my ($nick) = $msg =~ /^(\S+)?$/ or die 'ARGS';
142 my ($flags) = $dbh->selectrow_array(q{
143 SELECT array_to_string(array_agg(gid||':'||groupname),', ') FROM groups
145 $c->reply("Current flags: $flags");
149 my $f = $dbh->prepare(q{
150 SELECT username, pnick, hostmask, array_to_string(array_agg(gid),'') AS flags
152 LEFT OUTER JOIN (SELECT uid,gid FROM groupmembers ORDER BY uid,gid
154 WHERE username ILIKE ?
155 GROUP BY username,pnick,hostmask LIMIT 5
158 while (my $user = $f->fetchrow_hashref){
159 $c->reply("<b>$user->{username} (/$user->{pnick})</b> flags: ($user->{flags}) host: $user->{hostmask}");
162 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
167 : Help(syntax: .flag flag [-noflags] | Lists all users with the given flag. Can specify a second argument to remove members that has any of those flags. .flag M -C lists all members that doesn't have community flag)
170 my ($self,$c,$msg) = @_;
171 my ($flag,$noflag) = $msg =~ /^(\w)(?: -(\w+))?$/ or die 'ARGS';
174 my $f = $c->model->prepare(q{
175 SELECT array_to_string(array_agg(username),', '),count(username)
176 FROM (SELECT uid, username FROM users ORDER BY username) u
177 JOIN groupmembers gm USING (uid)
178 WHERE gid = $1 AND uid NOT IN (SELECT uid FROM groupmembers WHERE gid = ANY($2))
180 my @noflag = split //, $noflag;
181 my ($users,$count) = $c->model->selectrow_array($f,undef,$flag,\@noflag);
182 $c->reply("<b>$count</b> Users with flag <b>$flag</b>: $users");
186 : Help(syntax: .laston flag [days] | 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. Days can also specify forum and claim with irc|forum|claim syntax)
189 my ($self,$c,$msg) = @_;
190 my ($flag,$min,$forum,$claim) = $msg =~ /^(\w)(?: (\d+)(?:\|(\d+)\|(\d+))?)?$/
194 my $f = $c->model->prepare(q{
195 SELECT username, COALESCE(last::text,'?') AS last
196 ,COALESCE(lastforum::text,'?') AS lastforum
197 ,COALESCE(lastclaim::text,'?') AS lastclaim
198 FROM (SELECT username
199 ,date_part('day',now() - laston)::int AS last
200 ,date_part('day',now() -
201 (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid)) AS lastforum
202 ,date_part('day',now() -
203 (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid)) AS lastclaim
205 NATURAL JOIN groupmembers
208 WHERE COALESCE(last >= $2,TRUE) AND COALESCE(lastforum >= $3,TRUE)
209 AND COALESCE(lastclaim >= $4,TRUE)
210 ORDER BY a.last DESC, a.lastforum DESC, a.lastclaim DESC
212 $f->execute($flag,$min,$forum,$claim);
216 while (my $user = $f->fetchrow_hashref){
217 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
220 $c->reply("<b>$i</b> Users(days) with flag <b>$flag</b>: $text");
225 : Help(syntax: .lastseen username | Shows the number of days since the user(s) last visited irc, forum and raids)
228 my ($self,$c,$msg) = @_;
229 my ($username) = $msg =~ /^(\S+)$/ or die 'ARGS';
231 my $f = $c->model->prepare(q{
232 SELECT username, COALESCE(date_part('day',now() - laston)::text,'?') AS last
233 ,COALESCE(date_part('day',now() - (SELECT max(time)
234 FROM forum_thread_visits WHERE uid = u.uid))::text,'?') AS lastforum
235 ,COALESCE(date_part('day',now() - (SELECT max(timestamp)
236 FROM raid_claims WHERE uid = u.uid))::text,'?') AS lastclaim
238 WHERE username ILIKE $1 ORDER BY username
240 $f->execute($username);
244 while (my $user = $f->fetchrow_hashref){
245 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
248 $c->reply("<b>$i</b> Users(days): $text");
252 : Help(Usage: .getships ship | % can be used as wildcard, e.g. beet%, shipshome shows the number of ships currently home)
256 my ($self,$c,$msg) = @_;
257 my ($ship) = $msg =~ /^(\S+)$/ or die 'ARGS';
260 my $f = $dbh->prepare(q{
261 SELECT username,SUM(fs.amount) AS amount
263 JOIN (SELECT DISTINCT ON (pid) pid,fid FROM fleets
264 WHERE mission = 'Full fleet' AND name <> 'Unit'
265 ORDER BY pid,tick DESC,fid DESC
267 JOIN fleet_ships fs USING (fid)
268 WHERE ship ILIKE $1 AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
269 GROUP BY username ORDER BY amount DESC
271 if ($self->name eq 'shipshome'){
272 $f = $dbh->prepare(q{
273 SELECT username,SUM(amount) AS amount
275 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
276 GROUP BY username ORDER BY amount DESC
283 while (my $user = $f->fetchrow_hashref){
284 $text .= "$user->{username}: $user->{amount} ";
286 $total += $user->{amount};
289 $c->reply("<b>$i</b> Users with <b>$total $ship</b>: $text");
291 $c->reply("Couldn't find any user with <b>$ship</b>");
296 : Help(Usage: .getfleet username | % can be used as wildcard, e.g. barr%)
299 my ($self,$c,$msg) = @_;
300 my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
303 my $f = $dbh->prepare(q{
304 SELECT fs.ship, fs.amount, username
306 JOIN (SELECT fid,username
308 JOIN users u USING (pid)
309 WHERE mission = 'Full fleet' AND name <> 'Unit'
310 AND username ILIKE $1
311 ORDER BY pid,tick DESC,fid DESC
319 while (my $ship = $f->fetchrow_hashref){
320 unless (defined $username) {
321 $username = $ship->{username};
322 $text = "<b>$username</b> has: "
324 $text .= "$ship->{ship} $ship->{amount} ";
329 $c->reply("Couldn't find any fleet for $nick");
334 : Help(Usage: .sethost username [host] | if host isn't given then it resets to netgamers host)
337 my ($self,$c,$msg) = @_;
338 my ($nick,$host) = $msg =~ /^(\S+)(?: (\S+))?$/ or die 'ARGS';
341 my $f = $dbh->prepare(q{
342 SELECT uid,username,pnick,hostmask FROM users WHERE username ILIKE ?
345 my $user = $f->fetchrow_hashref;
347 $host //= "$user->{pnick}.users.netgamers.org";
349 $dbh->do(q{UPDATE users SET hostmask = ? WHERE uid = ?}
350 ,undef,$host,$user->{uid});
351 $c->reply("Updated <b>$user->{username}</b>'s host to: <b>$host</b>");
354 if ($@ =~ /duplicate key value violates unique constraint/){
355 my ($username, $hostname) = $dbh->selectrow_array(q{
356 SELECT username,hostmask FROM users WHERE hostmask = $1
358 $c->reply("<c04>Problem</c>, <b>$username</b> already uses host <b>$hostname</b>.");
363 }elsif ($f->rows == 0){
364 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
366 $c->reply("More than 1 user matched, please refine the search");
372 : Help(Usage: .setpnick username pnick | Changes a user's pnick)
375 my ($self,$c,$msg) = @_;
376 my ($nick,$pnick) = $msg =~ /^(\S+) (\S+)$/ or die 'ARGS';
379 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
381 my $user = $f->fetchrow_hashref;
383 my $hostname = "$pnick.users.netgamers.org";
385 $dbh->do(q{UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?}
386 ,undef,$pnick,$hostname,$user->{uid});
387 $c->reply("Updated <b>$user->{username}</b>'s pnick to: <b>$pnick</b> and hostname to <b>$hostname</b>");
390 if ($@ =~ /duplicate key value violates unique constraint/){
391 my ($username, $hostname, $pnick) = $dbh->selectrow_array(q{
392 SELECT username,hostmask,pnick FROM users WHERE hostmask = $1 OR pnick = $2
393 },undef,$hostname, $pnick);
394 $c->reply("<c04>Problem</c>, <b>$username</b> already uses host <b>$hostname</b> and pnick <b>$pnick</b>.");
399 }elsif ($f->rows == 0){
400 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
402 $c->reply("More than 1 user matched, please refine the search");
408 : Help(Usage: .a username [points] | % can be used for wildcards %arro% will match barrow, if the number of points isn't specified, then 1 will be assumed.)
411 my ($self,$c,$msg) = @_;
412 my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
415 my ($fleets) = $c->model->selectrow_array(q{
416 SELECT count(*) FROM raids r
417 JOIN raid_targets rt ON r.id = rt.raid
418 JOIN raid_claims rc ON rt.id = rc.target
419 WHERE not launched AND tick + 24 > tick()
420 AND uid = (SELECT uid FROM users WHERE username ILIKE $1);
423 if ($fleets > 0 && $points > 0){
424 $c->reply("$nick has $fleets claimed waves the last 24 ticks that aren't marked as launched, so no points.");
427 addPoints($c, 'attack', $nick, $points);
431 : Help(Usage: .d username [points] | % can be used for wildcards %arro% will match barrow, if the number of points isn't specified, then 1 will be assumed.)
435 my ($self,$c,$msg) = @_;
436 my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
438 addPoints($c, 'defense', $nick, $points);
442 : Help(Usage: .s username [points] | % can be used for wildcards %arro% will match barrow, if the number of points isn't specified, then 1 will be assumed.)
445 my ($self,$c,$msg) = @_;
446 my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
448 addPoints($c, 'scan', $nick, $points);
452 : Help(Usage: .h username [points] | % can be used for wildcards %arro% will match barrow, if the number of points isn't specified, then 1 will be assumed.)
455 my ($self,$c,$msg) = @_;
456 my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
458 addPoints($c, 'humor', $nick, $points);
462 my ($c,$type, $nick, $points) = @_;
467 if ($points*$points > 400){
468 $c->reply("Values between -20 and 20 please");
472 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
474 my $user = $f->fetchrow_hashref;
477 $dbh->do(qq{UPDATE users SET $type = $type + ? WHERE uid = ?}
478 ,undef,$points,$user->{uid});
479 $c->reply("$user->{username} has been given $points $type");
480 }elsif ($f->rows == 0){
481 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
483 $c->reply("More than 1 user matched, please refine the search");