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-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-gupnpigd-1.0 for better NAT traversing</li>
<li>gir1.2-networkmanager-1.0 for network lose detection</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> </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> <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 Q_
from common.i18n import ngettext 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 HAS_SOUND = True
if sys.platform == 'win32': if sys.platform == 'win32':
try: try:
@ -244,8 +256,11 @@ def parse_resource(resource):
""" """
if resource: if resource:
try: try:
from nbxmpp.stringprepare import resourceprep if HAS_PRECIS_I18N:
return resourceprep.prepare(resource) return resource.encode('Nickname').decode('utf-8')
else:
from nbxmpp.stringprepare import resourceprep
return resourceprep.prepare(resource)
except UnicodeError: except UnicodeError:
raise InvalidFormat('Invalid character in resource.') raise InvalidFormat('Invalid character in resource.')
@ -274,33 +289,44 @@ def prep(user, server, resource):
if not ip_address: if not ip_address:
if server is not None: if server is not None:
if len(server) < 1 or len(server) > 1023: if server.endswith('.'): # RFC7622, 3.2
raise InvalidFormat(_('Server must be between 1 and 1023 chars')) 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: try:
from nbxmpp.stringprepare import nameprep if HAS_IDNA:
server = nameprep.prepare(server) server = idna.encode(server).decode('utf-8')
else:
from nbxmpp.stringprepare import nameprep
server = nameprep.prepare(server)
except UnicodeError: except UnicodeError:
raise InvalidFormat(_('Invalid character in hostname.')) raise InvalidFormat(_('Invalid character in hostname.'))
else: else:
raise InvalidFormat(_('Server address required.')) raise InvalidFormat(_('Server address required.'))
if user is not None: if user is not None:
if len(user) < 1 or len(user) > 1023: if len(user) < 1 or len(user.encode('utf-8')) > 1023:
raise InvalidFormat(_('Username must be between 1 and 1023 chars')) raise InvalidFormat(_('Username must be between 1 and 1023 bytes'))
try: try:
from nbxmpp.stringprepare import nodeprep if HAS_PRECIS_I18N:
user = nodeprep.prepare(user) user = user.encode('UsernameCaseMapped').decode('utf-8')
else:
from nbxmpp.stringprepare import nodeprep
user = nodeprep.prepare(user)
except UnicodeError: except UnicodeError:
raise InvalidFormat(_('Invalid character in username.')) raise InvalidFormat(_('Invalid character in username.'))
else: else:
user = None user = None
if resource is not None: if resource is not None:
if len(resource) < 1 or len(resource) > 1023: if len(resource) < 1 or len(resource.encode('utf-8')) > 1023:
raise InvalidFormat(_('Resource must be between 1 and 1023 chars')) raise InvalidFormat(_('Resource must be between 1 and 1023 bytes'))
try: try:
from nbxmpp.stringprepare import resourceprep if HAS_PRECIS_I18N:
resource = resourceprep.prepare(resource) resource = resource.encode('OpaqueString').decode('utf-8')
else:
from nbxmpp.stringprepare import resourceprep
resource = resourceprep.prepare(resource)
except UnicodeError: except UnicodeError:
raise InvalidFormat(_('Invalid character in resource.')) raise InvalidFormat(_('Invalid character in resource.'))
else: else: