comparison sat/plugins/plugin_xep_0329.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 626046010a2d
children 08a3e34aead1
comparison
equal deleted inserted replaced
3365:626046010a2d 3366:e09cb08166a3
588 """Retrieve peer_jid and owner to use from IQ stanza 588 """Retrieve peer_jid and owner to use from IQ stanza
589 589
590 @param iq_elt(domish.Element): IQ stanza of the FIS request 590 @param iq_elt(domish.Element): IQ stanza of the FIS request
591 @return (tuple[jid.JID, jid.JID]): peer_jid and owner 591 @return (tuple[jid.JID, jid.JID]): peer_jid and owner
592 """ 592 """
593 to_jid = jid.JID(iq_elt['to'])
594 if to_jid.user:
595 user = self.host.plugins['XEP-0106'].unescape(to_jid.user)
596 if '@' in user:
597 # a full jid is specified
598 owner = jid.JID(user)
599 else:
600 # only user part is specified, we use our own host to build the full jid
601 owner = jid.JID(None, (user, client.host, None))
602 else:
603 owner = jid.JID(iq_elt["from"]).userhostJID()
604
605 peer_jid = jid.JID(iq_elt["from"])
606 return peer_jid, owner
607 593
608 @defer.inlineCallbacks 594 @defer.inlineCallbacks
609 def _compGetRootNodesCb(self, client, iq_elt): 595 def _compGetRootNodesCb(self, client, iq_elt):
610 peer_jid, owner = self._compParseJids(client, iq_elt) 596 peer_jid, owner = client.getOwnerAndPeer(iq_elt)
611 files_data = yield self.host.memory.getFiles( 597 files_data = yield self.host.memory.getFiles(
612 client, 598 client,
613 peer_jid=peer_jid, 599 peer_jid=peer_jid,
614 parent="", 600 parent="",
615 type_=C.FILE_TYPE_DIRECTORY, 601 type_=C.FILE_TYPE_DIRECTORY,
630 result stanza is then built and sent to requestor 616 result stanza is then built and sent to requestor
631 @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path, 617 @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path,
632 files_data): 618 files_data):
633 can be used to add data/elements 619 can be used to add data/elements
634 """ 620 """
635 peer_jid, owner = self._compParseJids(client, iq_elt) 621 peer_jid, owner = client.getOwnerAndPeer(iq_elt)
636 try: 622 try:
637 files_data = yield self.host.memory.getFiles( 623 files_data = yield self.host.memory.getFiles(
638 client, peer_jid=peer_jid, path=node_path, owner=owner 624 client, peer_jid=peer_jid, path=node_path, owner=owner
639 ) 625 )
640 except exceptions.NotFound: 626 except exceptions.NotFound:
731 return files 717 return files
732 718
733 # affiliations # 719 # affiliations #
734 720
735 async def _parseElement(self, client, iq_elt, element, namespace): 721 async def _parseElement(self, client, iq_elt, element, namespace):
736 peer_jid, owner = self._compParseJids(client, iq_elt) 722 peer_jid, owner = client.getOwnerAndPeer(iq_elt)
737 elt = next(iq_elt.elements(namespace, element)) 723 elt = next(iq_elt.elements(namespace, element))
738 path = Path("/", elt['path']) 724 path = Path("/", elt['path'])
739 if len(path.parts) < 2: 725 if len(path.parts) < 2:
740 raise RootPathException 726 raise RootPathException
741 namespace = elt.getAttribute('namespace') 727 namespace = elt.getAttribute('namespace')
836 ) = await self._parseElement(client, iq_elt, "affiliations", NS_FIS_AFFILIATION) 822 ) = await self._parseElement(client, iq_elt, "affiliations", NS_FIS_AFFILIATION)
837 except exceptions.CancelError: 823 except exceptions.CancelError:
838 return 824 return
839 except RootPathException: 825 except RootPathException:
840 # if root path is requested, we only get owner affiliation 826 # if root path is requested, we only get owner affiliation
841 peer_jid, owner = self._compParseJids(client, iq_elt) 827 peer_jid, owner = client.getOwnerAndPeer(iq_elt)
842 is_owner = peer_jid.userhostJID() == owner 828 is_owner = peer_jid.userhostJID() == owner
843 affiliations = {owner: 'owner'} 829 affiliations = {owner: 'owner'}
844 except exceptions.NotFound: 830 except exceptions.NotFound:
845 client.sendError(iq_elt, "item-not-found") 831 client.sendError(iq_elt, "item-not-found")
846 return 832 return
1078 def _onComponentCreateDir(self, iq_elt, client): 1064 def _onComponentCreateDir(self, iq_elt, client):
1079 iq_elt.handled = True 1065 iq_elt.handled = True
1080 defer.ensureDeferred(self.onComponentCreateDir(client, iq_elt)) 1066 defer.ensureDeferred(self.onComponentCreateDir(client, iq_elt))
1081 1067
1082 async def onComponentCreateDir(self, client, iq_elt): 1068 async def onComponentCreateDir(self, client, iq_elt):
1083 peer_jid, owner = self._compParseJids(client, iq_elt) 1069 peer_jid, owner = client.getOwnerAndPeer(iq_elt)
1084 if peer_jid.host not in client._file_sharing_allowed_hosts: 1070 if peer_jid.host not in client._file_sharing_allowed_hosts:
1085 client.sendError(iq_elt, 'forbidden') 1071 client.sendError(iq_elt, 'forbidden')
1086 return 1072 return
1087 create_dir_elt = next(iq_elt.elements(NS_FIS_CREATE, "dir")) 1073 create_dir_elt = next(iq_elt.elements(NS_FIS_CREATE, "dir"))
1088 namespace = create_dir_elt.getAttribute('namespace') 1074 namespace = create_dir_elt.getAttribute('namespace')