# Display "nick@address #channel" in XTerm title
# for irssi 0.7.98 by Timo Sirainen
# clean up by md@linux.it

use Irssi;
use strict;

if ($ENV{DISPLAY}) {
	my $oldfh = select STDERR; $| = 1; select $oldfh;
	Irssi::signal_add('window changed'			=> 'refresh_topic');
	Irssi::signal_add('window item changed'		=> 'refresh_topic');
	Irssi::signal_add('window server changed'	=> 'refresh_topic');
	Irssi::signal_add('server nick changed'		=> 'refresh_topic');
}

sub xterm_topic ($) {
	print STDERR "\033]0;$_[0]\007";
}

sub refresh_topic {
	my $server = Irssi::active_server();

	my $title;
	if (not $server) {
		$title = '(not connected)';
	} else {
		$title = $server->{nick} . '@' . $server->{address};
		my $item = Irssi::active_win->get_active_name;
		$title .= ' ' . $item if $item;
	}
	xterm_topic($title);
}

