From 4b0e481902084970e89eafd23acd535b0e6187bc Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 27 Dec 2020 02:23:16 +0100 Subject: [PATCH] Rework set_affiliation() to use slixmpp. Also remove two safeJID(). --- poezio/multiuserchat.py | 37 ------------------------------------- poezio/tabs/muctab.py | 38 ++++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 57 deletions(-) diff --git a/poezio/multiuserchat.py b/poezio/multiuserchat.py index d7029adf..9cd94fa8 100644 --- a/poezio/multiuserchat.py +++ b/poezio/multiuserchat.py @@ -169,40 +169,3 @@ def set_user_role( iq.append(query) iq['to'] = jid iq.send(callback=callback) - - -def set_user_affiliation( - xmpp: ClientXMPP, - muc_jid: JID, - affiliation: str, - callback: Callable[[Iq], None], - nick: Optional[str] = None, - jid: Optional[JID] = None, - reason: Optional[str] = None -) -> None: - """ - (try to) Set the affiliation of a MUC user - """ - muc_jid = safeJID(muc_jid) - query = ET.Element('{http://jabber.org/protocol/muc#admin}query') - if nick: - item = ET.Element('{http://jabber.org/protocol/muc#admin}item', { - 'affiliation': affiliation, - 'nick': nick - }) - else: - item = ET.Element('{http://jabber.org/protocol/muc#admin}item', { - 'affiliation': affiliation, - 'jid': str(jid) - }) - - if reason: - reason_item = ET.Element( - '{http://jabber.org/protocol/muc#admin}reason') - reason_item.text = reason - item.append(reason_item) - - query.append(item) - iq = xmpp.make_iq_set(query) - iq['to'] = muc_jid - iq.send(callback=callback) diff --git a/poezio/tabs/muctab.py b/poezio/tabs/muctab.py index 88fb5419..31b6d845 100644 --- a/poezio/tabs/muctab.py +++ b/poezio/tabs/muctab.py @@ -241,12 +241,6 @@ class MucTab(ChatTab): Change the affiliation of a nick or JID """ - def callback(iq: Iq) -> None: - if iq['type'] == 'error': - self.core.information( - "Could not set affiliation '%s' for '%s'." % - (affiliation, nick_or_jid), "Warning") - if not self.joined: return @@ -257,21 +251,25 @@ class MucTab(ChatTab): ', '.join(valid_affiliations), 'Error') return if nick_or_jid in [user.nick for user in self.users]: - muc.set_user_affiliation( - self.core.xmpp, - self.jid.bare, - affiliation, - nick=nick_or_jid, - callback=callback, - reason=reason) + nick = nick_or_jid + jid = None else: - muc.set_user_affiliation( - self.core.xmpp, - self.jid.bare, - affiliation, - jid=safeJID(nick_or_jid), - callback=callback, - reason=reason) + nick = None + try: + jid = JID(nick_or_jid) + except InvalidJID: + self.core.information('Invalid JID or missing occupant: %s' % nick_or_jid, 'Error') + return + + async def do_set_affiliation(room: JID, jid: Optional[JID], nick: Optional[str], affiliation: str, reason: str): + try: + await self.core.xmpp['xep_0045'].set_affiliation(room, jid, nick, affiliation=affiliation, reason=reason) + except (IqError, IqTimeout) as e: + self.core.information( + "Could not set affiliation '%s' for '%s': %s" % + (affiliation, nick_or_jid, e), "Warning") + + asyncio.ensure_future(do_set_affiliation(self.jid.bare, jid, nick, affiliation, reason)) def change_role(self, nick: str, role: str, reason: str = '') -> None: """