# Script to highlight IRC logs of Konversation
# Author: Alan Haggai Alavi

#!/usr/bin/perl

use strict;
use warnings;

die( "Format:\t$0 inputfile outputfile" ) unless $#ARGV == 1 || $#ARGV == 2;

my %members;
my %members_messages;
my %members_actions;
my @colours = (
	'#e90e7f', '#8e55e9', '#18b33c', '#b39875', '#3176b3', '#d76e5c', '#de7008', '#febd0f', '#483030', '#c6d639', '#d9c952', '#f34607', '#6087d3', '#eda164', '#a24d4d',

);

open FH1, $ARGV[ 0 ];

# Get all the chat participants in the %member hash
while( <FH1>  ) {
	if( /^\[.+\] <(.+?)>/ ) {
		$members{ $1 } = undef;
		$members_messages{ $1 } ++;
	}
}

open FH2, '>', $ARGV[ 1 ] or die( "$ARGV[ 1 ] cannot be opened" );
chomp( my $date = `date -u` );

my $title = "$ARGV[ 2 ] - $date";

print FH2 '
<html>
	<head>
		<title>'. $title . '</title>
		<style>
			body {
				padding: 0px;
				margin: 0px;
				font-family: sans-serif;
				line-height: 1.5em;
			}

			#header {
				text-align: center;
				font-size: 20px;
				line-height: 3.8em;
				height: 80px;
				margin: 0px;
				background-color: #7ABDDA;
				color: #fff;
				font-weight: bold;
				border-bottom: 3px solid #3f6580;
			}

			#members {
				border: 1px dotted #333;
				padding: 10px;
				margin-bottom: 10px;
			}

			#statistics {
				margin: 10px;
			}
			
			#statistics h3 {
				font-size: 18px;
				font-weight: bold;
			}

			#footer {
				color: #fff;
				background-color: #121621;
				text-align: center;
			}

			#footer a {
				color: #fff;
				text-decoration: none;
			}

			#footer a:hover { text-decoration: underline; }
			
			#log {
				margin: 20px;
			}
		</style>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
	</head>
	<body>
		<div id="header">' . $title . '</div>
		<div id="log">
			<div id="members">Participants:<br />
';

{
	my $i = 0;
	foreach( keys %members ) {
		$i = 0 if $i == 15;
		$members{ $_ } = $colours[ $i ++ ];
		print FH2 "			<span style='color: $members{ $_ }; margin-left: 20px;'><b>$_</b></span>" unless /^Guest\d*/;
	}
}

print FH2 "			</div>";
seek FH1, 0, 0;
while( <FH1> ) {
	s/</&lt;/ && s/>/&gt;/;

	# Smileys
	s!:-?\)!<img src='smilies/icon_e_smile.gif' />!g;
	s!:-?D!<img src='smilies/icon_e_biggrin.gif' />!g;
	s!:-?\(!<img src='smilies/icon_e_sad.gif' />!g;
	s!;-?\(!<img src='smilies/icon_e_wink.gif' />!g;
	s!:-?O!<img src='smilies/icon_e_surprised.gif' />!g;
	s!:-?\?!<img src='smilies/icon_e_confused.gif' />!g;
	s!8-?\)!<img src='smilies/icon_cool.gif' />!g;
	s!:-?\){2,}!<img src='smilies/icon_lol.gif' />!g;
	s!(?:x|X)-?D!<img src='smilies/icon_lol.gif' />!g;
	s!:-?\|!<img src='smilies/icon_neutral.gif' />!g;

	if( /^\[.+\] (?:\[.+\]|\*\*\*|--&gt;|&lt;--)/ ) {
		# Gray out all server messages, joins, parts, and notices. Also emphasise the topic.
		s/\n/<br \/>/;
		s/channel topic is/channel topic is <span style='color: #000; font-weight: bold'>/ && s/$/<\/span>/;
		print FH2 "<span style='color: gray'>$_</span>";
	} elsif( /&lt;(.+?)&gt;/ ) {
		# Colour and bolden <name> and gray out [11:11:11]
		my $name = $1;
		my $colour = $members{ $name };
		if( exists $members{ $name } ) {
			s!&lt;$name&gt;!<span style='color: $colour; font-weight: bold'>&lt;$name&gt;</span>!;
			s!(\[.+\])!<span style='color: gray'>$1</span>!;
			print FH2 $_, "<br />";
		}
	} elsif( /^\[.+\] \* \b(.+?)\b/ ) {
		# Colour and bolden /me and gray out [11:11:11]
		my $name = $1;
		my $colour = $members{ $name };
		if( exists $members{ $name } ) {
			s!$name!<span style='color: $colour; font-weight: bold'>$name</span>!;
			s!(\[.+\])!<span style='color: gray'>$1</span>!;
			print FH2 $_, "<br />";
		}
	}
}

close FH1;

print FH2 '
		</div>
		<div id="statistics">
			<h3>Statistics</h3>
			Top posts:
';

{
	my $i = 0;
	foreach( sort { $members_messages{ $b } <=> $members_messages{ $a } or "\L$a" cmp "\L$b" } ( keys( %members_messages ) ) ) {
		$i = 0 if $i == 15;
		$members{ $_ } = $colours[ $i ++ ];
		print FH2 "			<span style='color: $members{ $_ }; margin-left: 20px;'><b>$_</b></span> ($members_messages{ $_ })" unless /^Guest\d*/;
	}
}

print FH2 '
		</div>
		<div id="footer">
			<a href="http://forum.bordercollieuk.co.uk" title="Border Collie UK Forum">Team BCUK</a>
		</div>
	</body>
</html>
';
close FH2;

1;
