#by wiebe @ QuakeNet



#help deluser msg
bind msgm mn "help deluser" c_deluser:help:msg

proc c_deluser:help:msg { nick uhost handle text } {
  lappend output "Usage: deluser <nick>|<#account>"
  lappend output "Removes nick's account or #account given from the bot."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#deluser msg
bind msg mn deluser c_deluser:msg

proc c_deluser:msg { nick uhost handle text } {
  set target [lindex [split $text] 0]
  set check [lindex [split $text] 1]
  set md5 [string range [md5 "$nick $uhost $handle [string tolower $target]"] 0 9]
  if { [string match \#* $target] } {
    set account [string range $target 1 end]
  } else {
    set account [nick2hand $target]
  }

  if { [string equal $target ""] || [string equal $target \#] } {
    lappend output "Usage: deluser <nick>|<#account>"
  } elseif { [string equal $account ""] && ![string match \#* $target] } {
    lappend output "User $target is not known."
  } elseif { ![onchan $target] && ![string match \#* $target] } {
    lappend output "User $target is not in any of my channels."
  } elseif { ![validuser $account] } {
    lappend output "Account $account is not known."
  } elseif { ![string equal [lsearch $::owner $account] -1] } {
     if { [string match \#* $target] } {
      lappend output "Account $account is a permanent owner, this user can not be removed."
    } else {
      lappend output "User $target ($account) is a permanent owner, this user can not be removed."
    }
  } elseif { [matchattr $handle m] && ![matchattr $handle n] && [matchattr $account mn] } {
     if { [matchattr $account n] } { set is "an owner" } else { set is "a master" }
     if { [string match \#* $target] } {
      lappend output "Account $account is $is, you can not remove this user."
    } else {
      lappend output "User $target ($account) is $is, you can not remove this user."
    }
  } elseif { ![string equal $check $md5] } {
    if { [string match \#* $target] } {
      lappend output "WARNING: this will remove $account from the bot!"
    } else {
      lappend output "WARNING: this will remove $target ($account) from the bot!"
    }
    lappend output "To confirm this use: /msg $::botnick deluser $target $md5"
  } else {
    deluser $account
    if { [string match \#* $target] } {
      lappend output "Removed account $account from the bot."
    } else {
      lappend output "Removed user $target ($account) from the bot."
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

