/*_____________________________________________________________________________ |/ / IP2Country add-on by David "Saturn" van Moolenbroek | Version 0.3, released 01-01-2011 -- support on #help.script/Quakenet | Use/modify however you want, but please keep my name in it. Thank you! | | Keywords: IP to country, tree database, file handling | | This add-on provides a simple and inaccurate, but immediate (offline) way | of obtaining a country code from an IP address. It does this by reading | from the database that comes with this add-on, which is automatically | generated from stats data provided by the major IP-block registration | authorities: RIPE, ARIN, APNIC, LACNIC and AFRINIC. | | A typical use of this script would be to write your own, modified on JOIN | event, which displays the country code if someone with an IP address (as | opposed to a hostname) joins a channel. Note that if you want country | names instead of codes, you'll have to use one of the many scripts that | convert country codes to names. | | The script defines the following commands/identifiers: | | /ip2country
| | This command displays the two-letter country code belonging to the given | dotted IP address, or "unknown" if the country could not be found. | | $ip2country(address) | | Returns the two-letter country code for the given dotted IP address, or | $null if the country of the IP address could not be found. | | Simple example | | on *:TEXT:!ip2country &:#:msg $chan Code for $2 $+ : $ip2country($2) \ _\_____________________________________________________________________________ */ alias -l database return " $+ $scriptdir $+ ip2country.db" alias ip2country { if ($0 < 1) { if (!$isid) echo $color(info) -aqt * /ip2country: insufficient parameters return } var %a = $longip($1) if (%a !isnum) { if (!$isid) echo $color(info) -aqt * /ip2country: invalid parameters return } if (!$fopen(country)) { .fopen country $database if ($ferr) { if (!$isid) echo $color(info) -aqt * /ip2country: couldn't open country database .fclose country return } } var %b = 0, %i = 31, %c while (%i > 0) { .fseek country $calc((%b * 4 + $isbit(%a,$calc(%i + 1)) * 2 + $isbit(%a,%i)) * 3) if ($fread(country,3,&d) != 3) break if ($bvar(&d,1) == 255) { %c = $bvar(&d,2,3).text break } %b = $calc($bvar(&d,1) * 65536 + $bvar(&d,2).nword) if (!%b) break dec %i 2 } .fclose country if ($isid) return %c echo $color(info) -aqt * Country for $1 is $iif(%c != $null,$ifmatch,unknown) }