Merge branch 'use_precis' into 'master'

Use precis instead of stringprep when available. Fixes #8566

See merge request !116
This commit is contained in:
Yann Leboulanger 2017-08-12 14:55:08 +02:00
commit baa54f5010
2 changed files with 41 additions and 14 deletions

1
README
View File

@ -37,6 +37,7 @@
<li>gir1.2-farstream-0.2, gir1.2-gstreamer-1.0 and gir1.2-gst-plugins-base-1.0 for audio and video calls</li>
<li>gir1.2-gupnpigd-1.0 for better NAT traversing</li>
<li>gir1.2-networkmanager-1.0 for network lose detection</li>
<li>python3-idna and python3-precis-i18n for correctly parsing JIDs</li>
</ul>
<p>Some distributions also split too much python standard library. I know SUSE does. In such distros you also need python-xml the xml lib that *comes* with python and not pyxml or whatever.</p>

View File

@ -53,6 +53,18 @@ import nbxmpp
from common.i18n import Q_
from common.i18n import ngettext
try:
import precis_i18n.codec
HAS_PRECIS_I18N = True
except ImportError:
HAS_PRECIS_I18N = False
try:
import idna
HAS_IDNA = True
except ImportError:
HAS_IDNA = False
HAS_SOUND = True
if sys.platform == 'win32':
try:
@ -244,8 +256,11 @@ def parse_resource(resource):
"""
if resource:
try:
from nbxmpp.stringprepare import resourceprep
return resourceprep.prepare(resource)
if HAS_PRECIS_I18N:
return resource.encode('Nickname').decode('utf-8')
else:
from nbxmpp.stringprepare import resourceprep
return resourceprep.prepare(resource)
except UnicodeError:
raise InvalidFormat('Invalid character in resource.')
@ -274,33 +289,44 @@ def prep(user, server, resource):
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'))
if server.endswith('.'): # RFC7622, 3.2
server = server[:-1]
if len(server) < 1 or len(server.encode('utf-8')) > 1023:
raise InvalidFormat(_('Server must be between 1 and 1023 bytes'))
try:
from nbxmpp.stringprepare import nameprep
server = nameprep.prepare(server)
if HAS_IDNA:
server = idna.encode(server).decode('utf-8')
else:
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'))
if len(user) < 1 or len(user.encode('utf-8')) > 1023:
raise InvalidFormat(_('Username must be between 1 and 1023 bytes'))
try:
from nbxmpp.stringprepare import nodeprep
user = nodeprep.prepare(user)
if HAS_PRECIS_I18N:
user = user.encode('UsernameCaseMapped').decode('utf-8')
else:
from nbxmpp.stringprepare import nodeprep
user = nodeprep.prepare(user)
except UnicodeError:
raise InvalidFormat(_('Invalid character in username.'))
else:
user = None
if resource is not None:
if len(resource) < 1 or len(resource) > 1023:
raise InvalidFormat(_('Resource must be between 1 and 1023 chars'))
if len(resource) < 1 or len(resource.encode('utf-8')) > 1023:
raise InvalidFormat(_('Resource must be between 1 and 1023 bytes'))
try:
from nbxmpp.stringprepare import resourceprep
resource = resourceprep.prepare(resource)
if HAS_PRECIS_I18N:
resource = resource.encode('OpaqueString').decode('utf-8')
else:
from nbxmpp.stringprepare import resourceprep
resource = resourceprep.prepare(resource)
except UnicodeError:
raise InvalidFormat(_('Invalid character in resource.'))
else: