This script takes the longIP shown in eggdrop's dcc chat logs, for example, and converts them into their dotted quad IP, and can convert dotted quad IP into longIP.
I put this together with a bit of perl code translated to tcl, and some other snippets (I would still appreciate an answer to the question that I posted in the help forum about the inet_addr proc.. I'm using something different in this script for the time being.)
- The regexp will match numbers that are outside the 0-255 range, so you should add some code verifying the numbers before converting the ip to decimal.
- Detecting if the decimal ip provided by a user is within the allowed range can not be done by a simple 'if' - use format %u (which will generate an error if the number is too large)
- Detecting if the input is a decimal or a dotted ip is easy, so you could merge the two public commands and have the script figure out what to do.
Here's some code (the irc part of it is not tested):
proc longip ip {
if {[string is digit -strict $ip]} {
#edit: yes, it was a typo :)
if {![catch {format %u $ip}]} {n2a $ip}
} else {
if {![regexp {^([0-9]{1,3}\.){3}[0-9]{1,3}$} $ip]} {return ""}
foreach o [split $ip[set ip {}] .] {
# format %g to avoid "invalid octal number" errors:
set o [format %g $o]
if {$o>255} {return ""} {lappend ip $o}
}
a2n [join $ip .]
}
}
proc irclongip {p n u h c {a ""}} {
if {$p} {
if {[onchan $n]||[validuser $n]} {set a $c; set c $n} else {return}
}
# no need to trim both $a and $c
set a [string trim $a]
if {[set b [longip $a]]!=""} {
puthelp "PRIVMSG $c :$a => $b"
} else {
puthelp "PRIVMSG $c :Invalid IP '$a' - IP must be a valid dotted quad IP or LongIP"
}
}
Last edited by user on Mon Dec 18, 2006 5:56 pm, edited 1 time in total.