# Jester.tcl.  Written by Sinc <sinc@discover.discover-net.net>.
# This is my first attempt at writing a script so bear with me.
# I know the code is a bit messy (clean it up if you like :-)) but it
# gets the job done.
#
# This script checks any nick changes or joins to your channel against the
# bots user list.  If the nick is known by the bot and the user@host of the
# join/nick-change does not match the bots internal records, the person is
# warned and then kicked/banned if they do not either IDENT or change nicks.
# If the person doing the faking is +n, +m, +o, then they are allowed to fake
# a user whose access is less then theirs.  All sript activity is logged to
# the misc logfile.
#
# Feel free to modify it as you see fit, but I would
# be interesed in seeing any changes or improvements, so drop me the modified
# version at my email addr.  Be sure to let me know what your modifications do.
# :-)
#     
#                                         Enjoy
#                                           Sinc

#This is a vanity notice, I would just like to know how many people 
#actually use this script.  Comment it out if you wish. 
putserv "NOTICE Sinc :Loaded Jester.Tcl"

#Grace period (in seconds) before the bot kicks an imposter.
set warntime 60

#How long (in seconds) should they be banned from the channel for 
#failing to IDENT.
set bantime  60

proc nick_warning {nick uhost handle chan} {
  global botnick warntime

  putserv "PRIVMSG $chan :$nick --- Your host is not registered to that nickname."
  putserv "NOTICE $nick :You have $warntime seconds to either IDENT yourself or change your nickname."
  putserv "NOTICE $nick :If you do nothing you will be banned!"
  putserv "NOTICE $nick :To register $uhost as owning $nick, /MSG $botnick IDENT $nick 's-bot-password."
  utimer $warntime "check_up $nick $uhost $handle $chan"
}

proc check_up {nick uhost handle chan} {
  global botnick 

  set realnick [string tolower [finduser $nick!$uhost]]
   if {(!([string tolower $nick] == $realnick) && ([onchan $nick $chan]))} {
    nick_kb $nick $uhost $handle $chan
   }
} 
proc nick_kb {nick uhost handle chan} {
  global botnick bantime
    putserv "PRIVMSG $chan :A $bantime second ban will now be placed on the imposter $nick from $uhost!"
    putmsg $nick "If this is a mistake then /msg $botnick IDENT your-bot-password"
    pushmode $chan +b $nick!$uhost
    flushmode $chan
    utimer $bantime "pushmode $chan -b $nick!$uhost"
    putlog "Jester $bantime second ban for $nick."
}


proc join-jester {nick uhost handle chan} {
  global botnick 
 
  if {[validuser $nick]} {
  set realnick [string tolower [finduser $nick!$uhost]] 
   if {!($realnick == "*")} {
    if {!([string tolower $nick] == $realnick)} {
     if {!([matchattr $realnick n])} {
      if {!([matchattr $realnick m]) || ([matchattr $nick n])} { 
       if {!([matchattr $realnick o]) || ([matchattr $nick m])} {
        if {[botisop $chan]} {  
          putlog "$nick faked by $realnick from $uhost via join!  Warned!!"
          nick_warning $nick $uhost $handle $chan 
        } {
          putlog "$nick faked by $realnick from $uhost via join but was not warned because I am not an op."
          } 
       } {
         putlog "$nick faked by $realnick from $uhost via join but was not warned because he is a op."  
         } 
      } {
        putlog "$nick faked by $realnick from $uhost via join but was not warned because he is a master." 
        }
     } {
       putlog "$nick faked by $realnick from $uhost via join but was not warned because he is a owner."
       }
    }
  } {
    if {[botisop $chan]} {
     putlog "$nick faked by an unknown person from $uhost via join!  Warned!!"
     nick_warning $nick $uhost $handle $chan
    } {
      putlog "$nick facked by unknown person from $uhost via join but was not warned because I am not an op."
      }
    } 
 }
}

proc nick-jester {nick uhost handle chan newhand} {
  global botnick

  set realnick [string tolower [finduser $newhand!$uhost]]
  if {[validuser $newhand]} {
   if {!($realnick == "*")} {
    if {!([string tolower $newhand] == $realnick)} {
     if {!([matchattr $realnick n])} {
      if {!([matchattr $realnick m]) || ([matchattr $nick n])} {
       if {!([matchattr $realnick o]) || ([matchattr $newhand m])} {
        if {[botisop $chan]} {
          putlog "$newhand faked by $realnick from $uhost via nick change!  Warned!!"
          nick_warning $newhand $uhost $handle $chan
        } {
          putlog "$newhand faked by $realnick from $uhost via nick change but was not warned because I am not an op."
          } 
       } {
         putlog "$newhand faked by $realnick from $uhost via nick change but was not warned because he is a op."  
         } 
      } {
        putlog "$newhand faked by $realnick from $uhost via nick change but was not warned because he is a master." 
        }
     } {
       putlog "$newhand faked by $realnick from $uhost via nick change but was not warned because he is a owner."
       }
    }
   } {
     if {[botisop $chan]} {
      putlog "$nick faked by an unknown person from $uhost via join!  Warned!!"
      nick_warning $nick $uhost $handle $chan
     } {
       putlog "$nick facked by unknown person from $uhost via join but was not warned because I am not an op."
       }
     }
  } 
}


bind join - *!*@* join-jester
bind nick - * nick-jester 

putlog "Jester by Sinc loaded."
