From de548fbccec7df25b2221a2f9b67384ff16038cf Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 3 Dec 2020 22:05:47 +0100 Subject: [PATCH 1/4] XEP-0045: Add status_codes interface to the MUC element --- slixmpp/plugins/xep_0045/muc.py | 10 +++++----- slixmpp/plugins/xep_0045/stanza.py | 31 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py index f310c03e..d7b8f5c2 100644 --- a/slixmpp/plugins/xep_0045/muc.py +++ b/slixmpp/plugins/xep_0045/muc.py @@ -25,6 +25,7 @@ from slixmpp.plugins import BasePlugin from slixmpp.xmlstream import register_stanza_plugin, ET from slixmpp.xmlstream.handler.callback import Callback from slixmpp.xmlstream.matcher.xpath import MatchXPath +from slixmpp.xmlstream.matcher.stanzapath import StanzaPath from slixmpp.xmlstream.matcher.xmlmask import MatchXMLMask from slixmpp.exceptions import IqError, IqTimeout @@ -38,6 +39,7 @@ from slixmpp.plugins.xep_0045.stanza import ( MUCHistory, MUCOwnerQuery, MUCOwnerDestroy, + MUCStatus, ) @@ -62,6 +64,8 @@ class XEP_0045(BasePlugin): self.rooms = {} self.our_nicks = {} # load MUC support in presence stanzas + register_stanza_plugin(MUCMessage, MUCStatus) + register_stanza_plugin(MUCPresence, MUCStatus) register_stanza_plugin(Presence, MUCPresence) register_stanza_plugin(Presence, MUCJoin) register_stanza_plugin(MUCJoin, MUCHistory) @@ -99,11 +103,7 @@ class XEP_0045(BasePlugin): self.xmpp.register_handler( Callback( 'MUCConfig', - MatchXMLMask( - "" - "" - "" % self.xmpp.default_ns - ), + StanzaPath('message/muc/status'), self.handle_config_change )) self.xmpp.register_handler( diff --git a/slixmpp/plugins/xep_0045/stanza.py b/slixmpp/plugins/xep_0045/stanza.py index 9756790b..337363c8 100644 --- a/slixmpp/plugins/xep_0045/stanza.py +++ b/slixmpp/plugins/xep_0045/stanza.py @@ -7,6 +7,7 @@ See the file LICENSE for copying permission. """ +from typing import Iterable, Set import logging from slixmpp.xmlstream import ElementBase, ET, JID @@ -23,7 +24,26 @@ class MUCBase(ElementBase): name = 'x' namespace = NS_USER plugin_attrib = 'muc' - interfaces = {'affiliation', 'role', 'jid', 'nick', 'room'} + interfaces = {'affiliation', 'role', 'jid', 'nick', 'room', 'status_codes'} + + def get_status_codes(self) -> Set[str]: + status = self.xml.findall(f'{{{NS_USER}}}status') + return {int(status.attrib['code']) for status in status} + + def set_status_codes(self, codes: Iterable[int]): + self.del_status_codes() + for code in set(codes): + self._add_status_code(code) + + def del_status_codes(self): + status = self.xml.findall(f'{{{NS_USER}}}status') + for elem in status: + self.xml.remove(elem) + + def _add_status_code(self, code: int): + status = MUCStatus() + status['code'] = code + self.append(status) def get_item_attr(self, attr, default: str): item = self.xml.find(f'{{{NS_USER}}}item') @@ -196,3 +216,12 @@ class MUCAdminItem(ElementBase): plugin_attrib = 'item' interfaces = {'role', 'affiliation', 'nick', 'jid'} + +class MUCStatus(ElementBase): + namespace = NS_USER + name = 'status' + plugin_attrib = 'status' + interfaces = {'code'} + + def set_code(self, code: int): + self.xml.attrib['code'] = str(code) From 98b9a6f9e3d2932f2cc1f038cc8e82d3922a1c6e Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 3 Dec 2020 22:35:23 +0100 Subject: [PATCH 2/4] XEP-0045: Remove "self" returns from stanza methods --- slixmpp/plugins/xep_0045/stanza.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/slixmpp/plugins/xep_0045/stanza.py b/slixmpp/plugins/xep_0045/stanza.py index 337363c8..42aab794 100644 --- a/slixmpp/plugins/xep_0045/stanza.py +++ b/slixmpp/plugins/xep_0045/stanza.py @@ -69,12 +69,10 @@ class MUCBase(ElementBase): def set_affiliation(self, value): self.set_item_attr('affiliation', value) - return self def del_affiliation(self): # TODO: set default affiliation self.del_item_attr('affiliation') - return self def get_jid(self): return JID(self.get_item_attr('jid', '')) @@ -83,11 +81,9 @@ class MUCBase(ElementBase): if not isinstance(value, str): value = str(value) self.set_item_attr('jid', value) - return self def del_jid(self): self.del_item_attr('jid') - return self def get_role(self): return self.get_item_attr('role', '') @@ -95,12 +91,10 @@ class MUCBase(ElementBase): def set_role(self, value): # TODO: check for valid role self.set_item_attr('role', value) - return self def del_role(self): # TODO: set default role self.del_item_attr('role') - return self def get_nick(self): return self.parent()['from'].resource From 7033bc00611e0fa3110fd7b48bb78400445c1db9 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 3 Dec 2020 22:55:10 +0100 Subject: [PATCH 3/4] XEP-0045: Better invitation handling --- slixmpp/plugins/xep_0045/muc.py | 35 ++++++++++++++++++++---------- slixmpp/plugins/xep_0045/stanza.py | 10 ++++++++- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py index d7b8f5c2..29c03d6e 100644 --- a/slixmpp/plugins/xep_0045/muc.py +++ b/slixmpp/plugins/xep_0045/muc.py @@ -109,13 +109,15 @@ class XEP_0045(BasePlugin): self.xmpp.register_handler( Callback( 'MUCInvite', - MatchXPath("{%s}message/{%s}x/{%s}invite" % ( - self.xmpp.default_ns, - stanza.NS_USER, - stanza.NS_USER - )), + StanzaPath('message/muc/invite'), self.handle_groupchat_invite )) + self.xmpp.register_handler( + Callback( + 'MUCDecline', + StanzaPath('message/muc/decline'), + self.handle_groupchat_decline + )) def plugin_end(self): self.xmpp.plugin['xep_0030'].del_feature(feature=stanza.NS) @@ -124,12 +126,15 @@ class XEP_0045(BasePlugin): self.xmpp.plugin['xep_0030'].add_feature(stanza.NS) def handle_groupchat_invite(self, inv): - """ Handle an invite into a muc. - """ - logging.debug("MUC invite to %s from %s: %s", inv['to'], inv["from"], inv) + """ Handle an invite into a muc. """ if inv['from'] not in self.rooms.keys(): self.xmpp.event("groupchat_invite", inv) + def handle_groupchat_decline(self, decl): + """Handle an invitation decline.""" + if decl['from'] in self.room.keys(): + self.xmpp.event('groupchat_decline', decl) + def handle_config_change(self, msg): """Handle a MUC configuration change (with status code).""" self.xmpp.event('groupchat_config_status', msg) @@ -265,16 +270,24 @@ class XEP_0045(BasePlugin): iq['mucadmin_query'].append(item) await iq.send(**iqkwargs) - def invite(self, room: JID, jid: JID, reason='', *, + def invite(self, room: JID, jid: JID, reason: str = '', *, mfrom: Optional[JID] = None): """ Invite a jid to a room.""" msg = self.xmpp.make_message(room, mfrom=mfrom) - msg.enable('muc') - msg['muc']['invite'] = jid + msg['muc']['invite']['to'] = jid if reason: msg['muc']['invite']['reason'] = reason self.xmpp.send(msg) + def decline(self, room: JID, jid: JID, reason: str = '', *, + mfrom: Optional[JID] = None): + """Decline a mediated invitation.""" + msg = self.xmpp.make_message(room, mfrom=mfrom) + msg['muc']['decline']['to'] = jid + if reason: + msg['muc']['decline']['reason'] = reason + self.xmpp.send(msg) + def leave_muc(self, room: JID, nick: str, msg='', pfrom=None): """ Leave the specified room. """ diff --git a/slixmpp/plugins/xep_0045/stanza.py b/slixmpp/plugins/xep_0045/stanza.py index 42aab794..71223f0d 100644 --- a/slixmpp/plugins/xep_0045/stanza.py +++ b/slixmpp/plugins/xep_0045/stanza.py @@ -174,7 +174,15 @@ class MUCInvite(ElementBase): name = 'invite' plugin_attrib = 'invite' namespace = NS_USER - interfaces = {'to', 'reason'} + interfaces = {'to', 'from', 'reason'} + sub_interfaces = {'reason'} + + +class MUCDecline(ElementBase): + name = 'decline' + plugin_attrib = 'decline' + namespace = NS_USER + interfaces = {'to', 'from', 'reason'} sub_interfaces = {'reason'} From fa7e33110fa7c9afd30145b04e3965e0bcecf7f3 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 3 Dec 2020 22:56:46 +0100 Subject: [PATCH 4/4] XEP-0045: Misc small fixes. --- slixmpp/plugins/xep_0045/muc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py index 29c03d6e..630112f3 100644 --- a/slixmpp/plugins/xep_0045/muc.py +++ b/slixmpp/plugins/xep_0045/muc.py @@ -226,6 +226,7 @@ class XEP_0045(BasePlugin): async def destroy(self, room: JID, reason='', altroom='', *, ifrom: Optional[JID] = None, **iqkwargs) -> Iq: + """Destroy a room.""" iq = self.xmpp.make_iq_set(ifrom=ifrom, ito=room) iq.enable('mucowner_query') iq['mucowner_query'].enable('destroy') @@ -297,7 +298,6 @@ class XEP_0045(BasePlugin): self.xmpp.send_presence(pshow='unavailable', pto="%s/%s" % (room, nick), pfrom=pfrom) del self.rooms[room] - async def get_room_config(self, room: JID, ifrom=''): """Get the room config form in 0004 plugin format """ iq = self.xmpp.make_iq_get(stanza.NS_OWNER, ito=room, ifrom=ifrom) @@ -354,7 +354,7 @@ class XEP_0045(BasePlugin): return await iq.send(**iqkwargs) async def send_role_list(self, room: JID, roles: List[Tuple[str, str]], *, - ifrom: Optional[JID], **iqkwargs) -> Iq: + ifrom: Optional[JID] = None, **iqkwargs) -> Iq: """Send a role delta list""" iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom) for nick, affiliation in roles: