##
##  ~/.irc/openpkg -- ircII run-commands for OpenPKG settings
##  Copyright (c) 2005-2018 Ralf S. Engelschall <rse@engelschall.com>
##

#   Banner
^set display off
echo *** Loading ircII configuration: openpkg (OpenPKG)

#   Convenience Ratbox Services Login
#   Usage: /login [[[<nickname>] <username>] <password>]
alias login {
    #   determine command line arguments
    if ([$2] != [] && [$3] == []) {
        ^assign login.nickname $0
        ^assign login.username $1
        ^assign login.password $2
    }
    if ([$1] != [] && [$2] == []) {
        ^assign login.username $0
        ^assign login.password $1
    }
    if ([$0] != [] && [$1] == []) {
        ^assign login.password $0
    }

    #   optionally determine nickname
    if ([$login.nickname] == []) {
        ^assign login.nickname $IRC_LOGIN_NICKNAME
        if ([$login.nickname] == []) {
            ^assign login.nickname $IRCNICK
            if ([$login.nickname] == []) {
                ^assign login.nickname $N
            }
        }
    }
    if ([$login.nickname] == []) {
        echo *** login:ERROR: IRC authentication nickname not known
        echo *** login:ERROR: (set shell $IRC_LOGIN_NICKNAME, shell $IRCNICK, ircII $login.nickname or use /nick)
    }

    #   optionally determine username
    if ([$login.username] == []) {
        ^assign login.username $IRC_LOGIN_USERNAME
        if ([$login.username] == []) {
            ^assign login.username $IRCUSER
            if ([$login.username] == []) {
                ^assign login.username $login.nickname
            }
        }
    }
    if ([$login.username] == []) {
        echo *** login:ERROR: IRC authentication username not known
        echo *** login:ERROR: (set shell $IRC_LOGIN_USERNAME, shell $IRCUSER, or ircII $login.username)
    }

    #   optionally determine password
    if ([$login.password] == []) {
        ^assign login.password $IRC_LOGIN_PASSWORD
        if ([$login.password] == []) {
            ^assign login.password $IRCPASS
        }
    }
    if ([$login.password] == []) {
        echo *** login:ERROR: IRC authentication password not known
        echo *** login:ERROR: (set shell $IRC_LOGIN_PASSWORD, shell $IRCPASS, or ircII $login.password)
    }

    if ([$login.nickname] == [] || [$login.username] == [] || [$login.password] == []) {
        echo *** login:ERROR: Unable to authenticate you because of missing information.
        __login_cleanup
    }{
        __login_step1
    }
}
alias __login_step1 {
    #   communicating to UserServ
    if ([$login.nickname] == [$login.username]) {
        echo *** login: authenticating to UserServ with username ${login.username}
    }
    ^msg USERSERV login $login.username $login.password
    if ([$N] != [$login.nickname]) {
        timer 2 __login_step2
    }{
        __login_cleanup
    }
}
alias __login_step2 {
    #   communicating to NickServ
    echo *** login: requesting to NickServ the regaining of nickname ${login.nickname}
    ^msg NICKSERV regain $login.nickname
    timer 2 __login_step3
}
alias __login_step3 {
    #   communicating to IRC
    echo *** login: resurrecting nickname ${login.nickname}
    nick $login.nickname
    timer 2 nick
    __login_cleanup
}
alias __login_cleanup {
    ^assign -login.nickname
    ^assign -login.username
    ^assign -login.password
}

#   Flexible Channel Joining
#   Usage: /join [<channel>]
alias join {
    ^assign alias.join.channel $0
    ^on -473 "*Cannot join channel*+i*" {
        msg CHANSERV invite $alias.join.channel
        timer 2 __join_repeat
    }
    //join $alias.join.channel
    timer 4 __join_cleanup
}
alias __join_repeat {
    ^on -473 -"*Cannot join channel*+i*"
    //^join $alias.join.channel
}
alias __join_cleanup {
    ^assign -alias.join.channel
    ^on -473 -"*Cannot join channel*+i*"
}

#   Convenience Operator Privilege Regaining
#   Usage: /opme
alias opme {
    msg CHANSERV op $C
}

#   Convenience Voice Management
#   (for moderated channels)
#   Usage: /voice [[+]|- <nickname> ...]
alias __voice_display {
    #   display the nicks which have voice
    @ output = []
    @ nicks = chanusers($C)
    @ i = 0
    while (i < [$#nicks]) {
        @ nick = [$word($i $nicks)]
        @ type = hasvoice($nick $C)
        if ([$type] == [1]) {
            if ([$output] == []) {
                ^assign output ${nick}
            }{
                ^assign output $output, ${nick}
            }
        }
        @ i = i + 1
        ^assign -nick
    }
    if ([$output] == []) {
        echo +++ Voices: -none-
    }{
        echo +++ Voices: $output
    }
    ^assign -i
    ^assign -nicks
    ^assign -output
}
alias __voice_change {
    #   grant/take voice from nick(s)
    @ args = [$*]
    ^assign op +
    @ i = 0
    while (i < [$#args]) {
        @ arg = [$word($i $args)]
        if (left(1 $arg) == [-] || left(1 $arg) == [+]) {
            ^assign op $left(1 $arg)
            ^assign arg $strip([+-] $arg)
        }
        if ([$arg] == [*]) {
            @ nicks = chanusers($C)
        }{
            @ nicks = [$arg]
        }
        @ j = 0
        while (j < [$#nicks]) {
            @ nick = [$word($j $nicks)]
            @ type = hasvoice($nick $C)
            if ([$op] == [+]) {
                if ([$type] == [0]) {
                    echo +++ Voice granted to ${nick}
                    mode $C +v $nick
                }
            }{
                if ([$type] == [1]) {
                    echo +++ Voice taken from ${nick}
                    mode $C -v $nick
                }
            }
            @ j = j + 1
        }
        @ i = i + 1
    }
    ^assign -i
    ^assign -j
    ^assign -op
    ^assign -type
    ^assign -nick
    ^assign -nicks
    ^assign -arg
    ^assign -args
}
alias voice {
    if ([$C] == [0]) {
        echo +++ voice:ERROR: you are still not on a channel!
    }{
        if ([$*] != []) {
            __voice_change $*
            timer 2 __voice_display
        }{
            __voice_display
        }
    }
}

#   Convenience All-In-One Session Setup
alias session {
    clear
    echo ::: session: preparing main window
    window name main
    echo ::: session: logging in
    login
    echo ::: session: spawning other windows
    timer 2 __session_step2
    timer 8 __session_step3
    timer 14 __session_step4
}
alias __session_step2 {
    window new name foundation
    window refnum foundation
    clear
    echo ::: session: preparing foundation window
    timer 1 join \#foundation
}
alias __session_step3 {
    window new name community
    window refnum community
    clear
    echo ::: session: preparing community window
    timer 1 join \#community
}
alias __session_step4 {
    window refnum main
    window balance
    echo ::: session: established.
    ^msg STEWARD who
}

#   Shorthands for Services Communication
alias gl msg global $0-
alias al msg alis $0-
alias ns msg nickserv $0-
alias us msg userserv $0-
alias cs msg chanserv $0-
alias os msg operserv $0-
alias ob msg operbot $0-
alias js msg jupeserv $0-

#   Shorthands for Bot Communication
alias se msg secretary $0-
alias st msg steward $0-
alias vo msg voter $0-

#   Additional Shorthands
alias last msg secretary log $C last $*
alias match msg secretary log $C match $*

#   End of Configuration
^set display on

