#
# add the msg 'kick' command
#   this is an example of how you can use Tcl to add commands to eggdrop
#   it's very powerful... i've tried to document this clearly so you can
#   see what's going on
#

# eggdrop-specific "bind" will connect a Tcl procedure to a command
# here, binding a msg command which requires +op [o] called "kick"
# when an op does "/msg bot kick [whatever]", the bot will call "cmd_kick"
bind msg o kick cmd_kick

# a msg command is always passed:
#   the nickname of the user
#   the user@host of the user
#   the handle of the user (the nickname the bot knows this user by)
#   any command-line arguments (in one string)
proc cmd_kick {nick uhost hand arg} {
  global channel botnick
  # not enough arguments?
  if {$arg == ""} {
    putserv "NOTICE $nick :Usage: /msg $botnick kick <nick> <pass> \[reason\]"
    return 0
    # return value of 0 indicates that the command failed, and should not
    #   be logged
  }
  # split up the command line: who pass reason...
  set who [lindex $arg 0]
  set pass [lindex $arg 1]
  set reason [lrange $arg 2 end]
  # just ignore them if they didn't give the right password
  if {![passwdOk $hand $pass]} { return 0 }
  # global variable channel is blank if the bot is currently floating
  if {$channel == {}} {
    putserv "NOTICE $nick :I'm not on a channel!"
    return 0
  }
  # use one of the convenient eggdrop-specific procedures to determine
  #   if the desired nickname is even on the channel
  if {![onchan $who]} {
    putserv "NOTICE $nick :$who is not on the channel!"
    return 0
  }
  # nitpicky detail: desired target cannot be netsplit!
  if {[onchanSplit $who]} {
    putserv "NOTICE $nick :$who is currently net-split."
    return 0
  }
  # ok, first get the "user@host" for this nickname ($who)
  # then combine it into "nick!user@host"
  # then use that to make eggdrop retreive the handle (if any)
  append userhost $who "!" [getchanhost $who]
  set target [finduser $userhost]
  # finduser returns "*" for unknown users (historic reasons)
  if {$target != "*"} {
    # the target is known by the bot!  is it a friend or master?
    # can't use [matchattr $target mf] because that checks to see if
    #   the target has BOTH the master AND friend flag
    if {([matchattr $target f] || [matchattr $target m])} {
      putserv "NOTICE $nick :Can't kick a friend or master, sorry."
      return 0
    }
  }
  # check target nickname against bot's current nickname
  if {$who == $botnick} {
    putserv "NOTICE $nick :Yeah right, like I'm going to let you kick ME!"
    return 0
  }
  # if they gave no reason, use "requested"
  if {$reason == ""} { set reason "requested" }
  putserv "KICK $channel $who :$reason"
  return 1
}
