comparison sat/core/xmpp.py @ 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 d8a89a77d765
children 2c7b42f53e9a
comparison
equal deleted inserted replaced
3148:60a9e47ef988 3149:f3700175c6a3
20 import sys 20 import sys
21 import time 21 import time
22 import calendar 22 import calendar
23 import uuid 23 import uuid
24 from functools import partial 24 from functools import partial
25 import shortuuid
25 from sat.core.i18n import _ 26 from sat.core.i18n import _
26 from sat.core.constants import Const as C 27 from sat.core.constants import Const as C
27 from sat.memory import cache 28 from sat.memory import cache
28 from twisted.internet import defer, error as internet_error 29 from twisted.internet import defer, error as internet_error
29 from twisted.internet import ssl 30 from twisted.internet import ssl
169 "Password", "Connection", profile_key=profile 170 "Password", "Connection", profile_key=profile
170 ) 171 )
171 if profile in host.profiles: 172 if profile in host.profiles:
172 raise exceptions.InternalError( 173 raise exceptions.InternalError(
173 f"There is already a profile of name {profile} in host") 174 f"There is already a profile of name {profile} in host")
175
176 entity_jid_s = yield host.memory.asyncGetParamA(
177 "JabberID", "Connection", profile_key=profile)
178 entity_jid = jid.JID(entity_jid_s)
179
180 if not entity_jid.resource and not cls.is_component and entity_jid.user:
181 # if no resource is specified, we create our own instead of using
182 # server returned one, as it will then stay stable in case of reconnection.
183 # we only do that for client and if there is a user part, to let server
184 # decide for anonymous login
185 resource_dict = yield host.memory.storage.getPrivates(
186 "core:xmpp", ["resource"] , profile=profile)
187 try:
188 resource = resource_dict["resource"]
189 except KeyError:
190 resource = f"{C.APP_NAME_FILE}.{shortuuid.uuid()}"
191 yield host.memory.storage.setPrivateValue(
192 "core:xmpp", "resource", resource, profile=profile)
193
194 log.info(_("We'll use the stable resource {resource}").format(
195 resource=resource))
196 entity_jid.resource = resource
197
174 entity = host.profiles[profile] = cls( 198 entity = host.profiles[profile] = cls(
175 host, profile, jid.JID(host.memory.getParamA("JabberID", "Connection", 199 host, profile, entity_jid, password,
176 profile_key=profile)), password,
177 host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection", 200 host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection",
178 profile_key=profile) or None, 201 profile_key=profile) or None,
179 port, max_retries, 202 port, max_retries,
180 ) 203 )
181 204