Advertisement
Guest User

blacklist.tcl

a guest
Oct 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 10.21 KB | None | 0 0
  1. #  $Id: blacklist.tcl,v 0.4 2018/10/18 22:00:00 outsider Exp $  #
  2. #  original: perpleXa                                           #
  3. #                                                               #
  4. #  Commands:                                                    #
  5. #  $black add <#<chan>|*=global> <mask> [%<reason>                #
  6. #  $black del <mask|#id>                                        #
  7. #  $black list [mask]                                           #
  8. #################################################################
  9.  
  10. set blacklist_file "scripts/dbase/blacklist"
  11.  
  12. setudef flag blacklist
  13.  
  14. bind pub  m \$black         blacklist:pub
  15. bind time - "0* * * * *"    blacklist:sentry
  16. bind join - *               blacklist:join
  17. bind nick - *               blacklist:nick
  18.  
  19. proc blacklist:pub {nick host hand chan args} {
  20.     global blacklist lastbind ban-time
  21.     set args [blacklist:clean $args]
  22.    
  23.     set cmd [lindex $args 0]
  24.     switch -- $cmd {
  25.         "add" {
  26.             if {![channel get $chan blacklist]} {
  27.                 puthelp "NOTICE $nick :Sorry, blacklisting disabled here."
  28.                 return 0
  29.             }
  30.             #check for nick or hostname or id
  31.             set target [lindex $args 1]
  32.             if {[onchan $target]} {
  33.                 set banmask [blacklist:validate:host [maskhost [getchanhost $target] 2]]
  34.             } else {
  35.                 set banmask [blacklist:validate:host $target]
  36.             }
  37.            
  38.             set counter 2
  39.             set pchan [lindex $args 2]
  40.             if {[validchan $pchan] || $pchan eq "*"} {
  41.                 set uchan [string tolower $pchan]
  42.                 incr counter
  43.             } else {
  44.                 set uchan [string tolower $chan]
  45.             }
  46.            
  47.             if {[info exists blacklist($banmask,$uchan)]} {
  48.                 puthelp "NOTICE $nick :Sorry, couldn't add that hostmask. (Already present)"
  49.                 return 0
  50.             }
  51.                        
  52.             if {[string match %* [lindex $args $counter]]} {
  53.                 set expire [unixtime]
  54.                 set s [string range [lindex $args $counter] 1 e]
  55.                 if {$s ne 0} {
  56.                     set days [lindex [regexp "(\[0-9\]+)+d" $s] 1]
  57.                     if {$days ne ""} { incr expire $days*1440 }
  58.                     set hours [lindex [regexp "(\[0-9\]+)+h" $s] 1]
  59.                     if {$hours ne ""} { incr expire $hours*60 }
  60.                     set minutes [lindex [regexp "(\[0-9\]+)+m" $s] 1]
  61.                     if {$minutes ne ""} { incr expire $minutes }
  62.                 } else {
  63.                     set expire 0
  64.                 }
  65.                 incr counter
  66.             } else {
  67.                 set expire [expr [unixtime] + ${ban-time}]
  68.             }
  69.            
  70.             set reason [join [lrange $args $counter e]]
  71.             if {$reason eq ""} { set reason "Requested" }
  72.             set blacklist($banmask,$uchan) "$hand $expire $reason"
  73.             puthelp "NOTICE $nick :Done. $banmask blacklisted successfully (reason: $reason)."
  74.             blacklist:save
  75.             blacklist:sentry
  76.             return 1
  77.         }
  78.         "del" {
  79.             if {![channel get $chan blacklist]} {
  80.                 puthelp "NOTICE $nick :Sorry, blacklisting disabled here."
  81.                 return 0
  82.             }
  83.             if {[lindex $args 1] eq ""} {
  84.                 puthelp [format "NOTICE %s :%s del <nick|hostmask|id> \[<other channel|*=global>\]" $nick $lastbind]
  85.                 return 1
  86.             }
  87.             set pchan [lindex $args 2]
  88.             if {[validchan $pchan] || $pchan eq "*"} {
  89.                 set uchan [string tolower $pchan]
  90.             } else {
  91.                 set uchan [string tolower $chan]
  92.             }
  93.  
  94.             set target [lindex $args 1]
  95.             if {[onchan $target]} {
  96.                 set banmask [blacklist:validate:host [maskhost [getchanhost $target] 2]]
  97.             } elseif {[string is integer $target]} {
  98.                 set pos 0
  99.                 foreach entry [array names blacklist] {
  100.                     incr pos
  101.                     if {$pos eq $target} {
  102.                         unset -nocomplain blacklist($entry)
  103.                         puthelp "NOTICE $nick :Done. Entry removed."
  104.                         blacklist:save
  105.                         return 1
  106.                     }
  107.                 }
  108.                 puthelp "NOTICE $nick :Failed. Specified id does not exist. Try $lastbind list"
  109.                 return 0
  110.             } else {
  111.                 set banmask [blacklist:validate:host $target]
  112.             }
  113.            
  114.             if {[info exists blacklist($banmask,$uchan)]} {
  115.                 unset -nocomplain blacklist($banmask,$uchan)
  116.                 puthelp "NOTICE $nick :Done. Entry removed."
  117.                 blacklist:save
  118.                 return 1
  119.             }
  120.             puthelp "NOTICE $nick :Failed. Specified entry does not exist. Try $lastbind list"
  121.             return 0
  122.         }
  123.         "list" {
  124.             if {![channel get $chan blacklist]} {
  125.                 puthelp "NOTICE $nick :Sorry, blacklisting disabled here."
  126.                 return 0
  127.             }
  128.  
  129.             set arg1 [lindex $args 1]
  130.             set arg2 [lindex $args 2]
  131.             if {[validchan $arg1]} {
  132.                 set uchan [string tolower $arg1]
  133.             }
  134.             if {![info exists uchan] && [validchan $arg2]} {
  135.                 set uchan [string tolower $arg2]
  136.                 set mask [blacklist:validate:host $arg1]
  137.             } elseif {$arg1 eq "*"} {
  138.                 set uchan [string tolower $chan]
  139.             }
  140.            
  141.             set uchan [string tolower $uchan]
  142.            
  143.             set ml ""
  144.             set ml_owner 0
  145.             set ml_chan 0
  146.             set ml_expire 0
  147.             set ml_banmask 0
  148.  
  149.             set pos 0
  150.             foreach entry [array names blacklist] {
  151.                 incr pos
  152.                 set banmask [string tolower [lindex [split entry ,] 0]]
  153.                 set pchan [string tolower [lindex [split entry ,] 0]]
  154.                 if {![string match -nocase $uchan $pchan]} { continue }
  155.                 if {[info exists mask] && ![string match -nocase $banmask $mask]} { continue }
  156.                 set owner [lindex $blacklist($entry) 0]
  157.                 if {[lindex $blacklist($entry) 1] eq 0} {
  158.                     set expire "Never"
  159.                 } else {
  160.                     set expire [clock format [lindex $blacklist($entry) 1] -format %D %T]
  161.                 }
  162.                 set reason [lrange $blacklist($entry) 2 e]
  163.                 lappend ml [join "$pos $owner $pchan $banmask $expire $reason"]
  164.                 if {[string length $owner] > $ml_owner} { set ml_owner [string length $owner] }
  165.                 if {[string length $pchan] > $ml_chan} { set ml_chan [string length $pchan] }
  166.                 if {[string length $expire] > $ml_expire} { set ml_expire [string length $expire] }
  167.                 if {[string length $banmask] > $ml_banmask} { set ml_owner [string length $banmask] }
  168.             }
  169.             if {[llength $ml] eq 0} {
  170.                 puthelp "NOTICE $nick :No matching entries found."
  171.                 return 0
  172.             }
  173.             puthelp "NOTICE $nick :Returning a list of [llength $ml] blacklist matches."
  174.             puts [format "%-*s | %-*s | %-*s | %-*s | %-*s | %s" [string length $pos] "Id" $ml_owner "Owner" $ml_chan "Channel" $ml_banmask "Mask" $ml_expire "Expire" "Reason"]
  175.             foreach entry $ml {
  176.                 puts [format "%-*s | %-*s | %-*s | %-*s | %-*s | %s" [string length $pos] [lindex $ml 0]  $ml_owner [lindex $ml 1] $ml_chan [lindex $ml 2] $ml_banmask [lindex $ml 3] $ml_expire [lindex $ml 4] [lrange $ml 5 e]]
  177.             }
  178.             puthelp "NOTICE $nick :End of blacklist matches."
  179.             return 1
  180.         }
  181.         "on" {
  182.             if {![matchattr $hand +n]} { return 0 }
  183.             channel set $chan +blacklist
  184.             puthelp "NOTICE $nick :Blacklist enabled."
  185.             blacklist:sentry
  186.             return 1
  187.         }
  188.         "off" {
  189.             if {![matchattr $hand +n]} { return 0 }
  190.             channel set $chan +blacklist
  191.             puthelp "NOTICE $nick :Blacklist disabled."
  192.             return 1
  193.         }
  194.         default {
  195.             puthelp "NOTICE $nick :Blacklist commands:"
  196.             puthelp [format "NOTICE %s :%s add <nick|hostmask> \[<other channel|*=global>\] \[%s<XdXhXm|0=permanent>\] <reason>" $nick $lastbind "%"]
  197.             puthelp [format "NOTICE %s :%s del <nick|hostmask|id> \[<other channel|*=global>\]" $nick $lastbind]
  198.             puthelp [format "NOTICE %s :%s list \[<hostmask>\] \[<other channel|*=global>\]" $nick $lastbind]
  199.             if {[matchattr $hand +n]} {
  200.                 puthelp [format "NOTICE %s :%s on|off" $nick $lastbind]
  201.             }
  202.         }
  203.     }
  204. }
  205.  
  206. proc blacklist:sentry {{minute "0"} {hour "0"} {day "0"} {week "0"} {year "0"}} {
  207.     global blacklist
  208.     foreach chan [channels] {
  209.         foreach target [chanlist $chan] {
  210.             blacklist:join $target [getchanhost $target] [nick2hand $target] $chan
  211.         }
  212.     }
  213.     blacklist:save
  214. }
  215.  
  216. proc blacklist:nick {nick host hand chan newnick} {
  217.     blacklist:join $newnick $host $hand $chan
  218. }
  219.  
  220. proc blacklist:join {nick host hand chan} {
  221.     global blacklist
  222.     if {![botisop $chan] || ![channel get $chan blacklist]} { return }
  223.     if {[matchattr $hand +n]} { return }
  224.     set userhost [blacklist:weirdclean "$nick!$host"]
  225.     foreach entry [array names blacklist] {
  226.         set expire [lindex $blacklist($entry) 1]
  227.         if {$expire > 0 && $expire < [unixtime]} { continue }
  228.        
  229.         set blackhost [blacklist:weirdclean [lindex [split $entry ,] 0]]
  230.         set blackchan [blacklist:weirdclean [lindex [split $entry ,] 1]]
  231.         if {$blackchan ne "*" && ![string match -nocase $blackchan $chan]} { continue }
  232.         if {![string match -nocase $blackhost $userhost]} { continue }
  233.        
  234.         set reason [lrange [blacklist:clean $blacklist($entry)] 2 end]
  235.         putquick "MODE $chan -o+b $nick $blackhost"
  236.         putquick "KICK $chan $nick :[join $reason]"
  237.     }
  238. }
  239.  
  240. proc blacklist:validate:host {i} {
  241.     regsub -all {\*+} $i {*} i
  242.     array set ban {
  243.         ident *
  244.         host *
  245.     }
  246.     set ban(nick) $i
  247.     if {[regexp -- {!} $i]} {
  248.         regexp -- {^(.+?)!(.*?)(@(.*))?$} $i tmp ban(nick) ban(ident) tmp ban(host)
  249.     } elseif {[regexp -- {@} $i]} {
  250.         regexp -- {^(.+!)?(.*?)(@(.*))?$} $i tmp ban(nick) ban(ident) tmp ban(host)
  251.     }
  252.     foreach item [array names ban] {
  253.         if {[string length $ban($item)] < 1} {
  254.             set ban($item) *
  255.         }
  256.     }
  257.     return [string tolower $ban(nick)!$ban(ident)@$ban(host)]
  258. }
  259.  
  260. proc blacklist:load {} {
  261.     global blacklist blacklist_file
  262.     regexp {(\S+/)?} $blacklist_file tmp blacklist_dir
  263.     if {$blacklist_dir != ""} {
  264.         if {![file isdirectory $blacklist_dir]} {
  265.             file mkdir $blacklist_dir
  266.             putlog "Created directory: $blacklist_dir"
  267.         }
  268.     }
  269.     if {![file exists $blacklist_file]} {
  270.         array set blacklist {}
  271.         return
  272.     }
  273.     if {[array exists blacklist]} {
  274.         array unset blacklist
  275.     }
  276.     set file [open $blacklist_file r]
  277.     while {![eof $file]} {
  278.         gets $file line
  279.         if {[regexp -- {(\S+)\s(\S+)\s(\S+)\s(.+)} $line tmp banmask owner expire reason]} {
  280.             if {$expire >= [unixtime] || ($expire == 0)} {
  281.                 set blacklist($banmask) "$owner $expire $reason"
  282.             }
  283.         }
  284.     }
  285.     close $file
  286. }
  287.  
  288. proc blacklist:save {} {
  289.     global blacklist blacklist_file
  290.     set file "[open $blacklist_file w]"
  291.     foreach entry [array names blacklist] {
  292.         set expire [lindex $blacklist($entry) 1]
  293.         if {$expire >= [unixtime] || ($expire == 0)} {
  294.             puts $file "$entry $blacklist($entry)"
  295.         }
  296.     }
  297.     close $file
  298. }
  299.  
  300. proc blacklist:weirdclean {i} {
  301.     regsub -all -- \\\\ $i \001 i
  302.     regsub -all -- \\\[ $i \002 i
  303.     regsub -all -- \\\] $i \003 i
  304.     regsub -all -- \\\} $i \004 i
  305.     regsub -all -- \\\{ $i \005 i
  306.     return $i
  307. }
  308.  
  309. proc blacklist:clean {i} {
  310.     regsub -all -- \\\\ $i \\\\\\\\ i
  311.     regsub -all -- \\\[ $i \\\\\[ i
  312.     regsub -all -- \\\] $i \\\\\] i
  313.     regsub -all -- \\\} $i \\\\\} i
  314.     regsub -all -- \\\{ $i \\\\\{ i
  315.     regsub -all -- \\\" $i \\\\\" i
  316.     return $i
  317. }
  318.  
  319. blacklist:load
  320. putlog "Script loaded: Blacklist :O by perpleXa (modified by OUTsider)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement