Support IPV6 and IPV4 domainparts

https://tools.ietf.org/html/rfc7622#section-3.2
This commit is contained in:
Philipp Hörist 2017-06-16 23:40:42 +02:00
parent 4650615010
commit 49a78ae8ed
1 changed files with 29 additions and 11 deletions

View File

@ -173,6 +173,35 @@ def prep(user, server, resource):
"""
# This function comes from
#http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py
ip_address = False
try:
socket.inet_aton(server)
ip_address = True
except socket.error:
pass
if not ip_address and hasattr(socket, 'inet_pton'):
try:
socket.inet_pton(socket.AF_INET6, server.strip('[]'))
server = '[%s]' % server.strip('[]')
ip_address = True
except (socket.error, ValueError):
pass
if not ip_address:
if server is not None:
if len(server) < 1 or len(server) > 1023:
raise InvalidFormat(_('Server must be between 1 and 1023 chars'))
try:
from nbxmpp.stringprepare import nameprep
server = nameprep.prepare(server)
except UnicodeError:
raise InvalidFormat(_('Invalid character in hostname.'))
else:
raise InvalidFormat(_('Server address required.'))
if user is not None:
if len(user) < 1 or len(user) > 1023:
raise InvalidFormat(_('Username must be between 1 and 1023 chars'))
@ -184,17 +213,6 @@ def prep(user, server, resource):
else:
user = None
if server is not None:
if len(server) < 1 or len(server) > 1023:
raise InvalidFormat(_('Server must be between 1 and 1023 chars'))
try:
from nbxmpp.stringprepare import nameprep
server = nameprep.prepare(server)
except UnicodeError:
raise InvalidFormat(_('Invalid character in hostname.'))
else:
raise InvalidFormat(_('Server address required.'))
if resource is not None:
if len(resource) < 1 or len(resource) > 1023:
raise InvalidFormat(_('Resource must be between 1 and 1023 chars'))