summaryrefslogtreecommitdiff
path: root/slixmpp
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2021-01-10 14:38:43 +0100
committermathieui <mathieui@mathieui.net>2021-01-10 15:07:48 +0100
commitcccc1253aa12f62a6cf8403ee54facf656521504 (patch)
tree0a7cb411338ea4f76352aa114f4b9b53ea12292a /slixmpp
parentfb31e9c1fd47370ffec796d29f81264a9d8f197e (diff)
downloadslixmpp-cccc1253aa12f62a6cf8403ee54facf656521504.tar.gz
slixmpp-cccc1253aa12f62a6cf8403ee54facf656521504.tar.bz2
slixmpp-cccc1253aa12f62a6cf8403ee54facf656521504.tar.xz
slixmpp-cccc1253aa12f62a6cf8403ee54facf656521504.zip
XEP-0045: add more elements (<actor/>)
Diffstat (limited to 'slixmpp')
-rw-r--r--slixmpp/plugins/xep_0045/muc.py30
-rw-r--r--slixmpp/plugins/xep_0045/stanza.py64
2 files changed, 61 insertions, 33 deletions
diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py
index 4220978d..03d01dba 100644
--- a/slixmpp/plugins/xep_0045/muc.py
+++ b/slixmpp/plugins/xep_0045/muc.py
@@ -42,6 +42,8 @@ from slixmpp.plugins.xep_0045.stanza import (
MUCOwnerQuery,
MUCOwnerDestroy,
MUCStatus,
+ MUCActor,
+ MUCUserItem,
)
@@ -66,6 +68,9 @@ class XEP_0045(BasePlugin):
self.rooms = {}
self.our_nicks = {}
# load MUC support in presence stanzas
+ register_stanza_plugin(MUCMessage, MUCUserItem)
+ register_stanza_plugin(MUCPresence, MUCUserItem)
+ register_stanza_plugin(MUCUserItem, MUCActor)
register_stanza_plugin(MUCMessage, MUCInvite)
register_stanza_plugin(MUCMessage, MUCDecline)
register_stanza_plugin(MUCMessage, MUCStatus)
@@ -248,24 +253,22 @@ class XEP_0045(BasePlugin):
iq['mucowner_query']['destroy']['reason'] = reason
await iq.send(**iqkwargs)
- async def set_affiliation(self, room: JID, jid: Optional[JID] = None, nick: Optional[str] = None, *, affiliation: str,
- reason: str = '', ifrom: Optional[JID] = None, **iqkwargs):
+ async def set_affiliation(self, room: JID, affiliation: str, *, jid: Optional[JID] = None,
+ nick: Optional[str] = None, reason: str = '',
+ ifrom: Optional[JID] = None, **iqkwargs):
""" Change room affiliation."""
if affiliation not in AFFILIATIONS:
raise ValueError('%s is not a valid affiliation' % affiliation)
if not any((jid, nick)):
raise ValueError('One of jid or nick must be set')
iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom)
- iq.enable('mucadmin_query')
- item = MUCAdminItem()
- item['affiliation'] = affiliation
+ iq['mucadmin_query']['item']['affiliation'] = affiliation
if nick:
- item['nick'] = nick
+ iq['mucadmin_query']['item']['nick'] = nick
if jid:
- item['jid'] = jid
+ iq['mucadmin_query']['item']['jid'] = jid
if reason:
- item['reason'] = reason
- iq['mucadmin_query'].append(item)
+ iq['mucadmin_query']['item']['reason'] = reason
await iq.send(**iqkwargs)
async def set_role(self, room: JID, nick: str, role: str, *,
@@ -278,13 +281,10 @@ class XEP_0045(BasePlugin):
if role not in ROLES:
raise ValueError("Role %s does not exist" % role)
iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom)
- iq.enable('mucadmin_query')
- item = MUCAdminItem()
- item['role'] = role
- item['nick'] = nick
+ iq['mucadmin_query']['item']['role'] = role
+ iq['mucadmin_query']['item']['nick'] = nick
if reason:
- item['reason'] = reason
- iq['mucadmin_query'].append(item)
+ iq['mucadmin_query']['item']['reason'] = reason
await iq.send(**iqkwargs)
def invite(self, room: JID, jid: JID, reason: str = '', *,
diff --git a/slixmpp/plugins/xep_0045/stanza.py b/slixmpp/plugins/xep_0045/stanza.py
index 8de938fb..65c7dafe 100644
--- a/slixmpp/plugins/xep_0045/stanza.py
+++ b/slixmpp/plugins/xep_0045/stanza.py
@@ -7,7 +7,12 @@
See the file LICENSE for copying permission.
"""
-from typing import Iterable, Set
+from typing import (
+ Iterable,
+ Set,
+ Optional,
+ Union,
+)
import logging
from slixmpp.xmlstream import ElementBase, ET, JID
@@ -45,24 +50,21 @@ class MUCBase(ElementBase):
status['code'] = code
self.append(status)
- def get_item_attr(self, attr, default: str):
+ def get_item_attr(self, attr: str, default):
item = self.xml.find(f'{{{NS_USER}}}item')
if item is None:
return default
- return item.get(attr)
+ return self['item'][attr]
- def set_item_attr(self, attr, value: str):
- item = self.xml.find(f'{{{NS_USER}}}item')
- if item is None:
- item = ET.Element(f'{{{NS_USER}}}item')
- self.xml.append(item)
- item.attrib[attr] = value
+ def set_item_attr(self, attr: str, value: str):
+ item = self['item']
+ item[attr] = value
return item
def del_item_attr(self, attr):
item = self.xml.find(f'{{{NS_USER}}}item')
- if item is not None and attr in item.attrib:
- del item.attrib[attr]
+ if item is not None:
+ del self['item'][attr]
def get_affiliation(self):
return self.get_item_attr('affiliation', '')
@@ -71,13 +73,12 @@ class MUCBase(ElementBase):
self.set_item_attr('affiliation', value)
def del_affiliation(self):
- # TODO: set default affiliation
self.del_item_attr('affiliation')
- def get_jid(self):
+ def get_jid(self) -> JID:
return JID(self.get_item_attr('jid', ''))
- def set_jid(self, value):
+ def set_jid(self, value: Union[JID, str]):
if not isinstance(value, str):
value = str(value)
self.set_item_attr('jid', value)
@@ -85,10 +86,10 @@ class MUCBase(ElementBase):
def del_jid(self):
self.del_item_attr('jid')
- def get_role(self):
+ def get_role(self) -> str:
return self.get_item_attr('role', '')
- def set_role(self, value):
+ def set_role(self, value: str):
# TODO: check for valid role
self.set_item_attr('role', value)
@@ -96,10 +97,10 @@ class MUCBase(ElementBase):
# TODO: set default role
self.del_item_attr('role')
- def get_nick(self):
+ def get_nick(self) -> str:
return self.parent()['from'].resource
- def get_room(self):
+ def get_room(self) -> str:
return self.parent()['from'].bare
def set_nick(self, value):
@@ -232,3 +233,30 @@ class MUCStatus(ElementBase):
def set_code(self, code: int):
self.xml.attrib['code'] = str(code)
+
+
+class MUCUserItem(ElementBase):
+ namespace = NS_USER
+ name = 'item'
+ plugin_attrib = 'item'
+ interfaces = {'role', 'affiliation', 'jid', 'reason', 'nick'}
+ sub_interfaces = {'reason'}
+
+ def get_jid(self) -> Optional[JID]:
+ jid = self.xml.attrib.get('jid', None)
+ if jid:
+ return JID(jid)
+ return jid
+
+
+class MUCActor(ElementBase):
+ namespace = NS_USER
+ name = 'actor'
+ plugin_attrib = 'actor'
+ interfaces = {'jid', 'nick'}
+
+ def get_jid(self) -> Optional[JID]:
+ jid = self.xml.attrib.get('jid', None)
+ if jid:
+ return JID(jid)
+ return jid