use precis_i18n instead of stringprepare when available to sanitize JIDs
This commit is contained in:
parent
c66669ccf3
commit
1a2ac87f46
|
@ -53,6 +53,12 @@ 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
|
||||||
|
|
||||||
HAS_SOUND = True
|
HAS_SOUND = True
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
try:
|
try:
|
||||||
|
@ -244,8 +250,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,8 +283,8 @@ 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 len(server) < 1 or len(server.encode('utf-8')) > 1023:
|
||||||
raise InvalidFormat(_('Server must be between 1 and 1023 chars'))
|
raise InvalidFormat(_('Server must be between 1 and 1023 bytes'))
|
||||||
try:
|
try:
|
||||||
from nbxmpp.stringprepare import nameprep
|
from nbxmpp.stringprepare import nameprep
|
||||||
server = nameprep.prepare(server)
|
server = nameprep.prepare(server)
|
||||||
|
@ -285,22 +294,28 @@ def prep(user, server, resource):
|
||||||
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:
|
||||||
|
|
Loading…
Reference in New Issue