changeset 3149:f3700175c6a3

core (xmpp): use a stable resource (sat.[shortuuid]) instead of server one if nothing is specified
author Goffi <goffi@goffi.org>
date Wed, 29 Jan 2020 20:21:21 +0100
parents 60a9e47ef988
children d6f903c59a56
files sat/core/xmpp.py
diffstat 1 files changed, 25 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/sat/core/xmpp.py	Wed Jan 29 19:42:02 2020 +0100
+++ b/sat/core/xmpp.py	Wed Jan 29 20:21:21 2020 +0100
@@ -22,6 +22,7 @@
 import calendar
 import uuid
 from functools import partial
+import shortuuid
 from sat.core.i18n import _
 from sat.core.constants import Const as C
 from sat.memory import cache
@@ -171,9 +172,31 @@
         if profile in host.profiles:
             raise exceptions.InternalError(
                 f"There is already a profile of name {profile} in host")
+
+        entity_jid_s = yield host.memory.asyncGetParamA(
+            "JabberID", "Connection", profile_key=profile)
+        entity_jid = jid.JID(entity_jid_s)
+
+        if not entity_jid.resource and not cls.is_component and entity_jid.user:
+            # if no resource is specified, we create our own instead of using
+            # server returned one, as it will then stay stable in case of reconnection.
+            # we only do that for client and if there is a user part, to let server
+            # decide for anonymous login
+            resource_dict = yield host.memory.storage.getPrivates(
+                "core:xmpp", ["resource"] , profile=profile)
+            try:
+                resource = resource_dict["resource"]
+            except KeyError:
+                resource = f"{C.APP_NAME_FILE}.{shortuuid.uuid()}"
+                yield host.memory.storage.setPrivateValue(
+                    "core:xmpp", "resource", resource, profile=profile)
+
+            log.info(_("We'll use the stable resource {resource}").format(
+                resource=resource))
+            entity_jid.resource = resource
+
         entity = host.profiles[profile] = cls(
-            host, profile, jid.JID(host.memory.getParamA("JabberID", "Connection",
-            profile_key=profile)), password,
+            host, profile, entity_jid, password,
             host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection",
                                   profile_key=profile) or None,
             port, max_retries,