Compare commits
8 Commits
slix-1.8.4
...
xep356-iq
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56004802fa | ||
|
|
dcfa0f20f9 | ||
|
|
8bfe6177f4 | ||
|
|
7732af8991 | ||
|
|
25c28ff5d1 | ||
|
|
e3e0d8f43e | ||
|
|
13729e47a6 | ||
|
|
f12860bfad |
22
.readthedocs.yaml
Normal file
22
.readthedocs.yaml
Normal file
@@ -0,0 +1,22 @@
|
||||
# .readthedocs.yaml
|
||||
# Read the Docs configuration file
|
||||
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
|
||||
|
||||
# Required
|
||||
version: 2
|
||||
|
||||
# Set the version of Python and other tools you might need
|
||||
build:
|
||||
os: ubuntu-22.04
|
||||
tools:
|
||||
python: "3.11"
|
||||
|
||||
# Build documentation in the docs/ directory with Sphinx
|
||||
sphinx:
|
||||
configuration: docs/conf.py
|
||||
|
||||
# We recommend specifying your dependencies to enable reproducible builds:
|
||||
# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
|
||||
python:
|
||||
install:
|
||||
- requirements: docs/requirements.txt
|
||||
@@ -5,7 +5,7 @@ To contribute, the preferred way is to commit your changes on some
|
||||
publicly-available git repository (on a fork `on github
|
||||
<https://github.com/poezio/slixmpp>`_ or on your own repository) and to
|
||||
notify the developers with either:
|
||||
- a ticket `on the bug tracker <https://lab.louiz.org/poezio/slixmpp/issues/new>`_
|
||||
- a ticket `on the bug tracker <https://codeberg.org/poezio/slixmpp/issues/new>`_
|
||||
- a pull request on github
|
||||
- a simple message on `the XMPP MUC <xmpp:slixmpp@muc.poez.io>`_
|
||||
|
||||
|
||||
28
doap.xml
28
doap.xml
@@ -8,13 +8,13 @@
|
||||
<shortdesc xml:lang="en">Elegant Python library for XMPP</shortdesc>
|
||||
<shortdesc xml:lang="fr">Bibliothèque pour XMPP élégante, en Python</shortdesc>
|
||||
|
||||
<homepage rdf:resource="https://lab.louiz.org/poezio/slixmpp/"/>
|
||||
<download-page rdf:resource="https://lab.louiz.org/poezio/slixmpp/tags"/>
|
||||
<bug-database rdf:resource="https://lab.louiz.org/poezio/slixmpp/issues"/>
|
||||
<homepage rdf:resource="https://codeberg.org/poezio/slixmpp/"/>
|
||||
<download-page rdf:resource="https://codeberg.org/poezio/slixmpp/tags"/>
|
||||
<bug-database rdf:resource="https://codeberg.org/poezio/slixmpp/issues"/>
|
||||
<developer-forum rdf:resource="xmpp:slixmpp@muc.poez.io?join"/>
|
||||
<support-forum rdf:resource="xmpp:slixmpp@muc.poez.io?join"/>
|
||||
|
||||
<license rdf:resource="https://lab.louiz.org/poezio/slixmpp/blob/master/LICENSE"/>
|
||||
<license rdf:resource="https://codeberg.org/poezio/slixmpp/raw/brach/master/LICENSE"/>
|
||||
|
||||
<language>en</language>
|
||||
|
||||
@@ -59,8 +59,8 @@
|
||||
|
||||
<repository>
|
||||
<GitRepository>
|
||||
<browse rdf:resource="https://lab.louiz.org/poezio/slixmpp"/>
|
||||
<location rdf:resource="https://lab.louiz.org/poezio/slixmpp.git"/>
|
||||
<browse rdf:resource="https://codeberg.org/poezio/slixmpp"/>
|
||||
<location rdf:resource="https://codeberg.org/poezio/slixmpp.git"/>
|
||||
</GitRepository>
|
||||
</repository>
|
||||
|
||||
@@ -1012,56 +1012,56 @@
|
||||
<Version>
|
||||
<revision>1.6.0</revision>
|
||||
<created>2020-12-12</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.6.0/slixmpp-slix-1.6.0.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.6.0.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.7.0</revision>
|
||||
<created>2021-01-29</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.7.0/slixmpp-slix-1.7.0.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.7.0.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.7.1</revision>
|
||||
<created>2021-04-30</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.7.1/slixmpp-slix-1.7.1.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.7.1.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.8.0</revision>
|
||||
<created>2022-02-27</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.8.0/slixmpp-slix-1.8.0.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.8.0.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.8.1</revision>
|
||||
<created>2022-03-20</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.8.1/slixmpp-slix-1.8.1.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.8.1.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.8.2</revision>
|
||||
<created>2022-04-06</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.8.2/slixmpp-slix-1.8.2.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.8.2.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.8.3</revision>
|
||||
<created>2022-11-12</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.8.3/slixmpp-slix-1.8.3.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.8.3.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
<release>
|
||||
<Version>
|
||||
<revision>1.8.4</revision>
|
||||
<created>2023-05-28</created>
|
||||
<file-release rdf:resource="https://lab.louiz.org/poezio/slixmpp/-/archive/slix-1.8.4/slixmpp-slix-1.8.4.tar.gz"/>
|
||||
<file-release rdf:resource="https://codeberg.org/poezio/slixmpp/archive/slix-1.8.4.tar.gz"/>
|
||||
</Version>
|
||||
</release>
|
||||
</Project>
|
||||
|
||||
@@ -11,7 +11,7 @@ Create and Run a Server Component
|
||||
<xmpp:slixmpp@muc.poez.io?join>`_.
|
||||
|
||||
If you have not yet installed Slixmpp, do so now by either checking out a version
|
||||
with `Git <https://lab.louiz.org/poezio/slixmpp>`_.
|
||||
with `Git <https://codeberg.org/poezio/slixmpp>`_.
|
||||
|
||||
Many XMPP applications eventually graduate to requiring to run as a server
|
||||
component in order to meet scalability requirements. To demonstrate how to
|
||||
|
||||
@@ -11,7 +11,7 @@ Slixmpp Quickstart - Echo Bot
|
||||
<xmpp:slixmpp@muc.poez.io?join>`_.
|
||||
|
||||
If you have not yet installed Slixmpp, do so now by either checking out a version
|
||||
with `Git <https://lab.louiz.org/poezio/slixmpp>`_.
|
||||
with `Git <https://codeberg.org/poezio/slixmpp>`_.
|
||||
|
||||
As a basic starting project, we will create an echo bot which will reply to any
|
||||
messages sent to it. We will also go through adding some basic command line configuration
|
||||
@@ -325,7 +325,7 @@ The Final Product
|
||||
-----------------
|
||||
|
||||
Here then is what the final result should look like after working through the guide above. The code
|
||||
can also be found in the Slixmpp `examples directory <https://lab.louiz.org/poezio/slixmpp/tree/master/examples>`_.
|
||||
can also be found in the Slixmpp `examples directory <https://codeberg.org/poezio/slixmpp/src/branch/master/examples>`_.
|
||||
|
||||
.. compound::
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ Multi-User Chat (MUC) Bot
|
||||
<xmpp:slixmpp@muc.poez.io?join>`_.
|
||||
|
||||
If you have not yet installed Slixmpp, do so now by either checking out a version
|
||||
from `Git <https://lab.louiz.org/poezio/slixmpp>`_.
|
||||
from `Git <https://codeberg.org/poezio/slixmpp>`_.
|
||||
|
||||
Now that you've got the basic gist of using Slixmpp by following the
|
||||
echobot example (:ref:`echobot`), we can use one of the bundled plugins
|
||||
|
||||
@@ -4,9 +4,9 @@ Slixmpp
|
||||
.. sidebar:: Get the Code
|
||||
|
||||
The latest source code for Slixmpp may be found on the `Git repo
|
||||
<https://lab.louiz.org/poezio/slixmpp>`_. ::
|
||||
<https://codeberg.org/poezio/slixmpp>`_. ::
|
||||
|
||||
git clone https://lab.louiz.org/poezio/slixmpp
|
||||
git clone https://codeberg.org/poezio/slixmpp
|
||||
|
||||
An XMPP chat room is available for discussing and getting help with slixmpp.
|
||||
|
||||
@@ -14,7 +14,7 @@ Slixmpp
|
||||
`slixmpp@muc.poez.io <xmpp:slixmpp@muc.poez.io?join>`_
|
||||
|
||||
**Reporting bugs**
|
||||
You can report bugs at http://lab.louiz.org/poezio/slixmpp/issues.
|
||||
You can report bugs at http://codeberg.org/poezio/slixmpp/issues.
|
||||
|
||||
Slixmpp is an :ref:`MIT licensed <license>` XMPP library for Python 3.7+,
|
||||
|
||||
|
||||
2
setup.py
2
setup.py
@@ -80,7 +80,7 @@ setup(
|
||||
long_description=LONG_DESCRIPTION,
|
||||
author='Florent Le Coz',
|
||||
author_email='louiz@louiz.org',
|
||||
url='https://lab.louiz.org/poezio/slixmpp',
|
||||
url='https://codeberg.org/poezio/slixmpp',
|
||||
license='MIT',
|
||||
platforms=['any'],
|
||||
package_data={'slixmpp': ['py.typed']},
|
||||
|
||||
@@ -285,7 +285,7 @@ class BaseXMPP(XMLStream):
|
||||
if plugin in plugins.PLUGINS:
|
||||
self.register_plugin(plugin)
|
||||
else:
|
||||
raise NameError("Plugin %s not in plugins.__all__." % plugin)
|
||||
raise NameError("Plugin %s not in plugins.PLUGINS." % plugin)
|
||||
|
||||
def __getitem__(self, key):
|
||||
"""Return a plugin given its name, if it has been registered."""
|
||||
|
||||
@@ -101,6 +101,7 @@ PLUGINS = [
|
||||
'xep_0377', # Spam reporting
|
||||
'xep_0380', # Explicit Message Encryption
|
||||
'xep_0382', # Spoiler Messages
|
||||
'xep_0385', # Stateless Inline Media Sharing (SIMS)
|
||||
'xep_0394', # Message Markup
|
||||
'xep_0402', # PEP Native Bookmarks
|
||||
'xep_0403', # MIX-Presence
|
||||
@@ -115,6 +116,7 @@ PLUGINS = [
|
||||
'xep_0439', # Quick Response
|
||||
'xep_0441', # Message Archive Management Preferences
|
||||
'xep_0444', # Message Reactions
|
||||
'xep_0447', # Stateless file sharing
|
||||
'xep_0461', # Message Replies
|
||||
# Meant to be imported by plugins
|
||||
]
|
||||
|
||||
@@ -187,7 +187,7 @@ class Fin(ElementBase):
|
||||
name = 'fin'
|
||||
namespace = 'urn:xmpp:mam:2'
|
||||
plugin_attrib = 'mam_fin'
|
||||
interfaces = {'results'}
|
||||
interfaces = {'results', 'stable', 'complete'}
|
||||
|
||||
def setup(self, xml=None):
|
||||
ElementBase.setup(self, xml)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from slixmpp.plugins.base import register_plugin
|
||||
|
||||
from slixmpp.plugins.xep_0356 import stanza
|
||||
from slixmpp.plugins.xep_0356.stanza import Perm, Privilege
|
||||
from slixmpp.plugins.xep_0356.privilege import XEP_0356
|
||||
from . import stanza
|
||||
from .privilege import XEP_0356
|
||||
from .stanza import Perm, Privilege
|
||||
|
||||
register_plugin(XEP_0356)
|
||||
|
||||
36
slixmpp/plugins/xep_0356/permissions.py
Normal file
36
slixmpp/plugins/xep_0356/permissions.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import dataclasses
|
||||
from collections import defaultdict
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class RosterAccess(str, Enum):
|
||||
NONE = "none"
|
||||
GET = "get"
|
||||
SET = "set"
|
||||
BOTH = "both"
|
||||
|
||||
|
||||
class MessagePermission(str, Enum):
|
||||
NONE = "none"
|
||||
OUTGOING = "outgoing"
|
||||
|
||||
|
||||
class IqPermission(str, Enum):
|
||||
NONE = "none"
|
||||
GET = "get"
|
||||
SET = "set"
|
||||
BOTH = "both"
|
||||
|
||||
|
||||
class PresencePermission(str, Enum):
|
||||
NONE = "none"
|
||||
MANAGED_ENTITY = "managed_entity"
|
||||
ROSTER = "roster"
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Permissions:
|
||||
roster = RosterAccess.NONE
|
||||
message = MessagePermission.NONE
|
||||
iq = defaultdict(lambda: IqPermission.NONE)
|
||||
presence = PresencePermission.NONE
|
||||
@@ -1,14 +1,16 @@
|
||||
import logging
|
||||
import typing
|
||||
import uuid
|
||||
from collections import defaultdict
|
||||
|
||||
from slixmpp import Message, JID, Iq
|
||||
from slixmpp import JID, Iq, Message
|
||||
from slixmpp.plugins.base import BasePlugin
|
||||
from slixmpp.xmlstream.matcher import StanzaPath
|
||||
from slixmpp.xmlstream import StanzaBase
|
||||
from slixmpp.xmlstream.handler import Callback
|
||||
from slixmpp.xmlstream import register_stanza_plugin
|
||||
|
||||
from slixmpp.plugins.xep_0356 import stanza, Privilege, Perm
|
||||
from slixmpp.xmlstream.matcher import StanzaPath
|
||||
|
||||
from . import stanza
|
||||
from .permissions import IqPermission, MessagePermission, Permissions, RosterAccess
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -29,7 +31,7 @@ class XEP_0356(BasePlugin):
|
||||
dependencies = {"xep_0297"}
|
||||
stanza = stanza
|
||||
|
||||
granted_privileges = {"roster": "none", "message": "none", "presence": "none"}
|
||||
granted_privileges = defaultdict(Permissions)
|
||||
|
||||
def plugin_init(self):
|
||||
if not self.xmpp.is_component:
|
||||
@@ -49,32 +51,42 @@ class XEP_0356(BasePlugin):
|
||||
def plugin_end(self):
|
||||
self.xmpp.remove_handler("Privileges")
|
||||
|
||||
def _handle_privilege(self, msg: Message):
|
||||
def _handle_privilege(self, msg: StanzaBase):
|
||||
"""
|
||||
Called when the XMPP server advertise the component's privileges.
|
||||
|
||||
Stores the privileges in this instance's granted_privileges attribute (a dict)
|
||||
and raises the privileges_advertised event
|
||||
"""
|
||||
permissions = self.granted_privileges[msg.get_from()]
|
||||
for perm in msg["privilege"]["perms"]:
|
||||
self.granted_privileges[perm["access"]] = perm["type"]
|
||||
access = perm["access"]
|
||||
if access == "iq":
|
||||
for ns in perm["namespaces"]:
|
||||
permissions.iq[ns["ns"]] = ns["type"]
|
||||
elif access in _VALID_ACCESSES:
|
||||
setattr(permissions, access, perm["type"])
|
||||
else:
|
||||
log.warning("Received an invalid privileged access: %s", access)
|
||||
log.debug(f"Privileges: {self.granted_privileges}")
|
||||
self.xmpp.event("privileges_advertised")
|
||||
|
||||
def send_privileged_message(self, msg: Message):
|
||||
if self.granted_privileges["message"] == "outgoing":
|
||||
self._make_privileged_message(msg).send()
|
||||
else:
|
||||
log.error(
|
||||
if (
|
||||
self.granted_privileges[msg.get_from().domain].message
|
||||
!= MessagePermission.OUTGOING
|
||||
):
|
||||
raise PermissionError(
|
||||
"The server hasn't authorized us to send messages on behalf of other users"
|
||||
)
|
||||
else:
|
||||
self._make_privileged_message(msg).send()
|
||||
|
||||
def _make_privileged_message(self, msg: Message):
|
||||
stanza = self.xmpp.make_message(
|
||||
mto=self.xmpp.server_host, mfrom=self.xmpp.boundjid.bare
|
||||
)
|
||||
stanza["privilege"]["forwarded"].append(msg)
|
||||
return stanza
|
||||
server = msg.get_from().domain
|
||||
wrapped = self.xmpp.make_message(mto=server, mfrom=self.xmpp.boundjid.bare)
|
||||
wrapped["privilege"]["forwarded"].append(msg)
|
||||
return wrapped
|
||||
|
||||
def _make_get_roster(self, jid: typing.Union[JID, str], **iq_kwargs):
|
||||
return self.xmpp.make_iq_get(
|
||||
@@ -106,9 +118,15 @@ class XEP_0356(BasePlugin):
|
||||
|
||||
:param jid: user we want to fetch the roster from
|
||||
"""
|
||||
if self.granted_privileges["roster"] not in ("get", "both"):
|
||||
log.error("The server did not grant us privileges to get rosters")
|
||||
raise ValueError
|
||||
if isinstance(jid, str):
|
||||
jid = JID(jid)
|
||||
if self.granted_privileges[jid.domain].roster not in (
|
||||
RosterAccess.GET,
|
||||
RosterAccess.BOTH,
|
||||
):
|
||||
raise PermissionError(
|
||||
"The server did not grant us privileges to get rosters"
|
||||
)
|
||||
else:
|
||||
return await self._make_get_roster(jid).send(**send_kwargs)
|
||||
|
||||
@@ -137,8 +155,56 @@ class XEP_0356(BasePlugin):
|
||||
},
|
||||
}
|
||||
"""
|
||||
if self.granted_privileges["roster"] not in ("set", "both"):
|
||||
log.error("The server did not grant us privileges to set rosters")
|
||||
raise ValueError
|
||||
if isinstance(jid, str):
|
||||
jid = JID(jid)
|
||||
if self.granted_privileges[jid.domain].roster not in (
|
||||
RosterAccess.GET,
|
||||
RosterAccess.BOTH,
|
||||
):
|
||||
raise PermissionError(
|
||||
"The server did not grant us privileges to set rosters"
|
||||
)
|
||||
else:
|
||||
return await self._make_set_roster(jid, roster_items).send(**send_kwargs)
|
||||
|
||||
async def send_privileged_iq(
|
||||
self, encapsulated_iq: Iq, iq_id: typing.Optional[str] = None
|
||||
):
|
||||
"""
|
||||
Send an IQ on behalf of a user
|
||||
|
||||
Caution: the IQ *must* have the jabber:client namespace
|
||||
"""
|
||||
iq_id = iq_id or str(uuid.uuid4())
|
||||
encapsulated_iq["id"] = iq_id
|
||||
server = encapsulated_iq.get_to().domain
|
||||
perms = self.granted_privileges.get(server)
|
||||
if not perms:
|
||||
raise PermissionError(f"{server} has not granted us any privilege")
|
||||
itype = encapsulated_iq["type"]
|
||||
for ns in encapsulated_iq.plugins.values():
|
||||
type_ = perms.iq[ns.namespace]
|
||||
if type_ == IqPermission.NONE:
|
||||
raise PermissionError(
|
||||
f"{server} has not granted any IQ privilege for namespace {ns.namespace}"
|
||||
)
|
||||
elif type_ == IqPermission.BOTH:
|
||||
pass
|
||||
elif type_ != itype:
|
||||
raise PermissionError(
|
||||
f"{server} has not granted IQ {itype} privilege for namespace {ns.namespace}"
|
||||
)
|
||||
iq = self.xmpp.make_iq(
|
||||
itype=itype,
|
||||
ifrom=self.xmpp.boundjid.bare,
|
||||
ito=encapsulated_iq.get_from(),
|
||||
id=iq_id,
|
||||
)
|
||||
iq["privileged_iq"].append(encapsulated_iq)
|
||||
|
||||
resp = await iq.send()
|
||||
return resp["privilege"]["forwarded"]["iq"]
|
||||
|
||||
|
||||
# does not include iq access that is handled differently
|
||||
_VALID_ACCESSES = {"message", "roster", "presence"}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
from slixmpp.stanza import Message
|
||||
from slixmpp.xmlstream import (
|
||||
ElementBase,
|
||||
register_stanza_plugin,
|
||||
)
|
||||
from slixmpp.plugins.xep_0297 import Forwarded
|
||||
from slixmpp.stanza import Iq, Message
|
||||
from slixmpp.xmlstream import ElementBase, register_stanza_plugin
|
||||
|
||||
NS = "urn:xmpp:privilege:2"
|
||||
|
||||
|
||||
class Privilege(ElementBase):
|
||||
namespace = "urn:xmpp:privilege:2"
|
||||
namespace = NS
|
||||
name = "privilege"
|
||||
plugin_attrib = "privilege"
|
||||
|
||||
@@ -25,26 +24,40 @@ class Privilege(ElementBase):
|
||||
def presence(self):
|
||||
return self.permission("presence")
|
||||
|
||||
def iq(self):
|
||||
return self.permission("iq")
|
||||
|
||||
def add_perm(self, access, type):
|
||||
def add_perm(self, access, type_):
|
||||
# This should only be needed for servers, so maybe out of scope for slixmpp
|
||||
perm = Perm()
|
||||
perm["type"] = type
|
||||
perm["type"] = type_
|
||||
perm["access"] = access
|
||||
self.append(perm)
|
||||
|
||||
|
||||
class Perm(ElementBase):
|
||||
namespace = "urn:xmpp:privilege:2"
|
||||
namespace = NS
|
||||
name = "perm"
|
||||
plugin_attrib = "perm"
|
||||
plugin_multi_attrib = "perms"
|
||||
interfaces = {"type", "access"}
|
||||
|
||||
|
||||
class NameSpace(ElementBase):
|
||||
namespace = NS
|
||||
name = "namespace"
|
||||
plugin_attrib = "namespace"
|
||||
plugin_multi_attrib = "namespaces"
|
||||
interfaces = {"ns", "type"}
|
||||
|
||||
|
||||
class PrivilegedIq(ElementBase):
|
||||
namespace = NS
|
||||
name = "privileged_iq"
|
||||
plugin_attrib = "privileged_iq"
|
||||
|
||||
|
||||
def register():
|
||||
register_stanza_plugin(Message, Privilege)
|
||||
register_stanza_plugin(Iq, Privilege)
|
||||
register_stanza_plugin(Privilege, Forwarded)
|
||||
register_stanza_plugin(Privilege, Perm, iterable=True)
|
||||
register_stanza_plugin(Perm, NameSpace, iterable=True)
|
||||
register_stanza_plugin(Iq, PrivilegedIq)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import Optional
|
||||
|
||||
from slixmpp.stanza import Message
|
||||
from slixmpp.xmlstream import ElementBase, register_stanza_plugin
|
||||
|
||||
@@ -38,9 +40,22 @@ class FeatureFallBack(ElementBase):
|
||||
else:
|
||||
return body
|
||||
|
||||
def add_quoted_fallback(self, fallback: str):
|
||||
def add_quoted_fallback(self, fallback: str, nickname: Optional[str] = None):
|
||||
"""
|
||||
Add plain text fallback for clients not implementing XEP-0461.
|
||||
|
||||
``msg["feature_fallback"].add_quoted_fallback("Some text", "Bob")`` will
|
||||
prepend "> Bob:\n> Some text\n" to the body of the message, and set the
|
||||
fallback_body attributes accordingly, so that clients implementing
|
||||
XEP-0461 can hide the fallback text.
|
||||
|
||||
:param fallback: Body of the quoted message.
|
||||
:param nickname: Optional, nickname of the quoted participant.
|
||||
"""
|
||||
msg = self.parent()
|
||||
quoted = "\n".join("> " + x.strip() for x in fallback.split("\n")) + "\n"
|
||||
if nickname:
|
||||
quoted = "> " + nickname + ":\n" + quoted
|
||||
msg["body"] = quoted + msg["body"]
|
||||
msg["feature_fallback"]["for"] = NS
|
||||
msg["feature_fallback"]["fallback_body"]["start"] = 0
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import unittest
|
||||
from slixmpp import Message
|
||||
from slixmpp.test import SlixTest
|
||||
from slixmpp.xmlstream import register_stanza_plugin
|
||||
|
||||
from slixmpp.plugins.xep_0356 import stanza
|
||||
from slixmpp.plugins.xep_0356 import stanza, permissions
|
||||
|
||||
|
||||
class TestPermissions(SlixTest):
|
||||
@@ -12,30 +10,57 @@ class TestPermissions(SlixTest):
|
||||
|
||||
def testAdvertisePermission(self):
|
||||
xmlstring = """
|
||||
<message from='capulet.net' to='pubub.capulet.lit'>
|
||||
<message from='capulet.lit' to='pubsub.capulet.lit'>
|
||||
<privilege xmlns='urn:xmpp:privilege:2'>
|
||||
<perm access='roster' type='both'/>
|
||||
<perm access='message' type='outgoing'/>
|
||||
<perm access='presence' type='managed_entity'/>
|
||||
<perm access='iq' type='both'/>
|
||||
</privilege>
|
||||
</message>
|
||||
"""
|
||||
msg = self.Message()
|
||||
msg["from"] = "capulet.net"
|
||||
msg["to"] = "pubub.capulet.lit"
|
||||
# This raises AttributeError: 'NoneType' object has no attribute 'use_origin_id'
|
||||
# msg["id"] = "id"
|
||||
msg["from"] = "capulet.lit"
|
||||
msg["to"] = "pubsub.capulet.lit"
|
||||
|
||||
for access, type_ in [
|
||||
("roster", "both"),
|
||||
("message", "outgoing"),
|
||||
("presence", "managed_entity"),
|
||||
("roster", permissions.RosterAccess.BOTH),
|
||||
("message", permissions.MessagePermission.OUTGOING),
|
||||
("presence", permissions.PresencePermission.MANAGED_ENTITY),
|
||||
("iq", permissions.IqPermission.BOTH),
|
||||
]:
|
||||
msg["privilege"].add_perm(access, type_)
|
||||
|
||||
self.check(msg, xmlstring)
|
||||
# Should this one work? → # AttributeError: 'Message' object has no attribute 'permission'
|
||||
# self.assertEqual(msg.permission["roster"], "both")
|
||||
|
||||
def testIqPermission(self):
|
||||
x = stanza.Privilege()
|
||||
x["access"] = "iq"
|
||||
ns = stanza.NameSpace()
|
||||
ns["ns"] = "some_ns"
|
||||
ns["type"] = "get"
|
||||
x["perm"]["access"] = "iq"
|
||||
x["perm"].append(ns)
|
||||
ns = stanza.NameSpace()
|
||||
ns["ns"] = "some_other_ns"
|
||||
ns["type"] = "both"
|
||||
x["perm"].append(ns)
|
||||
self.check(
|
||||
x,
|
||||
"""
|
||||
<privilege xmlns='urn:xmpp:privilege:2'>
|
||||
<perm access='iq'>
|
||||
<namespace ns='some_ns' type='get' />
|
||||
<namespace ns='some_other_ns' type='both' />
|
||||
</perm>
|
||||
</privilege>
|
||||
"""
|
||||
)
|
||||
nss = set()
|
||||
for perm in x["perms"]:
|
||||
for ns in perm["namespaces"]:
|
||||
nss.add((ns["ns"], ns["type"]))
|
||||
assert nss == {("some_ns", "get"), ("some_other_ns", "both")}
|
||||
|
||||
|
||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestPermissions)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import unittest
|
||||
|
||||
from slixmpp import ComponentXMPP, Iq, Message
|
||||
from slixmpp.roster import RosterItem
|
||||
from slixmpp import Message, JID, Iq
|
||||
from slixmpp.plugins.xep_0356 import permissions
|
||||
from slixmpp.test import SlixTest
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ class TestPermissions(SlixTest):
|
||||
def setUp(self):
|
||||
self.stream_start(
|
||||
mode="component",
|
||||
plugins=["xep_0356"],
|
||||
plugins=["xep_0356", "xep_0045"],
|
||||
jid="pubsub.capulet.lit",
|
||||
server="capulet.net",
|
||||
server="capulet.lit",
|
||||
)
|
||||
|
||||
def testPluginEnd(self):
|
||||
@@ -23,26 +23,44 @@ class TestPermissions(SlixTest):
|
||||
self.assertFalse(exc)
|
||||
|
||||
def testGrantedPrivileges(self):
|
||||
# https://xmpp.org/extensions/xep-0356.html#example-4
|
||||
results = {"event": False}
|
||||
x = self.xmpp["xep_0356"]
|
||||
self.xmpp.add_event_handler(
|
||||
"privileges_advertised", lambda msg: results.__setitem__("event", True)
|
||||
)
|
||||
self.recv(
|
||||
"""
|
||||
<message from='capulet.net' to='pubub.capulet.lit' id='54321'>
|
||||
<message from='capulet.lit' to='pubsub.capulet.lit' id='54321'>
|
||||
<privilege xmlns='urn:xmpp:privilege:2'>
|
||||
<perm access='roster' type='both'/>
|
||||
<perm access='message' type='outgoing'/>
|
||||
<perm access='iq'>
|
||||
<namespace ns='some_ns' type='get' />
|
||||
<namespace ns='some_other_ns' type='both' />
|
||||
</perm>
|
||||
</privilege>
|
||||
</message>
|
||||
"""
|
||||
)
|
||||
self.assertEqual(self.xmpp["xep_0356"].granted_privileges["roster"], "both")
|
||||
server = JID("capulet.lit")
|
||||
self.assertEqual(
|
||||
self.xmpp["xep_0356"].granted_privileges["message"], "outgoing"
|
||||
x.granted_privileges[server].roster, permissions.RosterAccess.BOTH
|
||||
)
|
||||
self.assertEqual(
|
||||
x.granted_privileges[server].message, permissions.MessagePermission.OUTGOING
|
||||
)
|
||||
self.assertEqual(
|
||||
x.granted_privileges[server].presence, permissions.PresencePermission.NONE
|
||||
)
|
||||
self.assertEqual(
|
||||
x.granted_privileges[server].iq["nope"], permissions.IqPermission.NONE
|
||||
)
|
||||
self.assertEqual(
|
||||
x.granted_privileges[server].iq["some_ns"], permissions.IqPermission.GET
|
||||
)
|
||||
self.assertEqual(
|
||||
x.granted_privileges[server].iq["some_other_ns"], permissions.IqPermission.BOTH
|
||||
)
|
||||
self.assertEqual(self.xmpp["xep_0356"].granted_privileges["presence"], "none")
|
||||
self.assertTrue(results["event"])
|
||||
|
||||
def testGetRosterIq(self):
|
||||
@@ -94,7 +112,7 @@ class TestPermissions(SlixTest):
|
||||
|
||||
def testMakeOutgoingMessage(self):
|
||||
xmlstring = """
|
||||
<message xmlns="jabber:component:accept" from='pubsub.capulet.lit' to='capulet.net'>
|
||||
<message xmlns="jabber:component:accept" from='pubsub.capulet.lit' to='capulet.lit'>
|
||||
<privilege xmlns='urn:xmpp:privilege:2'>
|
||||
<forwarded xmlns='urn:xmpp:forward:0'>
|
||||
<message from="juliet@capulet.lit" to="romeo@montague.lit" xmlns="jabber:client">
|
||||
@@ -108,9 +126,49 @@ class TestPermissions(SlixTest):
|
||||
msg["from"] = "juliet@capulet.lit"
|
||||
msg["to"] = "romeo@montague.lit"
|
||||
msg["body"] = "I do not hate you"
|
||||
|
||||
|
||||
priv_msg = self.xmpp["xep_0356"]._make_privileged_message(msg)
|
||||
self.check(priv_msg, xmlstring, use_values=False)
|
||||
|
||||
def testDetectServer(self):
|
||||
msg = Message()
|
||||
msg["from"] = "juliet@something"
|
||||
msg["to"] = "romeo@montague.lit"
|
||||
msg["body"] = "I do not hate you"
|
||||
|
||||
priv_msg = self.xmpp["xep_0356"]._make_privileged_message(msg)
|
||||
assert priv_msg.get_to() == "something"
|
||||
assert priv_msg.get_from() == "pubsub.capulet.lit"
|
||||
|
||||
def testIqOnBehalf(self):
|
||||
iq = Iq()
|
||||
iq["mucadmin_query"]["item"]["affiliation"] = "member"
|
||||
iq.set_from("juliet@xxx")
|
||||
iq.set_to("somemuc@conf")
|
||||
iq.set_type("get")
|
||||
self.xmpp["xep_0356"].granted_privileges["conf"].iq["http://jabber.org/protocol/muc#admin"] = permissions.IqPermission.BOTH
|
||||
r = self.xmpp.loop.create_task(self.xmpp["xep_0356"].send_privileged_iq(iq, iq_id="0"))
|
||||
self.send(
|
||||
"""
|
||||
<iq from="pubsub.capulet.lit"
|
||||
to="juliet@xxx"
|
||||
xmlns="jabber:component:accept"
|
||||
type="get" id="0">
|
||||
<privileged_iq xmlns='urn:xmpp:privilege:2'>
|
||||
<iq xmlns='jabber:client'
|
||||
type='get'
|
||||
to='somemuc@conf'
|
||||
from='juliet@xxx'
|
||||
id="0">
|
||||
<query xmlns='http://jabber.org/protocol/muc#admin'>
|
||||
<item affiliation='member'/>
|
||||
</query>
|
||||
</iq>
|
||||
</privileged_iq>
|
||||
</iq>
|
||||
""",
|
||||
use_values=False
|
||||
)
|
||||
|
||||
|
||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestPermissions)
|
||||
|
||||
Reference in New Issue
Block a user