#!/usr/bin/perl
# no_serv_ops v2.0 - Automatic deopping of self if given ops by server.
# Copyright 2004 (C) James Michael Fultz <jmf@gtcom.net>
# License: GNU GPLv2 or later

use strict;
use warnings;

my %script  = (
    name        => 'no_serv_ops',
    version     => '2.0',
    description => 'Automatic deopping of self if given ops by server.'
);

# channels in which ops are retained
my %opchans;

Xchat::register($script{'name'}, $script{'version'}, $script{'description'});

Xchat::hook_server('WALLOPS', \&hook_wallops_host);
Xchat::hook_command('opjoin', \&cmd_opjoin);
Xchat::hook_command('opcycle', \&cmd_opcycle);

sub hook_wallops_host {
    my $wnick   = $_[0][4];
    my $chan    = $_[0][6];
    my $type    = $_[0][7];
    my $serv    = Xchat::get_info('server');
    my $nick    = Xchat::get_info('nick');

    if (Xchat::nickcmp($wnick, $nick) == 0 and $type eq '[HOST]') {
        if (not $opchans{$serv}->{lc($chan)}) {
            Xchat::command("deop $nick", $chan, $serv);
        }
        else {
            $opchans{$serv}->{lc($chan)}    = 0;
        }
    }

    return Xchat::EAT_NONE();
}

sub cmd_opjoin {
    my $jargs           = $_[1][1];
    my $chan            = $_[0][1];
    my $serv            = Xchat::get_info('server');

    $opchans{$serv}->{lc($chan)}    = 1;

    Xchat::command("join $jargs");
}

sub cmd_opcycle {
    my $chan        = Xchat::get_info('channel');
    my $serv        = Xchat::get_info('server');

    $opchans{$serv}->{lc($chan)}    = 1;

    Xchat::command('cycle');
}

Xchat::print("$script{'name'} v$script{'version'} - Loaded.");
