| View previous topic :: View next topic |
| Author |
Message |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Thu Dec 14, 2006 3:46 am Post subject: LongIP QuadIP Convertor script |
|
|
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.)
http://members.dandy.net/~fbn/longip.quadip.tcl.txt |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Thu Dec 14, 2006 12:51 pm Post subject: A couple of suggestions... |
|
|
- 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):
| Code: | proc a2n dot {
format %u [eval format 0x%02x%02x%02x%02x [split $dot .]]
}
proc n2a dec {
join [scan [format %08x $dec] %2x%2x%2x%2x] .
}
proc longip ip {
if {[string is digit -strict $ip]} {
if {![catch {format %u $ip}]} {n2a $ip}
} {
if {![regexp {^([0-9]{1,3}\.){3}[0-9]{1,3}$} $ip]} {return ""}
foreach o [split $ip .] {
if {$o>255} {return ""}
}
a2n $ip
}
}
bind pub - .longip [list irclongip 0]
bind msg - .longip [list irclongip 1]
proc irclongip {p n u h c {a ""}} {
if {$p} {set a $c; set c $n}
if {[set b [longip $a]]!=""} {
puthelp "PRIVMSG $c :$a => $b"
}
} |
_________________ Have you ever read "The Manual"? |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Dec 15, 2006 12:36 pm Post subject: |
|
|
<stares in awe>
WOW! You're awesome  |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Dec 15, 2006 12:52 pm Post subject: |
|
|
OK, the awe is wearing off <grin> I tested the above, the msg bind doesn't work, not sure why since it's the same as the pub bind:
Tcl error [irclongip 1]: wrong # args: should be "irclongip p n u h c a"
I just made a small proc to handle the msg bind:
| Code: |
bind msg - .lip longipmsg
proc longipmsg {n u h a} {
if {![onchan $n]} {return}
irclongip 1 $n $u $h $a ""
return
}
|
|
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Dec 15, 2006 1:00 pm Post subject: |
|
|
Oh wait, did you edit the example script, my first try did not have:
proc irclongip {p n u h c {a ""}} {
That works
I had:
proc irclongip {p n u h c a} {
which didn't work for msg. |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Dec 15, 2006 1:24 pm Post subject: |
|
|
| I uploaded the fixed script to the url above. Also added checking whether user is either on channel or validuser for the msg bind. |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Mon Dec 18, 2006 7:34 am Post subject: |
|
|
I edited the script while you were testing I guess (the 'a' vs '{a ""}' thing) ... See comments in the code below:
| Code: | 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"
}
} |
_________________ Have you ever read "The Manual"?
Last edited by user on Mon Dec 18, 2006 5:56 pm; edited 1 time in total |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Mon Dec 18, 2006 4:12 pm Post subject: |
|
|
Is this a typo:
| Code: |
if {[string is digit -strict $ip]} {
} else {
|
The original used:
| Code: |
if {[string is digit -strict $ip]} {
if {![catch {format %u $ip}]} {n2a $ip}
} else {
|
|
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Mon Dec 18, 2006 5:39 pm Post subject: |
|
|
Obviously it's a typo, just remove the "} else {" line. _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
|