###  whois-ban.tcl by pete@undernet.org
###  Users not recognised by the bot are /whois'd when they join : 'bad' channels/realname info results in 
###  a ban. This is a good way to get rid of #preeteensex perverts and spammers with recognisable whois info
###  
###  This script runs in *one* channel (making it global could well flood the bot). If you wish to use it
###  for multiple channels, run another instance of it on a different bot. (this also means you can have
###  different bans for different channels)
###
###  .whois-ban on the partyline for more help
###


#Set the channel you want it to run in
set chan  #middlesbrough

#You shouldnt need to edit anything below here....




proc readin {} {
global smelly smelly_reason n
set n 0
set in [open realnames.ban r]
 while { [ gets $in line ] != -1 } { 
  set smelly($n) [string range $line 0 [expr [string first : $line] -1]]
  set smelly_reason($n) [string range $line [expr [string first : $line] +2] end]
  incr n

  }
 close $in
}


proc readin2 {} {
global smelly2 smelly2_reason n2
set n2 0
set in2 [open channames.ban r]
 while { [ gets $in2 line ] != -1 } { 
  set smelly2($n2) [string range $line 0 [expr [string first : $line] -1]]
  set smelly2_reason($n2) [string range $line [expr [string first : $line] +2] end]
  incr n2

  }
 close $in2
}




bind dcc m|m whois-ban whois-ban 
bind join * $chan* whoisjoin
bind raw * 311 realname
bind raw * 319 channls

proc realname {from key arg} {
 global n
   global smelly chan smelly_reason banmask
          set banmask "*!*[lindex $arg 3]"
   set rn [string range $arg [expr [string first : $arg] +1] end]


     for {set i 0} {$i <= [expr $n - 1]} {incr i 1} {

         if { ([regexp  -nocase $smelly($i) $rn] != 0)  && ($smelly($i) != "")} {

#         newchanban $chan 
            putserv "privmsg $chan :$banmask  $smelly_reason($i)"
         }    
       }
}








proc whoisjoin {n uh h chan} {
  putserv "whois $n"
}


proc whois-ban {h idx args} {
      global smelly smelly_reason n n2 smelly2 smelly2_reason
      set args2 [lindex $args 0]
      set command [lindex $args2 0]
      set fdump [lrange $args2 1 end]
          switch -exact $command {

            "" { putdcc $idx "  These commands allow you to maintain a list of banned channels and realnames"
                 putdcc $idx "   .whois-ban : shows this help message"
                 putdcc $idx "   .whois-ban addrname <realname>: <ban reason> : add a banned 'realname' field"
                 putdcc $idx "   .whois-ban showrame : shows currently banned realnames"
                 putdcc $idx "   .whois-ban delrname <realname>: delete a banned realname"
                 putdcc $idx "-"
                 putdcc $idx "  example .whois-ban addrnam ircap: go away lamer"
                 putdcc $idx "  note that this matchs the pattern 'ircap', so eg www.ircap.mx would be banned also"
                 putdcc $idx "  also please note the syntax .. its <realname><colon><space><ban reason>"
                 putdcc $idx "  dont forget to include the colour codes if necissary"
                 putdcc $idx "-"
                 putdcc $idx "    .whois-ban addchan <chan>: <reason> : add a banned channel"
                 putdcc $idx "    .whois-ban showchan : shows banned channels"
                 putdcc $idx "    .whois-ban delchan <chan> : remove a banned channel"
                 putdcc $idx "-"
                 putdcc $idx "  Channel bans use pattern matching, thus banning 'preeteen' is an effective way to "
                 putdcc $idx "  prohibit cohabiting of channels such as #preeteensexpics #!!!!!preetensex  etc"
                 putdcc $idx "  This is a two edged sword : ban 'sex' and you're banning #essex "
                 putdcc $idx "    ... mabye not such a bad thing ;p"
                 putdcc $idx "  A bonus of this patern matching is that we can specifically ban the ops of "
                 putdcc $idx "  another channel, but not regular users eg '@#mp3-cru'"
                 putdcc $idx "  You can use spaces as delimiters if you wish  - '#mp3 ' only ban #mp3, not  "
                 putdcc $idx "  every channel starting with 'mp3'"
                putdcc $idx "-"
               }


            "addrname" {  set out [open realnames.ban a+]
                          puts $out $fdump
                          close $out
                    putdcc $idx "ban added"
                    readin
                       }

            "showrname" { 
                          for {set i 0} {$i <= [expr $n - 1]  } {incr i 1} {  
   		           putdcc $idx " ->    $smelly($i)   -  $smelly_reason($i)"
                          }    
                        }

            "delrname" {   set out [open realnames.ban w]
                           for {set i 0} {$i <= [expr $n - 1]} {incr i 1} { 
                          
                           if { ([regexp -nocase $fdump $smelly($i)] != 1)  } {

                                 puts $out "$smelly($i): $smelly_reason($i)"
                             }
                                
                           }
   		           putdcc $idx "$fdump removed from ban list"
                         close $out
                         readin
                    
                    }


            "showchan" { 
                          for {set i 0} {$i <= [expr $n2 - 1]  } {incr i 1} {  
   		           putdcc $idx " ->    $smelly2($i)   -  $smelly2_reason($i)"
                          }    
                        }


            "addchan" {  set out [open channames.ban a+]
                          puts $out $fdump
                          close $out
                    putdcc $idx "ban added"
                    readin2
                       }

            "delchan" {   set out [open channames.ban w]
                           for {set i 0} {$i <= [expr $n2 - 1]} {incr i 1} { 
                          
                           if { ([regexp -nocase $fdump $smelly2($i)] != 1)  } {

                                 puts $out "$smelly2($i): $smelly2_reason($i)"
                             }
                                
                           }
   		           putdcc $idx "$fdump removed from ban list"
                         close $out
                         readin2
                    
                    }
}

}



proc channls {from key arg} {
   global n2 smelly2 smelly2_reason chan banmask
     for {set i 0} {$i <= [expr $n2 - 1]} {incr i 1} {

         if { ([regexp -nocase $smelly2($i) $arg] != 0) } {

#         newchanban $chan 
            putserv "privmsg $chan :$banmask  $smelly2_reason($i)"
         }    
       }





}


















readin
readin2
