This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

LongIP QuadIP Convertor script

Support & discussion of released scripts, and announcements of new releases.
Post Reply
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

LongIP QuadIP Convertor script

Post by rosc2112 »

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
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

A couple of suggestions...

Post by user »

- 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: Select all

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"?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

<stares in awe>

WOW! You're awesome :)
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

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: Select all

bind msg - .lip longipmsg
proc longipmsg {n u h a} {
        if {![onchan $n]} {return}
        irclongip 1 $n $u $h $a ""
        return
}
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

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.
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

I uploaded the fixed script to the url above. Also added checking whether user is either on channel or validuser for the msg bind.
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

I edited the script while you were testing I guess (the 'a' vs '{a ""}' thing) ... See comments in the code below:

Code: Select all

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.
Have you ever read "The Manual"?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Is this a typo:

Code: Select all

   if {[string is digit -strict $ip]} {
   } else { 

The original used:

Code: Select all

if {[string is digit -strict $ip]} {
     if {![catch {format %u $ip}]} {n2a $ip}
} else {
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Obviously it's a typo, just remove the "} else {" line.
Post Reply