mirror of
https://github.com/progval/Limnoria.git
synced 2025-05-02 16:31:09 -05:00
Fixed bug #863808; added isIPV6.
This commit is contained in:
parent
8b6733ee5d
commit
a0d2a9da2a
@ -45,6 +45,7 @@ import copy
|
|||||||
import sets
|
import sets
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
|
import socket
|
||||||
import string
|
import string
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import operator
|
import operator
|
||||||
@ -162,7 +163,7 @@ def hostmaskPatternEqual(pattern, hostmask):
|
|||||||
|
|
||||||
_ipchars = string.digits + '.'
|
_ipchars = string.digits + '.'
|
||||||
def isIP(s):
|
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')
|
>>> isIP('255.255.255.255')
|
||||||
1
|
1
|
||||||
@ -170,16 +171,16 @@ def isIP(s):
|
|||||||
>>> isIP('abc.abc.abc.abc')
|
>>> isIP('abc.abc.abc.abc')
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
if s.translate(string.ascii, _ipchars) == '':
|
try:
|
||||||
quads = s.split('.')
|
return bool(socket.inet_aton(s))
|
||||||
if len(quads) <= 4:
|
except socket.error:
|
||||||
for quad in quads:
|
return False
|
||||||
if int(quad) >= 256:
|
|
||||||
return False
|
def isIPV6(s):
|
||||||
return True
|
"""Returns whether or not a given string is an IPV6 address."""
|
||||||
else:
|
try:
|
||||||
return False
|
return bool(socket.inet_pton(socket.AF_INET6, s))
|
||||||
else:
|
except socket.error:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def banmask(hostmask):
|
def banmask(hostmask):
|
||||||
@ -194,7 +195,13 @@ def banmask(hostmask):
|
|||||||
assert isUserHostmask(hostmask)
|
assert isUserHostmask(hostmask)
|
||||||
host = hostFromHostmask(hostmask)
|
host = hostFromHostmask(hostmask)
|
||||||
if isIP(host):
|
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:
|
else:
|
||||||
if '.' in host:
|
if '.' in host:
|
||||||
return '*!*@*%s' % host[host.find('.'):]
|
return '*!*@*%s' % host[host.find('.'):]
|
||||||
|
@ -130,6 +130,11 @@ class FunctionsTestCase(unittest.TestCase):
|
|||||||
self.failUnless(ircutils.isIP('100.100.100.100'))
|
self.failUnless(ircutils.isIP('100.100.100.100'))
|
||||||
self.failUnless(ircutils.isIP('255.255.255.255'))
|
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):
|
def testIsNick(self):
|
||||||
self.failUnless(ircutils.isNick('jemfinch'))
|
self.failUnless(ircutils.isNick('jemfinch'))
|
||||||
self.failUnless(ircutils.isNick('jemfinch0'))
|
self.failUnless(ircutils.isNick('jemfinch0'))
|
||||||
@ -152,6 +157,7 @@ class FunctionsTestCase(unittest.TestCase):
|
|||||||
msg.prefix),
|
msg.prefix),
|
||||||
'%r didn\'t match %r' % (msg.prefix, banmask))
|
'%r didn\'t match %r' % (msg.prefix, banmask))
|
||||||
self.assertEqual(ircutils.banmask('foobar!user@host'), '*!*@host')
|
self.assertEqual(ircutils.banmask('foobar!user@host'), '*!*@host')
|
||||||
|
self.assertEqual(ircutils.banmask('foo!bar@2001::'), '*!*@2001::*')
|
||||||
|
|
||||||
def testSeparateModes(self):
|
def testSeparateModes(self):
|
||||||
self.assertEqual(ircutils.separateModes(['+ooo', 'x', 'y', 'z']),
|
self.assertEqual(ircutils.separateModes(['+ooo', 'x', 'y', 'z']),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user