From: Christian Heller Date: Sat, 20 Sep 2025 06:23:11 +0000 (+0200) Subject: Don't try to add to chat messages log when disconnected, unify send failures handling... X-Git-Url: https://plomlompom.com/repos/booking/processes?a=commitdiff_plain;h=27f4ca2f705c9c9594857c594c873cfae3096ddb;p=ircplom Don't try to add to chat messages log when disconnected, unify send failures handling via SendFail. --- diff --git a/ircplom/client.py b/ircplom/client.py index ba57891..947945c 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -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.' diff --git a/ircplom/client_tui.py b/ircplom/client_tui.py index 468216c..8ac8e19 100644 --- a/ircplom/client_tui.py +++ b/ircplom/client_tui.py @@ -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.' diff --git a/ircplom/testing.py b/ircplom/testing.py index 2f3a7bc..befa3dc 100644 --- a/ircplom/testing.py +++ b/ircplom/testing.py @@ -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: diff --git a/test.txt b/test.txt index 602bc47..3f4f9c0 100644 --- a/test.txt +++ b/test.txt @@ -362,6 +362,16 @@ 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