use Irssi 20020120.0250 ();
$VERSION = "2";
%IRSSI = (
    authors     => 'fahren, David Leadbeater',
    contact     => 'fahren@bochnia.pl',
    name        => 'sping.pl',
    description => '/sping [server] returns lag to server specified by sending a remote PING to the server.',
    license     => 'unknown',
);

# us. /SPING [server]

use Irssi::Irc;
use Time::HiRes qw(gettimeofday);

my %askping;

sub cmd_sping {
	my ($target, $server, $winit) = @_;
	
	$target = $server->{address} unless $target;
	$askping{$target} = gettimeofday();
    # using nickname rather than server seems to work better here
	$server->send_raw("PING $server->{nick} $target");	
}

sub event_pong {
	my ($server, $args, $sname) = @_;
	return unless exists $askping{$sname};

	Irssi::signal_stop();
	Irssi::print(">> $sname latency: " .  sprintf("%0.3f",gettimeofday() - $askping{$sname}) . "s");
	delete $askping{$sname};
}

Irssi::signal_add("event pong", "event_pong");
Irssi::command_bind("sping", "cmd_sping");

