diff --git a/plugins/Fun.py b/plugins/Fun.py index 1a7279488..506d26ce1 100644 --- a/plugins/Fun.py +++ b/plugins/Fun.py @@ -77,7 +77,7 @@ class Fun(callbacks.Privmsg): Returns the hexadecimal IP for that IP. """ ip = privmsgs.getArgs(args) - if not ircutils.isIP(ip): + if not utils.isIP(ip): irc.error('%r is not a valid IP.' % ip) return quads = ip.split('.') diff --git a/plugins/Network.py b/plugins/Network.py index b94a12973..475a98df6 100644 --- a/plugins/Network.py +++ b/plugins/Network.py @@ -63,7 +63,7 @@ class Network(callbacks.Privmsg): Returns the ip of or the reverse DNS hostname of . """ host = privmsgs.getArgs(args) - if ircutils.isIP(host): + if utils.isIP(host): hostname = socket.getfqdn(host) if hostname == host: irc.reply('Host not found.') diff --git a/src/ircutils.py b/src/ircutils.py index 0ec6d7a99..d07cc3e55 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -52,6 +52,8 @@ import operator from itertools import imap from cStringIO import StringIO as sio +import utils + def isUserHostmask(s): """Returns whether or not the string s is a valid User hostmask.""" p1 = s.find('!') @@ -161,41 +163,6 @@ def hostmaskPatternEqual(pattern, hostmask): _hostmaskPatternEqualCache[(pattern, hostmask)] = b return b -_ipchars = string.digits + '.' -def isIP(s): - """Returns whether or not a given string is an IPV4 address. - - >>> isIP('255.255.255.255') - 1 - - >>> isIP('abc.abc.abc.abc') - 0 - """ - 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: - try: - socket.inet_pton(socket.AF_INET6, '::') - except socket.error: - # We gotta fake it. - if s.count('::') <= 1: - L = s.split(':') - if len(L) <= 8: - for x in L: - if x: - try: - int(x, 16) - except ValueError: - return False - return True - return False def banmask(hostmask): """Returns a properly generic banning hostmask for a hostmask. @@ -208,11 +175,11 @@ def banmask(hostmask): """ assert isUserHostmask(hostmask) host = hostFromHostmask(hostmask) - if isIP(host): + if utils.isIP(host): L = host.split('.') L[-1] = '*' return '*!*@' + '.'.join(L) - elif isIPV6(host): + elif utils.isIPV6(host): L = host.split(':') L[-1] = '*' return '*!*@' + ':'.join(L) @@ -374,7 +341,8 @@ def replyTo(msg): def dccIP(ip): """Returns in IP in the proper for DCC.""" - assert isIP(ip), 'argument must be a string ip in xxx.yyy.zzz.www format.' + assert utils.isIP(ip), \ + 'argument must be a string ip in xxx.yyy.zzz.www format.' i = 0 x = 256**3 for quad in ip.split('.'): diff --git a/src/utils.py b/src/utils.py index 2f5a6e264..31e1bc7b4 100755 --- a/src/utils.py +++ b/src/utils.py @@ -44,6 +44,7 @@ import re import md5 import sha import types +import socket import string import sgmllib import compiler @@ -534,6 +535,42 @@ def changeFunctionName(f, name, doc=None): newf.__doc__ = doc return newf +_ipchars = string.digits + '.' +def isIP(s): + """Returns whether or not a given string is an IPV4 address. + + >>> isIP('255.255.255.255') + 1 + + >>> isIP('abc.abc.abc.abc') + 0 + """ + 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: + try: + socket.inet_pton(socket.AF_INET6, '::') + except socket.error: + # We gotta fake it. + if s.count('::') <= 1: + L = s.split(':') + if len(L) <= 8: + for x in L: + if x: + try: + int(x, 16) + except ValueError: + return False + return True + return False + if __name__ == '__main__': import sys, doctest doctest.testmod(sys.modules['__main__']) diff --git a/test/test_ircutils.py b/test/test_ircutils.py index ca9a27505..55b4554b9 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -122,20 +122,6 @@ class FunctionsTestCase(unittest.TestCase): self.assertEqual(bolds, ircutils.safeArgument(bolds)) self.assertEqual(colors, ircutils.safeArgument(colors)) - def testIsIP(self): - self.failIf(ircutils.isIP('a.b.c')) - self.failIf(ircutils.isIP('256.0.0.0')) - self.failUnless(ircutils.isIP('127.1')) - self.failUnless(ircutils.isIP('0.0.0.0')) - self.failUnless(ircutils.isIP('100.100.100.100')) - # This test is too flaky to bother with. - # 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')) diff --git a/test/test_utils.py b/test/test_utils.py index aa7afefad..1fcbb80c0 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -320,7 +320,19 @@ class UtilsTest(unittest.TestCase): self.assertEqual(list(utils.nonCommentNonEmptyLines(L)), ['foo', 'bar', 'biff']) + def testIsIP(self): + self.failIf(utils.isIP('a.b.c')) + self.failIf(utils.isIP('256.0.0.0')) + self.failUnless(utils.isIP('127.1')) + self.failUnless(utils.isIP('0.0.0.0')) + self.failUnless(utils.isIP('100.100.100.100')) + # This test is too flaky to bother with. + # self.failUnless(utils.isIP('255.255.255.255')) + def testIsIPV6(self): + f = utils.isIPV6 + self.failUnless(f('2001::')) + self.failUnless(f('2001:888:0:1::666')) # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: