# AutoVoice v1.1 by David Gravereaux <david.gravereaux@snet.com>
# 1/30/97
#
# Tested with eggdrop1.0k and Tclsh 7.6 {gauranteed to work with Tcl7.3 as well}
#             as well as eggdrop95b3
#
# Changes from v1.0: -Had to update the 'toolkit' procedure.
#                    -A few other buggies fixed too.
# Changes from v0.9: -Uses 'newflag' which is a ToolKit specific command.
#                    -Uses 'pushmode' instead of 'putserv "MODE blah blah"'
#                    -fixed a bug with the msg_giveme_voice arguement handling.
#                    -DCC voice and devoice commands now default to the
#                      current console. Just like the op and deop commands.
#
# ** Use '.chattr <handle> +v' to set their NEW user flag, if you will be 
#     using the auto-voice option.
#
# ** The new dcc commands for +o (or better) are:
#
#     .voice <nick> [[channel] [channel] ...etc.]
#                   -to give <nick> a voice. If channel isn't specified,
#                     <nick> will be voiced on the current console
#                     channel. That is, if <nick> is on that channel.
#
#     .devoice <nick> [[channel] [channel] ...etc.] 
#                   -to revoke <nick>'s voice. same options above.
#
# ** The new /msg commands for +v or +o (or better) are:
#
#     /msg <botnick> voice [[channel] [channel] ...etc.] <Password>  
#                   -will give voice to the user if password matches.
#                     If channel is omitted, <nick> will be voiced on
#                     all channels the bot is on. That is, if <nick>
#                     is on all of those channels.
#
####################################################################
# IMPORTANT !!!
# REPLACE THE toolkit.tcl PROCEDURE, OF THE SAME NAME, WITH THIS ONE!!!
#
###############################################################
# Globals:
#  You may change values below.
###############################################################

# [0|1] enables or disables Auto-voicing on joining channel
# Will unbind commands when rehashed, if edited. To temporarily
# disable, you could '.set auto-voice 0' from the console, too.
set autovoice 1

################################################################

bind dcc o voice dcc_give_voice
bind dcc o devoice dcc_take_voice
bind msg vo voice msg_giveme_voice

proc dcc_give_voice {handle idx args} {
  set arglst [lindex $args 0]
  if {[expr ![string compare $arglst ""]]} {
    putdcc $idx "## Usage: .voice <nick> \[\[channel\] \[channel\] ...etc.\]"
    putdcc $idx "##"
    putdcc $idx "## if channel isn't specified, <nick> will be voiced"
    putdcc $idx "## on the console channel you are now on. That is, if"
    putdcc $idx "## <nick> is present on [lindex [console $idx] 0]."
    return 0
  }
  set vnick [lindex $arglst 0]
  if {[llength $arglst] == 1} {
    set conchan [lindex [console $idx] 0]
    if {[onchan $vnick $conchan]} {
      pushmode $conchan +v $vnick
      putdcc $idx "Gave Voice to $vnick on $conchan"
    } else {
      putdcc $idx "## Sorry, $vnick wasn't on $conchan ."
      putdcc $idx "## voice failed."
      return 0
    }
  } else {
    set i 0
    foreach channel {[channels]} {
      foreach argchan {[lrange $arglst 1 [llength $args]]} {
        if {[expr ![string compare [string tolower $argchan] [string tolower $channel]]]} {
          if {[onchan $vnick $channel]} {
            pushmode $channel +v $vnick
            putdcc $idx "Gave Voice to $vnick on $channel"
            set i [expr $i + 1]
          } else {
          putdcc $idx "## Sorry, $vnick wasn't on $argchan"
          }
        }
      }
    }
    if {$i == 0} {
      putdcc $idx "## Sorry, $vnick wasn't found on any of the channels you specified."
      putdcc $idx "## voice failed."
    }
  }
}

proc dcc_take_voice {handle idx args} {
  set arglst [lindex $args 0]
  if {[expr ![string compare $arglst ""]]} {
    putdcc $idx "## Usage: .devoice <nick> \[\[channel\] \[channel\] ...etc.\]"
    putdcc $idx "##"
    putdcc $idx "## if channel isn't specified, <nick> will be devoiced"
    putdcc $idx "## on the console channel you are now on. That is, if"
    putdcc $idx "## <nick> is present on [lindex [console $idx] 0]."
    return 0
  }
  set vnick [lindex $arglst 0]
  if {[llength $arglst] == 1} {
    set conchan [lindex [console $idx] 0]
    if {[onchan $vnick $conchan]} {
      pushmode $conchan -v $vnick
      putdcc $idx "Took Voice from $vnick on $conchan"
    } else {
      putdcc $idx "## Sorry, $vnick wasn't on $conchan ."
      putdcc $idx "## voice failed."
      return 0
    }
  } else {
    set i 0
    foreach channel [channels] {
      foreach argchan [lrange $arglst 1 [llength $args]] {
        if {[expr ![string compare [string tolower $argchan] [string tolower $channel]]]} {
          if {[onchan $vnick $channel]} {
            pushmode $channel -v $vnick
            putdcc $idx "Took Voice from $vnick on $channel"
            set i [expr $i + 1]
          } else {
            putdcc $idx "## Sorry, $vnick wasn't on $argchan"
          }
        }
      }
    }
    if {$i == 0} {
      putdcc $idx "## Sorry, $vnick wasn't found on any of the channels you specified."
      putdcc $idx "## devoice failed."
    }
  }
}

proc msg_giveme_voice {nick uhost handle args} {
 global botnick
  set arglst [lindex $args 0]
  if {[llength $arglst] == 0} {
    putnotc $nick "Usage: /msg $botnick voice \[\[channel\] \[channel\] ...etc.\] <password>"
    putnotc $nick ""
    putnotc $nick "If channel isn't specified, you will be voiced"
    putnotc $nick "on all channels the bot is on. That is, if you"
    putnotc $nick "are present on those channels."
    return 0
  }
  set i 0
  if {[passwdok $handle [lindex $arglst [expr [llength $arglst] - 1]]]} {
    set vchans [lrange $arglst 0 [expr [llength $arglst] - 2]]
    if {[llength $vchans] == 0} {
      foreach channel [channels] {
        if {[onchan $nick $channel]} {
          pushmode $channel +v $nick
          putnotc $nick "You have been Voiced on $channel"
          putlog "Voice given to $nick on $channel"
          set i [expr $i + 1]
        }
      }
    } else {
      set i 0
      foreach channel [channels] {
        foreach argchan [vchans] {
          if {[expr ![string compare [string tolower $argchan] [string tolower $channel]]]} {
            if {[onchan $nick $channel]} {
              pushmode $channel +v $vnick
              putnotc $nick "Gave you a Voice on $channel"
              putlog "Voice given to $nick on $channel"
              set i [expr $i + 1]
            } else {
              putnotc $nick "Your not on $channel."
            }
          }
        }
      }
      if {$i == 0} {putnotc $nick "Command failed"; return 0}
      return 1
    }
    #putnotc $nick "Get on one of my channels first."; return 0
  } else { 
    putnotc $nick "password is incorrect."; return 0
  }
}

proc join_auto_voice {nick uhost hand channel} {
 global autovoice
  if {$autovoice == 0} {
    unbind join v * join_auto_voice
    return 1
  } else {
    pushmode $channel v $nick
  }
}

if {[newflag v] != 1} {
  if {$toolkit_loaded != 1} {
    error "ERROR... toolkit.tcl is not loaded. avoice1.1.tcl is not initialized"
  } else {
    error "ERROR defining 'v' as a flag in avoice1.1.tcl. All user-defined flags are full or newflag{} procedure in toolkit.tcl is out-dated"
  }
} else {
  if {$autovoice == 1} {
    bind join v * join_auto_voice
    } else {
    bind join v * join_auto_voice
    unbind join v * join_auto_voice
  }
  putlog "AutoVoice v1.1 by DaveG initialized..."
}

