Fixed bug #863808; added isIPV6.

This commit is contained in:
Jeremy Fincher 2004-01-02 20:50:43 +00:00
parent 8b6733ee5d
commit a0d2a9da2a
2 changed files with 25 additions and 12 deletions

View File

@ -45,6 +45,7 @@ import copy
import sets
import time
import random
import socket
import string
import fnmatch
import operator
@ -162,7 +163,7 @@ def hostmaskPatternEqual(pattern, hostmask):
_ipchars = string.digits + '.'
def isIP(s):
"""Not quite perfect, but close enough until I can find the regexp I want.
"""Returns whether or not a given string is an IPV4 address.
>>> isIP('255.255.255.255')
1
@ -170,16 +171,16 @@ def isIP(s):
>>> isIP('abc.abc.abc.abc')
0
"""
if s.translate(string.ascii, _ipchars) == '':
quads = s.split('.')
if len(quads) <= 4:
for quad in quads:
if int(quad) >= 256:
return False
return True
else:
return False
else:
try:
return bool(socket.inet_aton(s))
except socket.error:
return False
def isIPV6(s):
"""Returns whether or not a given string is an IPV6 address."""
try:
return bool(socket.inet_pton(socket.AF_INET6, s))
except socket.error:
return False
def banmask(hostmask):
@ -194,7 +195,13 @@ def banmask(hostmask):
assert isUserHostmask(hostmask)
host = hostFromHostmask(hostmask)
if isIP(host):
return '*!*@%s.*' % host[:host.rfind('.')]
L = host.split('.')
L[-1] = '*'
return '*!*@' + '.'.join(L)
elif isIPV6(host):
L = host.split(':')
L[-1] = '*'
return '*!*@' + ':'.join(L)
else:
if '.' in host:
return '*!*@*%s' % host[host.find('.'):]

View File

@ -130,6 +130,11 @@ class FunctionsTestCase(unittest.TestCase):
self.failUnless(ircutils.isIP('100.100.100.100'))
self.failUnless(ircutils.isIP('255.255.255.255'))
def testIsIPV6(self):
f = ircutils.isIPV6
self.failUnless(f('2001::'))
self.failUnless(f('2001:888:0:1::666'))
def testIsNick(self):
self.failUnless(ircutils.isNick('jemfinch'))
self.failUnless(ircutils.isNick('jemfinch0'))
@ -152,6 +157,7 @@ class FunctionsTestCase(unittest.TestCase):
msg.prefix),
'%r didn\'t match %r' % (msg.prefix, banmask))
self.assertEqual(ircutils.banmask('foobar!user@host'), '*!*@host')
self.assertEqual(ircutils.banmask('foo!bar@2001::'), '*!*@2001::*')
def testSeparateModes(self):
self.assertEqual(ircutils.separateModes(['+ooo', 'x', 'y', 'z']),