#!/usr/bin/perl
# Verbrochen von: Sven Weise <sven.weise@unit0.net>
#
# Module for irssi to set topic to the actual rannking on top1000.org for selectet sites
#
# Free of use/modification
#
# In IRSSI you can use /setnewstopic to set the topic to the actual Ranking

use strict;
use Irssi;

# Channel to use
my $channel='#newsadmins';
# Sites we want in the topic
my %sites=(
	'news.unit0.net' => 1,
);
# wget command to fetch the data
my $wget='/usr/local/bin/wget -q -O -';
# URL to fetch the data
my $url='http://www.top1000.org/top1000.current.txt';
my (%ranking,$topic,$server);

sub get {
	my @now = localtime(time);
	$now[4]++;
	$now[5] += 1900;
	my $date = sprintf "%02d.%02d.%04d", $now[3], $now[4], $now[5];

	my (@servers) = Irssi::servers();
	$server=$servers[0];
	my @data=`$wget $url`;
	$topic='TOP1000 ranking (' . $date . '):';
	if (scalar(@data) >= 1) {
		for (@data) {
			chomp;
			my (undef,$rank,undef,$site)=split(/\t+/,$_,4);
			if ($sites{$site}) {
				$site.=':';
				$rank.=';';
				$topic=join(' ', $topic, $site, $rank);
			}
		}
	}
}

sub settopic {
	get();
	$server->send_raw("TOPIC $channel : $topic");
}

Irssi::command_bind('setnewstopic', 'settopic');
