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

####################################################
# unmute:help:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} help unmute" unmute:help:pubm
proc unmute:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "unmute: usage unmute \[<chan>\] <nick> \[<nick2> .. <nick6>\]"
  lappend o "unmute: unbans *!user@host to unmute the victims previously hit with mute or other means, the ban will not be removed when it is supposed to be set."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "unmute: " } }
  putloglev c $c "help: $n $u $h $c unmute"
  return 1
}


####################################################
# unmute:help:msgm
####################################################
bind msgm lomn|lomn "help unmute" unmute:help:msgm
proc unmute:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "unmute: usage unmute <chan> <nick> \[<nick2> .. <nick6>\]"
  lappend o "unmute: unbans *!user@host to unmute the victims previously hit with mute or other means, the ban will not be removed when it is supposed to be set."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "unmute: " } }
  putcmdlog "($n!$u) !$h! help unmute"
  return 1
}


####################################################
# unmute:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} unmute" unmute:pubm
bind pubm lomn|lomn "% ${botnet-nick} unmute *" unmute:pubm
proc unmute: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 "unmute: usage unmute <nick> \[<nick2> .. <nick6>\]"
  } else { set o [unmute:unmute $h $t $c] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "unmute: " } }
  putloglev c $c "unmute: $n $u $h $c $t"
  return 1
}


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


####################################################
# unmute:dcc
####################################################
bind dcc -|- unmute unmute:dcc
proc unmute: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 "unmute: usage unmute <chan> <nick> \[<nick2> .. <nick6>\]"
    lappend o "unmute: unbans *!user@host to unmute the victims previously hit with mute or other means, the ban will not be removed when it is supposed to be set."
  } else { set o [unmute:unmute $h $t $c] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# unmute:unmute
####################################################
proc unmute:unmute { 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]
  if {![validchan $c] || ![matchattr $h lomn|lomn $c]} {
    lappend o "unmute: no access or unknown channel $c"
  } elseif {[string equal [join $t] ""]} {
    lappend o "unmute: usage unmute \[<chan>\] <nick> \[<nick2> .. <nick6>\]"
  } elseif {![botonchan $c]} {
    lappend o "unmute: I am not on $c"
  } elseif {![botisop $c] && ![botishalfop $c]} {
    lappend o "unmute: I am not opped on $c"
  } else {
    set s ""; set f ""; set b ""
    foreach n $t {
      if {[string equal $n ""]} { continue }
      if {![onchan $n $c]} {
        if {![string equal [info procs chase] ""]} { set z [chase $n] }
        if {![string equal $z ""]} { set n $z }
      }
      if {![onchan $n $c]} { set m [whowas:get $n uhost $c] } else { set m [getchanhost $n] }
      if {[string equal $m ""]} { lappend f "'$n not found'"; continue }
      set h [nick2hand $n]
      if {[matchban $n!$m $c]} { lappend f "'$n is banned'"; continue }
      set m "*!$m"
      if {![unmute:set $c $m]} { lappend f "'$n not muted'"; continue }
      if {[matchattr $h k|k $c]} { lappend f "'$n is hostile'"; continue }
      lappend s $n; pushmode $c -b $m
    }
    if {![string equal $s ""]} { lappend o "unmute: unmuted on $c '[join $s "   "]'" }
    if {![string equal $f ""]} { lappend o "unmute: failed to unmute on $c [join $f "   "]" }

  }
  return $o
}


####################################################
# unmute:set
# returns 1 if the ban is already set
####################################################
proc unmute:set { c b } {
  foreach e [chanbans $c] { if {[string equal -nocase [lindex $e 0] $b]} { return 1 } }
  return 0
}


set scriptdb(unmute) {
  "provides unmute command for unbanning *!user@host to unmute the victims previously hit with mute or other means, the ban will not be removed when it is supposed to be set. script can use chase.tcl and whowas.tcl when available."
}

