use strict;
use vars qw($VERSION %IRSSI);

use Irssi;
$VERSION = '1.00';
%IRSSI = (
    authors     => 'tobbez',
    contact     => 'tobbez@ryara.net',
    name        => 'Last.fm now playing',
    description => 'Fethes the currently playing track and prints it to the current channel.',
    license     => 'MIT',
);

my $lastfmuser = "USERNAME";

sub getNp {

	my ($args, $server, $target) = @_;
	my ($raw_data);
	$raw_data = `curl --silent http://www.last.fm/user/$lastfmuser | grep -A 1 '<td class="subject">' | head -n2 | tail -n1 | sed -e 's#<[^>]*>##g' | sed 's#^[\ \t]*##' | recode utf-8..iso-8859-1`;

	if($raw_data eq "Add this track to your playlist")
	{
		Irssi::print "Nothing playing!";
		return;
	}

	if(!$server || !$server->{connected}) # are we even connected?
	{
		Irssi::print $raw_data;
		return;
	}

	if($args)
	{
		$server->command("msg $args $raw_data");
	}
	else
	{
		Irssi::active_win()->command('say ' . $raw_data) || Irssi::print $raw_data;
	}
}

Irssi::command_bind('np', 'getNp');
