# Log communications to a file which you need to set in $LogFile below
# I use this incase im spammed I don't have to scrollback, and it makes
# My desktop look more active because I open an eterm and tail -f it
# Send comments/suggestions to jarmour@linuxfreak.com
# - Josh
# Set this variable to your name
$CLmyName = "Mordegast";
$LogFile = "/home/estiann/.mcl/communication.log";
$CommLog = 0;
$commInit = 0;
sub logcomm
{
	@MyTime = localtime(time);
	$Hour = $MyTime[2];
	$AMPM="am";
	$Month = $MyTime[4]+1;
	$Year = 1900+$MyTime[5];
	if($Hour > 12)
	{
		$Hour-=12;
		$AMPM="pm";
	}
	if($CommLog)
	{
		print COMMLOG "\e[1;30m\[\e[1;34m$Month\e[0;36m\/\e[1;34m$MyTime[3]\e[0;36m\/\e[1;34m$Year $Hour\e[0;36m:\e[1;34m$MyTime[1]$AMPM\e[1;30m\]\e[0m $_[0]: $_[1] - $_[2]\n";
	}
}

sub commlogon
{
	if($CommLog == 0)
	{
		if(!$commInit) {
		print "\nOpening $LogFile\n"; }
		open(COMMLOG, '>> '.$LogFile)
			or print "Can't open $LogFile";
		select((select(COMMLOG),$| = 1)[0]);
		output_add(\&clogtrig);
	}
	$CommLog = 1;
	if(!$commInit) {
	print "Communication log is now on\n"; }
}

sub commlogoff
{
	if($CommLog)
	{
		close(COMMLOG);
		output_remove(\&clogtrig);
	}
	$CommLog = 0;
	print "Communication log is now off\n";
}

sub cmd_commlog
{
	if($_ eq "on")
	{
		commlogon;
		return;
	}
	elsif($_ eq "off")
	{
		commlogoff;
		return;
	}
	else
	{
		print "\nMedievia Communications Logger\n";
		print "--------------------------------------\n";
		print "Usage: commlog [on|off]\n";
		print "Use ${BWhite}setlogfile${White} to change the log file\n";
		print "Current log file: ${BYellow}${LogFile}${White}\n";
	}
	print "The communication log is currently ";
	if($CommLog)
	{
		print "on";
	}	
	else
	{
		print "off";
	}
	print "\n--------------------------------------\n";
}

sub clogtrig
{
	if(/${BCyan}You feel (.*) say, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'CLAN'.chr(27)."[0m",$1,$2);
		logcomm @Communication;
	}
	elsif(/${BCyan}(\d*)\% heard you say, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'CLAN'.chr(27)."[0m",$CLmyName,$2);
		logcomm @Communication;
	}
	elsif(/${BCyan}\[TOWN\] (.*) \((\d*)\) says, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'TOWN'.chr(27)."[0m",$1,$3);
		logcomm @Communication;
	}
	elsif(/${BCyan}(\w*) says, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'SAY'.chr(27)."[0m",$1,$2);
		logcomm @Communication;
	}
	elsif(/${BCyan}You say, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'SAY'.chr(27)."[0m",$CLmyName,$1);
		logcomm @Communication;
	}
	elsif(/${BCyan}(\w*) says to (\w*), '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'SAY'.chr(27)."[0m",$1." to ".$2,$3);
		logcomm @Communication;
	}
	elsif(/${BCyan}(\w*) tells the formation, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;36m".'FORM'.chr(27)."[0m",$1,$2);
		logcomm @Communication;
	}
	elsif(/${BCyan}(\w*):: (.*)/)
	{
		@Communication=(chr(27)."[1;36m".'IMM'.chr(27)."[0m",$1,$2);	
		logcomm @Communication;
	}
	elsif(/${BMagenta}You shout, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;35m".'SHOUT'.chr(27)."[0m","You",$1);
		logcomm @Communication;
	}
	elsif(/You hear (\w*) shout, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;35m".'SHOUT'.chr(27)."[0m",$1,$2);
		logcomm @Communication;
	}
	elsif(/(\w*) Quests, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;35m".'QUEST'.chr(27)."[0m",$1,$2);
		logcomm @Communication;
	}
	elsif(/${BRed}You telepath (.*), '(.*)'\./)
	{
		@Communication=(chr(27)."[1;31m".'TELL'.chr(27)."[0m","to ".$1,$2);
		logcomm @Communication;
	}
	elsif(/${BRed}(\w*) telepaths you, '(.*)'\./)
	{
		@Communication=(chr(27)."[1;31m".'TELL'.chr(27)."[0m",$1,$2);
		logcomm @Communication;
	}
# My Chat
$ChatString = $BBlack.chr(254).$White.chr(254).$BWhite.chr(254). " ${Cyan}<${BBlue}";
	if(/$ChatString(.*)${Cyan}> ${White}(.*)/)
	{
		my $ChatPerson = $1;
		my $ChatMsg = $2;
		my $TmpCharOne = chr(0);
		my $TmpCharTwo = chr(16);
		$ChatPerson =~ s/[$TmpCharOne-$TmpCharTwo|\xEA]//g;
		$ChatMsg =~ s/[$TmpCharOne-$TmpCharTwo|\xEA]//g;
		@Communication=(chr(27)."[1;31m".'CHAT'.chr(27)."[0m",$ChatPerson,$ChatMsg);
		logcomm @Communication;
	}
}

sub cmd_setlogfile
{
	if($_ eq "")
	{
		print "Usage: setlogfile [file]\n";
		return;
	}
	else
	{
		$LogFile = $_;
		print "Log file set to $LogFile\n";
	}
}

sub clogcleanup
{
	local (*FH) = $_[0];
	print FH "commfile $LogFile\n";
	if($CommLog)
	{
		close(COMMLOG);
	}
}

sub loadcommlog
{
	$LogFile = $_;
}

save_add(\&clogcleanup);
load_add("commfile",\&loadcommlog);

if($CommLog == 1)
{
	$commInit = 1;
	$CommLog = 0;
	commlogon;
	$commInit = 0;
}
1;
