use strict;
use Irssi;
use LWP::UserAgent;

use vars qw($VERSION %IRSSI);

$VERSION = "0.8";
%IRSSI = (
	authors		=> 'David O\'Rourke',
	contact		=> 'irssi \[at\] null-routed.com',
	name		=> 'kernel',
	description	=> 'Fetches information on the latest Linux kernels.',
	licence		=> 'GPL',
	changed		=> '16.10.2003 01:49'
);

sub get_kernel_version {
	my ($version) = @_;

	# Set the user agent
	my $ua = LWP::UserAgent->new;
	$ua->agent("irssi/0.8.x ");

	# get proxies
	$ua->env_proxy();

	# make a request
	my $request = HTTP::Request->new(GET => 'http://www.kernel.org/kdist/finger_banner');

	# get it now
	my $response = $ua->request($request);

	#check it
	if ($response->is_success) {
		my @lines = $response->content;
		my ($which, @vers, @info);
		
		# chomp and separate version from rest of line
		foreach my $line (@lines) {
			chomp($line);
			@vers = $line =~ /:\s*(\S+)\s*$/gm;
			@info = $line =~ /.*:/gm;
		}	

		# print our info
		foreach my $info (@info) {
			Irssi::print "$info[$which] %_$vers[$which]%_";
			$which = $which + 1;
		}		
	} else {
		Irssi::print("Couldn't fetch kernel information", Irssi::MSGLEVEL_CLIENTERROR);
	}
}

Irssi::command_bind('kernel', 'get_kernel_version');