# ho_operwall.pl
#
# $Id: ho_operwall.pl,v 1.11 2004/08/15 08:53:05 jvunder Exp $
#
# Part of the Hybrid Oper Script Collection (I hope)
#
# This script grabs all wallops starting OPERWALL and
# reformats them with the "operwall" format, and sends them to
# the 'operwall' window.
#

# ---------------------------------------------------------------------

use strict;
use Irssi;
use Irssi::Irc;
use HOSC::again;
use HOSC::again 'HOSC::Base';
use HOSC::again 'HOSC::Tools';

use vars qw[$VERSION %IRSSI $SCRIPT_NAME];

($VERSION) = '$Revision: 1.11 $' =~ / (\d+\.\d+) /;
%IRSSI = (
    authors     => 'James Seward',
    contact     => 'james@jamesoff.net', 
    name        => 'ho_operwall',
    description => 'Sends operwall and locops messages named windows.',
    license     => 'Public Domain',
    url         => 'http://www.jamesoff.net/irc',
    changed     => '06/04/2004 08:15',
    changes     => 'v1.2 - fixed typo',
);
$SCRIPT_NAME = 'Operwall';

# ---------------------------------------------------------------------
#
# Thanks to:
# Garion - for creating the hosc project :)
#
# ---------------------------------------------------------------------
# catch a line typed in any operwall window, and operwall it to the
# server

sub event_operwall_text {
    my ( $data, $server, $witem ) = @_;
    my $active_window = Irssi::active_win();

    if ($active_window->{name} eq "operwall") {
        $server->send_raw_now("OPERWALL :$data");
    } elsif ($active_window->{name} =~ /^ow_(.+)/) {
        my $server = Irssi::server_find_tag($1);
        if ($server) {
            $server->send_raw_now("OPERWALL :$data");
        } else {
            ho_print_error("Not connected to server with tag '$1'.");
        }
    } elsif ($active_window->{name} eq 'locops') {
        $server->send_raw_now("LOCOPS :$data");
    } elsif ($active_window->{name} =~ /^lo_(.+)/) {
        my $server = Irssi::server_find_tag($1);
        if ($server) {
            $server->send_raw_now("LOCOPS :$data");
        } else {
            ho_print_error("Not connected to server with tag '$1'.");
        }
    }
}

# ---------------------------------------------------------------------
# catch an incoming wallop and reformat if it's an operwall

sub event_wallop {
    my ($server, $args, $sender, $addr) = @_;

    my @ignorenicks = split(/ +/, Irssi::settings_get_str("ho_operwall_ignore"));
    if (grep /^$sender$/, @ignorenicks) {
        Irssi::signal_stop();
        return;
    }
 
    if ($args =~ s/^:OPERWALL - //) {
        my $win;
        if (Irssi::settings_get_bool("ho_operwall_multinetwork")) {
            $win = Irssi::window_find_name("ow_" . lc($server->{tag}));
        } else {
            $win = Irssi::window_find_name("operwall");
        }
        return unless defined $win;
        $win->printformat(MSGLEVEL_WALLOPS | MSGLEVEL_CLIENTCRAP, 'ho_operwall', 
			$sender, $args);
        Irssi::signal_stop();
        return;
    }
    
    if ($args =~ s/^:LOCOPS - //) {
        my $win;
        if (Irssi::settings_get_bool("ho_operwall_multinetwork")) {
            $win = Irssi::window_find_name("lo_" . lc($server->{tag}));
        } else {
            $win = Irssi::window_find_name("locops");
        }
        return unless defined $win;
        $win->printformat(MSGLEVEL_WALLOPS | MSGLEVEL_CLIENTCRAP, 
			'ho_locops', $sender, $args);
        Irssi::signal_stop();
        return;
    }
}

# ---------------------------------------------------------------------

ho_print_init_begin($SCRIPT_NAME);

Irssi::theme_register([
    # i like my nicks right-aligned to 9 chars, with overspill
    'ho_operwall', '[{nick $[!-9]0}] $1-',
    'ho_locops',   '[{nick $[!-9]0}] $1-',
]);

Irssi::signal_add('send text',     'event_operwall_text');
Irssi::signal_add('event wallops', 'event_wallop');

Irssi::settings_add_str("ho",  "ho_operwall_ignore",       '');
Irssi::settings_add_bool("ho", "ho_operwall_multinetwork", 0);

my $win;
if (Irssi::settings_get_bool("ho_operwall_multinetwork")) {
    ho_print('Multi-network mode enabled.');
    ho_print("Operwall windows should be named 'ow_<tag>' in ".
        "multi-network mode.");
    my @ow_windows;
    for my $window (Irssi::windows()) {
        push @ow_windows, $1 if $window->{name} =~ /^ow_(.+)/;
    }
    if (@ow_windows) {
        ho_print("Operwall windows found for tags: @ow_windows.");
    } else {
        ho_print("No operwall windows found.")
    }
} else {
    $win = Irssi::window_find_name("operwall");
    if (!defined($win)) {
        ho_print_error("You are missing the 'operwall' window. Use '/WINDOW ".
            "NEW HIDDEN' and '/WINDOW NAME operwall' to create it.\n".
            "OPERWALL messages will not be reformated until this window exists.");
    }
}

if (Irssi::settings_get_bool("ho_operwall_multinetwork")) {
    ho_print("Locops windows should be named 'lo_<tag>' in ".
        "multi-network mode.");
    my @lo_windows;
    for my $window (Irssi::windows()) {
        push @lo_windows, $1 if $window->{name} =~ /^lo_(.+)/;
    }
    if (@lo_windows) {
        ho_print("Locops windows found for tags: @lo_windows.");
    } else {
        ho_print("No locops windows found.")
    }
} else {
    $win = Irssi::window_find_name("locops");
    if (!defined($win)) {
        ho_print_error("You are missing the 'locops' window. Use '/WINDOW ".
            "NEW HIDDEN' and '/WINDOW NAME locops' to create it.\n".
            "LOCOPS messages will not be reformated until this window exists.");
    }
}

ho_print_init_end($SCRIPT_NAME);


