core (xmpp): add origin and origin_label to message data:
If a message comes from a gateway, its type is used as `origin` in `extra`, and it has a name, it's used as `origin_label`. `is-bot` is also set when the sending entity is advertised as a bot. rel 459
This commit is contained in:
parent
14f50afa64
commit
ceb2484a37
1 changed files with 25 additions and 5 deletions
|
|
@ -24,7 +24,7 @@ import mimetypes
|
|||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
from typing import Callable, Dict, Tuple, Optional
|
||||
from typing import Callable, Dict, Tuple, Optional, cast
|
||||
from urllib.parse import unquote, urlparse
|
||||
import uuid
|
||||
|
||||
|
|
@ -1316,7 +1316,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
|
|||
for child in elt.elements():
|
||||
self.normalize_ns(child, namespace)
|
||||
|
||||
def parse_message(self, message_elt: domish.Element) -> MessageData:
|
||||
async def parse_message(self, message_elt: domish.Element) -> MessageData:
|
||||
"""Parse a message XML and return message_data
|
||||
|
||||
@param message_elt(domish.Element): raw <message> xml
|
||||
|
|
@ -1346,16 +1346,19 @@ class SatMessageProtocol(xmppim.MessageProtocol):
|
|||
)
|
||||
)
|
||||
|
||||
client = self.parent
|
||||
client = cast(SatXMPPEntity, self.parent)
|
||||
assert client is not None
|
||||
|
||||
if not message_elt.hasAttribute("to"):
|
||||
message_elt["to"] = client.jid.full()
|
||||
|
||||
from_jid = jid.JID(message_elt["from"])
|
||||
from_jid_bare = from_jid.userhostJID()
|
||||
message = {}
|
||||
subject = {}
|
||||
extra = {}
|
||||
data = MessageData({
|
||||
"from": jid.JID(message_elt["from"]),
|
||||
"from": from_jid,
|
||||
"to": jid.JID(message_elt["to"]),
|
||||
"uid": message_elt.getAttribute(
|
||||
"uid", str(uuid.uuid4())
|
||||
|
|
@ -1373,6 +1376,23 @@ class SatMessageProtocol(xmppim.MessageProtocol):
|
|||
else:
|
||||
client.mess_id2uid[(data["from"], message_id)] = data["uid"]
|
||||
|
||||
# origin
|
||||
if not from_jid_bare in (client.jid.userhostJID(), client.server_jid):
|
||||
disco_infos = await self.host.memory.disco.get_infos(client, from_jid_bare)
|
||||
if from_jid_bare.user:
|
||||
domain_disco_infos = await self.host.memory.disco.get_infos(
|
||||
client, jid.JID(from_jid_bare.host)
|
||||
)
|
||||
else:
|
||||
domain_disco_infos = disco_infos
|
||||
if ("client", "bot") in disco_infos.identities:
|
||||
extra["is-bot"] = True
|
||||
for (category, type_), label in domain_disco_infos.identities.items():
|
||||
if category == "gateway":
|
||||
extra["origin"] = type_
|
||||
if label:
|
||||
extra["origin_name"] = label
|
||||
|
||||
# message
|
||||
for e in message_elt.elements(C.NS_CLIENT, "body"):
|
||||
message[e.getAttribute((C.NS_XML, "lang"), "")] = str(e)
|
||||
|
|
@ -1480,7 +1500,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
|
|||
return
|
||||
|
||||
try:
|
||||
data = self.parse_message(message_elt)
|
||||
data = await self.parse_message(message_elt)
|
||||
# we now do all post treatments added by plugins
|
||||
post_treat.callback(data)
|
||||
await post_treat
|
||||
|
|
|
|||
Loading…
Reference in a new issue