From 7033bc00611e0fa3110fd7b48bb78400445c1db9 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 3 Dec 2020 22:55:10 +0100 Subject: [PATCH] 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'}