From d693479d05f1ada999c0e29a9d9e2f021dd59e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 7 Apr 2019 22:14:13 +0100 Subject: safeJID: Remove some safeJID calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- poezio/core/commands.py | 3 ++- poezio/core/core.py | 14 ++++++++++++-- poezio/tabs/basetabs.py | 12 ++++++++++-- poezio/tabs/muctab.py | 5 ++--- poezio/tabs/privatetab.py | 28 ++++++++++++---------------- poezio/text_buffer.py | 2 +- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/poezio/core/commands.py b/poezio/core/commands.py index e785a93b..f7946b71 100644 --- a/poezio/core/commands.py +++ b/poezio/core/commands.py @@ -369,7 +369,8 @@ class CommandCore: # New tab if tab is None: tab = self.core.open_new_room(room, nick, password=password) - tab.join() + if tab is not None: + tab.join() else: self.core.focus_tab(tab) if tab.own_nick == nick and tab.joined: diff --git a/poezio/core/core.py b/poezio/core/core.py index 7553c57d..91eb4877 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -1179,7 +1179,12 @@ class Core: """ Open a Private conversation in a MUC and focus if needed. """ - complete_jid = room_name + '/' + user_nick + try: + complete_jid_str = room_name + '/' + user_nick + complete_jid = JID(complete_jid_str) + except InvalidJID: + self.information('Invalid XMPP address %r for chat tab.', complete_jid_str) + return None # if the room exists, focus it and return for tab in self.get_tabs(tabs.PrivateTab): if tab.name == complete_jid: @@ -1205,10 +1210,15 @@ class Core: nick: str, *, password: Optional[str] = None, - focus=True) -> tabs.MucTab: + focus=True) -> Optional[tabs.MucTab]: """ Open a new tab.MucTab containing a muc Room, using the specified nick """ + try: + room = JID(room) + except InvalidJID: + self.information('Invalid XMPP address %r for chat tab.', room) + return None new_tab = tabs.MucTab(self, room, nick, password=password) self.add_tab(new_tab, focus) self.refresh_window() diff --git a/poezio/tabs/basetabs.py b/poezio/tabs/basetabs.py index 213f39d7..42a33330 100644 --- a/poezio/tabs/basetabs.py +++ b/poezio/tabs/basetabs.py @@ -20,7 +20,7 @@ from datetime import datetime from xml.etree import cElementTree as ET from typing import Any, Callable, Dict, List, Optional -from slixmpp import JID, Message +from slixmpp import JID, InvalidJID, Message from poezio.core.structs import Command, Completion, Status from poezio import timed_events @@ -462,8 +462,16 @@ class ChatTab(Tab): plugin_keys = {} # type: Dict[str, Callable] message_type = 'chat' - def __init__(self, core, jid=''): + def __init__(self, core, jid: JID = None): Tab.__init__(self, core) + if jid is not None and not isinstance(jid, JID): + # XXX: Remove logging once we're more or less sure we've switched + # all calls. + log.debug('ChatTab.name: %r: Not a JID object.', jid, exc_info=True) + try: + jid = JID(jid) + except InvalidJID: + log.debug('ChatTab.name: invalid JID.') self.name = jid self.text_win = None self.directed_presence = None diff --git a/poezio/tabs/muctab.py b/poezio/tabs/muctab.py index 17abb369..1a9217b3 100644 --- a/poezio/tabs/muctab.py +++ b/poezio/tabs/muctab.py @@ -55,7 +55,7 @@ class MucTab(ChatTab): additional_information = {} # type: Dict[str, Callable[[str], str]] lagged = False - def __init__(self, core, jid, nick, password=None): + def __init__(self, core, jid: JID, nick: str, password: str = None): ChatTab.__init__(self, core, jid) self.joined = False self._state = 'disconnected' @@ -63,7 +63,6 @@ class MucTab(ChatTab): self.own_nick = nick # self User object self.own_user = None # type: Optional[User] - self.name = jid self.password = password # buffered presences self.presence_buffer = [] @@ -1482,7 +1481,7 @@ class MucTab(ChatTab): r = None for user in self.users: if user.nick == nick: - r = self.core.open_private_window(self.name, user.nick) + r = self.core.open_private_window(str(self.name), user.nick) if r and len(args) == 2: msg = args[1] self.core.tabs.current_tab.command_say( diff --git a/poezio/tabs/privatetab.py b/poezio/tabs/privatetab.py index cec68ac5..ae34527a 100644 --- a/poezio/tabs/privatetab.py +++ b/poezio/tabs/privatetab.py @@ -20,7 +20,6 @@ from poezio.tabs import OneToOneTab, MucTab, Tab from poezio import windows from poezio import xhtml -from poezio.common import safeJID from poezio.config import config from poezio.core.structs import Command from poezio.decorators import refresh_wrapper @@ -40,10 +39,9 @@ class PrivateTab(OneToOneTab): message_type = 'chat' additional_information = {} # type: Dict[str, Callable[[str], str]] - def __init__(self, core, name, nick): + def __init__(self, core, name: JID, nick: str): OneToOneTab.__init__(self, core, name) self.own_nick = nick - self.name = name self.text_win = windows.TextWin() self._text_buffer.add_window(self.text_win) self.info_header = windows.PrivateInfoWin() @@ -65,13 +63,13 @@ class PrivateTab(OneToOneTab): shortdesc='Get the software version of a jid.') self.resize() self.parent_muc = self.core.tabs.by_name_and_class( - safeJID(name).bare, MucTab) + self.name.bare, MucTab) self.on = True self.update_commands() self.update_keys() def remote_user_color(self): - user = self.parent_muc.get_user_by_name(safeJID(self.name).resource) + user = self.parent_muc.get_user_by_name(self.name.resource) if user: return dump_tuple(user.color) return super().remote_user_color() @@ -105,9 +103,7 @@ class PrivateTab(OneToOneTab): del PrivateTab.additional_information[plugin_name] def load_logs(self, log_nb): - logs = logger.get_logs( - safeJID(self.name).full.replace('/', '\\'), log_nb) - return logs + return logger.get_logs(self.name.full.replace('/', '\\'), log_nb) def log_message(self, txt, nickname, time=None, typ=1): """ @@ -221,7 +217,7 @@ class PrivateTab(OneToOneTab): """ if args: return self.core.command.version(args[0]) - jid = safeJID(self.name) + jid = self.name self.core.xmpp.plugin['xep_0092'].get_version( jid, callback=self.core.handler.on_version_result) @@ -233,7 +229,7 @@ class PrivateTab(OneToOneTab): if arg and arg[0]: self.parent_muc.command_info(arg[0]) else: - user = safeJID(self.name).resource + user = self.name.resource self.parent_muc.command_info(user) def resize(self): @@ -276,7 +272,7 @@ class PrivateTab(OneToOneTab): self.input.refresh() def get_nick(self): - return safeJID(self.name).resource + return self.name.resource def on_input(self, key, raw): if not raw and key in self.key_func: @@ -288,7 +284,7 @@ class PrivateTab(OneToOneTab): empty_after = self.input.get_text() == '' or ( self.input.get_text().startswith('/') and not self.input.get_text().startswith('//')) - tab = self.core.tabs.by_name_and_class(safeJID(self.name).bare, MucTab) + tab = self.core.tabs.by_name_and_class(self.name.bare, MucTab) if tab and tab.joined: self.send_composing_chat_state(empty_after) return False @@ -301,7 +297,7 @@ class PrivateTab(OneToOneTab): self.text_win.remove_line_separator() self.text_win.add_line_separator(self._text_buffer) - tab = self.core.tabs.by_name_and_class(safeJID(self.name).bare, MucTab) + tab = self.core.tabs.by_name_and_class(self.name.bare, MucTab) if tab and tab.joined and config.get_by_tabname( 'send_chat_states', self.general_jid) and self.on: self.send_chat_state('inactive') @@ -310,7 +306,7 @@ class PrivateTab(OneToOneTab): def on_gain_focus(self): self.state = 'current' curses.curs_set(1) - tab = self.core.tabs.by_name_and_class(safeJID(self.name).bare, MucTab) + tab = self.core.tabs.by_name_and_class(self.name.bare, MucTab) if tab and tab.joined and config.get_by_tabname( 'send_chat_states', self.general_jid, @@ -345,7 +341,7 @@ class PrivateTab(OneToOneTab): 'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT) }, typ=2) - new_jid = safeJID(self.name).bare + '/' + user.nick + new_jid = self.name.bare + '/' + user.nick self.name = new_jid return self.core.tabs.current_tab is self @@ -426,7 +422,7 @@ class PrivateTab(OneToOneTab): self.add_message(txt=reason, typ=2) def matching_names(self): - return [(3, safeJID(self.name).resource), (4, self.name)] + return [(3, self.name.resource), (4, self.name)] def add_error(self, error_message): theme = get_theme() diff --git a/poezio/text_buffer.py b/poezio/text_buffer.py index f942c48e..e3961f7f 100644 --- a/poezio/text_buffer.py +++ b/poezio/text_buffer.py @@ -204,7 +204,7 @@ class TextBuffer: """Mark a message as errored""" return self._edit_ack(-1, old_id, jid, append=error) - def _edit_ack(self, value: int, old_id: str, jid: str, + def _edit_ack(self, value: int, old_id: str, jid: JID, append: str = '') -> Union[None, bool, Message]: """ Edit the ack status of a message, and optionally -- cgit v1.2.3