home · contact · privacy
Avoid attempts to message into channels we're not in.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 19 Aug 2025 01:45:04 +0000 (03:45 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 19 Aug 2025 01:45:04 +0000 (03:45 +0200)
ircplom/client.py
ircplom/client_tui.py

index f3cdc94811cbb83a1c333b868fb35e92f9bd0faf..0c2a923106becfb2a784286338cc4d3a305e900c 100644 (file)
@@ -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 = ''
index b032106ece260c4f7989cfae14d9316e6c9cb3f1..3f40fbf7851173ff043bc366c571d75a243d49a4 100644 (file)
@@ -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: