#**************************************************************************
#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
#                                                                         *
#   This program is free software; you can redistribute it and/or modify  *
#   it under the terms of the GNU General Public License as published by  *
#   the Free Software Foundation; either version 2 of the License, or     *
#   (at your option) any later version.                                   *
#                                                                         *
#   This program is distributed in the hope that it will be useful,       *
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#   GNU General Public License for more details.                          *
#                                                                         *
#   You should have received a copy of the GNU General Public License     *
#   along with this program; if not, write to the                         *
#   Free Software Foundation, Inc.,                                       *
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
#**************************************************************************/

package NDIRC::Commands::Usermgm;

use strict;
use warnings;
use feature ':5.10';

use Moose;
use MooseX::MethodAttributes;

sub adduser
	: 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)
	: Alias(+user)
	: ACL(irc_adduser)
{
	my ($self,$c,$msg) = @_;
	my ($nick,$pnick) = $msg =~ /^(\w+)(?: ([^.\s]+))?$/ or die 'ARGS';
	$pnick //= $nick;

	my $dbh = $c->model;

	my $host = "$pnick.users.netgamers.org";
	my ($username,$hostname,$p_nick) = $dbh->selectrow_array(q{
SELECT username, hostmask,pnick
FROM users WHERE username ILIKE ? OR hostmask ILIKE ? OR pnick ILIKE ?
		},undef,$nick,$host,$pnick);

	if (defined $username){
		$c->reply("<b>$username ($p_nick)</b> already exists with host: <b>$hostname</b>");
	}else{
		$dbh->do(q{
INSERT INTO users (username,hostmask,pnick,password) VALUES(?,?,?,'')
		},undef,$nick,$host,$pnick);
		$c->reply("Added <b>$nick(/$pnick)</b> with host: <b>$host</b>");
	}
}

sub deactivateuser
	: Help(syntax: .-user nick | nick must be alphanum characters, if no pnick is given then it will be set to nick)
	: Alias(-user)
	: ACL(irc_deactivateuser)
{
	my ($self,$c,$msg) = @_;

	my ($nick) = $msg =~ /^(\S+)$/ or die 'ARGS';

	my $dbh = $c->model;
	my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
	$f->execute($nick);
	my ($uid,$username) = $f->fetchrow();

	if ($f->rows == 1){
		my $updated = $dbh->do(q{
UPDATE users SET hostmask = ?, password = '' WHERE uid = ?
		},undef,$username,$uid);
		if ($updated > 0){
			my $groups = $dbh->do(q{DELETE FROM groupmembers WHERE uid = ?},undef,$uid);
			$groups += 0;
			$c->reply("<b>$username</b> has been deactivated. Removed from $groups groups.");
		}else{
			$c->reply("Something went wrong when trying to modify <b>$username</b>");
		}
	}elsif ($f->rows == 0){
		$c->reply("No hit, maybe spelling mistake, or add % as wildcard");
	}else{
		$c->reply("More than 1 user matched, please refine the search");
	}
	$f->finish;
}

sub chattr
	: 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)
	: ACL(irc_chattr)
{
	my ($self,$c,$msg) = @_;

	my ($nick, $flags) = $msg =~ /^(\S+) ((\+|-)?\w+)$/ or die 'ARGS';
	my $dbh = $c->model;

	my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
	$f->execute($nick);
	my $user = $f->fetchrow_hashref;
	if ($f->rows == 1){
		my $update;
		if ($flags =~ /^(-)/){
			$update = $dbh->prepare(q{
DELETE FROM groupmembers WHERE uid = $1 AND
	gid IN (SELECT gid FROM groups WHERE flag = ANY($2))
			});
		}else{
			$update = $dbh->prepare(q{
INSERT INTO groupmembers (uid,gid) (SELECT $1,gid FROM groups
	WHERE flag = ANY($2) AND gid NOT IN (SELECT gid FROM groupmembers WHERE uid = $1))
			});
		}
		my @flags = split /\W*/,$flags;
		$update->execute($user->{uid},\@flags);
		$update = $dbh->prepare(q{
SELECT concat(flag)
FROM (SELECT uid,flag FROM groupmembers NATURAL JOIN groups ORDER BY uid,flag ) g
WHERE uid = ?
		});
		$flags = $dbh->selectrow_array($update,undef,$user->{uid});
		$c->reply("Flags for <b>$user->{username}</b> are now: $flags");
	}elsif ($f->rows == 0){
		$c->reply("No hit, maybe spelling mistake, or add % as wildcard");
	}else{
		$c->reply("More than 1 user matched, please refine the search");
	}
	$f->finish;
}

1;
