XEP-0045: Add status_codes interface to the MUC element

This commit is contained in:
mathieui 2020-12-03 22:05:47 +01:00
parent af46efc12a
commit de548fbcce
2 changed files with 35 additions and 6 deletions

View File

@ -25,6 +25,7 @@ from slixmpp.plugins import BasePlugin
from slixmpp.xmlstream import register_stanza_plugin, ET from slixmpp.xmlstream import register_stanza_plugin, ET
from slixmpp.xmlstream.handler.callback import Callback from slixmpp.xmlstream.handler.callback import Callback
from slixmpp.xmlstream.matcher.xpath import MatchXPath from slixmpp.xmlstream.matcher.xpath import MatchXPath
from slixmpp.xmlstream.matcher.stanzapath import StanzaPath
from slixmpp.xmlstream.matcher.xmlmask import MatchXMLMask from slixmpp.xmlstream.matcher.xmlmask import MatchXMLMask
from slixmpp.exceptions import IqError, IqTimeout from slixmpp.exceptions import IqError, IqTimeout
@ -38,6 +39,7 @@ from slixmpp.plugins.xep_0045.stanza import (
MUCHistory, MUCHistory,
MUCOwnerQuery, MUCOwnerQuery,
MUCOwnerDestroy, MUCOwnerDestroy,
MUCStatus,
) )
@ -62,6 +64,8 @@ class XEP_0045(BasePlugin):
self.rooms = {} self.rooms = {}
self.our_nicks = {} self.our_nicks = {}
# load MUC support in presence stanzas # 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, MUCPresence)
register_stanza_plugin(Presence, MUCJoin) register_stanza_plugin(Presence, MUCJoin)
register_stanza_plugin(MUCJoin, MUCHistory) register_stanza_plugin(MUCJoin, MUCHistory)
@ -99,11 +103,7 @@ class XEP_0045(BasePlugin):
self.xmpp.register_handler( self.xmpp.register_handler(
Callback( Callback(
'MUCConfig', 'MUCConfig',
MatchXMLMask( StanzaPath('message/muc/status'),
"<message xmlns='%s' type='groupchat'>"
"<x xmlns='http://jabber.org/protocol/muc#user'><status/></x>"
"</message>" % self.xmpp.default_ns
),
self.handle_config_change self.handle_config_change
)) ))
self.xmpp.register_handler( self.xmpp.register_handler(

View File

@ -7,6 +7,7 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from typing import Iterable, Set
import logging import logging
from slixmpp.xmlstream import ElementBase, ET, JID from slixmpp.xmlstream import ElementBase, ET, JID
@ -23,7 +24,26 @@ class MUCBase(ElementBase):
name = 'x' name = 'x'
namespace = NS_USER namespace = NS_USER
plugin_attrib = 'muc' 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): def get_item_attr(self, attr, default: str):
item = self.xml.find(f'{{{NS_USER}}}item') item = self.xml.find(f'{{{NS_USER}}}item')
@ -196,3 +216,12 @@ class MUCAdminItem(ElementBase):
plugin_attrib = 'item' plugin_attrib = 'item'
interfaces = {'role', 'affiliation', 'nick', 'jid'} 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)