#by wiebe @ QuakeNet



set c_gunban(xhost) "users.quakenet.org"



#help gunban pub

bind pubm mn|- "% ${botnet-nick} help gunban" c_gunban:help:pub

proc c_gunban:help:pub { nick uhost handle chan text } {
  lappend output "Usage: gunban <ban1> \[<ban2> .. <banN>\]"
  lappend output "Removes global bans. #account can be used to unban a *!*@<account>.$::c_gunban(xhost) mask."
  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 gunban msg

bind msgm mn|- "help gunban" c_gunban:help:msg

proc c_gunban:help:msg { nick uhost handle text } {
  lappend output "Usage: gunban <ban1> \[<ban2> .. <banN>\]"
  lappend output "Removes global bans. #account can be used to unban a *!*@<account>.$::g_unban(xhost) mask."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#gunban pub
#gunban mask1 mask2 mask3
bind pubm mn|- "% ${botnet-nick} gunban" c_gunban:pub
bind pubm mn|- "% ${botnet-nick} gunban *" c_gunban:pub

proc c_gunban:pub { nick uhost handle chan text } {
  set masks [join [lrange [split $text] 2 end]]
  set output [c_gunban:gunban $nick $masks]
  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]]"
}



#gunban msg
bind msg mn|- gunban c_gunban:msg

proc c_gunban:msg { nick uhost handle text } {
  set masks [lrange [split $text] 0 end]
  if { [string equal $masks ""] } {
    c_gunban:help:msg $nick $uhost $handle $text
  } else {
    set output [c_gunban:gunban $nick $masks]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#gunban
proc c_gunban:gunban { nick masks } {    
  set done ""; set failed ""; set output ""

#check and remove the masks
  foreach mask [split $masks] {

#transform to correct masks
#user@host
    if { [string match ?*@?* $mask] && ![string match ?*!?*@?* $mask] } {
      set mask *!$mask

#host
    } elseif { [string match *.* $mask] && ![string match ?*!?*@?* $mask] } {
      set mask *!*@$mask

#nick!user
    } elseif { [string match ?*!?* $mask] && ![string match ?*!?*@?* $mask] } {
      set mask $mask@*

# #account
  } elseif { [string match \#?* $mask] && ![string match ?*!?*@?* $mask] } {
    set mask *!*@[string range $mask 1 end].$::c_gunban(xhost)

#nick
    } elseif { ![string match ?*!?*@?* $mask] } {
      set mask $mask!*@*
    }

    if { [isban $mask] } {
      killban $mask
      lappend done $mask
    } elseif { ![isban $mask] } {
      lappend failed $mask
    }
  }

#reply
  set done [join $done]; set failed [join $failed]
  if { ![string equal $failed ""] } { lappend output "Not on global banlist: $failed" }
  if { ![string equal $done ""] } { lappend output "Global unbanned: $done" }
  if { [string equal $output ""] } { lappend output "Found no global bans to remove." }
  return $output
}

