###########################
### DNS.tcl		###
### Version 1.3		###
### By Wcc		###
### irc.dal.net		###
###########################

####################################################################
## This script does DNS lookup from the channel or the partyline. ##
####################################################################

##############
## COMMANDS ##
##########################################################
## DCC ## .dns <host/ip>			        ##
######### Preforms a dns lookup on the specified host   ##
######### or ip address.			        ##
##########################################################
## PUB ## !nickdns <nick>       		        ##
######### Preforms a dns lookup on the specified nick's ##
######### hostname or ip address.			##
######### -------------------------------------------   ##
######### !dns <host/ip>				##
######### Preforms a dns lookup on the specified host   ##
######### or ip address.			        ##
########################################################## 

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

###################################################
# Set the required flag to use the .dns commands. #
###################################################

set dns_flag "+o"

######################################
# Channels this script is active in. #
######################################

set dns_chans "#betasops #|DAWG|Net"

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

bind pub $dns_flag|$dns_flag !dns dns_pub
bind pub $dns_flag|$dns_flag !nickdns nick_dns_pub
bind dcc $dns_flag dns dns_dcc

proc dns_pub {nick uhost hand chan text} {
	global dns_command dns_chans botnick
	if {(([lsearch -exact [string tolower $dns_chans] [string tolower $chan]] != -1) || ($dns_chans == "*")) && (![matchattr $hand b]) && ($nick != $botnick)} {
		if {$text == ""} {
			putserv "PRIVMSG $chan :DNS: Usage: !dns <host/ip>"
		} else {
			foreach dns_addr [set dns_addrs [split $text {,;| }]] {
				if {[info commands dnslookup] != ""} {
					dnslookup $dns_addr dns_output $chan $text
				}
			}
		}
	}
}

proc nick_dns_pub {nick uhost hand chan text} {
	global dns_chans botnick
	if {(([lsearch -exact [string tolower $dns_chans] [string tolower $chan]] != -1) || ($dns_chans == "*")) && (![matchattr $hand b]) && ($nick != $botnick)} {
		if {$text == ""} {
			putserv "PRIVMSG $chan :DNS: Usage: !nickdns <nick>"
		} else {
			foreach dns_addr [set dns_addrs [lindex [split [getchanhost $text] @] 1]] {
				if {[info commands dnslookup] != ""} {
					dnslookup $dns_addr dns_output $chan $text
				}
			}
		}
	}
}
proc dns_dcc {hand idx text} {
	global dns_command dns_chans botnick
	if {(![matchattr $hand b])} {
		if {$text == ""} {
			putidx $idx "Usage: .dns <host/ip>"
		} else {
			foreach dns_addr [set dns_addrs [split $text {,;| }]] {
				if {[info commands dnslookup] != ""} {
					dnslookup $dns_addr dns_output_dcc $idx $text
				}
			}
		}
	}
}

proc dns_output {dns_ip dns_host dns_status dns_chan dns_addr} {
	if {$dns_status} {
		if {[string match *$dns_ip* $dns_addr]} {
			putserv "PRIVMSG $dns_chan :DNS: $dns_ip has been resolved to $dns_host"
		} else {
			putserv "PRIVMSG $dns_chan :DNS: $dns_host has been resolved to $dns_ip" 
		}
	} else {
		putserv "PRIVMSG $dns_chan :DNS: Could not resolve $dns_addr"
	}
}

proc dns_output_dcc {dns_ip dns_host dns_status idx dns_addr} {
	if {$dns_status} {
		if {[string match *$dns_ip* $dns_addr]} {
			putidx $idx "DNS: $dns_ip has been resolved to $dns_host."
		} else {
			putidx $idx "DNS: $dns_host has been resolved to $dns_ip." 
		}
	} else {
		putidx $idx "DNS: Could not resolve $dns_addr."
	}
}

putlog "DNS: DNS.tcl 1.0 by wcc is loaded."