core (xmpp): move is_local to SatXMPPEntity + type hints + some log level changes.

This commit is contained in:
Goffi 2024-09-06 17:42:07 +02:00
commit 5513da8792
2 changed files with 14 additions and 7 deletions

View file

@ -690,7 +690,7 @@ class SatXMPPEntity(core_types.SatXMPPEntity):
"""
raise NotImplementedError
async def a_send(self, obj):
async def a_send(self, obj: domish.Element) -> None:
# original send method accept string
# but we restrict to domish.Element to make trigger treatments easier
assert isinstance(obj, domish.Element)
@ -704,7 +704,7 @@ class SatXMPPEntity(core_types.SatXMPPEntity):
return
super().send(obj)
def send(self, obj):
def send(self, obj: domish.Element):
defer.ensureDeferred(self.a_send(obj))
async def send_message_data(self, mess_data):
@ -1151,6 +1151,12 @@ class SatXMPPComponent(SatXMPPEntity, component.Component):
def is_admin(self) -> bool:
return False
def is_local(self, jid_: jid.JID) -> bool:
"""Returns True if jid_ use a domain or subdomain of component's host"""
local_host = self.host.split(".")
assert local_host
return jid_.host.split(".")[-len(local_host) :] == local_host
def _create_sub_protocols(self):
self.messageProt = SatMessageProtocol(self.host_app)
self.messageProt.setHandlerParent(self)
@ -1310,7 +1316,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
@return(dict): message data
"""
if message_elt.name != "message":
log.warning(
log.error(
_(
"parse_message used with a non <message/> stanza, ignoring: {xml}".format(
xml=message_elt.toXml()
@ -1323,7 +1329,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
# xmlns may be None when wokkel element parsing strip out root namespace
self.normalize_ns(message_elt, None)
elif message_elt.uri != C.NS_CLIENT:
log.warning(
log.error(
_(
"received <message> with a wrong namespace: {xml}".format(
xml=message_elt.toXml()

View file

@ -764,9 +764,10 @@ class APGateway:
def is_local(self, jid_: jid.JID) -> bool:
"""Returns True if jid_ use a domain or subdomain of gateway's host"""
local_host = self.client.host.split(".")
assert local_host
return jid_.host.split(".")[-len(local_host) :] == local_host
# FIXME: kept for compatiblity, need to be removed in favor of
# "self.client.is_local".
return self.client.is_local(jid_)
async def is_pubsub(self, jid_: jid.JID) -> bool:
"""Indicate if a JID is a Pubsub service"""