]> ruin.nu Git - NDIRC.git/blob - Commands/Usermgm.pm
Introduce a uid member for the context and use it intead of the host in commands
[NDIRC.git] / Commands / Usermgm.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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
20 package NDIRC::Commands::Usermgm;
21
22 use strict;
23 use warnings;
24 use feature ':5.10';
25
26 use Moose;
27 use MooseX::MethodAttributes;
28
29 sub adduser
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)
31         : Alias(+user)
32         : ACL(irc_adduser)
33 {
34         my ($self,$c,$msg) = @_;
35         my ($nick,$pnick) = $msg =~ /^(\w+)(?: ([^.\s]+))?$/ or die 'ARGS';
36         $pnick //= $nick;
37
38         my $dbh = $c->model;
39
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);
45
46         if (defined $username){
47                 $c->reply("<b>$username ($p_nick)</b> already exists with host: <b>$hostname</b>");
48         }else{
49                 $dbh->do(q{
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>");
53         }
54 }
55
56 sub deactivateuser
57         : Help(syntax: .-user nick | nick must be alphanum characters, if no pnick is given then it will be set to nick)
58         : Alias(-user)
59         : ACL(irc_deactivateuser)
60 {
61         my ($self,$c,$msg) = @_;
62
63         my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
64
65         my $dbh = $c->model;
66         my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
67         $f->execute($nick);
68         my ($uid,$username) = $f->fetchrow();
69
70         if ($f->rows == 1){
71                 my $updated = $dbh->do(q{
72 UPDATE users SET hostmask = ?, password = '' WHERE uid = ?
73                 },undef,$username,$uid);
74                 if ($updated > 0){
75                         my $groups = $dbh->do(q{DELETE FROM groupmembers WHERE uid = ?},undef,$uid);
76                         $groups += 0;
77                         $c->reply("<b>$username</b> has been deactivated. Removed from $groups groups.");
78                 }else{
79                         $c->reply("Something went wrong when trying to modify <b>$username</b>");
80                 }
81         }elsif ($f->rows == 0){
82                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
83         }else{
84                 $c->reply("More than 1 user matched, please refine the search");
85         }
86         $f->finish;
87 }
88
89 sub chattr
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)
91         : ACL(irc_chattr)
92 {
93         my ($self,$c,$msg) = @_;
94
95         my ($nick, $flags) = $msg =~ /^(\S+) ((\+|-)?\w+)$/ or die 'ARGS';
96         my $dbh = $c->model;
97
98         my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
99         $f->execute($nick);
100         my $user = $f->fetchrow_hashref;
101         if ($f->rows == 1){
102                 my $update;
103                 if ($flags =~ /^(-)/){
104                         $update = $dbh->prepare(q{
105 DELETE FROM groupmembers WHERE uid = $1 AND gid = ANY($2)
106                         });
107                 }else{
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))
113                         });
114                 }
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
120 WHERE uid = ?
121                 });
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");
126         }else{
127                 $c->reply("More than 1 user matched, please refine the search");
128         }
129         $f->finish;
130 }
131
132 sub whois
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)
134         : Alias(flags)
135         : ACL(irc_whois)
136 {
137         my ($self,$c,$msg) = @_;
138         my ($nick) = $msg =~ /^(\S+)?$/ or die 'ARGS';
139         my $dbh = $c->model;
140
141         unless ($nick){
142                 my ($flags) = $dbh->selectrow_array(q{
143 SELECT array_to_string(array_agg(gid||':'||groupname),', ') FROM groups
144                 });
145                 $c->reply("Current flags: $flags");
146                 return;
147         }
148
149         my $f = $dbh->prepare(q{
150 SELECT username, pnick, hostmask, array_to_string(array_agg(gid),'') AS flags
151 FROM users u
152         LEFT OUTER JOIN (SELECT uid,gid FROM groupmembers ORDER BY uid,gid
153         ) g USING (uid)
154 WHERE username ILIKE ?
155 GROUP BY username,pnick,hostmask LIMIT 5
156                 });
157         $f->execute($nick);
158         while (my $user = $f->fetchrow_hashref){
159                 $c->reply("<b>$user->{username} (/$user->{pnick})</b> flags: ($user->{flags}) host: $user->{hostmask}");
160         }
161         if ($f->rows == 0){
162                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
163         }
164 }
165
166 sub flag
167         : Help(syntax: .flag flag | Lists all users with the given flag.)
168         : ACL(irc_flag)
169 {
170         my ($self,$c,$msg) = @_;
171         my ($flag) = $msg =~ /^(\w)$/ or die 'ARGS';
172
173         my $f = $c->model->prepare(q{
174 SELECT array_to_string(array_agg(username),', '),count(username)
175 FROM (SELECT uid, username FROM users ORDER BY username) u
176         JOIN groupmembers gm USING (uid)
177 WHERE gid = $1
178                 });
179         my ($users,$count) = $c->model->selectrow_array($f,undef,$flag);
180         $c->reply("<b>$count</b> Users with flag <b>$flag</b>: $users");
181 }
182
183 sub laston
184         : 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)
185         : ACL(irc_laston)
186 {
187         my ($self,$c,$msg) = @_;
188         my ($flag,$min,$forum,$claim) = $msg =~ /^(\w)(?: (\d+)(?:\|(\d+)\|(\d+))?)?$/
189                 or die 'ARGS';
190         $min //= 0;
191
192         my $f = $c->model->prepare(q{
193 SELECT username, COALESCE(last::text,'?') AS last
194         ,COALESCE(lastforum::text,'?') AS lastforum
195         ,COALESCE(lastclaim::text,'?') AS lastclaim
196 FROM (SELECT username
197                 ,date_part('day',now() - laston)::int AS last
198                 ,date_part('day',now() -
199                         (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid)) AS lastforum
200                 ,date_part('day',now() -
201                         (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid)) AS lastclaim
202         FROM users u
203                 NATURAL JOIN groupmembers
204         WHERE gid = $1
205         ) a
206 WHERE COALESCE(last >= $2,TRUE) AND COALESCE(lastforum >= $3,TRUE)
207         AND COALESCE(lastclaim >= $4,TRUE)
208 ORDER BY a.last DESC, a.lastforum DESC, a.lastclaim DESC
209                 });
210         $f->execute($flag,$min,$forum,$claim);
211
212         my $text;
213         my $i = 0;
214         while (my $user = $f->fetchrow_hashref){
215                 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
216                 $i++;
217         }
218         $c->reply("<b>$i</b> Users(days) with flag <b>$flag</b>: $text");
219 }
220
221
222 sub lastseen
223         : Help(syntax: .lastseen username | Shows the number of days since the user(s) last visited irc, forum and raids)
224         : ACL(irc_lastseen)
225 {
226         my ($self,$c,$msg) = @_;
227         my ($username) = $msg =~ /^(\S+)$/ or die 'ARGS';
228
229         my $f = $c->model->prepare(q{
230 SELECT username, COALESCE(date_part('day',now() - laston)::text,'?') AS last
231         ,COALESCE(date_part('day',now() - (SELECT max(time)
232                 FROM forum_thread_visits WHERE uid = u.uid))::text,'?') AS lastforum
233         ,COALESCE(date_part('day',now() - (SELECT max(timestamp)
234                 FROM raid_claims WHERE uid = u.uid))::text,'?') AS lastclaim
235 FROM users u
236 WHERE username ILIKE $1 ORDER BY username
237                 });
238         $f->execute($username);
239
240         my $text;
241         my $i = 0;
242         while (my $user = $f->fetchrow_hashref){
243                 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
244                 $i++;
245         }
246         $c->reply("<b>$i</b> Users(days): $text");
247 }
248
249 sub getships
250         : Help(Usage: .getships ship | % can be used as wildcard, e.g. beet%, shipshome shows the number of ships currently home)
251         : Alias(shipshome)
252         : ACL(irc_getships)
253 {
254         my ($self,$c,$msg) = @_;
255         my ($ship) = $msg =~ /^(\S+)$/ or die 'ARGS';
256         my $dbh = $c->model;
257
258         my $f = $dbh->prepare(q{
259 SELECT username,SUM(fs.amount) AS amount
260 FROM users u
261         JOIN (SELECT DISTINCT ON (pid) pid,fid FROM fleets
262                 WHERE mission = 'Full fleet' AND name <> 'Unit'
263                 ORDER BY pid,tick DESC,fid DESC
264         ) f  USING (pid)
265         JOIN fleet_ships fs USING (fid)
266 WHERE ship ILIKE $1 AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
267 GROUP BY username ORDER BY amount DESC
268                 });
269         if ($self->name eq 'shipshome'){
270                 $f = $dbh->prepare(q{
271 SELECT username,SUM(amount) AS amount
272 FROM available_ships
273 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
274 GROUP BY username ORDER BY amount DESC
275                 });
276         }
277         $f->execute($ship);
278         my $text;
279         my $i = 0;
280         my $total = 0;
281         while (my $user = $f->fetchrow_hashref){
282                 $text .= "$user->{username}: $user->{amount} ";
283                 $i++;
284                 $total += $user->{amount};
285         }
286         if ($text){
287                 $c->reply("<b>$i</b> Users with <b>$total $ship</b>: $text");
288         }else{
289                 $c->reply("Couldn't find any user with <b>$ship</b>");
290         }
291 }
292
293 sub getfleet
294         : Help(Usage: .getfleet username | % can be used as wildcard, e.g. barr%)
295         : ACL(irc_getfleet)
296 {
297         my ($self,$c,$msg) = @_;
298         my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
299         my $dbh = $c->model;
300
301         my $f = $dbh->prepare(q{
302 SELECT fs.ship, fs.amount, username
303 FROM fleet_ships fs
304         JOIN (SELECT fid,username
305                 FROM fleets f
306                         JOIN users u USING (pid)
307                 WHERE mission = 'Full fleet' AND name <> 'Unit'
308                         AND username ILIKE $1
309                 ORDER BY pid,tick DESC,fid DESC
310                 LIMIT 1
311         ) f  USING (fid)
312 ORDER BY num
313                 });
314         $f->execute($nick);
315         my $text;
316         my $username;
317         while (my $ship = $f->fetchrow_hashref){
318                 unless (defined $username) {
319                         $username = $ship->{username};
320                         $text = "<b>$username</b> has: "
321                 }
322                 $text .= "$ship->{ship} $ship->{amount} ";
323         }
324         if ($text){
325                 $c->reply($text);
326         }else{
327                 $c->reply("Couldn't find any fleet for $nick");
328         }
329 }
330
331 sub sethost
332         : Help(Usage: .sethost username [host] | if host isn't given then it resets to netgamers host)
333         : ACL(irc_sethost)
334 {
335         my ($self,$c,$msg) = @_;
336         my ($nick,$host) = $msg =~ /^(\S+)(?: (\S+))?$/ or die 'ARGS';
337         my $dbh = $c->model;
338
339         my $f = $dbh->prepare(q{
340 SELECT uid,username,pnick,hostmask FROM users WHERE username ILIKE ?
341                 });
342         $f->execute($nick);
343         my $user = $f->fetchrow_hashref;
344         if ($f->rows == 1){
345                 $host //= "$user->{pnick}.users.netgamers.org";
346                 eval{
347                         $dbh->do(q{UPDATE users SET hostmask = ? WHERE uid = ?}
348                                 ,undef,$host,$user->{uid});
349                         $c->reply("Updated <b>$user->{username}</b>'s host to: <b>$host</b>");
350                 };
351                 if($@){
352                         if ($@ =~ /duplicate key value violates unique constraint/){
353                                 my ($username, $hostname) = $dbh->selectrow_array(q{
354 SELECT username,hostmask FROM users WHERE hostmask = $1
355                                 },undef,$host);
356                                 $c->reply("<c04>Problem</c>, <b>$username</b> already uses host <b>$hostname</b>.");
357                         }else{
358                                 die $@;
359                         }
360                 }
361         }elsif ($f->rows == 0){
362                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
363         }else{
364                 $c->reply("More than 1 user matched, please refine the search");
365         }
366         $f->finish;
367 }
368
369 sub setpnick
370         : Help(Usage: .setpnick username pnick | Changes a user's pnick)
371         : ACL(irc_setpnick)
372 {
373         my ($self,$c,$msg) = @_;
374         my ($nick,$pnick) = $msg =~ /^(\S+) (\S+)$/ or die 'ARGS';
375         my $dbh = $c->model;
376
377         my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
378         $f->execute($nick);
379         my $user = $f->fetchrow_hashref;
380         if ($f->rows == 1){
381                 my $hostname = "$pnick.users.netgamers.org";
382                 eval{
383                         $dbh->do(q{UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?}
384                                 ,undef,$pnick,$hostname,$user->{uid});
385                         $c->reply("Updated <b>$user->{username}</b>'s pnick to: <b>$pnick</b> and hostname to <b>$hostname</b>");
386                 };
387                 if($@){
388                         if ($@ =~ /duplicate key value violates unique constraint/){
389                                 my ($username, $hostname, $pnick) = $dbh->selectrow_array(q{
390 SELECT username,hostmask,pnick FROM users WHERE hostmask = $1 OR pnick = $2
391                                 },undef,$hostname, $pnick);
392                                 $c->reply("<c04>Problem</c>, <b>$username</b> already uses host <b>$hostname</b> and pnick <b>$pnick</b>.");
393                         }else{
394                                 die $@;
395                         }
396                 }
397         }elsif ($f->rows == 0){
398                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
399         }else{
400                 $c->reply("More than 1 user matched, please refine the search");
401         }
402         $f->finish;
403 }
404
405 sub a
406         : 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.)
407         : ACL(irc_a)
408 {
409         my ($self,$c,$msg) = @_;
410         my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
411         $points //= 1;
412
413         my ($fleets) = $c->model->selectrow_array(q{
414 SELECT count(*) FROM raids r
415         JOIN raid_targets rt ON r.id = rt.raid
416         JOIN raid_claims rc ON rt.id = rc.target
417 WHERE not launched  AND tick + 24 > tick()
418         AND uid = (SELECT uid FROM users WHERE username ILIKE $1);
419                 },undef,$nick);
420
421         if ($fleets > 0 && $points > 0){
422                 $c->reply("$nick has $fleets claimed waves the last 24 ticks that aren't marked as launched, so no points.");
423                 return;
424         }
425         addPoints($c, 'attack', $nick, $points);
426 }
427
428 sub d
429         : 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.)
430         : ACL(irc_d)
431         : Type(def)
432 {
433         my ($self,$c,$msg) = @_;
434         my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
435
436         addPoints($c, 'defense', $nick, $points);
437 }
438
439 sub s
440         : 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.)
441         : ACL(irc_s)
442 {
443         my ($self,$c,$msg) = @_;
444         my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
445
446         addPoints($c, 'scan', $nick, $points);
447 }
448
449 sub h
450         : 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.)
451         : ACL(irc_h)
452 {
453         my ($self,$c,$msg) = @_;
454         my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
455
456         addPoints($c, 'humor', $nick, $points);
457 }
458
459 sub addPoints {
460         my ($c,$type, $nick, $points) = @_;
461         $points //= 1;
462
463         my $dbh = $c->model;
464
465         if ($points*$points > 400){
466                 $c->reply("Values between -20 and 20 please");
467                 return;
468         }
469
470         my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
471         $f->execute($nick);
472         my $user = $f->fetchrow_hashref;
473         if ($f->rows == 1){
474                 $type .= "_points";
475                 $dbh->do(qq{UPDATE users SET $type = $type + ? WHERE uid = ?}
476                         ,undef,$points,$user->{uid});
477                 $c->reply("$user->{username} has been given $points $type");
478         }elsif ($f->rows == 0){
479                 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
480         }else{
481                 $c->reply("More than 1 user matched, please refine the search");
482         }
483         $f->finish;
484 }
485
486 1;