mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-18 06:30:57 +01:00
Moved isIP* to utils from ircutils.
This commit is contained in:
parent
b2f23c6f63
commit
29426e2f1d
@ -77,7 +77,7 @@ class Fun(callbacks.Privmsg):
|
|||||||
Returns the hexadecimal IP for that IP.
|
Returns the hexadecimal IP for that IP.
|
||||||
"""
|
"""
|
||||||
ip = privmsgs.getArgs(args)
|
ip = privmsgs.getArgs(args)
|
||||||
if not ircutils.isIP(ip):
|
if not utils.isIP(ip):
|
||||||
irc.error('%r is not a valid IP.' % ip)
|
irc.error('%r is not a valid IP.' % ip)
|
||||||
return
|
return
|
||||||
quads = ip.split('.')
|
quads = ip.split('.')
|
||||||
|
@ -63,7 +63,7 @@ class Network(callbacks.Privmsg):
|
|||||||
Returns the ip of <host> or the reverse DNS hostname of <ip>.
|
Returns the ip of <host> or the reverse DNS hostname of <ip>.
|
||||||
"""
|
"""
|
||||||
host = privmsgs.getArgs(args)
|
host = privmsgs.getArgs(args)
|
||||||
if ircutils.isIP(host):
|
if utils.isIP(host):
|
||||||
hostname = socket.getfqdn(host)
|
hostname = socket.getfqdn(host)
|
||||||
if hostname == host:
|
if hostname == host:
|
||||||
irc.reply('Host not found.')
|
irc.reply('Host not found.')
|
||||||
|
@ -52,6 +52,8 @@ import operator
|
|||||||
from itertools import imap
|
from itertools import imap
|
||||||
from cStringIO import StringIO as sio
|
from cStringIO import StringIO as sio
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
def isUserHostmask(s):
|
def isUserHostmask(s):
|
||||||
"""Returns whether or not the string s is a valid User hostmask."""
|
"""Returns whether or not the string s is a valid User hostmask."""
|
||||||
p1 = s.find('!')
|
p1 = s.find('!')
|
||||||
@ -161,41 +163,6 @@ def hostmaskPatternEqual(pattern, hostmask):
|
|||||||
_hostmaskPatternEqualCache[(pattern, hostmask)] = b
|
_hostmaskPatternEqualCache[(pattern, hostmask)] = b
|
||||||
return 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):
|
def banmask(hostmask):
|
||||||
"""Returns a properly generic banning hostmask for a hostmask.
|
"""Returns a properly generic banning hostmask for a hostmask.
|
||||||
@ -208,11 +175,11 @@ def banmask(hostmask):
|
|||||||
"""
|
"""
|
||||||
assert isUserHostmask(hostmask)
|
assert isUserHostmask(hostmask)
|
||||||
host = hostFromHostmask(hostmask)
|
host = hostFromHostmask(hostmask)
|
||||||
if isIP(host):
|
if utils.isIP(host):
|
||||||
L = host.split('.')
|
L = host.split('.')
|
||||||
L[-1] = '*'
|
L[-1] = '*'
|
||||||
return '*!*@' + '.'.join(L)
|
return '*!*@' + '.'.join(L)
|
||||||
elif isIPV6(host):
|
elif utils.isIPV6(host):
|
||||||
L = host.split(':')
|
L = host.split(':')
|
||||||
L[-1] = '*'
|
L[-1] = '*'
|
||||||
return '*!*@' + ':'.join(L)
|
return '*!*@' + ':'.join(L)
|
||||||
@ -374,7 +341,8 @@ def replyTo(msg):
|
|||||||
|
|
||||||
def dccIP(ip):
|
def dccIP(ip):
|
||||||
"""Returns in IP in the proper for DCC."""
|
"""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
|
i = 0
|
||||||
x = 256**3
|
x = 256**3
|
||||||
for quad in ip.split('.'):
|
for quad in ip.split('.'):
|
||||||
|
37
src/utils.py
37
src/utils.py
@ -44,6 +44,7 @@ import re
|
|||||||
import md5
|
import md5
|
||||||
import sha
|
import sha
|
||||||
import types
|
import types
|
||||||
|
import socket
|
||||||
import string
|
import string
|
||||||
import sgmllib
|
import sgmllib
|
||||||
import compiler
|
import compiler
|
||||||
@ -534,6 +535,42 @@ def changeFunctionName(f, name, doc=None):
|
|||||||
newf.__doc__ = doc
|
newf.__doc__ = doc
|
||||||
return newf
|
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__':
|
if __name__ == '__main__':
|
||||||
import sys, doctest
|
import sys, doctest
|
||||||
doctest.testmod(sys.modules['__main__'])
|
doctest.testmod(sys.modules['__main__'])
|
||||||
|
@ -122,20 +122,6 @@ class FunctionsTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(bolds, ircutils.safeArgument(bolds))
|
self.assertEqual(bolds, ircutils.safeArgument(bolds))
|
||||||
self.assertEqual(colors, ircutils.safeArgument(colors))
|
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):
|
def testIsNick(self):
|
||||||
self.failUnless(ircutils.isNick('jemfinch'))
|
self.failUnless(ircutils.isNick('jemfinch'))
|
||||||
self.failUnless(ircutils.isNick('jemfinch0'))
|
self.failUnless(ircutils.isNick('jemfinch0'))
|
||||||
|
@ -320,7 +320,19 @@ class UtilsTest(unittest.TestCase):
|
|||||||
self.assertEqual(list(utils.nonCommentNonEmptyLines(L)),
|
self.assertEqual(list(utils.nonCommentNonEmptyLines(L)),
|
||||||
['foo', 'bar', 'biff'])
|
['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:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user