Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0329.py @ 2923:1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
when used by a component, local part can now be used to specify owner of the file. Local part is escaped using XEP-0106, then:
- if the unescaped text contain a "@", it is used as a jid
- else, a jid is build by using this text and host of the component
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 26 Apr 2019 11:57:26 +0200 |
parents | 90146552cde5 |
children | db0890c9c7db |
comparison
equal
deleted
inserted
replaced
2922:28c969432557 | 2923:1fd3ecb3351a |
---|---|
40 C.PI_NAME: "File Information Sharing", | 40 C.PI_NAME: "File Information Sharing", |
41 C.PI_IMPORT_NAME: "XEP-0329", | 41 C.PI_IMPORT_NAME: "XEP-0329", |
42 C.PI_TYPE: "XEP", | 42 C.PI_TYPE: "XEP", |
43 C.PI_MODES: C.PLUG_MODE_BOTH, | 43 C.PI_MODES: C.PLUG_MODE_BOTH, |
44 C.PI_PROTOCOLS: ["XEP-0329"], | 44 C.PI_PROTOCOLS: ["XEP-0329"], |
45 C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300"], | 45 C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300", "XEP-0106"], |
46 C.PI_MAIN: "XEP_0329", | 46 C.PI_MAIN: "XEP_0329", |
47 C.PI_HANDLER: "yes", | 47 C.PI_HANDLER: "yes", |
48 C.PI_DESCRIPTION: _(u"""Implementation of File Information Sharing"""), | 48 C.PI_DESCRIPTION: _(u"""Implementation of File Information Sharing"""), |
49 } | 49 } |
50 | 50 |
392 | 392 |
393 # common methods | 393 # common methods |
394 | 394 |
395 def _requestHandler(self, client, iq_elt, root_nodes_cb, files_from_node_cb): | 395 def _requestHandler(self, client, iq_elt, root_nodes_cb, files_from_node_cb): |
396 iq_elt.handled = True | 396 iq_elt.handled = True |
397 owner = jid.JID(iq_elt["from"]).userhostJID() | |
398 node = iq_elt.query.getAttribute("node") | 397 node = iq_elt.query.getAttribute("node") |
399 if not node: | 398 if not node: |
400 d = defer.maybeDeferred(root_nodes_cb, client, iq_elt, owner) | 399 d = defer.maybeDeferred(root_nodes_cb, client, iq_elt) |
401 else: | 400 else: |
402 d = defer.maybeDeferred(files_from_node_cb, client, iq_elt, owner, node) | 401 d = defer.maybeDeferred(files_from_node_cb, client, iq_elt, node) |
403 d.addErrback( | 402 d.addErrback( |
404 lambda failure_: log.error( | 403 lambda failure_: log.error( |
405 _(u"error while retrieving files: {msg}").format(msg=failure_) | 404 _(u"error while retrieving files: {msg}").format(msg=failure_) |
406 ) | 405 ) |
407 ) | 406 ) |
475 else: | 474 else: |
476 raise exceptions.InternalError( | 475 raise exceptions.InternalError( |
477 _(u"unexpected type: {type}").format(type=node_type) | 476 _(u"unexpected type: {type}").format(type=node_type) |
478 ) | 477 ) |
479 | 478 |
480 def _getRootNodesCb(self, client, iq_elt, owner): | 479 def _getRootNodesCb(self, client, iq_elt): |
481 peer_jid = jid.JID(iq_elt["from"]) | 480 peer_jid = jid.JID(iq_elt["from"]) |
482 iq_result_elt = xmlstream.toResponse(iq_elt, "result") | 481 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
483 query_elt = iq_result_elt.addElement((NS_FIS, "query")) | 482 query_elt = iq_result_elt.addElement((NS_FIS, "query")) |
484 for name, node in client._XEP_0329_root_node.iteritems(): | 483 for name, node in client._XEP_0329_root_node.iteritems(): |
485 if not node.checkPermissions(client, peer_jid, check_parents=False): | 484 if not node.checkPermissions(client, peer_jid, check_parents=False): |
486 continue | 485 continue |
487 directory_elt = query_elt.addElement("directory") | 486 directory_elt = query_elt.addElement("directory") |
488 directory_elt["name"] = name | 487 directory_elt["name"] = name |
489 client.send(iq_result_elt) | 488 client.send(iq_result_elt) |
490 | 489 |
491 def _getFilesFromNodeCb(self, client, iq_elt, owner, node_path): | 490 def _getFilesFromNodeCb(self, client, iq_elt, node_path): |
492 """Main method to retrieve files/directories from a node_path""" | 491 """Main method to retrieve files/directories from a node_path""" |
493 peer_jid = jid.JID(iq_elt[u"from"]) | 492 peer_jid = jid.JID(iq_elt[u"from"]) |
494 try: | 493 try: |
495 node, path = ShareNode.find(client, node_path, peer_jid) | 494 node, path = ShareNode.find(client, node_path, peer_jid) |
496 except (exceptions.PermissionError, exceptions.NotFound): | 495 except (exceptions.PermissionError, exceptions.NotFound): |
523 client, iq_elt, self._getRootNodesCb, self._getFilesFromNodeCb | 522 client, iq_elt, self._getRootNodesCb, self._getFilesFromNodeCb |
524 ) | 523 ) |
525 | 524 |
526 # Component | 525 # Component |
527 | 526 |
527 def _compParseJids(self, client, iq_elt): | |
528 """Retrieve peer_jid and owner to use from IQ stanza | |
529 | |
530 @param iq_elt(domish.Element): IQ stanza of the FIS request | |
531 @return (tuple[jid.JID, jid.JID]): peer_jid and owner | |
532 """ | |
533 to_jid = jid.JID(iq_elt['to']) | |
534 if to_jid.user: | |
535 user = self.host.plugins['XEP-0106'].unescape(to_jid.user) | |
536 if u'@' in user: | |
537 # a full jid is specified | |
538 owner = jid.JID(user) | |
539 else: | |
540 # only user part is specified, we use our own host to build the full jid | |
541 owner = jid.JID(None, (user, client.host, None)) | |
542 else: | |
543 owner = jid.JID(iq_elt["from"]).userhostJID() | |
544 | |
545 peer_jid = jid.JID(iq_elt["from"]) | |
546 return peer_jid, owner | |
547 | |
528 @defer.inlineCallbacks | 548 @defer.inlineCallbacks |
529 def _compGetRootNodesCb(self, client, iq_elt, owner): | 549 def _compGetRootNodesCb(self, client, iq_elt): |
530 peer_jid = jid.JID(iq_elt["from"]) | 550 peer_jid, owner = self._compParseJids(client, iq_elt) |
531 files_data = yield self.host.memory.getFiles( | 551 files_data = yield self.host.memory.getFiles( |
532 client, | 552 client, |
533 peer_jid=peer_jid, | 553 peer_jid=peer_jid, |
534 parent=u"", | 554 parent=u"", |
535 type_=C.FILE_TYPE_DIRECTORY, | 555 type_=C.FILE_TYPE_DIRECTORY, |
542 directory_elt = query_elt.addElement(u"directory") | 562 directory_elt = query_elt.addElement(u"directory") |
543 directory_elt[u"name"] = name | 563 directory_elt[u"name"] = name |
544 client.send(iq_result_elt) | 564 client.send(iq_result_elt) |
545 | 565 |
546 @defer.inlineCallbacks | 566 @defer.inlineCallbacks |
547 def _compGetFilesFromNodeCb(self, client, iq_elt, owner, node_path): | 567 def _compGetFilesFromNodeCb(self, client, iq_elt, node_path): |
548 """retrieve files from local files repository according to permissions | 568 """Retrieve files from local files repository according to permissions |
549 | 569 |
550 result stanza is then built and sent to requestor | 570 result stanza is then built and sent to requestor |
551 @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path, | 571 @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path, |
552 files_data): | 572 files_data): |
553 can be used to add data/elements | 573 can be used to add data/elements |
554 """ | 574 """ |
555 peer_jid = jid.JID(iq_elt["from"]) | 575 peer_jid, owner = self._compParseJids(client, iq_elt) |
556 try: | 576 try: |
557 files_data = yield self.host.memory.getFiles( | 577 files_data = yield self.host.memory.getFiles( |
558 client, peer_jid=peer_jid, path=node_path, owner=owner | 578 client, peer_jid=peer_jid, path=node_path, owner=owner |
559 ) | 579 ) |
560 except exceptions.NotFound: | 580 except exceptions.NotFound: |