############################
### ChanMax.tcl          ###
### Version 1.2          ###
### By Wcc               ###
### wcc@techmonkeys.org  ###
### http://dawg.dynu.com ###
### EFnet #|DAWG|Tcl     ###
############################

############################################################################
### Copyright © 2000 - 2001 |DAWG| Scripting Group. All rights reserved. ###
############################################################################

####################################################################
## This script bans users from +chanmax channels that are on more ## 
## than X amount of channels. Thanks to iif for the idea for this ##
## script.                                                        ##
####################################################################

##############
## COMMANDS ##
########################################
## DCC ## .chanset <channel> +chanmax ##
######### Enables on-join max-channel ##
######### checking for a channel.     ##
########################################

##########################################################
## Just load the script, edit the settings, and rehash. ##
##########################################################

#########################################################
# Set the max amount of channels a user can be in here. #
#########################################################

set chanmax_setting(max) "10"

#####################################
# Set the kick message to use here. #
#####################################

set chanmax_setting(msg) "You are on too many channels."

#############################
# Set the ban type here:    #
# 1 - *!*@host.domain       #
# 2 - *!user@host.domain    #
# 3 - nick!*@host.domain    #
# 4 - nick!user@host.domain #
# 5 - *!?user@*.host.domain #
#############################

set chanmax_setting(bantype) "1"

####################
# Code begins here #
####################

if {![string match 1.6.* $version]} { putlog "\002CHANMAX:\002 \002WARNING:\002 This script is intended to run on eggdrop 1.6.x or later." }
if {[info tclversion] < 8.2} { putlog "\002CHANMAX:\002 \002WARNING:\002 This script is intended to run on Tcl Version 8.2 or later." }

bind raw - 319 chanmax_getchans
bind join - * chanmax_join

setudef flag chanmax

proc chanmax_maskban {nick uhost} {
	global chanmax_setting
	switch -- $chanmax_setting(bantype) {
		1 { set ban "*!*@[lindex [split $uhost @] 1]" }
		2 { set ban "*!$uhost" }
		3 { set ban "$nick!*@[lindex [split $uhost @] 1]" }
		4 { set ban "$nick!$uhost" }
		5 { set ban "[maskhost $uhost]" }
		default { set ban "*!*@[lindex [split $uhost @] 1]" }
	}
	return $ban
}
proc chanmax_join {nick uhost hand chan} {
	if {[lsearch -exact [channel info $chan] +chanmax] != -1 && ![string match [string tolower $nick] [string tolower $botnick]] && [botisop $chan] && ![matchattr $hand o|o $chan]} { puthelp "WHOIS $nick" }
}
proc chanmax_getchans {from keyword text} {
	global chanmax_setting
	if {[llength [lrange [split $text] 2 end]] <= $chanmax_setting(max)} { return }
	foreach chan [channels] {
		if {[lsearch -exact [channel info $chan] +chanmax] == -1 || ![botisop $chan] || [matchattr [nick2hand [set nick [lindex [split $text] 1]]] o|o $chan]} { break }
		putserv "MODE $chan +b *!*@[chanmax_maskban $nick [getchanhost $nick]]"
		putserv "KICK $chan $nick :$chanmax_setting(msg)"
	}
}
putlog "\002CHANMAX:\002 ChanMax.tcl Version 1.2 by Wcc is loaded."