Rework set_affiliation() to use slixmpp.

Also remove two safeJID().
This commit is contained in:
Emmanuel Gil Peyrot 2020-12-27 02:23:16 +01:00 committed by Link Mauve
parent 0474d0f4b2
commit 4b0e481902
2 changed files with 18 additions and 57 deletions

View file

@ -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)

View file

@ -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:
"""