#by wiebe @ QuakeNet
#requires chase.tcl



#help mute pub
bind pubm lomn|lomn "% ${botnet-nick} help mute" c_mute:help:pub

proc c_mute:help:pub { nick uhost handle chan text } {
  lappend output "Usage: mute <nick1> \[<nick2> .. <nick6>\]"
  lappend output "Works on regular users. Sets mode +b *!user@host, preventing them from sending any messages to the channel, changing nick, or leaving a quit/part message. Does not work when +enforcebans is set."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help mute msg
bind msgm lomn|lomn "help mute" c_mute:help:msg

proc c_mute:help:msg { nick uhost handle text } {
  lappend output "Usage: mute <channel> <nick1> \[<nick2> .. <nick6>\]"
  lappend output "Works on regular users. Sets mode +b *!user@host, preventing them from sending any messages to the channel, changing nick, or leaving a quit/part message. Does not work when +enforcebans is set."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#mute pub
bind pubm lomn|lomn "% ${botnet-nick} mute" c_mute:pub
bind pubm lomn|lomn "% ${botnet-nick} mute *" c_mute:pub

proc c_mute:pub { nick uhost handle chan text } {
  set target [join [lrange [split $text] 2 7]]
  set output ""
  if { [string equal $text ""] } {
    lappend output "Usage: mute <nick1> \[<nick2> .. <nick6>\]"
  } elseif { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } elseif { [channel get $chan enforcebans] } {
    lappend output "This command is rendered useless by current channel settings (+enforcebans) on $chan"
  } else {
    set bans ""
    foreach user [split $target] { lappend bans [c_mute:+b $chan $user] }
    if { ![string equal $bans ""] } {
      set bans "+[string repeat b [llength $bans]] [join $bans]"
      putquick "MODE $chan $bans"
    } else {
      lappend output "Failed to find any valid targets to mute on $chan"
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#mute msg
bind msg lomn|lomn mute c_mute:msg

proc c_mute:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set target [join [lrange [split $text] 1 6]]
  set output ""
  if { [string equal $chan ""] || [string equal $target ""] } {
    lappend output "usage: mute <channel> <nick1> \[<nick2> .. <nick6>\]"
  } elseif { ![validchan $chan] || ![matchattr $handle lomn|lomn $chan] || ![onchan $nick $chan] } {
    lappend output "No access or unknown channel $chan"
  } elseif { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } elseif { [channel get $chan enforcebans] } {
    lappend output "This command is rendered useless by current channel settings (+enforcebans) on $chan"
  } else {
    set bans ""
    foreach user [split $target] { lappend bans [c_mute:+b $chan $user] }
    if { ![string equal $bans ""] } {
      set bans "+[string repeat b [llength $bans]] [join $bans]"
      putquick "MODE $chan $bans"
    } else {
      lappend output "Failed to find any valid targets to mute on $chan"
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#sets +b
proc c_mute:+b { chan user } {

#chasenick
  if { ![onchan $user $chan] } {
    if { [catch {set cu [chase:chasenick $user]} error] } {
      putlog "ERROR using chase.tcl, perhaps you have not loaded it?"
    }
    if { ![string equal $cu -1] && [onchan $cu $chan] && ![onchansplit $cu] } { set user $cu }
  }

  if { ![onchan $user $chan] } { return }
  if { [onchansplit $user] } { return }
  if { [isop $user $chan] } { return }
  if { [ishalfop $user $chan] } { return }
  if { [isvoice $user $chan] } { return }
  if { [matchattr [nick2hand $user] bfvlomn|fvlomn $chan] } { return }
  set mask *![getchanhost $user]
  if { [isban $mask $chan] } { return }
  if { [ischanban $mask $chan] } { return }
  return $mask
}

