# simply, use /csize <link> and wait a sec ...
# after that, script will print <link_adress> (size_of_this_file)
#
# example: /cs http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.16.15.tar.bz2

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

$VERSION = '0.1';
%IRSSI = (
	  authors     => 'tomekk',
	  contact     => 'tomekk\@\oswiecim\.\eu\.\org',
	  name        => 'csize',
	  description => 'script adding file size to end of line with link',
	  license     => 'GPL',
	  url         => 'http://irssi.org/',
	  changed     => 'wen may 10 16:38:22 CEST 2006',
	  commands    => '/cs'
);

sub check_size {
	
	my ($data, $server, $channel) = @_;

	my $link;
	my $size;
	my $formats;  
	
	#if you want other, just add new, like: zip|ace|zoo|wma
	$formats = "mpg|wmv|mp3|avi|wav|jpg|bmp|png|gif|bz2|gz|zip|rar";	
	
	$link = "$data";

	if ($link =~ m/^(http)/i && $link =~ m/($formats)$/i) {
		$size = `wget -t 1 --connect-timeout=10 -U "Mozilla/5.0 (X11; Linux i686; U; en) Opera" --spider "$link" -o /tmp/csize_irssi.tmp; grep Length /tmp/csize_irssi.tmp'`;

		if($size) {
		
			my $output = "$link $size";

			$output =~ s/\s+$//; #whitespaces
			
			Irssi::active_win()->command('say ' . $output);
			
		} else {
			
			Irssi::print "file/link does not exist";
			
		}

	} else {
		
		Irssi::print "bad link format";
		
	}
}

Irssi::command_bind('cs', 'check_size');
