home · contact · privacy
Don't try to add to chat messages log when disconnected, unify send failures handling...
authorChristian Heller <c.heller@plomlompom.de>
Sat, 20 Sep 2025 06:23:11 +0000 (08:23 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 20 Sep 2025 06:23:11 +0000 (08:23 +0200)
ircplom/client.py
ircplom/client_tui.py
ircplom/testing.py
test.txt

index ba57891fba535e9aded97de83da65c8321c3bc8d..947945cbc216ba1fc64b074b7f4391884377b128 100644 (file)
@@ -20,7 +20,7 @@ from ircplom.msg_parse_expectations import MSG_EXPECTATIONS
 _NAMES_DESIRED_SERVER_CAPS = ('sasl',)
 
 
-class Alert(BaseException):
+class SendFail(BaseException):
     pass
 
 
@@ -325,13 +325,10 @@ class LogScope(Enum):
     'Where log messages should go.'
     ALL = auto()
     SERVER = auto()
-    #
     RAW = auto()
-    #
     CHAT = auto()
     USER = auto()
     USER_NO_CHANNELS = auto()
-    #
     SAME = auto()
 
 
@@ -829,13 +826,11 @@ class Client(ABC, ClientQueueMixin):
 
     def send(self, verb: str, *args) -> None:
         'Send msg over socket, on success log .raw.'
-        if self.conn:
-            msg = IrcMessage(verb, args)
-            self.conn.send(msg)
-            self._log(msg.raw, scope=LogScope.RAW, out=True)
-        else:
-            self._log('cannot send, connection seems closed', alert=True,
-                      scope=LogScope.SAME)
+        if not self.conn:
+            raise SendFail('cannot send, connection seems closed')
+        msg = IrcMessage(verb, args)
+        self.conn.send(msg)
+        self._log(msg.raw, scope=LogScope.RAW, out=True)
 
     def handle_msg(self, msg: IrcMessage) -> None:
         'Log msg.raw, then process incoming msg into appropriate client steps.'
index 468216c2ea807d0d30d2c111b19ae6dd2d0fb006..8ac8e190956c7e51714d0756291c952205491538 100644 (file)
@@ -7,8 +7,8 @@ from ircplom.tui_base import (BaseTui, PromptWidget, TuiEvent, Window,
                               CMD_SHORTCUTS)
 from ircplom.client import (
         AutoAttrMixin, Channel, Client, ClientQueueMixin, Dict, DictItem,
-        IrcConnSetup, LogScope, NewClientEvent, NickUserHost, ServerCapability,
-        SharedClientDbFields, User)
+        IrcConnSetup, LogScope, NewClientEvent, NickUserHost, SendFail,
+        ServerCapability, SharedClientDbFields, User)
 
 CMD_SHORTCUTS['disconnect'] = 'window.disconnect'
 CMD_SHORTCUTS['join'] = 'window.join'
@@ -474,13 +474,15 @@ class ClientKnowingTui(Client):
 
     def privmsg(self, target: str, msg: str) -> None:
         'Catch /privmsg, only allow for channel if in channel, else complain.'
-        if self.db.is_chan_name(target)\
-                and target not in self.db.channels.keys():
-            self._log('not sending, since not in channel',
-                      scope=LogScope.SAME, alert=True)
-            return
-        self.send('PRIVMSG', target, msg)
-        self._log(msg, scope=LogScope.CHAT, target=target, out=True)
+        try:
+            if self.db.is_chan_name(target)\
+                    and target not in self.db.channels.keys():
+                raise SendFail('not sending, since not in channel')
+            self.send('PRIVMSG', target, msg)
+        except SendFail as e:
+            self._log(f'{e}', scope=LogScope.SAME, alert=True)
+        else:
+            self._log(msg, scope=LogScope.CHAT, target=target, out=True)
 
     def reconnect(self) -> None:
         'Catch /reconnect, only initiate if not connected, else complain back.'
index 2f3a7bc16f88016a065d8f6f53d618890e849f4a..befa3dc500091ef157c593aaa419d1bd8fb7800e 100644 (file)
@@ -122,7 +122,6 @@ class TestingClientTui(ClientTui):
         assert expected_msg == msg_sans_time, info
         assert expected_win_ids == win_ids, info
         self._play_till_next_log()
-        print(win_ids, msg)
         return win_ids, logged_msg
 
     def _play_till_next_log(self) -> None:
index 602bc47e1af23aefd9dbf184a129298793efbb7f..3f4f9c0944d341409ff24f70fc8c3c2352b1cd69 100644 (file)
--- a/test.txt
+++ b/test.txt
 1,2 $ channels cleared
 1,2 $ users cleared
 
+# fail to send in disconnect, check alert window is command prompt window
+> /window 7
+> /privmsg barbar test
+7 !$ cannot send, connection seems closed
+> /window 1
+> /privmsg barbar test
+1 !$ cannot send, connection seems closed
+> /privmsg #test test
+1 !$ not sending, since not in channel
+
 # test setting up second client, but 432 irrecoverably
 > /connect baz.bar.foo ?foo foo:foo
 8,9 $ isupport cleared