#!/usr/bin/perl

use CGI;
use CGI::Carp 'fatalsToBrowser';
use DateTime;
use Data::UUID;
use DBI;
use Digest::SHA1 qw[sha1_hex];
use LWP::Simple;
use RDF::TrineShortcuts;

my $cgi = CGI->new;

my $dbh   = DBI->connect('dbi:Pg:dbname=mttlbot2', 'tai')
	or die "Could not connect to database.";
my $store = RDF::Trine::Store::DBI::Pg->new('mttlbot2', $dbh)
	or die "Could not instantiate store.";
my $model = RDF::Trine::Model->new($store)
	or die "Could not instantiate model.";

my $message;
if ($cgi->param('nick') && $cgi->param('pwd') && $cgi->param('msg'))
{
	my $check_pwd = sprintf('ASK WHERE {
		GRAPH <%s> { <%s> <http://buzzword.org.uk/2010/mttlbot2/vocab#password_hash> "%s" . }
	}',
	'http://buzzword.org.uk/2010/mttlbot2/graph/passwords',
	'irc://irc.freenode.net/'.$cgi->param('nick').',isnick',
	Digest::SHA1::sha1_hex($cgi->param('pwd')),
	);
	
	die "Password didn't match"
		unless rdf_query($check_pwd, $model);
	
	my $relay_uri  = 'urn:uuid:'.Data::UUID->new->create_str;
	my $relay_data = {
		'http://buzzword.org.uk/2010/mttlbot2/vocab#from' => [
			{ value => 'irc://irc.freenode.net/'.$cgi->param('nick').',isnick', type=>'uri' },
			],
		'http://buzzword.org.uk/2010/mttlbot2/vocab#to' => [
			{ value => 'irc://irc.freenode.net/swig', type=>'uri' },
			],
		'http://buzzword.org.uk/2010/mttlbot2/vocab#content' => [
			{ value => $cgi->param('msg'), type=>'literal' },
			],
		'http://buzzword.org.uk/2010/mttlbot2/vocab#date' => [
			{ value => DateTime->now->iso8601, type=>'literal' },
			],
		};
	
	$model->add_hashref(
		{ $relay_data => $relay_data },
		RDF::Trine::Node::Resource->new('http://buzzword.org.uk/2010/mttlbot2/graph/relaying'));
		
	$message = "<p><b>Relayed as &lt;$relay_uri&gt;.</b></p>\n";
}

my $data = get(sprintf('http://chatlogs.planetrdf.com/swig/%s.txt', DateTime->now->ymd('-')));
my @data = split /\n/, $data;

my $html;
my $count;
while (my $line = pop @data)
{
	next if $line =~ /^\d\d:\d\d:\d\d <(.+)> \g{-1} (has quit|has joined|is now known as|has left)/;
	$count++;
	
	last if $count > 20;
	
	my ($time, $who, $message) = ( $line =~ m/^(\d\d:\d\d:\d\d) <([^>]+)> (.+)$/ );
	my $colour = {
		0  => 'red',
		1  => 'orange',
		2  => 'lime',
		3  => 'green',
		4  => 'cyan',
		5  => 'blue',
		6  => '#9900CC',
		7  => 'magenta',
		8  => 'black',
		9  => '#666666',
		a  => '#AAAAAA',
		b  => '#660000',
		c  => '#006600',
		d  => '#000066',
		e  => '#660066',
		f  => '#006666',
		}->{lc substr(sha1_hex($who),0,1) };

	my $line_html = sprintf("<font color=\"%s\"><b>%s:</b> %s <small><i>[%s]</i></small></font><br/>\n",
		$colour,
		$cgi->escapeHTML($who),
		$cgi->escapeHTML($message),
		$cgi->escapeHTML($time));

	$html = $line_html . $html;
}

$html = "<title>#swig Lite</title>\n${message}\n${html}\n";

$html .= <<'FORM';
<hr>
<form action="" method="post">
<label for="nick">nick</label><br>
<input id="nick" name="nick" value="tobyink"><br>
<label for="pwd">pwd</label><br>
<input id="pwd" name="pwd"><br>
<label for="msg">msg</label><br>
<input id="msg" name="msg"><br>
<input type="submit">
<p>The password above is NOT your NickServ password, but your mttlbot password.</p>
</form>
FORM

print $cgi->header('text/html;charset=utf-8'), $html;
