Add (back?) reason and altroom arguments for /destroy_room.

Also make use of slixmpp’s new destroy_room() function.
This commit is contained in:
Emmanuel Gil Peyrot 2020-12-27 01:37:35 +01:00 committed by Link Mauve
parent 826fa779c4
commit 85b1222222
3 changed files with 34 additions and 52 deletions

View file

@ -56,7 +56,7 @@ These commands work in *any* tab.
- ``/join / password`` - ``/join / password``
/destroy_room /destroy_room
**Usage:** ``/destroy_room [room JID]`` **Usage:** ``/destroy_room [room JID [reason [alternative venue]]]``
Try to destroy the room given as a parameter, or the current room Try to destroy the room given as a parameter, or the current room
is not parameter is given and the current tab is a chatroom. is not parameter is given and the current tab is a chatroom.

View file

@ -8,7 +8,7 @@ from typing import List, Optional, Tuple
import logging import logging
from slixmpp import Iq, JID, InvalidJID from slixmpp import Iq, JID, InvalidJID
from slixmpp.exceptions import XMPPError from slixmpp.exceptions import XMPPError, IqError, IqTimeout
from slixmpp.xmlstream.xmlstream import NotConnectedError from slixmpp.xmlstream.xmlstream import NotConnectedError
from slixmpp.xmlstream.stanzabase import StanzaBase from slixmpp.xmlstream.stanzabase import StanzaBase
from slixmpp.xmlstream.handler import Callback from slixmpp.xmlstream.handler import Callback
@ -1151,26 +1151,43 @@ class CommandCore:
"disconnected", self.core.exit, disposable=True) "disconnected", self.core.exit, disposable=True)
self.core.disconnect(msg) self.core.disconnect(msg)
@command_args_parser.quoted(0, 1, ['']) @command_args_parser.quoted(0, 3, ['', '', ''])
def destroy_room(self, args: List[str]) -> None: def destroy_room(self, args: List[str]):
""" """
/destroy_room [JID] /destroy_room [JID [reason [alternative room JID]]]
""" """
async def do_destroy(room: JID, reason: str, altroom: JID):
try:
await self.core.xmpp['xep_0045'].destroy(room, reason, altroom)
except (IqError, IqTimeout) as e:
xmpp.core.information('Unable to destroy room %s: %s' % (room, e), 'Info')
else:
xmpp.core.information('Room %s destroyed' % room, 'Info')
if not args[0] and isinstance(self.core.tabs.current_tab, tabs.MucTab): if not args[0] and isinstance(self.core.tabs.current_tab, tabs.MucTab):
muc.destroy_room(self.core.xmpp, room = self.core.tabs.current_tab.general_jid
self.core.tabs.current_tab.general_jid) else:
return None try:
room = JID(args[0])
except InvalidJID:
room = None
else:
if room.resource:
room = None
try: if room is None:
room = JID(args[0]).bare self.core.information('Invalid room JID: "%s"' % args[0], 'Error')
if room: return
muc.destroy_room(self.core.xmpp, room)
return None
except InvalidJID:
pass
self.core.information('Invalid JID: "%s"' % args[0], 'Error') reason = args[1]
return None if args[2]:
try:
altroom = JID(args[2])
except InvalidJID:
self.core.information('Invalid alternative room JID: "%s"' % args[2], 'Error')
return
asyncio.ensure_future(do_destroy(room, reason, altroom))
@command_args_parser.quoted(1, 1, ['']) @command_args_parser.quoted(1, 1, [''])
def bind(self, args): def bind(self, args):

View file

@ -39,41 +39,6 @@ NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
NS_MUC_OWNER = 'http://jabber.org/protocol/muc#owner' NS_MUC_OWNER = 'http://jabber.org/protocol/muc#owner'
def destroy_room(
xmpp: ClientXMPP,
room: str,
reason: str = '',
altroom: str = ''
) -> bool:
"""
destroy a room
"""
room = safeJID(room)
if not room:
return False
iq = xmpp.make_iq_set()
iq['to'] = room
query = ET.Element('{%s}query' % NS_MUC_OWNER)
destroy = ET.Element('{%s}destroy' % NS_MUC_OWNER)
if altroom:
destroy.attrib['jid'] = altroom
if reason:
xreason = ET.Element('{%s}reason' % NS_MUC_OWNER)
xreason.text = reason
destroy.append(xreason)
query.append(destroy)
iq.append(query)
def callback(iq: Iq) -> None:
if not iq or iq['type'] == 'error':
xmpp.core.information('Unable to destroy room %s' % room, 'Info')
else:
xmpp.core.information('Room %s destroyed' % room, 'Info')
iq.send(callback=callback)
return True
def change_show( def change_show(
xmpp: ClientXMPP, xmpp: ClientXMPP,
jid: JID, jid: JID,