X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Usermgm.pm;h=e24ee550f195e1460b4ffd676856cccc57b507ae;hb=f442c3c5bb7d67583c875ffbb5aeecc137d6928c;hp=8939b801e9fc2beb29498b4689c154521da7e55d;hpb=3726b0b1cb45a00a301acea4daa19647d82489e2;p=NDIRC.git diff --git a/Usermgm.pm b/Usermgm.pm index 8939b80..e24ee55 100644 --- a/Usermgm.pm +++ b/Usermgm.pm @@ -16,42 +16,45 @@ # Free Software Foundation, Inc., * # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * #**************************************************************************/ -package ND::IRC::Usermgm; +package NDIRC::Usermgm; use strict; use warnings; use ND::DB; -use ND::IRC::Access; +use NDIRC::Access; require Exporter; our @ISA = qw/Exporter/; -our @EXPORT = qw/addUser whois flags flag laston addPoints chattrG setHost deactivateUser/; +our @EXPORT = qw/addUser whois flags flag laston addPoints chattrG setHost setPNick deactivateUser getShips/; sub addUser { - my ($nick,$host) = @_; - DB(); + my ($msg, $command) = @_; + + my ($nick,$pnick); + if(defined $msg && $msg =~ /^(\w+)(?: ([^.\s]+))?$/){ + $nick = $1; + $pnick = $2; + }else{ + $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"); + return; + } if (hc()){ - unless (defined $host){ - my @nicks = $ND::server->nicks_get_same($nick); - if (@nicks){ - $nicks[1]->{host} =~ /.*@(.*)/; - $host = $1; - }else{ - $host = "$nick.users.netgamers.org"; - } - } - my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE username ILIKE ? OR hostmask ILIKE ?",undef,$nick,$host); - if ((not defined $username) && $ND::DBH->do("INSERT INTO users (username,hostmask,password) VALUES(?,?,'')" - ,undef,$nick,$host)){ - $ND::server->command("msg $ND::target Added $ND::B$nick$ND::B with host: $ND::B$host$ND::B"); + $pnick = $nick unless $pnick; + my $host = "$pnick.users.netgamers.org"; + my ($username,$hostname,$p_nick) = $ND::DBH->selectrow_array(q{SELECT username, hostmask,pnick + FROM users WHERE username ILIKE ? OR hostmask ILIKE ? OR pnick ILIKE ?} + ,undef,$nick,$host,$pnick); + if ((not defined $username) && $ND::DBH->do("INSERT INTO users (username,hostmask,pnick,password) VALUES(?,?,?,'')" + ,undef,$nick,$host,$pnick)){ + $ND::server->command("msg $ND::target Added $ND::B$nick(/$pnick)$ND::B with host: $ND::B$host$ND::B"); }elsif(defined $username){ - $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B."); + $ND::server->command("msg $ND::target $ND::B$username ($p_nick)$ND::B already exists with host: $ND::B$hostname$ND::B."); }else{ - $ND::server->command("msg $ND::target Something went wrong when trying to add $ND::B$nick$ND::B with host: $ND::B$host$ND::B, most likely duplicate user name"); + $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); } }else{ - $ND::server->command("msg $ND::target Only HCs are allowed to check that"); + $ND::server->command("msg $ND::target Only HCs are allowed to add users"); } } sub whois { @@ -96,13 +99,13 @@ sub flag { if (officer()|| ($ND::target eq $ND::scanchan && $flag eq 'S')){ my $f = $ND::DBH->prepare(qq{ -SELECT TRIM(', ' FROM concat(username||', ')) FROM +SELECT TRIM(', ' FROM concat(username||', ')),count(username) FROM (SELECT uid, username FROM users ORDER BY username) u NATURAL JOIN groupmembers gm JOIN groups g ON g.gid = gm.gid WHERE flag = ?; }); - if (my ($users) = $ND::DBH->selectrow_array($f,undef,$flag)){ - $ND::server->command("msg $ND::target Users with flag $ND::B$flag$ND::B: $users"); + if (my ($users,$count) = $ND::DBH->selectrow_array($f,undef,$flag)){ + $ND::server->command("msg $ND::target $ND::B$count$ND::B Users with flag $ND::B$flag$ND::B: $users"); } }else{ $ND::server->command("msg $ND::target Only officers are allowed to check that"); @@ -119,16 +122,44 @@ sub laston { $min = 0 unless defined $min; $f->execute($flag,$min); my $text; + my $i = 0; while (my $user = $f->fetchrow_hashref){ $user->{last} = '?' unless defined $user->{last}; $text .= "$user->{username}($user->{last}) "; + $i++; } - $ND::server->command("msg $ND::target Users(days) with flag $ND::B$flag$ND::B: $text"); + $ND::server->command("msg $ND::target $ND::B$i$ND::B Users(days) with flag $ND::B$flag$ND::B: $text"); }else{ $ND::server->command("msg $ND::target Only officers are allowed to check that"); } } + +sub getShips { + my ($ship) = @_; + + if (officer() || dc()){ + my $f = $ND::DBH->prepare(qq{SELECT username,amount + FROM users u JOIN fleets f USING (uid) JOIN fleet_ships fs ON f.id = fs.fleet WHERE f.fleet = 0 AND ship ILIKE ? ORDER BY amount DESC + }); + $f->execute($ship); + my $text; + my $i = 0; + my $total = 0; + while (my $user = $f->fetchrow_hashref){ + $user->{last} = '?' unless defined $user->{last}; + $text .= "$user->{username}: $user->{amount} "; + $i++; + $total += $user->{amount}; + } + if ($text){ + $ND::server->command("notice $ND::nick $ND::B$i$ND::B Users with $ND::B$total $ship$ND::B: $text"); + }else{ + $ND::server->command("msg $ND::target $ND::B$i$ND::B Couldn't find any user with $ND::B$ship$ND::B:"); + } + } +} + sub addPoints { my ($t,$nick,$p) = @_; DB(); @@ -173,9 +204,17 @@ sub addPoints { } sub chattrG { - my ($nick, $flags) = @_; - DB(); - if (hc() || ($flags =~ /^(\+|-)?x$/ && $ND::address eq 'Assassin.users.netgamers.org')){ + my ($msg, $command) = @_; + + my ($nick, $flags); + if(defined $msg && $msg =~ /^(\S+) ((\+|-)?\w+)$/){ + $nick = $1; + $flags = $2; + }else{ + $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"); + return; + } + if (hc()){# || ($flags =~ /^(\+|-)?x$/ && $ND::address eq 'Assassin.users.netgamers.org')){ my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); $f->execute($nick); my @user = $f->fetchrow(); @@ -204,8 +243,16 @@ sub chattrG { } sub setHost { - my ($nick, $host) = @_; - DB(); + my ($msg, $command) = @_; + + my ($nick, $host); + if(defined $msg && $msg =~ /^(\S+) (\S+)$/){ + $nick = $1; + $host = $2; + }else{ + $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow"); + return; + } if (hc()){ my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); $f->execute($nick); @@ -228,9 +275,50 @@ sub setHost { } } +sub setPNick { + my ($msg, $command) = @_; + + my ($nick, $pnick); + if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){ + $nick = $1; + $pnick = $2; + }else{ + $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow"); + return; + } + if (hc()){ + my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); + $f->execute($nick); + my ($uid,$nick) = $f->fetchrow(); + if ($f->rows == 1){ + my ($username,$p_nick) = $ND::DBH->selectrow_array("SELECT username, pnick FROM users WHERE pnick ILIKE ? AND NOT (username ILIKE ?)",undef,$pnick,$nick); + my $hostname = "$pnick.users.netgamers.org"; + if ((not defined $username) && $ND::DBH->do("UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?",undef,$pnick,$hostname,$uid) > 0){ + $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"); + }elsif(defined $username){ + $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with pnick $ND::B$p_nick$ND::B."); + }else{ + $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host"); + } + }elsif ($f->rows == 0){ + $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard"); + }else{ + $ND::server->command("msg $ND::target More than 1 user matched, please refine the search"); + } + $f->finish; + } +} + sub deactivateUser { - my ($nick) = @_; - DB(); + my ($msg, $command) = @_; + + my $nick; + if(defined $msg && $msg =~ /^(\S+)$/){ + $nick = $1; + }else{ + $ND::server->command("notice $ND::nick syntax: .$command user"); + return; + } if (hc()){ my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?"); $f->execute($nick);