#	$eterna: autoop,v 1.4 2002/01/22 02:46:44 mrg Exp $
#
# AutoOp   by Ian Frechette 12-10-92 
# Written for ircII2.2pre8 and beyond

# initial list of trusted chan ops..
# format is   nickname!username@hostname   wildcards can be used in 
# any of the three fields
# in the following list the first name would autoop anyone with the 
# nickname 'example_nick'
# the second example will autoop anyone with the username
# 'bob' coming from any machine in the .nocolorado.edu domain

eval if (op_list == []) {@ op_list = [example_nick!*@* *!bob@*.nocolorado.edu]}

# People will only be opped on the following channels..  
# wildcards are allowed and thus just * means every channel.  Use
# delchan to remove stuff from the list, addchan to add
eval if (chan_list == []) {@chan_list = [* #example]}

# Show the the people currently in the autoop list
alias showop
{
    @ ao.tmp = 0
    @ ao.name = []
    echo *** AutoOp list.  Addop nick!user@host to add.  Delop <num>  to remove
    echo *** <num> nickname!username@hostname 
    while (ao.name = word($ao.tmp $op_list)) {
        echo *** $[5]ao.tmp $ao.name
        @ao.tmp = ao.tmp + 1
    }
}

# Just like showop but works with channels.
alias showchan
{
    @ ao.tmp = 0
    @ ao.name = []
    echo *** AutoOp chan list.  Addchan #channel add.  Delchan <num>  to remove
    echo *** <num> #channel
    while (ao.name = word($ao.tmp $chan_list)) {
        echo *** $[5]ao.tmp $ao.name
        @ao.tmp = ao.tmp + 1
    }
}

# Given a number.. deletes that person from the autoop list.. 
# use SHOWOP to list.
alias delop {
    if (!rmatch($0. *1* *2* *3* *4* *5* *6* *7* *8* *9* *0*))
        { echo *** Usage: delop <number>;echo *** See showop }
        { @ op_list = notword(${[$0] + 1} $op_list) }
}
    
# Given a number.. deletes that channel from the autoop channel list.. 
# use SHOWCHAN to list.
alias delchan {
    if (!rmatch($0. *1* *2* *3* *4* *5* *6* *7* *8* *9* *0*))
        { echo *** Usage: delchan <number>;echo *** See showchan }
        { @ chan_list = notword(${[$0] + 1} $chan_list) }
}

# Add an autochanop to the list.
alias addop {
    if ([$0])
    { @ op_list = op_list ## [ $0] }
    { echo *** Usage addop nick!username@host;echo *** wildcards are allowed }
}

# Add an autochanop channel to the list.
alias addchan {
    if ([$0])
    { @ chan_list = chan_list ## [ $0] }
    { echo *** Usage addchan #channel;echo *** wildcards are allowed }
}

# the actual ON that does the work
on #-join 666 * {
    if (rmatch($1 $chan_list))
    {
	if (rmatch($0!$userhost() $op_list) && ischanop($N $1))
	    { timer ${10 + rand(10)} conditionalop $1 $0 }
    }
}

alias conditionalop {
    if (!ischanop($1 $0))
    {
	mode $0 +o $1
    }
}

# The perfect complement to the $word() function.  
# $notword(index words)  returns words minus the indexed word. 
# the special handling of nw.sep is to deal with the cases when 
# the index points to the first or last word.
alias notword {
    if ([$0] > 0)
    {
    if (([$0] > 1) && ([$0] < rmatch($~ $1-)))
        { @ nw.sep = [ ] }
        { @ nw.sep = [] }
        
    @ function_return = [$(1-${[$0]-1})] ## [$nw.sep] ## [$(${[$0]+1}-)]
    }
    {
        @ function_return = [$1-]
    }
}
