Use the new xep_0045 elements from slixmpp 1.6.0

This commit is contained in:
Emmanuel Gil Peyrot 2020-12-12 17:28:22 +01:00
parent 93ddfecc6b
commit c14a08d708
4 changed files with 23 additions and 46 deletions

View file

@ -112,7 +112,7 @@ class HandlerCore:
""" """
# first, look for the x (XEP-0045 version 1.28) # first, look for the x (XEP-0045 version 1.28)
if message.xml.find('{http://jabber.org/protocol/muc#user}x') is not None: if message.match('message/muc'):
log.debug('MUC-PM from %s with <x>', with_jid) log.debug('MUC-PM from %s with <x>', with_jid)
return True return True
@ -220,12 +220,7 @@ class HandlerCore:
jid = message['from'] jid = message['from']
if jid.bare in self.core.pending_invites: if jid.bare in self.core.pending_invites:
return return
# there are 2 'x' tags in the messages, making message['x'] useless invite = message['muc']['invite']
invite = StanzaBase(
self.core.xmpp,
xml=message.xml.find(
'{http://jabber.org/protocol/muc#user}x/{http://jabber.org/protocol/muc#user}invite'
))
# TODO: find out why pylint thinks "inviter" is a list # TODO: find out why pylint thinks "inviter" is a list
#pylint: disable=no-member #pylint: disable=no-member
inviter = invite['from'] inviter = invite['from']
@ -281,9 +276,7 @@ class HandlerCore:
When receiving private message from a muc OR a normal message When receiving private message from a muc OR a normal message
(from one of our contacts) (from one of our contacts)
""" """
if message.xml.find( if message.match('message/muc/invite'):
'{http://jabber.org/protocol/muc#user}x/{http://jabber.org/protocol/muc#user}invite'
) is not None:
return return
if message['type'] == 'groupchat': if message['type'] == 'groupchat':
return return
@ -1128,8 +1121,7 @@ class HandlerCore:
### Presence-related handlers ### ### Presence-related handlers ###
def on_presence(self, presence): def on_presence(self, presence):
if presence.match('presence/muc') or presence.xml.find( if presence.match('presence/muc'):
'{http://jabber.org/protocol/muc#user}x') is not None:
return return
jid = presence['from'] jid = presence['from']
contact = roster[jid.bare] contact = roster[jid.bare]
@ -1168,8 +1160,7 @@ class HandlerCore:
""" """
A JID got offline A JID got offline
""" """
if presence.match('presence/muc') or presence.xml.find( if presence.match('presence/muc'):
'{http://jabber.org/protocol/muc#user}x') is not None:
return return
jid = presence['from'] jid = presence['from']
status = presence['status'] status = presence['status']
@ -1201,8 +1192,7 @@ class HandlerCore:
""" """
A JID got online A JID got online
""" """
if presence.match('presence/muc') or presence.xml.find( if presence.match('presence/muc'):
'{http://jabber.org/protocol/muc#user}x') is not None:
return return
jid = presence['from'] jid = presence['from']
contact = roster[jid.bare] contact = roster[jid.bare]

View file

@ -57,8 +57,6 @@ if TYPE_CHECKING:
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
# getters for tab colors (lambdas, so that they are dynamic) # getters for tab colors (lambdas, so that they are dynamic)
STATE_COLORS = { STATE_COLORS = {
'disconnected': lambda: get_theme().COLOR_TAB_DISCONNECTED, 'disconnected': lambda: get_theme().COLOR_TAB_DISCONNECTED,
@ -690,8 +688,7 @@ class ChatTab(Tab):
self.chat_state = state self.chat_state = state
msg['no-store'] = True msg['no-store'] = True
if isinstance(self, PrivateTab): if isinstance(self, PrivateTab):
x = ET.Element('{%s}x' % NS_MUC_USER) msg.enable('muc')
msg.append(x)
msg.send() msg.send()
def send_composing_chat_state(self, empty_after: bool) -> None: def send_composing_chat_state(self, empty_after: bool) -> None:

View file

@ -68,7 +68,6 @@ if TYPE_CHECKING:
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
NS_MUC_USER = 'http://jabber.org/protocol/muc#user' NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
STATUS_XPATH = '{%s}x/{%s}status' % (NS_MUC_USER, NS_MUC_USER)
COMPARE_USERS_LAST_TALKED = lambda x: x.last_talked COMPARE_USERS_LAST_TALKED = lambda x: x.last_talked
@ -483,13 +482,11 @@ class MucTab(ChatTab):
def handle_presence(self, presence: Presence) -> None: def handle_presence(self, presence: Presence) -> None:
"""Handle MUC presence""" """Handle MUC presence"""
self.reset_lag() self.reset_lag()
status_codes = set() status_codes = presence['muc']['status_codes']
for status_code in presence.xml.findall(STATUS_XPATH):
status_codes.add(status_code.attrib['code'])
if presence['type'] == 'error': if presence['type'] == 'error':
self.core.room_error(presence, self.jid.bare) self.core.room_error(presence, self.jid.bare)
elif not self.joined: elif not self.joined:
own = '110' in status_codes own = 110 in status_codes
if own or len(self.presence_buffer) >= 10: if own or len(self.presence_buffer) >= 10:
self.process_presence_buffer(presence, own) self.process_presence_buffer(presence, own)
else: else:
@ -550,12 +547,10 @@ class MucTab(ChatTab):
self.users.append(new_user) self.users.append(new_user)
self.core.events.trigger('muc_join', presence, self) self.core.events.trigger('muc_join', presence, self)
if own: if own:
status_codes = set() status_codes = presence['muc']['status_codes']
for status_code in presence.xml.findall(STATUS_XPATH):
status_codes.add(status_code.attrib['code'])
self.own_join(from_nick, new_user, status_codes) self.own_join(from_nick, new_user, status_codes)
def own_join(self, from_nick: str, new_user: User, status_codes: Set[str]) -> None: def own_join(self, from_nick: str, new_user: User, status_codes: Set[int]) -> None:
""" """
Handle the last presence we received, entering the room Handle the last presence we received, entering the room
""" """
@ -593,12 +588,12 @@ class MucTab(ChatTab):
} }
self.add_message(MucOwnJoinMessage(enable_message), typ=2) self.add_message(MucOwnJoinMessage(enable_message), typ=2)
self.core.enable_private_tabs(self.jid.bare, enable_message) self.core.enable_private_tabs(self.jid.bare, enable_message)
if '201' in status_codes: if 201 in status_codes:
self.add_message( self.add_message(
InfoMessage('Info: The room has been created'), InfoMessage('Info: The room has been created'),
typ=0 typ=0
) )
if '170' in status_codes: if 170 in status_codes:
self.add_message( self.add_message(
InfoMessage( InfoMessage(
'\x19%(warn_col)s}Warning:\x19%(info_col)s}' '\x19%(warn_col)s}Warning:\x19%(info_col)s}'
@ -608,7 +603,7 @@ class MucTab(ChatTab):
}, },
), ),
typ=0) typ=0)
if '100' in status_codes: if 100 in status_codes:
self.add_message( self.add_message(
InfoMessage( InfoMessage(
'\x19%(warn_col)s}Warning:\x19%(info_col)s}' '\x19%(warn_col)s}Warning:\x19%(info_col)s}'
@ -620,7 +615,7 @@ class MucTab(ChatTab):
typ=0) typ=0)
mam.schedule_tab_open(self) mam.schedule_tab_open(self)
def handle_presence_joined(self, presence: Presence, status_codes: Set[str]) -> None: def handle_presence_joined(self, presence: Presence, status_codes: Set[int]) -> None:
""" """
Handle new presences when we are already in the room Handle new presences when we are already in the room
""" """
@ -629,12 +624,12 @@ class MucTab(ChatTab):
return None return None
dissected_presence = dissect_presence(presence) dissected_presence = dissect_presence(presence)
from_nick, from_room, affiliation, show, status, role, jid, typ = dissected_presence from_nick, from_room, affiliation, show, status, role, jid, typ = dissected_presence
change_nick = '303' in status_codes change_nick = 303 in status_codes
kick = '307' in status_codes and typ == 'unavailable' kick = 307 in status_codes and typ == 'unavailable'
ban = '301' in status_codes and typ == 'unavailable' ban = 301 in status_codes and typ == 'unavailable'
shutdown = '332' in status_codes and typ == 'unavailable' shutdown = 332 in status_codes and typ == 'unavailable'
server_initiated = '333' in status_codes and typ == 'unavailable' server_initiated = 333 in status_codes and typ == 'unavailable'
non_member = '322' in status_codes and typ == 'unavailable' non_member = 322 in status_codes and typ == 'unavailable'
user = self.get_user_by_name(from_nick) user = self.get_user_by_name(from_nick)
# New user # New user
if not user and typ != "unavailable": if not user and typ != "unavailable":
@ -740,8 +735,7 @@ class MucTab(ChatTab):
self.core.on_user_rejoined_private_conversation(self.jid.bare, from_nick) self.core.on_user_rejoined_private_conversation(self.jid.bare, from_nick)
def on_user_nick_change(self, presence: Presence, user: User, from_nick: str) -> None: def on_user_nick_change(self, presence: Presence, user: User, from_nick: str) -> None:
new_nick = presence.xml.find( new_nick = presence['muc']['nick']
'{%s}x/{%s}item' % (NS_MUC_USER, NS_MUC_USER)).attrib['nick']
old_color = user.color old_color = user.color
if user.nick == self.own_nick: if user.nick == self.own_nick:
self.own_nick = new_nick self.own_nick = new_nick

View file

@ -13,7 +13,6 @@ the ConversationTab (such as tab-completion on nicks from the room).
import curses import curses
import logging import logging
from typing import Dict, Callable from typing import Dict, Callable
from xml.etree import cElementTree as ET
from slixmpp import JID from slixmpp import JID
@ -31,8 +30,6 @@ from poezio.ui.types import BaseMessage, Message, InfoMessage
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
class PrivateTab(OneToOneTab): class PrivateTab(OneToOneTab):
""" """
@ -156,8 +153,7 @@ class PrivateTab(OneToOneTab):
) )
msg['type'] = 'chat' msg['type'] = 'chat'
msg['body'] = line msg['body'] = line
x = ET.Element('{%s}x' % NS_MUC_USER) msg.enable('muc')
msg.append(x)
# trigger the event BEFORE looking for colors. # trigger the event BEFORE looking for colors.
# This lets a plugin insert \x19xxx} colors, that will # This lets a plugin insert \x19xxx} colors, that will
# be converted in xhtml. # be converted in xhtml.