# Channel Logger by Urquart
print "${BGreen}Loading chlog.pl\n";

my $mcldir = "$ENV{HOME}/.mcl/logs/";
my $chlogtext = "";
my $chlogname = "";
my $greptext = "";
my $madechatlog = "";
my $makeamount = 0;
my $logname = "";
my $logtext = "";

makelogs();
cmd_chlogreset();

sub cmd_shlist($) { $makeamount = @_; readlog("shout"); }
sub cmd_shreset { logreset("shout"); }
sub cmd_qureset { logreset("quest"); }
sub cmd_qulist($) { $makeamount = @_; readlog("quest"); }
sub cmd_telist($) { $makeamount = @_; readlog("tell"); }
sub cmd_folist($) { $makeamount = @_; readlog("form"); }
sub cmd_tereset { logreset("tell"); }
sub cmd_foreset { logreset("form"); }
sub cmd_imlist($) { $makeamount = @_; readlog("imm"); }
sub cmd_cllist($) { $makeamount = @_; readlog("clan"); }
sub cmd_tolist($) { $makeamount = @_; readlog("town"); }
sub cmd_bllist($) { $makeamount = @_; readlog("bloodline"); }
sub cmd_salist($) { $makeamount = @_; readlog("say"); }
sub cmd_imreset { logreset("imm"); }
sub cmd_blreset { logreset("bloodline"); }
sub cmd_toreset { logreset("town"); }
sub cmd_clreset { logreset("clan"); }
sub cmd_sareset { logreset("say"); }

sub cmd_chsh($) { $makeamount = $_; makechatlog("shout"); mcl_chatall("$madechatlog"); }
sub cmd_chfo($) { $makeamount = $_; makechatlog("form"); mcl_chatall("$madechatlog"); }
sub cmd_chte($) { $makeamount = $_; makechatlog("tell"); mcl_chatall("$madechatlog"); }
sub cmd_chcl($) { $makeamount = $_; makechatlog("clan"); mcl_chatall("$madechatlog"); }
sub cmd_chto($) { $makeamount = $_; makechatlog("town"); mcl_chatall("$madechatlog"); }
sub cmd_chsa($) { $makeamount = $_; makechatlog("say"); mcl_chatall("$madechatlog"); }
sub cmd_chim($) { $makeamount = $_; makechatlog("imm"); mcl_chatall("$madechatlog"); }
sub cmd_chbl($) { $makeamount = $_; makechatlog("bloodline"); mcl_chatall("$madechatlog"); }
sub cmd_chqu($) { $makeamount = $_; makechatlog("quest"); mcl_chatall("$madechatlog"); }
sub cmd_chlogreset {
 logreset("tell");
 logreset("say");
 logreset("town");
 logreset("clan");
 logreset("form");
 logreset("quest");
 logreset("imm");
 logreset("bloodline");
 logreset("shout");
 makelogs();
}

sub makechatlog($) {
	$madechatlog = "%l%l";
	system("cat ${mcldir}@_.log >> ${mcldir}chlogtail.log");
	if ($makeamount > 0) {
	 system("tail -n $makeamount ${mcldir}@_.log >> ${mcldir}chlogtail.log");
	}
	open (READLOG, "${mcldir}chlogtail.log");	
	while ($readlog = <READLOG>)
	 {
	  chop($readlog);
	  ($logname, $logtext) = split("::::",$readlog);
	  $madechatlog = "$madechatlog${BBlack}[${BCyan}@_${BBlack}]${BRed} $logname${BYellow}:${BWhite}$logtext %l";
	 }
	close READLOG;
	system("/bin/rm ${mcldir}chlogtail.log");
	$makeamount = 0;
}	

sub cmd_chgrepcl { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("clan"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepbl { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("bloodline"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepim { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("imm"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepto { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("town"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepsa { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("say"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepfo { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("form"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepte { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("tell"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepsh { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("shout"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_chgrepqu { $makeamount = 10; $greptext = $_; system("/bin/rm ${mcldir}chloggrep.log"); greplog("quest"); makechatlog("chloggrep"); mcl_chatall("$madechatlog"); }
sub cmd_grepcl { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("clan"); readlog("chloggrep"); }
sub cmd_grepbl { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("bloodline"); readlog("chloggrep"); }
sub cmd_grepim { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("imm"); readlog("chloggrep"); }
sub cmd_grepto { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("town"); readlog("chloggrep"); }
sub cmd_grepsa { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("say"); readlog("chloggrep"); }
sub cmd_grepfo { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("form"); readlog("chloggrep"); }
sub cmd_grepte { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("tell"); readlog("chloggrep"); }
sub cmd_grepsh { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("shout"); readlog("chloggrep"); }
sub cmd_grepqu { system("/bin/rm ${mcldir}chloggrep.log"); $greptext = $_; greplog("quest"); readlog("chloggrep"); }

sub greplog($) { 
 system ("grep $greptext ${mcldir}@_.log >> ${mcldir}chloggrep.log"); 
 $greptext = ""; 
}

sub logreset($) { system ("/bin/rm ${mcldir}@_.log"); }

sub makelogs {
	open (CHLOG, ">> ${mcldir}clan.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}tell.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}quest.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}town.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}bloodline.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}shout.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}imm.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}say.log"); print CHLOG ""; close CHLOG;
	open (CHLOG, ">> ${mcldir}form.log"); print CHLOG ""; close CHLOG;
}	

sub startchlog($) {
	open (CHLOG, ">> ${mcldir}@_.log");
	print CHLOG "${chlogname}\:\:\:\: $chlogtext\n";
	close CHLOG;
	$chlogname = "";
	$chlogtext = "";
	system ("tail -n 20 ${mcldir}@_.log >> ${mcldir}@_.log2");
	system ("/bin/mv ${mcldir}@_.log2 ${mcldir}@_.log");
}

sub chlogtrig {
	if(/${BCyan}You feel (.*) say, '(.*)'\./) { $chlogname = "$1"; $chlogtext = "$2"; startchlog("clan"); }
	elsif(/${BCyan}\[TOWN\] (.*) \((\d*)\) says, '(.*)'\./) { $chlogname = "$1"; $chlogtext = "$3"; startchlog("town"); }
	elsif(/${BCyan}(\w*) says, '(.*)'\./)  { $chlogname = "$1"; $chlogtext = "$2"; startchlog("say"); }
	elsif(/${BCyan}(\w*) says to (\w*), '(.*)'\./) { $chlogname = "$1 to $2"; $chlogtext = "$3"; startchlog("say"); }
	elsif(/${BCyan}(\w*) tells the formation, '(.*)'\./)  { $chlogname = "$1"; $chlogtext = "$2"; startchlog("form"); }
	elsif(/${BCyan}(\w*):: (.*)/)  { $chlogname = "$1"; $chlogtext = "$2"; startchlog("imm"); }
	elsif(/You hear (\w*) shout, '(.*)'\./)  { $chlogname = "$1"; $chlogtext = "$2"; startchlog("shout"); }
	elsif(/(\w*) Quests, '(.*)'\./)  { $chlogname = "$1"; $chlogtext = "$2"; startchlog("quest"); }
	elsif(/${BRed}(\w*) telepaths you, '(.*)'\./)  { $chlogname = "$1"; $chlogtext = "$2"; startchlog("tell"); }
	elsif(/(.*)\[BLOODLINE\] (.*) says, '(.*)'\./) { $chlogname = "$2"; $chlogtext = "$3"; startchlog("bloodline"); }
	elsif(/${BCyan}(\d*)\% heard you say, '(.*)'\./) { $chlogname = "You"; $chlogtext = "$2"; startchlog("clan"); }
	elsif(/${BCyan}You say, '(.*)'\./) { $chlogname = "You"; $chlogtext = "$1"; startchlog("say"); }
	elsif(/${BMagenta}You shout, '(.*)'\./) { $chlogname = "You"; $chlogtext = "$1"; startchlog("shout"); }
	elsif(/${BRed}You telepath (.*), '(.*)'\./) { $chlogname = "You to $1"; $chlogtext = "$2"; startchlog("tell"); } 
}

sub readlog($) {
	$readlog = 1;
	$rloutput = "\n";
	if ($makeamount > 0) {
	 system("tail -n $makeamount ${mcldir}@_.log >> ${mcldir}chlogtail.log");
	 open (READLOG, "${mcldir}chlogtail.log");
	}
	else {	
	 open (READLOG, "${mcldir}@_.log");
	}
	while ($readlog = <READLOG>)
	 {
	  chop($readlog);
	  ($logname, $logtext) = split("::::",$readlog);
	  if (($logname) && ($logtext)) {
	   $rloutput = "${rloutput}${BBlack}[${BCyan}@_${BBlack}]${BRed} $logname${BYellow}:${BWhite}$logtext\n";
	  } 
	}
	if (length($rloutput) > 4096) {
	 print("\n${BBlack}[${BCyan}@_${BBlack}]${BRed} Log too large. You should reset log or list less.\n${White}");
	}
	else {
	 print($rloutput);
	}
	close READLOG;
}

output_add(\&chlogtrig);

1;