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 #**************************************************************************/
25 use NDIRC::Dispatcher;
28 help => q(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 }, class extends NDIRC::Command {
32 method execute ($c,$msg) {
33 my ($nick,$pnick) = $msg =~ /^(\w+)(?: ([^.\s]+))?$/ or die 'ARGS';
38 my $host = "$pnick.users.netgamers.org";
39 my ($username,$hostname,$p_nick) = $dbh->selectrow_array(q{
40 SELECT username, hostmask,pnick
41 FROM users WHERE username = $1 OR hostmask = $2 OR pnick = $3
42 },undef,$nick,$host,$pnick);
44 if (defined $username){
45 $c->reply("<b>$username ($p_nick)</b> already exists with host: <b>$hostname</b>");
48 INSERT INTO users (username,hostmask,pnick) VALUES($1,$2,$3)
49 },undef,$nick,$host,$pnick);
50 $c->reply("Added <b>$nick(/$pnick)</b> with host: <b>$host</b>");
56 help => q(syntax: .-user nick | nick must be alphanum characters, if no pnick is given then it will be set to nick),
58 acl => 'irc_deactivateuser'
59 }, class extends NDIRC::Command {
60 method execute ($c,$msg) {
61 my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
64 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
66 my ($uid,$username) = $f->fetchrow();
69 my $updated = $dbh->do(q{
70 UPDATE users SET hostmask = $1, password = NULL WHERE uid = $2
71 },undef,$username,$uid);
73 my $groups = $dbh->do(q{DELETE FROM groupmembers WHERE uid = ?},undef,$uid);
75 $c->reply("<b>$username</b> has been deactivated. Removed from $groups groups.");
77 $c->reply("Something went wrong when trying to modify <b>$username</b>");
79 }elsif ($f->rows == 0){
80 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
82 $c->reply("More than 1 user matched, please refine the search");
89 help => q(syntax: .chattr username [-]flags | % can be used for wildcards \%arro% will match barrow, if a - is given then flags will be removed, otherwise added),
92 }, class extends NDIRC::Command {
93 method execute ($c,$msg) {
94 my ($nick, $mode, $flags) = $msg =~ /^(\S+) (\+|-)?(\w+)$/ or die 'ARGS';
97 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
99 my $user = $f->fetchrow_hashref;
103 $update = $dbh->prepare(q{
104 DELETE FROM groupmembers WHERE uid = $1 AND gid = ANY($2)
107 $update = $dbh->prepare(q{
108 INSERT INTO groupmembers (uid,gid)
109 (SELECT $1,gid FROM unnest($2::text[]) gid WHERE
110 gid NOT IN (SELECT gid FROM groupmembers WHERE uid = $1)
111 AND gid IN (SELECT gid FROM groups))
114 my @flags = split /\W*/,$flags;
115 $update->execute($user->{uid},\@flags);
116 $update = $dbh->prepare(q{
117 SELECT array_to_string(array_agg(gid),'')
118 FROM (SELECT uid,gid FROM groupmembers ORDER BY uid,gid ) g
121 $flags = $dbh->selectrow_array($update,undef,$user->{uid});
122 $c->reply("Flags for <b>$user->{username}</b> are now: $flags");
123 }elsif ($f->rows == 0){
124 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
126 $c->reply("More than 1 user matched, please refine the search");
133 help => q(Lists all the available flags),
135 }, class extends NDIRC::Command {
136 method execute ($c,$msg) {
138 my ($flags) = $dbh->selectrow_array(q{
139 SELECT array_to_string(array_agg(gid||':'||groupname),', ') FROM groups
141 $c->reply("Current flags: $flags");
146 help => q(syntax: .whois username | % can be used for wildcards \%arro% will match barrow.),
148 }, class extends NDIRC::Command {
149 method execute ($c,$msg) {
150 my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
154 my $f = $dbh->prepare(q{
155 SELECT username, pnick, hostmask, array_to_string(array_agg(gid),'') AS flags
157 LEFT OUTER JOIN (SELECT uid,gid FROM groupmembers ORDER BY uid,gid
159 WHERE username ILIKE ?
160 GROUP BY username,pnick,hostmask LIMIT 5
163 while (my $user = $f->fetchrow_hashref){
164 $c->reply("<b>$user->{username} (/$user->{pnick})</b> flags: ($user->{flags}) host: $user->{hostmask}");
167 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
173 help => q(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),
175 }, class extends NDIRC::Command {
176 method execute ($c,$msg) {
177 my ($flag,$noflag) = $msg =~ /^(\w)(?: -(\w+))?$/ or die 'ARGS';
180 my $f = $c->model->prepare(q{
181 SELECT array_to_string(array_agg(username),', '),count(username)
182 FROM (SELECT uid, username FROM users ORDER BY username) u
183 JOIN groupmembers gm USING (uid)
184 WHERE gid = $1 AND uid NOT IN (SELECT uid FROM groupmembers WHERE gid = ANY($2))
186 my @noflag = split //, $noflag;
187 my ($users,$count) = $c->model->selectrow_array($f,undef,$flag,\@noflag);
188 $c->reply("<b>$count</b> Users with flag <b>$flag</b>: $users");
193 help => q(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),
195 }, class extends NDIRC::Command {
196 method execute ($c,$msg) {
197 my ($flag,$min,$forum,$claim) = $msg =~ /^(\w)(?: (\d+)(?:\|(\d+)\|(\d+))?)?$/
201 my $f = $c->model->prepare(q{
202 SELECT username, COALESCE(last::text,'?') AS last
203 ,COALESCE(lastforum::text,'?') AS lastforum
204 ,COALESCE(lastclaim::text,'?') AS lastclaim
205 FROM (SELECT username
206 ,date_part('day',now() - laston)::int AS last
207 ,date_part('day',now() -
208 (SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid)) AS lastforum
209 ,date_part('day',now() -
210 (SELECT max(timestamp) FROM raid_claims WHERE uid = u.uid)) AS lastclaim
212 NATURAL JOIN groupmembers
215 WHERE COALESCE(last >= $2,TRUE) AND COALESCE(lastforum >= $3,TRUE)
216 AND COALESCE(lastclaim >= $4,TRUE)
217 ORDER BY a.last DESC, a.lastforum DESC, a.lastclaim DESC
219 $f->execute($flag,$min,$forum,$claim);
223 while (my $user = $f->fetchrow_hashref){
224 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
227 $c->reply("<b>$i</b> Users(days) with flag <b>$flag</b>: $text");
232 command lastseen => {
233 help => q(syntax: .lastseen username | Shows the number of days since the user(s) last visited irc, forum and raids),
234 acl => 'irc_lastseen'
235 }, class extends NDIRC::Command {
236 method execute ($c,$msg) {
237 my ($username) = $msg =~ /^(\S+)$/ or die 'ARGS';
239 my $f = $c->model->prepare(q{
240 SELECT username, COALESCE(date_part('day',now() - laston)::text,'?') AS last
241 ,COALESCE(date_part('day',now() - (SELECT max(time)
242 FROM forum_thread_visits WHERE uid = u.uid))::text,'?') AS lastforum
243 ,COALESCE(date_part('day',now() - (SELECT max(timestamp)
244 FROM raid_claims WHERE uid = u.uid))::text,'?') AS lastclaim
246 WHERE username ILIKE $1 ORDER BY username
248 $f->execute($username);
252 while (my $user = $f->fetchrow_hashref){
253 $text .= "$user->{username}($user->{last}|$user->{lastforum}|$user->{lastclaim}) ";
256 $c->reply("<b>$i</b> Users(days): $text");
260 command getships => {
261 help => q(Usage: .getships ship | % can be used as wildcard, e.g. beet%, shipshome shows the number of ships currently home),
262 alias => 'shipshome',
263 acl => 'irc_getships'
264 }, class extends NDIRC::Command {
265 method execute ($c,$msg) {
266 my ($ship) = $msg =~ /^(\S+)$/ or die 'ARGS';
269 my $f = $dbh->prepare(q{
270 SELECT username,SUM(fs.amount) AS amount
272 JOIN (SELECT DISTINCT ON (pid) pid,fid FROM fleets
273 WHERE mission = 'Full fleet' AND name <> 'Unit'
274 ORDER BY pid,tick DESC,fid DESC
276 JOIN fleet_ships fs USING (fid)
277 WHERE ship ILIKE $1 AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
278 GROUP BY username ORDER BY amount DESC
280 if ($self->name eq 'shipshome'){
281 $f = $dbh->prepare(q{
282 SELECT username,SUM(amount) AS amount
284 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
285 GROUP BY username ORDER BY amount DESC
292 while (my $user = $f->fetchrow_hashref){
293 $text .= "$user->{username}: $user->{amount} ";
295 $total += $user->{amount};
298 $c->reply("<b>$i</b> Users with <b>$total $ship</b>: $text");
300 $c->reply("Couldn't find any user with <b>$ship</b>");
305 command getfleet => {
306 help => q(Usage: .getfleet username | % can be used as wildcard, e.g. barr%),
307 acl => q(irc_getfleet)
308 }, class extends NDIRC::Command {
309 method execute ($c,$msg) {
310 my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';
313 my $f = $dbh->prepare(q{
314 SELECT fs.ship, fs.amount, username
316 JOIN (SELECT fid,username
318 JOIN users u USING (pid)
319 WHERE mission = 'Full fleet' AND name <> 'Unit'
320 AND username ILIKE $1
321 ORDER BY pid,tick DESC,fid DESC
329 while (my $ship = $f->fetchrow_hashref){
330 unless (defined $username) {
331 $username = $ship->{username};
332 $text = "<b>$username</b> has: "
334 $text .= "$ship->{ship} $ship->{amount} ";
339 $c->reply("Couldn't find any fleet for $nick");
345 help => q(Usage: .sethost username [host] | if host isn't given then it resets to netgamers host),
346 acl => q(irc_sethost)
347 }, class extends NDIRC::Command {
348 method execute ($c,$msg) {
349 my ($nick,$host) = $msg =~ /^(\S+)(?: (\S+))?$/ or die 'ARGS';
352 my $f = $dbh->prepare(q{
353 SELECT uid,username,pnick,hostmask FROM users WHERE username ILIKE ?
356 my $user = $f->fetchrow_hashref;
358 $host //= "$user->{pnick}.users.netgamers.org";
360 $dbh->do(q{UPDATE users SET hostmask = ? WHERE uid = ?}
361 ,undef,$host,$user->{uid});
362 $c->reply("Updated <b>$user->{username}</b>'s host to: <b>$host</b>");
365 if ($@ =~ /duplicate key value violates unique constraint/){
366 my ($username, $hostname) = $dbh->selectrow_array(q{
367 SELECT username,hostmask FROM users WHERE hostmask = $1
369 $c->reply("<c04>Problem</c>, <b>$username</b> already uses host <b>$hostname</b>.");
374 }elsif ($f->rows == 0){
375 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
377 $c->reply("More than 1 user matched, please refine the search");
383 command setpnick => {
384 help => q(Usage: .setpnick username pnick | Changes a user's pnick),
385 acl => q(irc_setpnick)
386 }, class extends NDIRC::Command {
387 method execute ($c,$msg) {
388 my ($nick,$pnick) = $msg =~ /^(\S+) (\S+)$/ or die 'ARGS';
391 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
393 my $user = $f->fetchrow_hashref;
395 my $hostname = "$pnick.users.netgamers.org";
397 $dbh->do(q{UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?}
398 ,undef,$pnick,$hostname,$user->{uid});
399 $c->reply("Updated <b>$user->{username}</b>'s pnick to: <b>$pnick</b> and hostname to <b>$hostname</b>");
402 if ($@ =~ /duplicate key value violates unique constraint/){
403 my ($username, $hostname, $pnick) = $dbh->selectrow_array(q{
404 SELECT username,hostmask,pnick FROM users WHERE hostmask = $1 OR pnick = $2
405 },undef,$hostname, $pnick);
406 $c->reply("<c04>Problem</c>, <b>$username</b> already uses host <b>$hostname</b> and pnick <b>$pnick</b>.");
411 }elsif ($f->rows == 0){
412 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
414 $c->reply("More than 1 user matched, please refine the search");
420 command setdiscordid => {
421 help => q(Usage: .setdiscordid username discordid | Changes a user's discord id/tag Nick#id),
422 acl => q(bot_setdiscordid)
423 }, class extends NDIRC::Command {
424 method execute ($c,$msg) {
425 my ($nick,$discordid) = $msg =~ /^(\S+) (\S+)$/ or die 'ARGS';
428 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
430 my $user = $f->fetchrow_hashref;
433 $dbh->do(q{UPDATE users SET discord_id = NULLIF($1, 'NULL') WHERE uid = $2}
434 ,undef,$discordid,$user->{uid});
435 $c->reply("Updated <b>$user->{username}</b>'s discord id to: <b>$discordid</b>");
438 if ($@ =~ /duplicate key value violates unique constraint/){
439 my ($username, $discordid) = $dbh->selectrow_array(q{
440 SELECT username,discord_id FROM users WHERE discord_id = $1
442 $c->reply("<c04>Problem</c>, <b>$username</b> already uses discord id <b>$discordid</b>.");
447 }elsif ($f->rows == 0){
448 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
450 $c->reply("More than 1 user matched, please refine the search");
456 my $points = class extends NDIRC::Command {
462 method execute ($c,$msg) {
463 my ($nick,$points) = $msg =~ /^(\S+)(?: (-?(:?\d+|\d*\.\d+)))?$/ or die 'ARGS';
467 return unless $self->check($c,$nick,$points);
471 if ($points*$points > 400){
472 $c->reply("Values between -20 and 20 please");
476 my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
478 my $user = $f->fetchrow_hashref;
480 my $type .= $self->point . "_points";
481 $dbh->do(qq{UPDATE users SET $type = $type + ? WHERE uid = ?}
482 ,undef,$points,$user->{uid});
483 $c->reply("$user->{username} has been given $points $type");
484 }elsif ($f->rows == 0){
485 $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
487 $c->reply("More than 1 user matched, please refine the search");
491 method check ($c,$nick,$points) {
497 help => q(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.),
501 extends $points->name;
502 method check ($c,$nick,$points) {
503 my ($fleets) = $c->model->selectrow_array(q{
504 SELECT count(*) FROM raids r
505 JOIN raid_targets rt ON r.id = rt.raid
506 JOIN raid_claims rc ON rt.id = rc.target
507 WHERE not launched AND tick + 24 > tick()
508 AND uid = (SELECT uid FROM users WHERE username ILIKE $1);
511 if ($fleets > 0 && $points > 0){
512 $c->reply("$nick has $fleets claimed waves the last 24 ticks that aren't marked as launched, so no points.");
520 help => q(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.),
527 help => q(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.),
533 help => q(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.),
539 help => q(Usage: .getanti class [eta] | class is the target class, like fi or bs, eta is optional max eta for the ships.),
540 acl => q(irc_getships),
541 }, class extends NDIRC::Command {
542 method execute ($c,$msg) {
543 my ($class,$eta) = $msg =~ /^(\S+)(?: (\d+))?$/ or die 'ARGS';
548 my @classes = qw/Fi Co Fr De Cr Bs/;
550 if (lc $class eq lc $_){
562 my $f = $dbh->prepare(q{
563 SELECT username, ship, amount, CASE WHEN $1 = t1 THEN 't1' ELSE 't2' END AS t
564 FROM available_ships a
565 JOIN ship_stats s USING (ship)
566 WHERE uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
569 ORDER BY amount*(metal+crystal+eonium)*CASE WHEN $1 = t1 THEN 1.0 ELSE 0.6 END DESC
571 $f->execute($class,\@classes);
575 while (my $user = $f->fetchrow_hashref){
576 $text .= "($user->{username}: $user->{ship} $user->{amount} $user->{t}) ";
578 $total += $user->{amount};
581 $c->reply("<b>$i</b> fleets with anti-<b>$class</b>: $text");
583 $c->reply("Couldn't find any user with anti-<b>$class</b>");
589 help => q(Gives new users a random password.),
591 }, class extends NDIRC::Command {
593 ###########################################################
594 # Written by Guy Malachi http://guymal.com
596 ###########################################################
597 sub generate_random_string
599 my $length_of_randomstring=shift;# the length of
600 # the random string to generate
602 my @chars=('a'..'z','A'..'Z','0'..'9','_');
604 foreach (1..$length_of_randomstring)
606 # rand @chars will generate a random
607 # number between 0 and scalar @chars
608 $random_string .= $chars[rand @chars];
610 return $random_string;
613 method execute ($c,$msg) {
616 my $password = generate_random_string 16;
617 my $update = $dbh->do(q{
618 UPDATE users SET password = $1
619 WHERE uid = $2 AND password IS NULL
620 },undef,$password,$c->uid);
622 $c->reply("Password set to: $password (you can change it on webbie)");
624 $c->reply("Couldn't set password. Either it has already been set or you don't have an account");