####################################################
# by wiebe @ QuakeNet
#
# invite users to the channel when they are kicked by netrider protection
#
####################################################

####################################################
# netrider:kick
####################################################
bind kick -|- "% % net rider" netrider:kick
proc netrider:kick { n u h c t r } {
  if {![validchan $c]} { return 0 }
  if {[isbotnick $t]} { return 0 }
  if {![botisop $c] && ![botishalfop $c]} { return 0 }
  if {![string equal $n ""]} { return 0 }
  if {![string equal -nocase $r "net rider"]} { return 0 }
  if {![string match "*.*" $u]} { return 0 }
  if {![string match {*[ik]*} [lindex [split [getchanmode $c]] 0]]} { return 0 }
  set h [nick2hand $t $c]
  if {![matchattr $h bomn|fvlomn $c]} { return 0 }
  if {![netrider:whichbot $c $t "$t $c"]} { return 0 }
  putloglev k $c "$c: inviting $t ($h) victim of netrider protection"
  puthelp "INVITE $t $c"
}


####################################################
# netrider:whichbot
# returns 1 if this bot should respond, 0 otherwise
####################################################
proc netrider:whichbot { c v t } {
  if {[string equal [info procs script:check] ""]} { return 1 }
  global botnet-nick; set a [chanlist $c b]; lappend r ${botnet-nick}
  foreach b $a {
    if {[string equal $b $v]} { continue }
    if {[onchansplit $b $c]} { continue }
    if {![isop $b $c] && ![ishalfop $b $c]} { continue }
    if {![script:check $b netrider]} { continue }
    set h [nick2hand $b $c]
    if {![string equal [lsearch -exact $r $h] -1]} { continue }
    lappend r $h
  }
  set r [lrange [lsort $r] 0 15]; set x [expr [llength $r] -1]
  if {[string equal $x "0"]} { return 1 }
  set d [lsearch -exact [split a01b23c45d67e89f ""] [string range [md5 $t] 0 0]]
  set b [lindex $r [expr round(${x}. / 16. * $d)]]
  if {[string equal $b ${botnet-nick}]} { return 1 } else { return 0 }
}


set scriptdb(netrider) {
  "invites known users with global omn flags or fvlomn channel flags back to the channel when they get kicked by the server's anti netrider protection."
}

