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

$VERSION = '1.00';
%IRSSI = (
    authors 	=> 'Eric Jansen',
    contact 	=> 'chaos@sorcery.net',
    name 	=> 'operstuff',
    description => 'Reformat globops/locops and server notices',
    license 	=> 'GPL',
    modules	=> '',
    url		=> 'http://xyrion.org/irssi/',
    changed 	=> 'Sat Mar  1 13:36:48 CET 2003'
);

sub event_notice {

    my ($server, $text, $nick, $address) = @_;

    return unless $nick eq $server->{'real_address'};

    my $window = Irssi::window_find_name($nick);

    if(!defined $window) {

	$window = Irssi::Windowitem::window_create($nick, 1);
	$window->set_name($nick);
    }

    if($text =~ /^\*\*\* Global \-\- from ([^\s]+): (.+)$/) {

	$window->printformat(MSGLEVEL_SNOTES, 'globops', $1, $2);
    }
    elsif($text =~ /^\*\*\* LocOps \-\- from ([^\s]+): (.+)$/) {

	$window->printformat(MSGLEVEL_SNOTES, 'locops', $1, $2);
    }
    elsif($text =~ /^\*\*\* Notice \-\- Client connecting on port (\d+): ([^\s]+) \(([^\s]+)\)/) {

	$window->printformat(MSGLEVEL_SNOTES, 'connect', $1, $2, $3);
    }
    elsif($text =~ /^\*\*\* Notice \-\- Client exiting: ([^\s]+) \(([^\s]+)\) \[(.+?)\]/) {

	$window->printformat(MSGLEVEL_SNOTES, 'disconnect', $1, $2, $3);
    }
    elsif($text =~ /^\*\*\* Notice \-\- Received KILL message for ([^\s]+)!([^\s]+) from ([^\s]+) Path: [^\s]+ \((.+?)\)$/) {

	$window->printformat(MSGLEVEL_SNOTES, 'kill', $1, $2, $3, $4);
    }
    elsif($text =~ /^\*\*\* Notice \-\- Received HEAL message for ([^\s]+) from ([^\s]+)$/) {

	$window->printformat(MSGLEVEL_SNOTES, 'heal', $1, $2);
    }
    elsif($text =~ /^\*\*\* Notice \-\- Received Hurt Message for ([^\s]+) from ([^\s]+) \(([^\s]+)\) \((.+?)\)$/) {

	$window->printformat(MSGLEVEL_SNOTES, 'hurt', $1, $2, $3, $4);
    }
    elsif($text =~ /^\*\*\* Notice \-\- (.+)$/) {

	$window->printformat(MSGLEVEL_SNOTES, 'notices', $1);
    }
    else {

	$window->printformat(MSGLEVEL_SNOTES, 'locops', $nick, $text);
    }
}

Irssi::theme_register([
    'connect', '{line_start}Connecting on port $0: {nick $1} {nickhost $2}',
    'disconnect', '{line_start}Disconnecting: {nick $0} {nickhost $1} {reason $2}',
    'globops', '{msgnick %G$0%n}$1',
    'locops', '{msgnick %Y$0%n}$1',
    'heal', '{line_start}{nick $0} was healed by {nick $1}',
    'hurt', '{line_start}{nick $0} was hurt by {nick $1} ($2) {reason $3}',
    'kill', '{line_start}{nick $0} {nickhost $1} was killed by {nick $2} {reason $3}',
    'notices', '{line_start}$0'
]);
Irssi::signal_add('message irc notice', 'event_notice');
