From 2bb611249671472a1146a3a79c3d95bcfe6ea781 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 19 Aug 2025 03:45:04 +0200 Subject: [PATCH] Avoid attempts to message into channels we're not in. --- ircplom/client.py | 11 +++++++---- ircplom/client_tui.py | 14 +++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ircplom/client.py b/ircplom/client.py index f3cdc94..0c2a923 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -439,15 +439,16 @@ class _ClientDb(_Db, SharedClientDbFields): 'Reply if attribute of key may reasonably be addressed without an arg.' return not isinstance(getattr(self, key), (bool, int, str, tuple)) + @property + def chan_names(self) -> tuple[str, ...]: + 'Return names of joined channels.' + return tuple(self._channels.keys()) + def del_chan(self, name: str) -> None: 'Remove DB for channel of name.' del self._channels[name] self._on_update(name) - def has_chan(self, name: str) -> bool: - 'Test if entry of name in channels dictionary.' - return name in self._channels - def chan(self, name: str) -> _ChannelDb: 'Produce DB for channel of name – pre-existing, or newly created.' if name not in self._channels: @@ -552,6 +553,8 @@ class Client(ABC, ClientQueueMixin): if self.conn: self.conn.close() self.conn = None + for name in self._db.chan_names: + self._db.del_chan(name) self._db.isupports.clear() self._db.nickname = '' self._db.sasl_auth_state = '' diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index b032106..3f40fbf 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -58,7 +58,7 @@ class _ClientWindow(Window, ClientQueueMixin): def cmd__privmsg(self, target: str, msg: str) -> None: 'Send chat message msg to target.' - self._send_msg('PRIVMSG', (target, msg), log_target=target, to_log=msg) + self._client_trigger('privmsg', target=target, msg=msg) def cmd__raw(self, verb: str, params_str: str = '') -> None: 'Send raw command, with direct input of params string.' @@ -333,6 +333,15 @@ class _ClientKnowingTui(Client): self._put(TuiEvent.affector('for_client_do').kw( client_id=self.client_id, todo=todo, **kwargs)) + def privmsg(self, target: str, msg: str) -> None: + 'Catch /privmsg, only allow for channel if in channel, else complain.' + if target[0] == '#' and target not in self._db.chan_names: + self._log('not sending, since not in channel', + scope=LogScope.SAME, alert=True) + return + self.send(IrcMessage('PRIVMSG', (target, msg)), + log_target=target, to_log=msg) + def reconnect(self) -> None: 'Catch /reconnect, only initiate if not connected, else complain back.' if self.conn: @@ -356,8 +365,7 @@ class _ClientKnowingTui(Client): is_chan = path[0] == '#' display = '' if arg: - if is_chan and self._db.has_chan(path): - is_chan = True + if is_chan and path in self._db.chan_names: if (chan := self._db.chan(path)) and hasattr(chan, arg): value = getattr(chan, arg) else: -- 2.30.2