Code: Select all
bind pub -|- !whereis whereis
setudef flag whereis
package require http
# Set your filtered list here with or without wildcards,
# only these will be displayed and the order you choose
# here is the exact order in which it will be output. To
# display everything, all the site has to offer and see
# what there is to choose from, use a single wildcard,
# "*", in the list below.
variable whereisFilter [list "city:" "country:"]
proc whereis {nick uhost hand chan text} {
# flag
# useragent
::http::config -useragent "Mozilla/5.0 ; Gecko"
# geturl, with error handler
catch { set http [::http::geturl "http://www.ip-adress.com/ip_tracer/$text" -timeout 5000] } error
# error condition 1: invalid http session
if {![string match -nocase "::http::*" $error]} {
putserv "privmsg $chan :[string totitle $error] \( http://www.ip-adress.com/ip_tracer/$text \)"
return 0
}
# error condition 2: http error
if {![string equal -nocase [::http::status $http] "ok"]} {
putserv "privmsg $chan :[string totitle [::http::status $http]] \( http://www.ip-adress.com/ip_tracer/$text \)"
return 0
}
# no error, get data
set data [::http::data $http]
# cleanup http token
::http::cleanup $http
# cleanse html for parsing
regsub -all {\[<.*?>\]} $data "" data
regsub -all "<script.*?>.*?</script>" $data "" data
regsub -all "<a href.*?</a>" $data "" data
regsub -all "<img src=.*?>" $data "" data
regsub -all {(?:\n|\t|\v|\r|</span>)} $data "" data
regsub -all {<span.*?>} $data "" data
# can we retrieve table fields?
while {[regexp -nocase -- {<th>(.*?)</th>.*?<td>(.*?)</td>} $data x type attrib]} {
lappend output "[string totitle [string trim [string map {"IP country code" "country code" "IP address" " "} $type]]] [string trim $attrib]"
regsub -nocase -- {<th>.*?</th>.*?<td>.*?</td>} $data "" data
}
# do we have output?
if {[info exists output]} {
# yes, determine what exactly to output...
foreach entry $::whereisFilter {
foreach attribute $output {
# does the attribute match any filter masks?
if {[string match -nocase $entry [lindex [split $attribute] 0]]} {
# yes, add to spam list
lappend spamline $attribute
}
}
}
# was a spam list created?
if {[info exists spamline]} {
# if created, does it have any contents?
if {[llength $spamline]} {
# yes, spam first 9
puthelp "privmsg $chan :[join [lrange $spamline 0 8] " ~ "]"
# more than 9?
if {[llength $spamline] > 15} {
# yes, spam the rest
puthelp "privmsg $chan :[join [lrange $spamline 9 [llength $spamline]] " ~ "]"
}
}
} else {
# we have output but nothing in the spam list, declare
puthelp "privmsg $chan :$text returns some useful information for me to reply with, but filtering is preventing me from showing you... ;/"
}
} else {
# we have no output at all, declare
puthelp "privmsg $chan :$text returns no useful information for me to reply with... ;/"
}
# eof
}
Zainul