####################################################
# by wiebe @ QuakeNet
#
# can use chase.tcl when available
####################################################

####################################################
# mute:help:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} help mute" mute:help:pubm
proc mute:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "mute: usage mute \[<chan>\] <nick> \[<nick2> .. <nick6>\]"
  lappend o "mute: works on regular users. bans *!user@host preventing victims from sending any messages to the channel, changing nick or leaving a quit/part message. mute does not work when +enforcebans is set as the bot would enforce the bans."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "mute: " } }
  putloglev c $c "help: $n $u $h $c mute"
  return 1
}


####################################################
# mute:help:msgm
####################################################
bind msgm lomn|lomn "help mute" mute:help:msgm
proc mute:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "mute: usage mute <chan> <nick> \[<nick2> .. <nick6>\]"
  lappend o "mute: works on regular users. bans *!user@host preventing victims from sending any messages to the channel, changing nick or leaving a quit/part message. mute does not work when +enforcebans is set as the bot would enforce the bans."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "mute: " } }
  putcmdlog "($n!$u) !$h! help mute"
  return 1
}


####################################################
# mute:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} mute" mute:pubm
bind pubm lomn|lomn "% ${botnet-nick} mute *" mute:pubm
proc mute:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "mute: usage mute \[<chan>\] <nick> \[<nick2> .. <nick6>\]"
  } else { set o [mute:mute $h $t $c] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "mute: " } }
  putloglev c $c "mute: $n $u $h $c $t"
  return 1
}


####################################################
# mute:msg
####################################################
bind msg lomn|lomn mute mute:msg
proc mute:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} {
    lappend o "mute: usage mute <chan> <nick> \[<nick2> .. <nick6>\]"
  } else { set o [mute:mute $h $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "mute: " } }
  return 1
}


####################################################
# mute:dcc
####################################################
bind dcc -|- mute mute:dcc
proc mute:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  if {[string equal $t ""]} {
    lappend o "mute: usage mute <chan> <nick> \[<nick2> .. <nick6>\]"
    lappend o "mute: works on regular users. bans *!user@host preventing victims from sending any messages to the channel, changing nick or leaving a quit/part message. mute does not work when +enforcebans is set as the bot would enforce the bans."
  } else { set o [mute:mute $h $t $c] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# mute:mute
####################################################
proc mute:mute { h t {c ""} } {
  set t [split $t]; set d [lindex $t 0]
  if {[string equal $c ""] || [string match {[#&]*} $d]} { set c $d; set t [lrange $t 1 end] }
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set t [lrange $t 0 5]; set y [llength [chanbans $c]]
  if {[info exists ::max-bans]} { set x ${::max-bans} } else { set x "45" }
  if {![validchan $c] || ![matchattr $h lomn|lomn $c]} {
    lappend o "mute: no access or unknown channel $c"
  } elseif {[string equal [join $t] ""]} {
    lappend o "mute: usage mute \[<chan>\] <nick> \[<nick2> .. <nick6>\]"
  } elseif {![botonchan $c]} {
    lappend o "mute: I am not on $c"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "mute: I am not opped on $c"
  } elseif {[channel get $c enforcebans]} {
    lappend o "mute: +enforcebans is set on $c"
  } elseif {$y > $x} {
    lappend o "mute: channel banlist is full on $c"
  } else {
    set s ""; set f ""; set b ""; set z 0
    foreach n $t {
      if {[string equal $n ""]} { continue }
      if {![onchan $n $c]} {
        if {![string equal [info procs chase] ""]} { set d [chase $n] }
        if {![string equal $d ""]} { set n $d }
      }
      if {![onchan $n]} { lappend f "'$n not found'"; continue }
      if {[isop $n $c] || [ishalfop $n $c]} { lappend f "'$n is op'"; continue }
      if {[isvoice $n $c]} { lappend f "'$n is voice'"; continue }
      set g [nick2hand $n]; set m [getchanhost $n]
      if {[matchban $n!$m $c]} { lappend f "'$n is banned'"; continue }
      if {[mute:set $c "$n!$m"]} { lappend f "'$n already muted'"; continue }
      if {[matchattr $g bfvlomn|fvlomn $c] && ![matchattr $g kZ|k $c]} {
        lappend f "'$n is friendly'"; continue
      }
      if {[expr $y + $z] >= $x} { lappend f "'$n banlist full'"; continue }
      set m "*!$m"; lappend s $n
      if {[string equal [lsearch -exact $b $m] "-1"]} { lappend b $m; incr z }
    }
    if {![string equal $s ""]} { lappend o "mute: muted on $c '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "mute: failed to mute on $c [join $f "   "]" }
    if {![string equal $b ""]} { putquick "MODE $c +[string repeat b [llength $b]] [join $b]" }
  }
  return $o
}


####################################################
# mute:set
# returns 1 if the ban is already set
####################################################
proc mute:set { c b } {
  set b [mute:lower $b]
  foreach e [chanbans $c] {
    set e [lindex $e 0]; set e [mute:lower $e]
    regsub -all {[][\\]} $e {\\\0} e
    if {[string match $e $b]} { return 1 }
  }
  return 0
}


####################################################
# mute:lower
####################################################
proc mute:lower { t } {
  global rfc-compliant
  if {[info exists rfc-compliant] && [string equal ${rfc-compliant} "1"]} {
    set t [string map "\\{ \[ \\} \] ~ ^ \\\\ |" $t]
  }
  set t [string tolower $t]
  return $t
}


set scriptdb(mute) {
  "provides mute command for muting regular users on channels. it bans *!user@host preventing victims from sending any messages to the channel, changing nick or leaving a quit/part message. mute does not work when +enforcebans is set as the bot would enforce the bans. script can use chase.tcl when available."
}

