# This is a Magic 8 Ball irssi script.
#
# Usage:
# !8-ball question you have?
#
# It will return an answer pulled from the 8ball text file.
#
# This script requires you to have the text file "8ball" in your /home/user/.irssi/ directory


use Irssi;

use vars qw($VERSION %IRSSI);

$VERSION = "1.0";
%IRSSI = (
    author => 'pleia2',
    contact => 'lyz@princessleia.com ',
    name => '8-ball',
    description => 'An 8-ball script',
    license => 'GNU GPL v2 or later',
    url => 'http://www.princessleia.com'
);

open ( BALL, "<.irssi/8ball" )or die "can't open 8ball:$!\n";
chomp( @eightball = <BALL> );
close BALL;

sub event_privmsg {
my ($server, $data, $nick, $mask) =@_;
my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;
    	return if ( $text !~ /^!8-ball$|^!8-ball.*\w$|^!8-ball..*\?$|^!8ball.*/i );
	if ($text =~ /^!8-ball$/i ) {
        $server->command ( "msg $target You have to ask a question! Type !8-ball your question?" );
	}
        elsif ($text =~ /^!8-ball.*\w$/i ) {
        $server->command ( "msg $target Your question must end with a question mark. Type !8-ball your question? " );
	}
        elsif ($text =~ /^!8ball.*/i ) {
        $server->command ( "msg $target You have to ask a question! Type !8-ball your question?" );
	}
        elsif ($text =~ /^!8-ball..*\?$/i ) {
        $answer = $eightball[rand @eightball];
        $server->command ( "msg $target $answer" );
	}
}

Irssi::signal_add('event privmsg', 'event_privmsg');
