Blob Blame History Raw
From 795d14a968a23ea50f8f96b6a888f69fd60c7f96 Mon Sep 17 00:00:00 2001
From: Michal Schmidt <mschmidt@redhat.com>
Date: Fri, 4 Jan 2019 17:59:15 +0100
Subject: [PATCH 2/3] Revert "Dont test if precis_i18n is available"

This reverts commit 08bde952b9e5fd1a77ff94196d2d6830c0ae4a18.

Until python-precis-i18n is available in Fedora, let's make
its use optional again in Gajim.
---
 gajim/common/helpers.py | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py
index ba3baf72c0..2138ebf5b9 100644
--- a/gajim/common/helpers.py
+++ b/gajim/common/helpers.py
@@ -48,8 +48,6 @@ from encodings.punycode import punycode_encode
 from string import Template
 
 import nbxmpp
-from nbxmpp.stringprepare import nameprep
-import precis_i18n.codec  # pylint: disable=unused-import
 
 from gajim.common import caps_cache
 from gajim.common import configpaths
@@ -57,6 +55,12 @@ from gajim.common.i18n import Q_
 from gajim.common.i18n import _
 from gajim.common.i18n import ngettext
 
+try:
+    import precis_i18n.codec  # pylint: disable=unused-import
+    HAS_PRECIS_I18N = True
+except ImportError:
+    HAS_PRECIS_I18N = False
+
 log = logging.getLogger('gajim.c.helpers')
 
 special_groups = (_('Transports'), _('Not in Roster'), _('Observers'), _('Groupchats'))
@@ -154,7 +158,10 @@ def parse_resource(resource):
     """
     if resource:
         try:
-            return resource.encode('OpaqueString').decode('utf-8')
+            if HAS_PRECIS_I18N:
+                return resource.encode('OpaqueString').decode('utf-8')
+            from nbxmpp.stringprepare import resourceprep
+            return resourceprep.prepare(resource)
         except UnicodeError:
             raise InvalidFormat('Invalid character in resource.')
 
@@ -188,6 +195,7 @@ def prep(user, server, resource):
             if not server 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)
             except UnicodeError:
                 raise InvalidFormat(_('Invalid character in hostname.'))
@@ -198,7 +206,11 @@ def prep(user, server, resource):
         if not user or len(user.encode('utf-8')) > 1023:
             raise InvalidFormat(_('Username must be between 1 and 1023 bytes'))
         try:
-            user = user.encode('UsernameCaseMapped').decode('utf-8')
+            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:
@@ -208,7 +220,11 @@ def prep(user, server, resource):
         if not resource or len(resource.encode('utf-8')) > 1023:
             raise InvalidFormat(_('Resource must be between 1 and 1023 bytes'))
         try:
-            resource = resource.encode('OpaqueString').decode('utf-8')
+            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:
-- 
2.20.1