This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Tomekk's badwords script

Support & discussion of released scripts, and announcements of new releases.
T
Torrevado
Op
Posts: 101
Joined: Wed Aug 02, 2006 6:29 pm

Tomekk's badwords script

Post by Torrevado »

Hi, would be possible to add "X" warnings before kicking? and setting up how much time the bot will remember the offences? and ban time duration?

I know there are another scripts with that features, but I need this one :P

Hey, Tommek, are you there? 8)

Code: Select all

# description: Kicks/Bans people who say bad words. Words are stored in a file. Op protect is allowed. You can add/del/list words via priv msg. You don't need to rehash/restart your bot.

# Author: Tomekk
# e-mail:  tomekk/@/oswiecim/./eu/./org
# home page: http://tomekk.oswiecim.eu.org/
#
# Version 0.6
#
# This file is Copyrighted under the GNU Public License.
# http://www.gnu.org/copyleft/gpl.html

# default ban type is: *!*ident*@some.domain.com [4]

# fixed: some code fixes

# USE '/msg <botnick> bword' for more information

# sets ban type
# 1)  *!*@*.domain.com
# 2)  *!*@some.domain.com
# 3)  *nick*!*@some.domain.com
# 4)  *!*ident*@some.domain.com
set btype 4

# 1 - protect ops, 0 - dont
set protectops "0"

# 1 - enable ban, 0 - disable ban
set want_ban "0"

# wait 'waitb' times before ban
# if you want to ban user after one bad word, set 'waitb' to 0
set waitb "3"

# base directory
set dirname "badwords.db"

# kick message
set msg "bad word!"

# users directory
set usersdir "$dirname/lusers"

# dbase with bad words
set dbase "words_bank.db"

if {[file exists $dirname] == 0} {
	file mkdir $dirname
	set crtdb [open $dirname/$dbase a+]
        puts $crtdb "[censored]\nshit"
        close $crtdb
}

if {[file exists $usersdir] == 0} {
	file mkdir $usersdir
}

proc create_db { dbname definfo } {
	if {[file exists $dbname] == 0} {
		set crtdb [open $dbname a+]
		puts $crtdb "$definfo"
		close $crtdb
	}
}

proc readdb { rdb } {
	global ident times
	set fs_open [open $rdb r]
	gets $fs_open dbout
	close $fs_open

	set separate [split $dbout "&"]
        set ident [lindex $separate 0]
        set times [lindex $separate 1]
}

proc get_all_bwords { } {
	global dirname dbase

	set bf [open $dirname/$dbase]
	set allwords [read $bf]
	close $bf

	set bw_split [split $allwords "\n"]
        return $bw_split
}

proc add_new_bword { bword } {
	global dirname dbase

	set bfile_handle [open $dirname/$dbase a]
	puts $bfile_handle "$bword"
	close $bfile_handle
}

proc delete_bword { dbword {dlall 0}} {
	global dirname dbase

	set old_bwdb [get_all_bwords]
	
	set file_tow [open $dirname/$dbase w]
	
	if {$dlall == 0} {
		foreach chk_bw $old_bwdb {
			if {$chk_bw != $dbword} {
				if {$chk_bw != ""} {
					puts $file_tow $chk_bw
				}
			}
		}
	}
	close $file_tow
}

proc check_bw_exist { chword } {
	set old_chbwdb [get_all_bwords]

        set bw_exist 0

	foreach check_bword $old_chbwdb {
		if {$check_bword == $chword} {
			 set bw_exist 1
		 }
	}
	return $bw_exist
}
											 

bind pubm - * word_fct
bind msgm - "*" bpmsg_fct

proc word_fct { nick uhost hand chan arg } {
  	global botnick dbase dirname protectops usersdir times ident waitb want_ban msg

	set nasty_words [get_all_bwords]

	set kill 0

	if {[isop $botnick $chan]} {
		foreach i [string tolower $nasty_words] {
			if {$i != ""} {
				if {[string match *$i* [string tolower $arg]]} {
					if {$protectops == "0"} {
						set kill "1"
					} {
						if {[isop $nick $chan] == "1"} {
							continue
						} {
							set kill "1"
						}
					}
				}
			}
		}
		
		if {$kill == "1"} {
			if {$want_ban == "1"} {
				if {$waitb == 0} {
					putquick "MODE $chan +b [banmask $uhost $nick]"
					append msg " - banned"
				} {
					if {[file exists $usersdir/$nick] == 0} {
						create_db "$usersdir/$nick" "$uhost&1"
					} { 
						readdb "$usersdir/$nick"
	
						file delete $usersdir/$nick
	
						set overall [expr $times + 1]

						create_db "$usersdir/$nick" "$ident&$overall"
				
						readdb "$usersdir/$nick"

						if {"$uhost" == "$ident"} {				
							if {$times >= $waitb} {
								putquick "MODE $chan +b [banmask $uhost $nick]"
								file delete $usersdir/$nick
								append msg " - banned"
							}
						} { 
							file delete $usersdir/$nick
							create_db "$usersdir/$nick" "$uhost&1"
						}
				
					}
				}
			}
			putkick $chan $nick $msg
		}
	}
}

proc bpmsg_fct { nick uhost hand arg } {
	set all_bargs [split $arg]
	set bargs_opt1 [lindex $all_bargs 0]
	set bargs_opt2 [lindex $all_bargs 1]
	set bargs_opt_newords [string tolower [lrange $all_bargs 2 end]]

	set bw_list [list]
	
	if {$bargs_opt1 == "bword"} {
		if {$bargs_opt2 != ""} {
			if {$bargs_opt2 == "add"} {
				if {$bargs_opt_newords != ""} {
					foreach nbword $bargs_opt_newords {
						if {[check_bw_exist $nbword]} {
							putquick "PRIVMSG $nick :word $nbword allready exist"
						} {
							add_new_bword $nbword
							putquick "PRIVMSG $nick :word $nbword added"
						}
					}
				} {
					putquick "PRIVMSG $nick :use: /msg <botnick> bword add <new_word> ... <new_word>"
				}
			} elseif {$bargs_opt2 == "list"} {
				set list_bwords [get_all_bwords]

				if {$list_bwords != ""} {
					foreach bw_word $list_bwords {
						lappend bw_list "$bw_word"
					}
					putquick "PRIVMSG $nick :[join $bw_list ", "]"
				} else {
					putquick "PRIVMSG $nick :db is empty"
				}
			} elseif {$bargs_opt2 == "del"} {
				if {$bargs_opt_newords != ""} {
					if {$bargs_opt_newords == "delall"} {
						delete_bword 0 1
						putquick "PRIVMSG $nick :all words deleted"
					} {
						foreach dbword $bargs_opt_newords {
							if {[check_bw_exist $dbword]} {
								delete_bword $dbword
								putquick "PRIVMSG $nick :word $dbword deleted"
							} {
								putquick "PRIVMSG $nick :word $dbword not found"
							}
						}
					}
				} {
					putquick "PRIVMSG $nick :use: /msg <botnick> bword del <word> ... <word>"
					putquick "PRIVMSG $nick :if you want to delete all words use: /msg <botnick> del delall"
				}
			}
				
		} {
			putquick "PRIVMSG $nick :use: /msg <botnick> bword <add|del|list>"
		}
	}
}

proc banmask {host nick} {
	global btype

	switch -- $btype {

		1 {
			set mask "*!*@[lindex [split [maskhost $host] "@"] 1]"
		  }

                2 {
			set mask "*!*@[lindex [split $host @] 1]"
                  }

                3 {
  			set mask "*$nick*!*@[lindex [split $host "@"] 1]"
	          }

		4 {
		        set mask "*!*[lindex [split $host "@"] 0]*@[lindex [split $host "@"] 1]"
		  }

		  return $mask
       }
}

putlog "tkbadword.tcl ver 0.6 by Tomekk loaded"
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

new version:

Code: Select all

# description: Kicks/Bans people who say bad words. Words are stored in a file. Op protect is allowed. You can add/del/list words via priv msg. You don't need to rehash/restart your bot. You can define ban duration time etc. Script is very simple to use

# Author: tomekk 
# e-mail:  tomekk/@/oswiecim/./eu/./org 
# home page: http://tomekk.oswiecim.eu.org/ 
# 
# Version 0.7 
# 
# This file is Copyrighted under the GNU Public License. 
# http://www.gnu.org/copyleft/gpl.html 

# default ban type is: *!*ident*@some.domain.com [4] 

# added: ban duration time, "offence" duration time (a time between one bad word and 2nd) 
# fixed: some code fixes 

# USE '/msg <botnick> bword' for more information 

# sets ban type 
# 1)  *!*@*.domain.com 
# 2)  *!*@some.domain.com 
# 3)  *nick*!*@some.domain.com 
# 4)  *!*ident*@some.domain.com 
set btype 4 

# 1 - protect ops, 0 - dont 
set protectops 0 

# 1 - enable ban, 0 - disable ban 
set want_ban 1 

# wait 'waitb' times before ban 
# if you want to ban user after one bad word, set 'waitb' to 0 
set waitb 3 

# 'waitb' info type 
# 1 - kick after bad word and ban after 'waitb' times 
# 2 - write message after bad word to the nick, 'nick: message' and ban after 'waitb' times 
# 3 - do nothing, just ban after 'waitb' times 
set waitb_type 3 

# if u set 'waitb' type 2, then here is a option to set this message 
set waitb_message "watch your language kid !" 

# ban duration time (minutes) 
# 0 - infinite 
set ban_duration 0 

# "offence" duration time, how long bot should remember last bad word time (minutes) 
# 0 - infinite 
set offence_duration 0 

# kick message 
set kmsg "bad word! - kick" 

# ban message 
set bmsg "banned!" 

# base directory 
set dirname "badwords.db" 

# users directory 
set usersdir "$dirname/lusers" 

# banned hosts file 
set banned_hosts "$dirname/bndhosts" 

# dbase with bad words 
set dbase "$dirname/words_bank.db" 

##################################################################
if {[file exists $dirname] == 0} {
	file mkdir $dirname
	set crtdb [open $dbase a+]
        puts $crtdb "[censored]\nshit"
        close $crtdb
}

if {[file exists $usersdir] == 0} {
	file mkdir $usersdir
}

proc create_db { dbname definfo } {
	if {[file exists $dbname] == 0} {
		set crtdb [open $dbname a+]
		puts $crtdb "$definfo"
		close $crtdb
	}
}

proc readdb { rdb } {
	set fs_open [open $rdb r]
	gets $fs_open dbout
	close $fs_open

	return $dbout
}

proc get_all_bwords { } {
	global dbase

	set bf [open $dbase r]
	set allwords [read $bf]
	close $bf

	set bw_split [split $allwords "\n"]
        return $bw_split
}

proc add_new_bword { bword } {
	global dbase

	set bfile_handle [open $dbase a]
	puts $bfile_handle "$bword"
	close $bfile_handle
}

proc add_new_bnd_host { new_host  } {
	global banned_hosts

	set bnd_hosts [open $banned_hosts a]
	puts $bnd_hosts $new_host
	close $bnd_hosts
}

proc delete_bword { dbword {dlall 0}} {
	global dbase

	set old_bwdb [get_all_bwords]
	
	set file_tow [open $dbase w]
	
	if {$dlall == 0} {
		foreach chk_bw $old_bwdb {
			if {$chk_bw != $dbword} {
				if {$chk_bw != ""} {
					puts $file_tow $chk_bw
				}
			}
		}
	}
	close $file_tow
}

proc check_bw_exist { chword } {
	set old_chbwdb [get_all_bwords]

        set bw_exist 0

	foreach check_bword $old_chbwdb {
		if {$check_bword == $chword} {
			 set bw_exist 1
		 }
	}
	return $bw_exist
}

proc ban_checker_timer { } {
	ban_checker

	if {[string match *ban_checker_timer* [utimers]] != 1} {
		utimer 20 ban_checker_timer
	}
}

bind pubm - * word_fct
bind msgm - "*" bpmsg_fct

proc word_fct { nick uhost hand chan arg } {
  	global botnick protectops usersdir waitb waitb_type waitb_message want_ban kmsg bmsg offence_duration ban_duration

	set nasty_words [get_all_bwords]

	set kill 0

	set ban_status 0

	set msg $kmsg

	if {[isop $botnick $chan]} {
		foreach i [string tolower $nasty_words] {
			if {$i != ""} {
				if {[string match *$i* [string tolower $arg]]} {
					if {$protectops == 0} {
						set kill 1
					} {
						if {[isop $nick $chan] == 1} {
							continue
						} {
							set kill 1
						}
					}
				}
			}
		}
		
		if {$kill == 1} {
			if {$want_ban == 1} {
				set time_in_seconds [clock seconds]
				set user_ban_mask [banmask $uhost $nick]

				if {$waitb == 0} {
					putquick "MODE $chan +b $user_ban_mask"

					if {$ban_duration > 0}  {
						add_new_bnd_host "$chan&$user_ban_mask&$time_in_seconds"
					}

					set msg $bmsg

					putkick $chan $nick $msg

					set ban_status 1
				} {
					if {[file exists $usersdir/$nick] == 0} {
						create_db "$usersdir/$nick" "$uhost&1&$time_in_seconds"
					} { 
						set each_user_data [split [readdb "$usersdir/$nick"] "&"]
						set ident [lindex $each_user_data 0]
						set times [lindex $each_user_data 1]
						set first_time [lindex $each_user_data 2]

						if {([expr $time_in_seconds - $first_time] >= [expr $offence_duration * 60]) && ($offence_duration > 0)} {
							file delete $usersdir/$nick
							create_db "$usersdir/$nick" "$uhost&1&$time_in_seconds"
						} {
							file delete $usersdir/$nick

							set overall [expr $times + 1]

							create_db "$usersdir/$nick" "$ident&$overall&$first_time"
							set ident [lindex [split [readdb "$usersdir/$nick"] "&"] 0]

							if {$uhost == $ident} {				
								if {$overall >= $waitb} {
									putquick "MODE $chan +b $user_ban_mask"

									if {$ban_duration > 0} {
										add_new_bnd_host "$chan&$user_ban_mask&$time_in_seconds"
									}

									file delete $usersdir/$nick
									set msg $bmsg

									putkick $chan $nick $msg

									set ban_status 1
								}
							} { 
								file delete $usersdir/$nick
								create_db "$usersdir/$nick" "$uhost&1"
							}
						}	
					}
				}
			}

			if {($ban_status == 0) && ($waitb_type < 3)} {
				if {$waitb_type == 1} {
					putkick $chan $nick $msg
				} {
					putquick "PRIVMSG $chan :$nick: $waitb_message"
				}
			}
		}
	}
}

proc bpmsg_fct { nick uhost hand arg } {
	set all_bargs [split $arg]
	set bargs_opt1 [lindex $all_bargs 0]
	set bargs_opt2 [lindex $all_bargs 1]
	set bargs_opt_newords [string tolower [lrange $all_bargs 2 end]]

	set bw_list [list]
	
	if {$bargs_opt1 == "bword"} {
		if {$bargs_opt2 != ""} {
			if {$bargs_opt2 == "add"} {
				if {$bargs_opt_newords != ""} {
					foreach nbword $bargs_opt_newords {
						if {[check_bw_exist $nbword]} {
							putquick "PRIVMSG $nick :word $nbword allready exists"
						} {
							add_new_bword $nbword
							putquick "PRIVMSG $nick :word $nbword has been added"
						}
					}
				} {
					putquick "PRIVMSG $nick :use: /msg <botnick> bword add <new_word> ... <new_word>"
				}
			} elseif {$bargs_opt2 == "list"} {
				set list_bwords [get_all_bwords]

				if {$list_bwords != ""} {
					foreach bw_word $list_bwords {
						lappend bw_list "$bw_word"
					}
					putquick "PRIVMSG $nick :[join $bw_list ", "]"
				} else {
					putquick "PRIVMSG $nick :db is empty"
				}
			} elseif {$bargs_opt2 == "del"} {
				if {$bargs_opt_newords != ""} {
					if {$bargs_opt_newords == "delall"} {
						delete_bword 0 1
						putquick "PRIVMSG $nick :all words have been deleted"
					} {
						foreach dbword $bargs_opt_newords {
							if {[check_bw_exist $dbword]} {
								delete_bword $dbword
								putquick "PRIVMSG $nick :word $dbword have been deleted"
							} {
								putquick "PRIVMSG $nick :word $dbword not found"
							}
						}
					}
				} {
					putquick "PRIVMSG $nick :use: /msg <botnick> bword del <word> ... <word>"
					putquick "PRIVMSG $nick :if you want to delete all words use: /msg <botnick> del delall"
				}
			}
				
		} {
			putquick "PRIVMSG $nick :use: /msg <botnick> bword <add|del|list>"
		}
	}
}

proc banmask {host nick} {
	global btype

	switch -- $btype {

		1 {
			set mask "*!*@[lindex [split [maskhost $host] "@"] 1]"
		  }

                2 {
			set mask "*!*@[lindex [split $host @] 1]"
                  }

                3 {
  			set mask "*$nick*!*@[lindex [split $host "@"] 1]"
	          }

		4 {
		        set mask "*!*[lindex [split $host "@"] 0]*@[lindex [split $host "@"] 1]"
		  }

		  return $mask
       }
}

proc ban_checker { } {
	global banned_hosts ban_duration

	if {([file exists $banned_hosts]) && ($ban_duration > 0)} {
		set hosts_hand [open $banned_hosts r]
		set give_me_hosts [read $hosts_hand]
		close $hosts_hand

		set time_in_seconds [clock seconds]

		set rewrite_hosts_hand [open $banned_hosts w]
		foreach banned_h [split $give_me_hosts "\n"] {
			if {$banned_h != ""} {
				set split_banned_h [split $banned_h "&"]
				set host_ban_chan [lindex $split_banned_h 0]
				set host_ban_mask [lindex $split_banned_h 1]
				set need_ban_time [lindex $split_banned_h 2]
	
				if {[expr $time_in_seconds - $need_ban_time] >= [expr $ban_duration * 60]} {
					putquick "MODE $host_ban_chan -b $host_ban_mask"
				} {
					puts $rewrite_hosts_hand "$host_ban_chan&$host_ban_mask&$need_ban_time"
				}
			}
		}
		close $rewrite_hosts_hand
	}
}

if {[string match *ban_checker_timer* [utimers]] != 1} {
	        utimer 20 ban_checker_timer
}

putlog "tkbadword.tcl ver 0.7 by tomekk loaded"

added:
- ban duration (script checks every 20 sec file with banned hosts and their chans)
- "offence" duration
- now, you can choose an action for "bad word" , kick, message or nothing

I think message after X kicks is kinda weird,
for example: 3 messages and 1 kick, 3 kick and ban = 3 * 3 = 9 before bot will ban :)

You can type just message and after X messages will be ban :)

try this ver :>
Last edited by tomekk on Fri Jan 02, 2009 5:35 am, edited 2 times in total.
T
Torrevado
Op
Posts: 101
Joined: Wed Aug 02, 2006 6:29 pm

Post by Torrevado »

It's perfect, thank you very much Tomekk :)
T
Torrevado
Op
Posts: 101
Joined: Wed Aug 02, 2006 6:29 pm

Post by Torrevado »

Tommek, I have a problem.
When I set ban type 2, the script removes perfectly the ban in file with banned hosts, but not in chans...
It seems to work fine with the other 3 ban types.
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

Torrevado wrote:Tommek, I have a problem.
When I set ban type 2, the script removes perfectly the ban in file with banned hosts, but not in chans...
It seems to work fine with the other 3 ban types.
ban type 2, 1 minute:
13:45:51 < rambo> bad
13:45:51 < rambo> bad
13:45:53 < rambo> bad
13:45:54 -!- mode/#chan [+b *!*@bux184.internetdsl.tpnet.pl] by botty
13:45:54 -!- rambo was kicked from #chan by botty [banned!]
13:46:58 -!- mode/#chan [-b *!*@bux184.internetdsl.tpnet.pl] by botty
hmm, any special chars in chann name or smth, or some wierd host?
T
Torrevado
Op
Posts: 101
Joined: Wed Aug 02, 2006 6:29 pm

Post by Torrevado »

I think it could be a shell's problem (they're upgrading freeBSD) or maybe the script is chocking with another one...
Sometimes it works (for me) but sometimes it doesn't...

There are no special chars in channel name or host :)
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

hmm, maybe its to fast, try to change:

Code: Select all

putquick "MODE $host_ban_chan -b $host_ban_mask"
to:

Code: Select all

putserv "MODE $host_ban_chan -b $host_ban_mask"
T
Torrevado
Op
Posts: 101
Joined: Wed Aug 02, 2006 6:29 pm

Post by Torrevado »

Both putquick and putserv works in the same way.
I noticed the script works better in short bans (1-2 mins) but (in my case) it doesn't work with, for example, a 5 minutes ban.

Thanks anyway, tomekk
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

Torrevado wrote:Both putquick and putserv works in the same way.
I noticed the script works better in short bans (1-2 mins) but (in my case) it doesn't work with, for example, a 5 minutes ban.

Thanks anyway, tomekk
hmm, hard to say
I just tested it with 7-10 minutes, with diffrent ban masks, with many hosts and all is working good

Try to add before "putquick "MODE $host_ban_chan -b $host_ban_mask"" in ban_checker proc:

Code: Select all

putquick "PRIVMSG #chan :test"
Replace #chan with your chan name, then run script with ban (5 and more minutes), add some ban and wait.

We will see if bot will print "test" or no.

Are you using this script on one channel or multiple chans?
T
Torrevado
Op
Posts: 101
Joined: Wed Aug 02, 2006 6:29 pm

Post by Torrevado »

First of all, thanks for your patience, tomekk :)

I tried that test message and the problem was still there (some times it worked and sometimes it didn't). So, I re-compiled my egg several times, but nothing to do. I decided to delete all my bot folders and start a new eggdrop install. Now it works like a charm, always, for all type of bans and for any ban time selected (even +5 minutes) :)

Anyway, I changed every "putquick" in the script for "putserv" to let my egg have a restful life (he's getting old :P ).

I use your script only for one chan, but my bot is opped on more channels, so I added the line "if {[lsearch {#mychannelname} $chan] == -1} return" to proc word_fct.

Code: Select all

proc word_fct { nick uhost hand chan arg } {
if {[lsearch {#mychannelname} $chan] == -1} return 
     global botnick protectops usersdir waitb waitb_type waitb_message want_ban kmsg bmsg offence_duration ban_duration
Thanks for all, if I had any problem, I'll disturb you again :lol:
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

yeap no problem, anyway thanks for beta tests :)

Hpy Nw Yr
regards
d
demonoid
Voice
Posts: 2
Joined: Mon Jan 19, 2009 12:28 pm

Post by demonoid »

Hi tomekk, I would know if all users on channel can add/delete the bwords with command /msg bot bword add/del word. If yes it's possible change this command (change "bword" with a myrandom code), for prevently abusing? Thanks you.
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

sure, go on :)
p
pen
Voice
Posts: 4
Joined: Thu Apr 23, 2009 2:04 pm

Post by pen »

I'd like to use this script to just kick and not ban, but I'm not sure what to change to do that. I've tried setting "set want_ban 0" and that doesn't do it, and neither does commenting out that line. Help would be much appreciated :)
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

Code: Select all

# description: Kicks/Bans people who say bad words. Words are stored in a file. Op protect is allowed. You can add/del/list words via priv msg. You don't need to rehash/restart your bot. You can define ban duration time etc. Script is very simple to use.

# Author: tomekk 
# e-mail:  tomekk/@/oswiecim/./eu/./org 
# home page: http://tomekk.oswiecim.eu.org/ 
# 
# Version 0.8
# 
# This file is Copyrighted under the GNU Public License. 
# http://www.gnu.org/copyleft/gpl.html 

# default ban type is: *!*ident*@some.domain.com [4] 

# added: you can now choose badwords script channels
# fixed: want_ban

# USE '/msg <botnick> bword' for more information 

# check for 'badwords' on channels:
set bdw_channels {#channel #channel2}

# sets ban type 
# 1)  *!*@*.domain.com 
# 2)  *!*@some.domain.com 
# 3)  *nick*!*@some.domain.com 
# 4)  *!*ident*@some.domain.com 
set btype 4 

# 1 - protect ops, 0 - don't
# 1 means that user with +o can use 'badwords'
set protectops 0

# 1 - enable ban, 0 - disable ban (just kick after bad word)
set want_ban 1

# wait 'waitb' times before ban, works only when want_ban is set to 1
# if you want to ban user after one bad word, set 'waitb' to 0 
set waitb 3 

# 'waitb' info type, works only when want_ban is set to 1
# 1 - kick after bad word and ban after 'waitb' times 
# 2 - write message after bad word to the nick, 'nick: message' and ban after 'waitb' times 
# 3 - do nothing, just ban after 'waitb' times
set waitb_type 2

# if you set 'waitb' to 2, then here is a option to set this message 
set waitb_message "watch your language kid !" 

# ban duration time (minutes) 
# 0 - infinite 
set ban_duration 0

# "offence" duration time, how long bot should remember last bad word time (minutes) 
# 0 - infinite 
set offence_duration 0 

# kick message 
set kmsg "bad word! - kick" 

# ban message 
set bmsg "banned!" 

# dirs and files, better leave it ;)
# base directory 
set dirname "badwords.db" 

# users directory 
set usersdir "$dirname/lusers" 

# banned hosts file 
set banned_hosts "$dirname/bndhosts" 

# dbase with bad words 
set bw_dbase "$dirname/words_bank.db" 

##################################################################
if {[file exists $dirname] == 0} {
	file mkdir $dirname
	set crtdb [open $bw_dbase a+]
        puts $crtdb "[censored]\nshit"
        close $crtdb
}

if {[file exists $usersdir] == 0} {
	file mkdir $usersdir
}

proc create_db { dbname definfo } {
	if {[file exists $dbname] == 0} {
		set crtdb [open $dbname a+]
		puts $crtdb "$definfo"
		close $crtdb
	}
}

proc readdb { rdb } {
	set fs_open [open $rdb r]
	gets $fs_open dbout
	close $fs_open

	return $dbout
}

proc get_all_bwords { } {
	global bw_dbase

	set bf [open $bw_dbase r]
	set allwords [read $bf]
	close $bf

	set bw_split [split $allwords "\n"]
        return $bw_split
}

proc add_new_bword { bword } {
	global bw_dbase

	set bfile_handle [open $bw_dbase a]
	puts $bfile_handle "$bword"
	close $bfile_handle
}

proc add_new_bnd_host { new_host  } {
	global banned_hosts

	set bnd_hosts [open $banned_hosts a]
	puts $bnd_hosts $new_host
	close $bnd_hosts
}

proc delete_bword { dbword {dlall 0}} {
	global bw_dbase

	set old_bwdb [get_all_bwords]
	
	set file_tow [open $bw_dbase w]
	
	if {$dlall == 0} {
		foreach chk_bw $old_bwdb {
			if {$chk_bw != $dbword} {
				if {$chk_bw != ""} {
					puts $file_tow $chk_bw
				}
			}
		}
	}
	close $file_tow
}

proc check_bw_exist { chword } {
	set old_chbwdb [get_all_bwords]

        set bw_exist 0

	foreach check_bword $old_chbwdb {
		if {$check_bword == $chword} {
			 set bw_exist 1
		 }
	}
	return $bw_exist
}

proc ban_checker_timer { } {
	ban_checker

	if {[string match *ban_checker_timer* [utimers]] != 1} {
		utimer 20 ban_checker_timer
	}
}

bind pubm - * word_fct
bind msgm - "*" bpmsg_fct

proc word_fct { nick uhost hand chan arg } {
  	global botnick protectops usersdir waitb waitb_type waitb_message want_ban kmsg bmsg offence_duration ban_duration bdw_channels

	if {[lsearch $bdw_channels $chan] == -1} {
		return                                
	}  

	set nasty_words [get_all_bwords]

	set kill 0

	set ban_status 0

	set bb_msg $kmsg

	if {[isop $botnick $chan]} {
		foreach i [string tolower $nasty_words] {
			if {$i != ""} {
				if {[string match *$i* [string tolower $arg]]} {
					if {$protectops == 0} {
						set kill 1
					} {
						if {[isop $nick $chan] == 1} {
							continue
						} {
							set kill 1
						}
					}
				}
			}
		}
		
		if {$kill == 1} {
			if {$want_ban == 1} {
				set time_in_seconds [clock seconds]
				set user_ban_mask [banmask $uhost $nick]

				if {$waitb == 0} {
					putquick "MODE $chan +b $user_ban_mask"

					if {$ban_duration > 0}  {
						add_new_bnd_host "$chan&$user_ban_mask&$time_in_seconds"
					}

					set bb_msg $bmsg

					putkick $chan $nick $bb_msg

					set ban_status 1
				} {
					if {[file exists $usersdir/$nick] == 0} {
						create_db "$usersdir/$nick" "$uhost&1&$time_in_seconds"
					} { 
						set each_user_data [split [readdb "$usersdir/$nick"] "&"]
						set ident [lindex $each_user_data 0]
						set times [lindex $each_user_data 1]
						set first_time [lindex $each_user_data 2]

						if {([expr $time_in_seconds - $first_time] >= [expr $offence_duration * 60]) && ($offence_duration > 0)} {
							file delete $usersdir/$nick
							create_db "$usersdir/$nick" "$uhost&1&$time_in_seconds"
						} {
							file delete $usersdir/$nick

							set overall [expr $times + 1]

							create_db "$usersdir/$nick" "$ident&$overall&$first_time"
							set ident [lindex [split [readdb "$usersdir/$nick"] "&"] 0]

							if {$uhost == $ident} {				
								if {$overall >= $waitb} {
									putquick "MODE $chan +b $user_ban_mask"

									if {$ban_duration > 0} {
										add_new_bnd_host "$chan&$user_ban_mask&$time_in_seconds"
									}

									file delete $usersdir/$nick
									set bb_msg $bmsg

									putkick $chan $nick $bb_msg

									set ban_status 1
								}
							} { 
								file delete $usersdir/$nick
								create_db "$usersdir/$nick" "$uhost&1"
							}
						}	
					}
				}
			}

			if {$want_ban == 1} {
				if {($ban_status == 0) && ($waitb_type < 3)} {
					if {$waitb_type == 1} {
						putkick $chan $nick $bb_msg
					} {
						putquick "PRIVMSG $chan :$nick: $waitb_message"
					}
				}
			} {
				putkick $chan $nick $bb_msg
			}
		}
	}
}

proc bpmsg_fct { nick uhost hand arg } {
	set all_bargs [split $arg]
	set bargs_opt1 [lindex $all_bargs 0]
	set bargs_opt2 [lindex $all_bargs 1]
	set bargs_opt_newords [string tolower [lrange $all_bargs 2 end]]

	set bw_list [list]
	
	if {$bargs_opt1 == "bword"} {
		if {$bargs_opt2 != ""} {
			if {$bargs_opt2 == "add"} {
				if {$bargs_opt_newords != ""} {
					foreach nbword $bargs_opt_newords {
						if {[check_bw_exist $nbword]} {
							putquick "PRIVMSG $nick :word $nbword allready exists"
						} {
							add_new_bword $nbword
							putquick "PRIVMSG $nick :word $nbword has been added"
						}
					}
				} {
					putquick "PRIVMSG $nick :use: /msg <botnick> bword add <new_word> ... <new_word>"
				}
			} elseif {$bargs_opt2 == "list"} {
				set list_bwords [get_all_bwords]

				if {$list_bwords != ""} {
					foreach bw_word $list_bwords {
						lappend bw_list "$bw_word"
					}
					putquick "PRIVMSG $nick :[join $bw_list ", "]"
				} else {
					putquick "PRIVMSG $nick :db is empty"
				}
			} elseif {$bargs_opt2 == "del"} {
				if {$bargs_opt_newords != ""} {
					if {$bargs_opt_newords == "delall"} {
						delete_bword 0 1
						putquick "PRIVMSG $nick :all words have been deleted"
					} {
						foreach dbword $bargs_opt_newords {
							if {[check_bw_exist $dbword]} {
								delete_bword $dbword
								putquick "PRIVMSG $nick :word $dbword have been deleted"
							} {
								putquick "PRIVMSG $nick :word $dbword not found"
							}
						}
					}
				} {
					putquick "PRIVMSG $nick :use: /msg <botnick> bword del <word> ... <word>"
					putquick "PRIVMSG $nick :if you want to delete all words use: /msg <botnick> del delall"
				}
			}
				
		} {
			putquick "PRIVMSG $nick :use: /msg <botnick> bword <add|del|list>"
		}
	}
}

proc banmask {host nick} {
	global btype

	switch -- $btype {

		1 {
			set mask "*!*@[lindex [split [maskhost $host] "@"] 1]"
		  }

                2 {
			set mask "*!*@[lindex [split $host @] 1]"
                  }

                3 {
  			set mask "*$nick*!*@[lindex [split $host "@"] 1]"
	          }

		4 {
		        set mask "*!*[lindex [split $host "@"] 0]*@[lindex [split $host "@"] 1]"
		  }

		  return $mask
       }
}

proc ban_checker { } {
	global banned_hosts ban_duration

	if {([file exists $banned_hosts]) && ($ban_duration > 0)} {
		set hosts_hand [open $banned_hosts r]
		set give_me_hosts [read $hosts_hand]
		close $hosts_hand

		set time_in_seconds [clock seconds]

		set rewrite_hosts_hand [open $banned_hosts w]
		foreach banned_h [split $give_me_hosts "\n"] {
			if {$banned_h != ""} {
				set split_banned_h [split $banned_h "&"]
				set host_ban_chan [lindex $split_banned_h 0]
				set host_ban_mask [lindex $split_banned_h 1]
				set need_ban_time [lindex $split_banned_h 2]
	
				if {[expr $time_in_seconds - $need_ban_time] >= [expr $ban_duration * 60]} {
					putquick "MODE $host_ban_chan -b $host_ban_mask"
				} {
					puts $rewrite_hosts_hand "$host_ban_chan&$host_ban_mask&$need_ban_time"
				}
			}
		}
		close $rewrite_hosts_hand
	}
}

if {[string match *ban_checker_timer* [utimers]] != 1} {
	        utimer 20 ban_checker_timer
}

putlog "tkbadword.tcl ver 0.8 by tomekk loaded"
ver 0.8

this ver. should work better and now you have to define on which channels script should check for bad words,
i added this because some ppl were asking me about this option, have fun

Code: Select all

# check for 'badwords' on channels:
set bdw_channels {#channel #channel2}
cheers

P.S
thanks
Post Reply