comparison sat/core/xmpp.py @ 3366:e09cb08166a3

plugin XEP-0329, core(xmpp): moved `_compParseJids` to `SatXMPPComponent`: This method to retrieve owner and peer JID from an element is generally useful for components, thus it has been moved. The part to retrieve owner JID from local JID has been splitted in its own `getOwnerFromJid` method.
author Goffi <goffi@goffi.org>
date Sun, 20 Sep 2020 14:04:11 +0200
parents 5369ce5bcecf
children dea30b1eb166
comparison
equal deleted inserted replaced
3365:626046010a2d 3366:e09cb08166a3
19 import sys 19 import sys
20 import time 20 import time
21 import calendar 21 import calendar
22 import uuid 22 import uuid
23 import mimetypes 23 import mimetypes
24 from typing import Tuple
24 from urllib.parse import urlparse, unquote 25 from urllib.parse import urlparse, unquote
25 from functools import partial 26 from functools import partial
26 from pathlib import Path 27 from pathlib import Path
27 import shortuuid 28 import shortuuid
28 from sat.core.i18n import _ 29 from sat.core.i18n import _
1057 1058
1058 def addPostXmlCallbacks(self, post_xml_treatments): 1059 def addPostXmlCallbacks(self, post_xml_treatments):
1059 if self.sendHistory: 1060 if self.sendHistory:
1060 post_xml_treatments.addCallback(self.messageAddToHistory) 1061 post_xml_treatments.addCallback(self.messageAddToHistory)
1061 1062
1063 def getOwnerFromJid(self, to_jid: jid.JID) -> jid.JID:
1064 """Retrieve "owner" of a component resource from the destination jid of the request
1065
1066 This method needs plugin XEP-0106 for unescaping, if you use it you must add the
1067 plugin to your dependencies.
1068 A "user" part must be present in "to_jid" (otherwise, the component itself is addressed)
1069 @param to_jid: destination JID of the request
1070 """
1071 try:
1072 unescape = self.host_app.plugins['XEP-0106'].unescape
1073 except KeyError:
1074 raise exceptions.MissingPlugin("Plugin XEP-0106 is needed to retrieve owner")
1075 else:
1076 user = unescape(to_jid.user)
1077 if '@' in user:
1078 # a full jid is specified
1079 return jid.JID(user)
1080 else:
1081 # only user part is specified, we use our own host to build the full jid
1082 return jid.JID(None, (user, self.host, None))
1083
1084 def getOwnerAndPeer(self, iq_elt: domish.Element) -> Tuple[jid.JID, jid.JID]:
1085 """Retrieve owner of a component jid, and the jid of the requesting peer
1086
1087 "owner" is found by either unescaping full jid from node, or by combining node
1088 with our host.
1089 Peer jid is the requesting jid from the IQ element
1090 @param iq_elt: IQ stanza sent from the requested
1091 @return: owner and peer JIDs
1092 """
1093 to_jid = jid.JID(iq_elt['to'])
1094 if to_jid.user:
1095 owner = self.getOwnerFromJid(to_jid)
1096 else:
1097 owner = jid.JID(iq_elt["from"]).userhostJID()
1098
1099 peer_jid = jid.JID(iq_elt["from"])
1100 return peer_jid, owner
1101
1102
1062 1103
1063 class SatMessageProtocol(xmppim.MessageProtocol): 1104 class SatMessageProtocol(xmppim.MessageProtocol):
1064 1105
1065 def __init__(self, host): 1106 def __init__(self, host):
1066 xmppim.MessageProtocol.__init__(self) 1107 xmppim.MessageProtocol.__init__(self)