Mercurial > libervia-backend
comparison libervia/backend/memory/disco.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children | 94e0968987cd |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
109 d.addCallback(fill_hashes) | 109 d.addCallback(fill_hashes) |
110 return d | 110 return d |
111 | 111 |
112 | 112 |
113 class Discovery(object): | 113 class Discovery(object): |
114 """ Manage capabilities of entities """ | 114 """Manage capabilities of entities""" |
115 | 115 |
116 def __init__(self, host): | 116 def __init__(self, host): |
117 self.host = host | 117 self.host = host |
118 # TODO: remove legacy hashes | 118 # TODO: remove legacy hashes |
119 | 119 |
170 self, | 170 self, |
171 client: SatXMPPEntity, | 171 client: SatXMPPEntity, |
172 category: str, | 172 category: str, |
173 type_: str, | 173 type_: str, |
174 jid_: Optional[jid.JID] = None, | 174 jid_: Optional[jid.JID] = None, |
175 node: str = "" | 175 node: str = "", |
176 ) -> bool: | 176 ) -> bool: |
177 """Tell if an entity has the requested identity | 177 """Tell if an entity has the requested identity |
178 | 178 |
179 @param category: identity category | 179 @param category: identity category |
180 @param type_: identity type | 180 @param type_: identity type |
262 jid_ = client.server_jid | 262 jid_ = client.server_jid |
263 | 263 |
264 if jid_ == client.server_jid and not node: | 264 if jid_ == client.server_jid and not node: |
265 # we cache items only for our own server and if node is not set | 265 # we cache items only for our own server and if node is not set |
266 try: | 266 try: |
267 items = self.host.memory.entity_data_get( | 267 items = self.host.memory.entity_data_get(client, jid_, ["DISCO_ITEMS"])[ |
268 client, jid_, ["DISCO_ITEMS"] | 268 "DISCO_ITEMS" |
269 )["DISCO_ITEMS"] | 269 ] |
270 log.debug("[%s] disco items are in cache" % jid_.full()) | 270 log.debug("[%s] disco items are in cache" % jid_.full()) |
271 if not use_cache: | 271 if not use_cache: |
272 # we ignore cache, so we pretend we haven't found it | 272 # we ignore cache, so we pretend we haven't found it |
273 raise KeyError | 273 raise KeyError |
274 except (KeyError, exceptions.UnknownEntityError): | 274 except (KeyError, exceptions.UnknownEntityError): |
275 log.debug("Caching [%s] disco items" % jid_.full()) | 275 log.debug("Caching [%s] disco items" % jid_.full()) |
276 items = yield client.disco.requestItems(jid_, nodeIdentifier=node) | 276 items = yield client.disco.requestItems(jid_, nodeIdentifier=node) |
277 self.host.memory.update_entity_data( | 277 self.host.memory.update_entity_data(client, jid_, "DISCO_ITEMS", items) |
278 client, jid_, "DISCO_ITEMS", items | |
279 ) | |
280 else: | 278 else: |
281 try: | 279 try: |
282 items = yield client.disco.requestItems(jid_, nodeIdentifier=node) | 280 items = yield client.disco.requestItems(jid_, nodeIdentifier=node) |
283 except StanzaError as e: | 281 except StanzaError as e: |
284 log.warning( | 282 log.warning( |
379 TIMEOUT, d.cancel | 377 TIMEOUT, d.cancel |
380 ) # FIXME: one bad service make a general timeout | 378 ) # FIXME: one bad service make a general timeout |
381 return d | 379 return d |
382 | 380 |
383 def generate_hash(self, services): | 381 def generate_hash(self, services): |
384 """ Generate a unique hash for given service | 382 """Generate a unique hash for given service |
385 | 383 |
386 hash algorithm is the one described in XEP-0115 | 384 hash algorithm is the one described in XEP-0115 |
387 @param services: iterable of disco.DiscoIdentity/disco.DiscoFeature, as returned by discoHandler.info | 385 @param services: iterable of disco.DiscoIdentity/disco.DiscoFeature, as returned by discoHandler.info |
388 | 386 |
389 """ | 387 """ |
411 s.append(feature) | 409 s.append(feature) |
412 s.append(b"<") | 410 s.append(b"<") |
413 | 411 |
414 # extensions | 412 # extensions |
415 ext = list(services.extensions.values()) | 413 ext = list(services.extensions.values()) |
416 ext.sort(key=lambda f: f.formNamespace.encode('utf-8')) | 414 ext.sort(key=lambda f: f.formNamespace.encode("utf-8")) |
417 for extension in ext: | 415 for extension in ext: |
418 s.append(extension.formNamespace.encode('utf-8')) | 416 s.append(extension.formNamespace.encode("utf-8")) |
419 s.append(b"<") | 417 s.append(b"<") |
420 fields = extension.fieldList | 418 fields = extension.fieldList |
421 fields.sort(key=lambda f: f.var.encode('utf-8')) | 419 fields.sort(key=lambda f: f.var.encode("utf-8")) |
422 for field in fields: | 420 for field in fields: |
423 s.append(field.var.encode('utf-8')) | 421 s.append(field.var.encode("utf-8")) |
424 s.append(b"<") | 422 s.append(b"<") |
425 values = [v.encode('utf-8') for v in field.values] | 423 values = [v.encode("utf-8") for v in field.values] |
426 values.sort() | 424 values.sort() |
427 for value in values: | 425 for value in values: |
428 s.append(value) | 426 s.append(value) |
429 s.append(b"<") | 427 s.append(b"<") |
430 | 428 |
431 cap_hash = b64encode(sha1(b"".join(s)).digest()).decode('utf-8') | 429 cap_hash = b64encode(sha1(b"".join(s)).digest()).decode("utf-8") |
432 log.debug(_("Capability hash generated: [{cap_hash}]").format(cap_hash=cap_hash)) | 430 log.debug(_("Capability hash generated: [{cap_hash}]").format(cap_hash=cap_hash)) |
433 return cap_hash | 431 return cap_hash |
434 | 432 |
435 @defer.inlineCallbacks | 433 @defer.inlineCallbacks |
436 def _disco_infos( | 434 def _disco_infos( |
462 values = [C.bool_const(v) for v in values] | 460 values = [C.bool_const(v) for v in values] |
463 fields.append((data, values)) | 461 fields.append((data, values)) |
464 | 462 |
465 extensions[form_type or ""] = fields | 463 extensions[form_type or ""] = fields |
466 | 464 |
467 defer.returnValue(( | 465 defer.returnValue( |
468 [str(f) for f in disco_infos.features], | 466 ( |
469 [(cat, type_, name or "") | 467 [str(f) for f in disco_infos.features], |
470 for (cat, type_), name in list(disco_infos.identities.items())], | 468 [ |
471 extensions)) | 469 (cat, type_, name or "") |
470 for (cat, type_), name in list(disco_infos.identities.items()) | |
471 ], | |
472 extensions, | |
473 ) | |
474 ) | |
472 | 475 |
473 def items2tuples(self, disco_items): | 476 def items2tuples(self, disco_items): |
474 """convert disco items to tuple of strings | 477 """convert disco items to tuple of strings |
475 | 478 |
476 @param disco_items(iterable[disco.DiscoItem]): items | 479 @param disco_items(iterable[disco.DiscoItem]): items |
484 | 487 |
485 @defer.inlineCallbacks | 488 @defer.inlineCallbacks |
486 def _disco_items( | 489 def _disco_items( |
487 self, entity_jid_s, node="", use_cache=True, profile_key=C.PROF_KEY_NONE | 490 self, entity_jid_s, node="", use_cache=True, profile_key=C.PROF_KEY_NONE |
488 ): | 491 ): |
489 """ Discovery method for the bridge | 492 """Discovery method for the bridge |
490 | 493 |
491 @param entity_jid_s: entity we want to discover | 494 @param entity_jid_s: entity we want to discover |
492 @param node(unicode): optional node to use | 495 @param node(unicode): optional node to use |
493 @param use_cache(bool): if True, use cached data if available | 496 @param use_cache(bool): if True, use cached data if available |
494 @return: list of tuples""" | 497 @return: list of tuples""" |