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

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

##########################################################################
## This script provides tools for managing user's hostmasks on the bot. ##
##########################################################################

##############
## COMMANDS ##
##################################################
## DCC ## .hosttools <command> (Can be changed)	##
######### Use .hosttools help for a full list	##
######### of commands.				##
##################################################

##########################################################
## Just load the script, set the variables, and rehash.	##
##########################################################

################################################################
# Set the flag required for using the hosttools commands here. #
################################################################

set hosttools_setting(flag) "+m"

##########################################################
# Set the command for using the hosttools commands here. #
##########################################################

set hosttools_setting(cmd) "hosttools"

###################################
# Enable use of bold in DCC chat? #
###################################

set hosttools_setting(bold) 1

#############################################
# Prefix "HOSTTOOLS:" in DCC chat messages? #
#############################################

set hosttools_setting(HOSTTOOLS:) 1

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

if {![string match 1.6.* $version]} {
	putlog "\002HOSTTOOLS:\002 \002CRITICAL ERROR\002 HostTools.tcl requires eggdrop 1.6.x to run."
	die "\002HOSTTOOLS:\002 \002CRITICAL ERROR\002 HostTools.tcl requires eggdrop 1.6.x to run."
}
bind dcc $hosttools_setting(flag) $hosttools_setting(cmd) hosttools_hosttools

proc hosttools_dopre {} {
	global hosttools_setting
	if {!$hosttools_setting(HOSTTOOLS:)} {
		return ""
	} elseif {!$hosttools_setting(bold)} {
		return "HOSTTOOLS: "
	} else {
		return "\002HOSTTOOLS:\002 "
	}
}
proc hosttools_putidx {idx text} {
	putidx $idx "[hosttools_dopre]$text"
}
proc hosttools_hosttools {hand idx text} {
	global hosttools_setting
	set cmd [lindex [split $text] 0]
	set arg1 [lindex [split $text] 1]
	set arg2 [lindex [split $text] 2]
	if {($cmd == "") || (([string match -nocase help $cmd]) && ($arg1 == ""))} {
		hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) <command>"
		hosttools_putidx $idx "Commands:"
		hosttools_putidx $idx "Clear - Clears all hotmasks for the specified user or bot."
		hosttools_putidx $idx "ClearAll - Clears all hotmasks for all users and bots."
		hosttools_putidx $idx "ClearAllMatch - Clears all hostmasks for a user or bot if one of it's hostmasks matches the mask given."
		hosttools_putidx $idx "ClearBots - Clears all hotmasks for all bots."
		hosttools_putidx $idx "ClearMatch - Clears all matching hostmasks from all users and bots."
		hosttools_putidx $idx "ClearUsers - Clears all hotmasks for all users."
		hosttools_putidx $idx "See .$hosttools_setting(cmd) help <command> for more information."
	} elseif {[string match -nocase help $cmd]} {
		if {[string match -nocase clear $arg1]} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) Clear <user or bot>"
			hosttools_putidx $idx "This command clears all hotmasks for the specified user or bot."
		} elseif {[string match -nocase clearall $arg1]} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearAll"
			hosttools_putidx $idx "This command clears all hotmasks for all users and bots."
		} elseif {[string match -nocase clearallmatch $arg1]} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearMatch <mask>"
			hosttools_putidx $idx "This command clears all hostmasks for"
			hosttools_putidx $idx "a user or bot if one of the user's hostmasks"
			hosttools_putidx $idx "matches the mask given."
		} elseif {[string match -nocase clearbots $arg1]} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearBots"
			hosttools_putidx $idx "This command clears all hotmasks for all bots."
		} elseif {[string match -nocase clearallmatch $arg1]} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearAllMatch <mask>"
			hosttools_putidx $idx "This command clears all matching hostmasks"
			hosttools_putidx $idx "from all users and bots."
		} elseif {[string match -nocase clearbots $arg1]} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearUsers"
			hosttools_putidx $idx "This command clears all hotmasks for all users."
		} else {
			hosttools_putidx $idx "No help is available for that command."
		}
	} elseif {[string match -nocase clear $cmd]} {
		if {$arg1 == ""} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) Clear <user>"
		} elseif {![validuser $arg1]} {
			hosttools_putidx $idx "Invalid user."
		} else {
			setuser $arg1 HOSTS
			hosttools_putidx $idx "All hosts for $arg1 have been cleared."
		}
	} elseif {[string match -nocase clearall $cmd]} {
		set hcl 0
		foreach u [userlist] {
			setuser $u HOSTS
			incr hcl
		}
		hosttools_putidx $idx "All hosts for $hcl users and bots have been cleared."
	} elseif {[string match -nocase clearbots $cmd]} {
		set hcl 0
		foreach u [userlist] {
			if {[matchattr $u b]} {
				setuser $u HOSTS
				incr hcl
			}
		}
		hosttools_putidx $idx "All hosts for $hcl bots have been cleared."
	} elseif {[string match -nocase clearallmatch $cmd]} {
		if {$arg1 == ""} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearAllMatch <mask>"
		} else {
			set ghcl 0
			foreach u [userlist] {
				set hcl 0
				foreach h [getuser $u HOSTS] {
					if {[string match -nocase $arg1 $h]} {
						setuser $u HOSTS
						incr hcl
					}
				}
				if {$hcl != 0} {
					hosttools_putidx $idx "All hosts for $u have been cleared."
					incr ghcl
				}
			}
			if {$ghcl == 0} {
				hosttools_putidx $idx "No matches found."
			}
		}
	} elseif {[string match -nocase clearmatch $cmd]} {
		if {$arg1 == ""} {
			hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) ClearMatch <mask>"
		} else {
			set ghcl 0
			foreach u [userlist] {
				set hcl 0
				foreach h [getuser $u HOSTS] {
					if {[string match -nocase $arg1 $h]} {
						delhost $u $h
						incr hcl
					}
				}
				if {$hcl != 0} {
					hosttools_putidx $idx "$hcl hosts for $u have been cleared."
					incr ghcl
				}
			}
			if {$ghcl == 0} {
				hosttools_putidx $idx "No matches found."
			}
		}
	} elseif {[string match -nocase clearusers $cmd]} {
		set hcl 0
		foreach u [userlist] {
			if {![matchattr $u b]} {
				setuser $u HOSTS
				incr hcl
			}
		}
		hosttools_putidx $idx "All hosts for $hcl users have been cleared."
	} else {
		hosttools_putidx $idx "Usage: .$hosttools_setting(cmd) <command>"
		hosttools_putidx $idx "See .$hosttools_setting(cmd) help for a command list."
	}
}
putlog "\002HOSTTOOLS:\002 HostTools.tcl Version 1.5 by Wcc is loaded."