#**************************************************************************
#   Copyright (C) 2008 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::Channel;

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

use Moose;
use MooseX::MethodAttributes;

sub op
	: Alias(qw/deop voice devoice/)
	: Help(syntax: .op [nicks] | Gives op to the specified nicks, or yourself if no command is given)
	: Type(channel)
{
	my ($self,$c,$msg) = @_;

	my ($access) = $self->name =~ /(op|voice)/;

	my $where = "";
	if ($msg =~ /^\s*$/){
		$msg = $c->nick;
		$where = "OR f.name = 'auto_$access'";
	}

	my $mode = qq{
SELECT DISTINCT c.name FROM users u
	JOIN groupmembers g ON g.uid = u.uid
	JOIN channel_group_flags gf ON g.gid = gf.group
	JOIN channels c ON gf.channel = c.id
	JOIN channel_flags f ON f.id = gf.flag
WHERE u.hostmask ILIKE ? AND c.name = ? AND (f.name = '$access' $where);
		};
	if ($c->check_user_roles(qw/irc_masterop/)){
		$mode = 1;
	}else{
		($mode) = $c->model->selectrow_array($mode,undef,$c->host,$c->channel);
	}
	if ($mode){
		$c->server->command($self->name . " " . $c->channel . " $msg");
	}else{
		$c->reply("No access to " . $self->name . " in this channel");
	}
}

sub invite
	:Help(Syntax: invite [channel] | If no channel is specified it invites you to all channel you have auto invite access on)
	: Type(pm)
{
	my ($self,$c,$msg) = @_;

	my ($channel) = $msg =~ /^\s*(\S+)?\s*$/ or die 'ARGS';

	my @channels;
	if ($channel && $c->check_user_roles('irc_masterinvite')){
		push @channels,$channel;
	}else{
		my @access = ('auto_invite');
		push @access, 'invite' if $channel;
		my $channels = $c->model->prepare(q{
SELECT DISTINCT c.name FROM users u
	JOIN groupmembers g ON g.uid = u.uid
	JOIN channel_group_flags gf ON g.gid = gf.group
	JOIN channels c ON gf.channel = c.id
	JOIN channel_flags f ON f.id = gf.flag
WHERE u.hostmask ILIKE $1 AND COALESCE(c.name = $2,TRUE)
	AND (f.name = ANY($3) )
		});
		$channels->execute($c->host,$channel,\@access);
		while (my ($channel) = $channels->fetchrow()){
			push @channels,$channel;
		}
	}
	for (@channels){
		$c->server->command("invite ". $c->nick ." $_");
	}
}

sub hostname
	: Help(Shows your hostname, as seen by the bots.)
{
	my ($self,$c,$msg) = @_;

	$c->reply('Your hostname is: '.$c->host);
}

###########################################################
# Written by Guy Malachi http://guymal.com
# 18 August, 2002
###########################################################
sub generate_random_string
{
	my $length_of_randomstring=shift;# the length of
			 # the random string to generate

	my @chars=('a'..'z','A'..'Z','0'..'9','_');
	my $random_string;
	foreach (1..$length_of_randomstring)
	{
		# rand @chars will generate a random
		# number between 0 and scalar @chars
		$random_string .= $chars[rand @chars];
	}
	return $random_string;
}

sub getpass
	: Help(Gives new users a random password.)
	: Type(pm)
{
	my ($self,$c,$msg) = @_;
	my $dbh = $c->model;

	my $password = generate_random_string 10;
	my $update = $dbh->do(q{
UPDATE users SET password = MD5( ? )
WHERE hostmask ILIKE ? AND password =''
		},undef,$password,$c->host);
	if ($update > 0){
		$c->reply("Password set to: $password (you can change it on webbie)");
	}else{
		$c->reply("Couldn't set password. Either it has already been set or you don't have an account");
	}
}

1;
