#**************************************************************************
#   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.         *
#**************************************************************************/

use strict;
use warnings;

use MooseX::Declare;
use NDIRC::Dispatcher;

command commands => {
	help => q(commands <command> | Gives help about all available commands or lists all commands available in the current channel),
}, class extends NDIRC::Command {
	method execute ($c,$command) {
		unless($command){
			my @commands;
			for (sort keys %{$c->disp->commands}){
				next unless $c->disp->has_command($_,$c->channel);
				my $acl = $c->disp->commands->{$_}->acl;
				next if $acl && !$c->check_user_roles($acl);

				push @commands,$_;
			}
			$c->reply(join ', ', @commands);
		}elsif (exists $c->disp->commands->{$command}){
			$c->reply($c->disp->commands->{$command}->help);
		}
	}
};

command help => {
	help => q(Prints basic help),
	type => 'help',
}, class extends NDIRC::Command {
	method execute ($c,$command) {

		$c->reply("Use .commands <command> to show help about a specific command. "
			. "Use .commands to list the diffenrent commands you have access to in this channel. "
			. "Instead of . you can use ! to get reply in pm or ~ to get reply in channel.");
	}
};

command say => {
	help => q(.say target message | sends message to target),
	type => q(pm),
	acl => q(irc_say),
}, class extends NDIRC::Command {
	method execute ($c,$msg) {
		my ($target,$message) = $msg =~ /^(\S+)\s+(.+)$/ or die 'ARGS';

		$c->message(privmsg => $target => $message );
	}
};

command cmd => {
	help => q(.cmd command args | run a given irc command),
	type => q(pm),
	acl  => q(irc_cmd),
}, class extends NDIRC::Command {
	method execute ($c,$msg) {

		$c->command(split /\s/, $msg);
	}
};

command anon => {
	help => q(syntax: .anon nick message),
	type => q(anon),
	acl => q(irc_anon),
}, class extends NDIRC::Command {
	method execute($c,$msg) {
		my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';

		$c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
			.$c->channel.")</c>");
		$c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
	}
};


1;

