diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py index f310c03e..d7b8f5c2 100644 --- a/slixmpp/plugins/xep_0045/muc.py +++ b/slixmpp/plugins/xep_0045/muc.py @@ -25,6 +25,7 @@ from slixmpp.plugins import BasePlugin from slixmpp.xmlstream import register_stanza_plugin, ET from slixmpp.xmlstream.handler.callback import Callback from slixmpp.xmlstream.matcher.xpath import MatchXPath +from slixmpp.xmlstream.matcher.stanzapath import StanzaPath from slixmpp.xmlstream.matcher.xmlmask import MatchXMLMask from slixmpp.exceptions import IqError, IqTimeout @@ -38,6 +39,7 @@ from slixmpp.plugins.xep_0045.stanza import ( MUCHistory, MUCOwnerQuery, MUCOwnerDestroy, + MUCStatus, ) @@ -62,6 +64,8 @@ class XEP_0045(BasePlugin): self.rooms = {} self.our_nicks = {} # load MUC support in presence stanzas + register_stanza_plugin(MUCMessage, MUCStatus) + register_stanza_plugin(MUCPresence, MUCStatus) register_stanza_plugin(Presence, MUCPresence) register_stanza_plugin(Presence, MUCJoin) register_stanza_plugin(MUCJoin, MUCHistory) @@ -99,11 +103,7 @@ class XEP_0045(BasePlugin): self.xmpp.register_handler( Callback( 'MUCConfig', - MatchXMLMask( - "" - "" - "" % self.xmpp.default_ns - ), + StanzaPath('message/muc/status'), self.handle_config_change )) self.xmpp.register_handler( diff --git a/slixmpp/plugins/xep_0045/stanza.py b/slixmpp/plugins/xep_0045/stanza.py index 9756790b..337363c8 100644 --- a/slixmpp/plugins/xep_0045/stanza.py +++ b/slixmpp/plugins/xep_0045/stanza.py @@ -7,6 +7,7 @@ See the file LICENSE for copying permission. """ +from typing import Iterable, Set import logging from slixmpp.xmlstream import ElementBase, ET, JID @@ -23,7 +24,26 @@ class MUCBase(ElementBase): name = 'x' namespace = NS_USER plugin_attrib = 'muc' - interfaces = {'affiliation', 'role', 'jid', 'nick', 'room'} + interfaces = {'affiliation', 'role', 'jid', 'nick', 'room', 'status_codes'} + + def get_status_codes(self) -> Set[str]: + status = self.xml.findall(f'{{{NS_USER}}}status') + return {int(status.attrib['code']) for status in status} + + def set_status_codes(self, codes: Iterable[int]): + self.del_status_codes() + for code in set(codes): + self._add_status_code(code) + + def del_status_codes(self): + status = self.xml.findall(f'{{{NS_USER}}}status') + for elem in status: + self.xml.remove(elem) + + def _add_status_code(self, code: int): + status = MUCStatus() + status['code'] = code + self.append(status) def get_item_attr(self, attr, default: str): item = self.xml.find(f'{{{NS_USER}}}item') @@ -196,3 +216,12 @@ class MUCAdminItem(ElementBase): plugin_attrib = 'item' interfaces = {'role', 'affiliation', 'nick', 'jid'} + +class MUCStatus(ElementBase): + namespace = NS_USER + name = 'status' + plugin_attrib = 'status' + interfaces = {'code'} + + def set_code(self, code: int): + self.xml.attrib['code'] = str(code)