Rework set_role() to use slixmpp.

Also remove a safeJID().
This commit is contained in:
Emmanuel Gil Peyrot 2020-12-27 02:42:49 +01:00 committed by Link Mauve
parent 4b0e481902
commit c38538f6b5
2 changed files with 9 additions and 39 deletions

View file

@ -14,7 +14,6 @@ from __future__ import annotations
from xml.etree import ElementTree as ET
from typing import (
Callable,
Optional,
TYPE_CHECKING,
)
@ -35,10 +34,6 @@ if TYPE_CHECKING:
from poezio.tabs import Tab
NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
NS_MUC_OWNER = 'http://jabber.org/protocol/muc#owner'
def change_show(
xmpp: ClientXMPP,
jid: JID,
@ -143,29 +138,3 @@ def leave_groupchat(
"muc.leave_groupchat: could not leave the room %s",
jid,
exc_info=True)
def set_user_role(
xmpp: ClientXMPP,
jid: JID,
nick: str,
reason: str,
role: str,
callback: Callable[[Iq], None]
) -> None:
"""
(try to) Set the role of a MUC user
(role = 'none': eject user)
"""
jid = safeJID(jid)
iq = xmpp.make_iq_set()
query = ET.Element('{%s}query' % NS_MUC_ADMIN)
item = ET.Element('{%s}item' % NS_MUC_ADMIN, {'nick': nick, 'role': role})
if reason:
reason_el = ET.Element('{%s}reason' % NS_MUC_ADMIN)
reason_el.text = reason
item.append(reason_el)
query.append(item)
iq.append(query)
iq['to'] = jid
iq.send(callback=callback)

View file

@ -276,12 +276,6 @@ class MucTab(ChatTab):
Change the role of a nick
"""
def callback(iq: Iq) -> None:
if iq['type'] == 'error':
self.core.information(
"Could not set role '%s' for '%s'." % (role, nick),
"Warning")
valid_roles = ('none', 'visitor', 'participant', 'moderator')
if not self.joined or role not in valid_roles:
@ -296,8 +290,15 @@ class MucTab(ChatTab):
self.core.information('Invalid nick', 'Info')
return
muc.set_user_role(
self.core.xmpp, self.jid.bare, nick, reason, role, callback=callback)
async def do_set_role(room: JID, nick: str, role: str, reason: str):
try:
await self.core.xmpp['xep_0045'].set_role(room, nick, role, reason=reason)
except (IqError, IqTimeout) as e:
self.core.information(
"Could not set role '%s' for '%s': %s" % (role, nick, e),
"Warning")
asyncio.ensure_future(do_set_role(self.jid.bare, nick, role, reason))
@refresh_wrapper.conditional
def print_info(self, nick: str) -> bool: