comparison sat/plugins/plugin_xep_0060.py @ 3756:aa923e6b369f

plugin XEP-0060: better filtering when looking for default pubsub service
author Goffi <goffi@goffi.org>
date Fri, 13 May 2022 18:22:06 +0200
parents 783d6dc87b80
children 5bda9d2e8b35
comparison
equal deleted inserted replaced
3755:e14847bf65c0 3756:aa923e6b369f
63 63
64 Extra = namedtuple("Extra", ("rsm_request", "extra")) 64 Extra = namedtuple("Extra", ("rsm_request", "extra"))
65 # rsm_request is the rsm.RSMRequest build with rsm_ prefixed keys, or None 65 # rsm_request is the rsm.RSMRequest build with rsm_ prefixed keys, or None
66 # extra is a potentially empty dict 66 # extra is a potentially empty dict
67 TIMEOUT = 30 67 TIMEOUT = 30
68 # minimum features that a pubsub service must have to be selectable as default
69 DEFAULT_PUBSUB_MIN_FEAT = {
70 'http://jabber.org/protocol/pubsub#persistent-items',
71 'http://jabber.org/protocol/pubsub#publish',
72 'http://jabber.org/protocol/pubsub#retract-items',
73 }
68 74
69 class XEP_0060(object): 75 class XEP_0060(object):
70 OPT_ACCESS_MODEL = "pubsub#access_model" 76 OPT_ACCESS_MODEL = "pubsub#access_model"
71 OPT_PERSIST_ITEMS = "pubsub#persist_items" 77 OPT_PERSIST_ITEMS = "pubsub#persist_items"
72 OPT_MAX_ITEMS = "pubsub#max_items" 78 OPT_MAX_ITEMS = "pubsub#max_items"
313 319
314 def getHandler(self, client): 320 def getHandler(self, client):
315 client.pubsub_client = SatPubSubClient(self.host, self) 321 client.pubsub_client = SatPubSubClient(self.host, self)
316 return client.pubsub_client 322 return client.pubsub_client
317 323
318 @defer.inlineCallbacks 324 async def profileConnected(self, client):
319 def profileConnected(self, client):
320 client.pubsub_watching = set() 325 client.pubsub_watching = set()
321 try: 326 try:
322 client.pubsub_service = jid.JID( 327 client.pubsub_service = jid.JID(
323 self.host.memory.getConfig("", "pubsub_service") 328 self.host.memory.getConfig("", "pubsub_service")
324 ) 329 )
325 except RuntimeError: 330 except RuntimeError:
326 log.info( 331 log.info(
327 _( 332 _(
328 "Can't retrieve pubsub_service from conf, we'll use first one that we find" 333 "Can't retrieve pubsub_service from conf, we'll use first one that "
334 "we find"
329 ) 335 )
330 ) 336 )
331 client.pubsub_service = yield self.host.findServiceEntity( 337 pubsub_services = await self.host.findServiceEntities(
332 client, "pubsub", "service" 338 client, "pubsub", "service"
333 ) 339 )
340 for service_jid in pubsub_services:
341 infos = await self.host.memory.disco.getInfos(client, service_jid)
342 if not DEFAULT_PUBSUB_MIN_FEAT.issubset(infos.features):
343 continue
344 names = {(n or "").lower() for n in infos.identities.values()}
345 if "libervia pubsub service" in names:
346 # this is the name of Libervia's side project pubsub service, we know
347 # that it is a suitable default pubsub service
348 client.pubsub_service = service_jid
349 break
350 categories = {(i[0] or "").lower() for i in infos.identities.keys()}
351 if "gateway" in categories or "gateway" in names:
352 # we don't want to use a gateway as default pubsub service
353 continue
354 if "jabber:iq:register" in infos.features:
355 # may be present on gateways, and we don't want a service
356 # where registration is needed
357 continue
358 client.pubsub_service = service_jid
359 break
360 else:
361 client.pubsub_service = None
362 pubsub_service_str = (
363 client.pubsub_service.full() if client.pubsub_service else "PEP"
364 )
365 log.info(f"default pubsub service: {pubsub_service_str}")
334 366
335 def getFeatures(self, profile): 367 def getFeatures(self, profile):
336 try: 368 try:
337 client = self.host.getClient(profile) 369 client = self.host.getClient(profile)
338 except exceptions.ProfileNotSetError: 370 except exceptions.ProfileNotSetError: