#
# Type /ask to ask the question, and a
# single Y or N followed by ENTER to respond
#
# Statusbar not working (in 0.8.10?)
#

use strict;
use warnings FATAL => qw(all);

use vars qw($VERSION %IRSSI);
$VERSION = '2006032201';
%IRSSI = (
	authors     => 'Cydex',
	version     => '1.0',
	name        => 'question',
	description => 'Ask user a question',
	license     => 'BSD',
);

use Irssi::TextUI ();

our $question_asked = 0;
our $question_text = '%8 Continue (y/n)? ';
our $question_answer = sub {
	shift =~ /^\s*(?i:y|yes|n|no)\b/
};

sub check_question {
	my ($msg) = @_;

	if ($question_asked) {
		if ($question_answer->($msg)) {
			$question_asked = 0;
			question_sb_refresh();
		}

		# Do some stuff depending on answer

		Irssi::signal_stop();
	}
}

sub question_sb {
	my ($item, $get_size_only) = @_;

	$item->default_handler($get_size_only,
		($question_asked ? $question_text : ''), '', 1);
}

sub question_sb_refresh {
	Irssi::statusbar_items_redraw('question');
}

sub cmd_ask {
	$question_asked = 1;
	question_sb_refresh();
}

Irssi::statusbar_item_register('question', '$0', 'question_sb');
Irssi::command('^statusbar prompt add -after prompt_empty -priority 11 question');

Irssi::signal_add_first({
	map { $_ => 'check_question' } (
		'send command',
		'send text',
	),
});

question_sb_refresh();

Irssi::command_bind(
	'ask' => 'cmd_ask'
);

print $IRSSI{name} . ' v' . $VERSION . ' loaded as ' . __PACKAGE__;

