#**************************************************************************
#   Copyright (C) 2006 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::Misc;
use strict;
use warnings;
use feature ':5.10';

require Exporter;

use NDIRC::Context;

our @ISA = qw/Exporter/;

our @EXPORT = qw/valuecolor parseCommand/;

sub valuecolor {
	my $s = $_;
	$s = $_[1] if $#_ >= 1;
	$s = "" unless defined $s;
	return chr(3)."5$s".chr(15) if $s eq 'Hostile';
	return chr(3)."3$s".chr(15) if $s eq 'Friendly';
	return chr(3)."3$s".chr(15) if $s eq 'Nap' or $s eq 'NAP';
	return chr(2)."$s".chr(15) if $_[0];
	return $s;
}

sub parseCommand {
	my ($msg, $server, $nick, $address, $channel, $disp,$model) = @_;

	return if $channel !~ /^#/ && $msg =~ /^~/;
	$msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;

	my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/) or return 0;

	return 0 unless $disp->has_command($command,$channel);

	my $reply_string;
	given ($p){
		when ('!'){
			$reply_string = "msg $nick";
		}
		when ('~'){
			$reply_string = "msg $channel";
		}
		default {
			$reply_string = "notice $nick";
		}
	}

	$address =~ s/.*@(.*)/$1/;
	my $c = NDIRC::Context->new({
			host => $address,
			nick => $nick,
			channel => $channel,
			disp => $disp,
			model => $model,
			server => $server,
			reply_string => $reply_string,
		});

	return $disp->run_command($c,$command,$args);
}

1;
