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

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

#######################################################################################
## This script adds a global ban to the bot's banlist without using DCC chat.        ##
## The format for the ban time is <x min(s)/hour(s)/day(s)/week(s)/month(s)/year(s). ## 
## E.g. '!GBan Wcc 1 week BAD Wcc!' would ban my host for 1 week with the reason     ##								     ##
## 'BAD Wcc!' "Perm" makes the ban permanent.					     ##
#######################################################################################

##############
## COMMANDS ##
###################################################################
## MSG ## /msg <botnick> GBan <nick or hostmask> <time> <reason> ##
######### Adds a ban to the bot's banlist. The time is appended  ##
######### to the end of the ban reason.				 ##
###################################################################
## PUB ## !GBan <nick or hostmask> <time> <reason>		 ##
######### Adds a ban to the bot's banlist. The time is appended	 ##
######### to the end of the ban reason.				 ##
###################################################################

######################################
## Just load the script and rehash. ##
######################################

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

if {![string match 1.6.* $version]} {
	putlog "\002GBAN:\002 \002CRITICAL ERROR\002 GBan.tcl requires eggdrop 1.6.x to run."
	die "\002GBAN:\002 \002CRITICAL ERROR\002 GBan.tcl requires eggdrop 1.6.x to run."
}
bind pub +o !gban gban_gban_pub
bind msg +o gban gban_gban_msg

proc gban_gban_pub {nick uhost hand chan text} {
	if {[lindex [split $text] 1] == ""} {
		putserv "NOTICE $nick :Usage: !gban <nick or hostmask> <time> <reason>"
	} else {
		if {[string match -nocase "Perm" [lindex [split $text] 1]]} {
			set time 0
		} elseif {[string match "* min*" [lrange [split $text] 1 2]]} {
			set time [lindex [split $text] 1]
		} elseif {[string match "* hour*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 60]
		} elseif {[string match "* day*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 1440]
		} elseif {[string match "* week*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 10080]
		} elseif {[string match "* month*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 43200]
		} elseif {[string match "* year*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 525600]
		} else {
			set time ""
		}
		set onch 0
		foreach c [channels] {
			if {[onchan [lindex $text 0] $c]} {
				set banmask [maskhost [getchanhost [lindex [split $text] 0]]]
				set onch 1
			}
		}
		if {!$onch} {
			set banmask [lindex $text 0]
			set bnck ""
		}
		if {$time == ""} {
			putserv "NOTICE $nick :Global ban added: $banmask.  \([lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)\)"
		} elseif {$time == 0} {
			putserv "NOTICE $nick :Global ban added: $banmask. Never Expires. \([lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)\)"
		} else {
			putserv "NOTICE $nick :Global ban added: $banmask. Expires in [lrange [split $text] 1 2]. \([lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)\)"
		}
		newban $banmask $nick "[lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)" $time
		foreach c [channels] {
			foreach u [chanlist $chan] {
				if {[string match -nocase $banmask $u![getchanhost $u $c]]} {
					putserv "KICK $c $u :[lrange [split $text] 3 end]"
				}
			}
		}
	}
}
proc gban_gban_msg {nick uhost hand text} {
	if {[lindex [split $text] 1] == ""} {
		putserv "NOTICE $nick :Usage: !gban <nick or hostmask> <time> <reason>"
	} else {
		if {[string match -nocase "Perm" [lindex [split $text] 1]]} {
			set time 0
		} elseif {[string match "* min*" [lrange [split $text] 1 2]]} {
			set time [lindex [split $text] 1]
		} elseif {[string match "* hour*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 60]
		} elseif {[string match "* day*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 1440]
		} elseif {[string match "* week*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 10080]
		} elseif {[string match "* month*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 43200]
		} elseif {[string match "* year*" [lrange [split $text] 1 2]]} {
			set time [expr [lindex [split $text] 1] * 525600]
		} else {
			set time ""
		}
		set onch 0
		foreach c [channels] {
			if {[onchan [lindex $text 0] $c]} {
				set banmask [maskhost [getchanhost [lindex [split $text] 0]]]
				set onch 1
			}
		}
		if {!$onch} {
			set banmask [lindex $text 0]
			set bnck ""
		}
		if {$time == ""} {
			putserv "NOTICE $nick :Global ban added: $banmask.  \([lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)\)"
		} elseif {$time == 0} {
			putserv "NOTICE $nick :Global ban added: $banmask. Never Expires. \([lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)\)"
		} else {
			putserv "NOTICE $nick :Global ban added: $banmask. Expires in [lrange [split $text] 1 2]. \([lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)\)"
		}
		newban $banmask $nick "[lrange [split $text] 3 end] \([clock format [clock seconds] -format "%D %I:%M %p"]\)" $time
		foreach c [channels] {
			foreach u [chanlist $chan] {
				if {[string match -nocase $banmask $u![getchanhost $u $c]]} {
					putserv "KICK $c $u :[lrange [split $text] 3 end]"
				}
			}
		}
	}
}
putlog "\002GBAN:\002 GBan Version 1.3 by Wcc is loaded."