diff options
-rw-r--r-- | poezio/core/core.py | 16 | ||||
-rw-r--r-- | poezio/core/handlers.py | 58 | ||||
-rw-r--r-- | poezio/logger.py | 19 | ||||
-rw-r--r-- | poezio/tabs/basetabs.py | 32 | ||||
-rw-r--r-- | poezio/tabs/conversationtab.py | 13 | ||||
-rw-r--r-- | poezio/tabs/muctab.py | 65 | ||||
-rw-r--r-- | poezio/tabs/privatetab.py | 43 | ||||
-rw-r--r-- | poezio/tabs/rostertab.py | 2 | ||||
-rw-r--r-- | poezio/ui/types.py | 16 |
9 files changed, 137 insertions, 127 deletions
diff --git a/poezio/core/core.py b/poezio/core/core.py index f41eb363..5ea8978f 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -71,7 +71,11 @@ from poezio.core.structs import ( POSSIBLE_SHOW, ) -from poezio.ui.types import Message, InfoMessage +from poezio.ui.types import ( + Message, + InfoMessage, + PersistentInfoMessage, +) log = logging.getLogger(__name__) @@ -1328,7 +1332,7 @@ class Core: """ tab = self.tabs.by_name_and_class(jid, tabs.ConversationTab) if tab is not None: - tab.add_message(InfoMessage(msg), typ=2) + tab.add_message(PersistentInfoMessage(msg)) if self.tabs.current_tab is tab: self.refresh_window() @@ -1743,12 +1747,11 @@ class Core: nickname='Error', nick_color=get_theme().COLOR_ERROR_MSG, ), - typ=2, ) code = error['error']['code'] if code == '401': msg = 'To provide a password in order to join the room, type "/join / password" (replace "password" by the real password)' - tab.add_message(InfoMessage(msg), typ=2) + tab.add_message(PersistentInfoMessage(msg)) if code == '409': if config.getstr('alternative_nickname') != '': if not tab.joined: @@ -1757,11 +1760,10 @@ class Core: else: if not tab.joined: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'You can join the room with another nick, ' 'by typing "/join /other_nick"' - ), - typ=2, + ) ) self.refresh_window() diff --git a/poezio/core/handlers.py b/poezio/core/handlers.py index 5fffbef8..55792cbd 100644 --- a/poezio/core/handlers.py +++ b/poezio/core/handlers.py @@ -38,7 +38,13 @@ from poezio.logger import logger from poezio.roster import roster from poezio.text_buffer import CorrectionError, AckError from poezio.theming import dump_tuple, get_theme -from poezio.ui.types import XMLLog, Message as PMessage, BaseMessage, InfoMessage +from poezio.ui.types import ( + XMLLog, + Message as PMessage, + BaseMessage, + InfoMessage, + PersistentInfoMessage, +) from poezio.core.commands import dumb_callback @@ -318,7 +324,7 @@ class HandlerCore: error = '\x19%s}%s\x19o' % (dump_tuple(get_theme().COLOR_CHAR_NACK), error_msg) if not tab.nack_message('\n' + error, message['id'], message['to']): - tab.add_message(InfoMessage(error), typ=0) + tab.add_message(InfoMessage(error)) self.core.refresh_window() def on_normal_message(self, message): @@ -421,8 +427,7 @@ class HandlerCore: history=delayed, identifier=message['id'], jid=jid, - ), - typ=1, + ) ) if not own and 'private' in config.getlist('beep_on'): @@ -627,7 +632,7 @@ class HandlerCore: identifier=message['id'], ) typ = 2 - tab.add_message(ui_msg, typ) + tab.add_message(ui_msg) if highlight: self.core.events.trigger('highlight', message, tab) @@ -723,8 +728,7 @@ class HandlerCore: user=user, identifier=message['id'], jid=message['from'], - ), - typ=1, + ) ) if sent: tab.set_last_sent_message(message, correct=replaced) @@ -1214,53 +1218,53 @@ class HandlerCore: if show_unavailable or hide_unavailable or non_priv or logging_off\ or non_anon or semi_anon or full_anon: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'Info: A configuration change not privacy-related occurred.' ), - typ=2) + ) modif = True if show_unavailable: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'Info: The unavailable members are now shown.' ), - typ=2) + ) elif hide_unavailable: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'Info: The unavailable members are now hidden.', ), - typ=2) + ) if non_anon: tab.add_message( - InfoMessage( + PersistentInfoMessage( '\x191}Warning:\x19%(info_col)s} The room is now not anonymous. (public JID)' % info_col ), - typ=2) + ) elif semi_anon: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'Info: The room is now semi-anonymous. (moderators-only JID)', ), - typ=2) + ) elif full_anon: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'Info: The room is now fully anonymous.', ), - typ=2) + ) if logging_on: tab.add_message( - InfoMessage( + PersistentInfoMessage( '\x191}Warning: \x19%(info_col)s}This room is publicly logged' % info_col ), - typ=2) + ) elif logging_off: tab.add_message( - InfoMessage( + PersistentInfoMessage( 'Info: This room is not logged anymore.', ), - typ=2) + ) if modif: self.core.refresh_window() @@ -1303,18 +1307,18 @@ class HandlerCore: if nick_from: tab.add_message( - InfoMessage( + PersistentInfoMessage( "%(user)s set the subject to: \x19%(text_col)s}%(subject)s" % fmt, time=time, ), - typ=2) + ) else: tab.add_message( - InfoMessage( + PersistentInfoMessage( "The subject is: \x19%(text_col)s}%(subject)s" % fmt, time=time, ), - typ=2) + ) tab.topic = subject tab.topic_from = nick_from if self.core.tabs.by_name_and_class( diff --git a/poezio/logger.py b/poezio/logger.py index b55e579d..a5916050 100644 --- a/poezio/logger.py +++ b/poezio/logger.py @@ -11,13 +11,14 @@ conversations and roster changes import mmap import re -from typing import List, Dict, Optional, IO, Any +from typing import List, Dict, Optional, IO, Any, Union from datetime import datetime from poezio import common from poezio.config import config from poezio.xhtml import clean_text from poezio.theming import dump_tuple, get_theme +from poezio.ui.types import Message, BaseMessage, LoggableTrait import logging @@ -134,10 +135,7 @@ class Logger: def log_message(self, jid: str, - nick: str, - msg: str, - date: Optional[datetime] = None, - typ: int = 1) -> bool: + msg: Union[BaseMessage, Message]) -> bool: """ log the message in the appropriate jid's file type: @@ -147,7 +145,16 @@ class Logger: """ if not config.get_by_tabname('use_log', jid): return True - logged_msg = build_log_message(nick, msg, date=date, typ=typ) + if not isinstance(msg, LoggableTrait): + return True + date = msg.time + txt = msg.txt + typ = 2 + nick = '' + if isinstance(msg, Message): + nick = msg.nickname or '' + typ = 1 + logged_msg = build_log_message(nick, txt, date=date, typ=typ) if not logged_msg: return True jid = str(jid).replace('/', '\\') diff --git a/poezio/tabs/basetabs.py b/poezio/tabs/basetabs.py index 75d8bf71..2f221afe 100644 --- a/poezio/tabs/basetabs.py +++ b/poezio/tabs/basetabs.py @@ -49,8 +49,13 @@ from poezio.text_buffer import TextBuffer from poezio.theming import get_theme, dump_tuple from poezio.user import User from poezio.ui.funcs import truncate_nick -from poezio.ui.types import BaseMessage, InfoMessage, Message from poezio.timed_events import DelayedEvent +from poezio.ui.types import ( + BaseMessage, + Message, + PersistentInfoMessage, + LoggableTrait, +) from slixmpp import JID, InvalidJID, Message as SMessage @@ -581,6 +586,11 @@ class ChatTab(Tab): raise TypeError("Name %r must be of type JID or str." % value) @property + def log_name(self) -> str: + """Name used for the log filename""" + return self.jid.bare + + @property def jid(self) -> JID: return copy(self._jid) @@ -595,22 +605,17 @@ class ChatTab(Tab): def general_jid(self) -> JID: raise NotImplementedError - def log_message(self, message: BaseMessage, typ: int = 1): + def log_message(self, message: BaseMessage): """ Log the messages in the archives. """ - name = self.jid.bare - if not isinstance(message, Message): + if not isinstance(message, LoggableTrait): return - was_logged = logger.log_message( - name, message.nickname or '', message.txt, - date=message.time, typ=typ - ) - if not was_logged: + if not logger.log_message(self.log_name, message): self.core.information('Unable to write in the log file', 'Error') - def add_message(self, message: BaseMessage, typ=1): - self.log_message(message, typ=typ) + def add_message(self, message: BaseMessage): + self.log_message(message) self._text_buffer.add_message(message) def modify_message(self, @@ -627,7 +632,7 @@ class ChatTab(Tab): txt, old_id, new_id, user=user, jid=jid, time=time ) if message: - self.log_message(message, typ=1) + self.log_message(message) self.text_win.modify_message(message.identifier, message) self.core.refresh_window() return True @@ -1025,8 +1030,7 @@ class OneToOneTab(ChatTab): if status.show in SHOW_NAME: msg += 'show: %s, ' % SHOW_NAME[status.show] self.add_message( - InfoMessage(txt=msg[:-2]), - typ=2, + PersistentInfoMessage(txt=msg[:-2]) ) def ack_message(self, msg_id: str, msg_jid: JID): diff --git a/poezio/tabs/conversationtab.py b/poezio/tabs/conversationtab.py index fc89146e..8f608e84 100644 --- a/poezio/tabs/conversationtab.py +++ b/poezio/tabs/conversationtab.py @@ -178,7 +178,7 @@ class ConversationTab(OneToOneTab): (' and their last status was %s' % status) if status else '', ) - self.add_message(InfoMessage(msg), typ=0) + self.add_message(InfoMessage(msg)) self.core.refresh_window() self.core.xmpp.plugin['xep_0012'].get_last_activity( @@ -206,15 +206,12 @@ class ConversationTab(OneToOneTab): 'status': status, } ), - typ=0, - ) - return True - else: - self.add_message( - InfoMessage("No information available"), - typ=0, ) return True + self.add_message( + InfoMessage("No information available"), + ) + return True @command_args_parser.quoted(0, 1) async def command_version(self, args): diff --git a/poezio/tabs/muctab.py b/poezio/tabs/muctab.py index 4fbde32e..606506b6 100644 --- a/poezio/tabs/muctab.py +++ b/poezio/tabs/muctab.py @@ -29,6 +29,7 @@ from typing import ( Tuple, Union, Set, + Type, Pattern, TYPE_CHECKING, ) @@ -58,6 +59,8 @@ from poezio.ui.types import ( Message, MucOwnJoinMessage, MucOwnLeaveMessage, + StatusMessage, + PersistentInfoMessage, ) if TYPE_CHECKING: @@ -220,7 +223,7 @@ class MucTab(ChatTab): 'color_spec': spec_col, 'nick': self.own_nick, } - self.add_message(MucOwnLeaveMessage(msg), typ=2) + self.add_message(MucOwnLeaveMessage(msg)) self.disconnect() muc.leave_groupchat(self.core.xmpp, self.jid.bare, self.own_nick, message) @@ -343,7 +346,7 @@ class MucTab(ChatTab): 'role': user.role or 'None', 'status': '\n%s' % user.status if user.status else '' } - self.add_message(InfoMessage(info), typ=0) + self.add_message(InfoMessage(info)) return True def change_topic(self, topic: str) -> None: @@ -374,7 +377,6 @@ class MucTab(ChatTab): "The subject of the room is: \x19%s}%s %s" % (norm_text, self.topic, user_string), ), - typ=0, ) @refresh_wrapper.always @@ -576,12 +578,11 @@ class MucTab(ChatTab): 'nick_col': color, 'info_col': info_col, } - self.add_message(MucOwnJoinMessage(enable_message), typ=2) + self.add_message(MucOwnJoinMessage(enable_message)) self.core.enable_private_tabs(self.jid.bare, enable_message) if 201 in status_codes: self.add_message( - InfoMessage('Info: The room has been created'), - typ=0 + PersistentInfoMessage('Info: The room has been created'), ) if 170 in status_codes: self.add_message( @@ -590,9 +591,9 @@ class MucTab(ChatTab): ' This room is publicly logged' % { 'info_col': info_col, 'warn_col': warn_col - }, + } ), - typ=0) + ) if 100 in status_codes: self.add_message( InfoMessage( @@ -602,7 +603,7 @@ class MucTab(ChatTab): 'warn_col': warn_col }, ), - typ=0) + ) mam.schedule_tab_open(self) def handle_presence_joined(self, presence: Presence, status_codes: Set[int]) -> None: @@ -664,8 +665,8 @@ class MucTab(ChatTab): MucOwnLeaveMessage( 'You have been kicked because you ' 'are not a member and the room is now members-only.' - ), - typ=2) + ) + ) self.disconnect() def on_muc_shutdown(self) -> None: @@ -674,8 +675,8 @@ class MucTab(ChatTab): MucOwnLeaveMessage( 'You have been kicked because the' ' MUC service is shutting down.' - ), - typ=2) + ) + ) self.disconnect() def on_user_join(self, from_nick: str, affiliation: str, show: str, status: str, role: str, jid: JID, @@ -719,7 +720,7 @@ class MucTab(ChatTab): 'jid_color': dump_tuple(theme.COLOR_MUC_JID), 'color_spec': spec_col, } - self.add_message(InfoMessage(msg), typ=2) + self.add_message(PersistentInfoMessage(msg)) 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: @@ -747,7 +748,7 @@ class MucTab(ChatTab): old_color = color = "3" info_col = dump_tuple(get_theme().COLOR_INFORMATION_TEXT) self.add_message( - InfoMessage( + PersistentInfoMessage( '\x19%(old_color)s}%(old)s\x19%(info_col)s} is' ' now known as \x19%(color)s}%(new)s' % { 'old': from_nick, @@ -756,8 +757,8 @@ class MucTab(ChatTab): 'old_color': old_color, 'info_col': info_col }, - ), - typ=2) + ) + ) # rename the private tabs if needed self.core.rename_private_tabs(self.jid.bare, from_nick, user) @@ -765,7 +766,7 @@ class MucTab(ChatTab): """ When someone is banned from a muc """ - cls = InfoMessage + cls: Type[InfoMessage] = PersistentInfoMessage self.users.remove(user) by = presence.xml.find('{%s}x/{%s}item/{%s}actor' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER)) @@ -842,13 +843,13 @@ class MucTab(ChatTab): 'reason': reason.text, 'info_col': info_col } - self.add_message(cls(kick_msg), typ=2) + self.add_message(cls(kick_msg)) def on_user_kicked(self, presence: Presence, user: User, from_nick: str) -> None: """ When someone is kicked from a muc """ - cls = InfoMessage + cls: Type[InfoMessage] = PersistentInfoMessage self.users.remove(user) actor_elem = presence.xml.find('{%s}x/{%s}item/{%s}actor' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER)) @@ -922,7 +923,7 @@ class MucTab(ChatTab): 'reason': reason.text, 'info_col': info_col } - self.add_message(cls(kick_msg), typ=2) + self.add_message(cls(kick_msg)) def on_user_leave_groupchat(self, user: User, @@ -987,7 +988,7 @@ class MucTab(ChatTab): } if status: leave_msg += ' (\x19o%s\x19%s})' % (status, info_col) - self.add_message(InfoMessage(leave_msg), typ=2) + self.add_message(PersistentInfoMessage(leave_msg)) self.core.on_user_left_private_conversation(from_room, user, status) def on_user_change_status(self, user: User, from_nick: str, from_room: str, affiliation: str, @@ -1072,18 +1073,6 @@ class MucTab(ChatTab): """ return self.topic.replace('\n', '|') - def log_message(self, msg: BaseMessage, typ: int = 1) -> None: - """ - Log the messages in the archives, if it needs - to be - """ - if not isinstance(msg, Message): - return - if not msg.history and self.joined and msg.nickname and msg.txt: # don't log the history messages - if not logger.log_message(self.jid.bare, msg.nickname, msg.txt, typ=typ): - self.core.information('Unable to write in the log file', - 'Error') - def get_user_by_name(self, nick: str) -> Optional[User]: """ Gets the user associated with the given nick, or None if not found @@ -1093,12 +1082,12 @@ class MucTab(ChatTab): return user return None - def add_message(self, msg: BaseMessage, typ: int = 1) -> None: + def add_message(self, msg: BaseMessage) -> None: """Add a message to the text buffer and set various tab status""" # reset self-ping interval if self.self_ping_event: self.enable_self_ping_event() - super().add_message(msg, typ=typ) + super().add_message(msg) if not isinstance(msg, Message): return if msg.user: @@ -1130,7 +1119,7 @@ class MucTab(ChatTab): user=user, jid=jid) if message: - self.log_message(message, typ=1) + self.log_message(message) self.text_win.modify_message(message.identifier, message) return highlight return False @@ -1585,7 +1574,7 @@ class MucTab(ChatTab): buff.append('\n') message = ' '.join(buff) - self.add_message(InfoMessage(message), typ=0) + self.add_message(InfoMessage(message)) self.text_win.refresh() self.input.refresh() diff --git a/poezio/tabs/privatetab.py b/poezio/tabs/privatetab.py index a85518af..526a7570 100644 --- a/poezio/tabs/privatetab.py +++ b/poezio/tabs/privatetab.py @@ -26,7 +26,12 @@ from poezio.decorators import refresh_wrapper from poezio.logger import logger from poezio.theming import get_theme, dump_tuple from poezio.decorators import command_args_parser -from poezio.ui.types import BaseMessage, Message, InfoMessage +from poezio.ui.types import ( + BaseMessage, + InfoMessage, + Message, + PersistentInfoMessage, +) log = logging.getLogger(__name__) @@ -66,6 +71,11 @@ class PrivateTab(OneToOneTab): self.update_commands() self.update_keys() + @property + def log_name(self) -> str: + """Overriden from ChatTab because this is a case where we want the full JID""" + return self.jid.full + def remote_user_color(self): user = self.parent_muc.get_user_by_name(self.jid.resource) if user: @@ -102,16 +112,6 @@ class PrivateTab(OneToOneTab): def remove_information_element(plugin_name): del PrivateTab.additional_information[plugin_name] - def log_message(self, msg: BaseMessage, typ=1): - """ - Log the messages in the archives. - """ - if not isinstance(msg, Message): - return - if not logger.log_message( - self.jid.full, msg.nickname or '', msg.txt or '', date=msg.time, typ=typ): - self.core.information('Unable to write in the log file', 'Error') - def on_close(self): super().on_close() self.parent_muc.privates.remove(self) @@ -306,7 +306,7 @@ class PrivateTab(OneToOneTab): display a message. """ self.add_message( - InfoMessage( + PersistentInfoMessage( '\x19%(nick_col)s}%(old)s\x19%(info_col)s} is now ' 'known as \x19%(nick_col)s}%(new)s' % { 'old': old_nick, @@ -315,7 +315,7 @@ class PrivateTab(OneToOneTab): 'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT) }, ), - typ=2) + ) new_jid = self.jid.bare + '/' + user.nick self.name = new_jid return self.core.tabs.current_tab is self @@ -335,7 +335,7 @@ class PrivateTab(OneToOneTab): if not status_message: self.add_message( - InfoMessage( + PersistentInfoMessage( '\x19%(quit_col)s}%(spec)s \x19%(nick_col)s}' '%(nick)s\x19%(info_col)s} has left the room' % { 'nick': user.nick, @@ -345,10 +345,10 @@ class PrivateTab(OneToOneTab): 'info_col': dump_tuple(theme.COLOR_INFORMATION_TEXT) }, ), - typ=2) + ) else: self.add_message( - InfoMessage( + PersistentInfoMessage( '\x19%(quit_col)s}%(spec)s \x19%(nick_col)s}' '%(nick)s\x19%(info_col)s} has left the room' ' (%(status)s)' % { @@ -360,7 +360,7 @@ class PrivateTab(OneToOneTab): 'info_col': dump_tuple(theme.COLOR_INFORMATION_TEXT) }, ), - typ=2) + ) return self.core.tabs.current_tab is self @refresh_wrapper.conditional @@ -378,7 +378,7 @@ class PrivateTab(OneToOneTab): if user: color = dump_tuple(user.color) self.add_message( - InfoMessage( + PersistentInfoMessage( '\x19%(join_col)s}%(spec)s \x19%(color)s}%(nick)s\x19' '%(info_col)s} joined the room' % { 'nick': nick, @@ -388,18 +388,18 @@ class PrivateTab(OneToOneTab): 'info_col': dump_tuple(theme.COLOR_INFORMATION_TEXT) }, ), - typ=2) + ) return self.core.tabs.current_tab is self def activate(self, reason=None): self.on = True if reason: - self.add_message(InfoMessage(reason), typ=2) + self.add_message(PersistentInfoMessage(reason)) def deactivate(self, reason=None): self.on = False if reason: - self.add_message(InfoMessage(reason), typ=2) + self.add_message(PersistentInfoMessage(reason)) def matching_names(self): return [(3, self.jid.resource), (4, self.name)] @@ -415,6 +415,5 @@ class PrivateTab(OneToOneTab): nickname='Error', nick_color=theme.COLOR_ERROR_MSG, ), - typ=2, ) self.core.refresh_window() diff --git a/poezio/tabs/rostertab.py b/poezio/tabs/rostertab.py index 4aa68fd5..5556cf77 100644 --- a/poezio/tabs/rostertab.py +++ b/poezio/tabs/rostertab.py @@ -364,7 +364,7 @@ class RosterInfoTab(Tab): 'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT), 'jid': message['from'], } - tab.add_message(InfoMessage(message), typ=0) + tab.add_message(InfoMessage(message)) @command_args_parser.ignored async def command_list_blocks(self): diff --git a/poezio/ui/types.py b/poezio/ui/types.py index 8c049525..07d395ab 100644 --- a/poezio/ui/types.py +++ b/poezio/ui/types.py @@ -7,7 +7,6 @@ from poezio import poopt from poezio.user import User from poezio.theming import dump_tuple, get_theme - class BaseMessage: __slots__ = ('txt', 'time', 'identifier') @@ -34,11 +33,20 @@ class InfoMessage(BaseMessage): super().__init__(txt=txt, identifier=identifier, time=time) -class MucOwnLeaveMessage(InfoMessage): +class LoggableTrait: + """Trait for classes of messages that should go through the logger""" + pass + + +class PersistentInfoMessage(InfoMessage, LoggableTrait): + pass + + +class MucOwnLeaveMessage(InfoMessage, LoggableTrait): """Status message displayed on our room leave/kick/ban""" -class MucOwnJoinMessage(InfoMessage): +class MucOwnJoinMessage(InfoMessage, LoggableTrait): """Status message displayed on our room join""" @@ -93,7 +101,7 @@ class StatusMessage(BaseMessage): self.txt = self.format_string.format(**real_args) -class Message(BaseMessage): +class Message(BaseMessage, LoggableTrait): __slots__ = ('nick_color', 'nickname', 'user', 'delayed', 'history', 'top', 'highlight', 'me', 'old_message', 'revisions', 'jid', 'ack') |