home · contact · privacy
Refuse to send messages to server above a certain length.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 18 Feb 2026 14:52:43 +0000 (15:52 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 18 Feb 2026 14:52:43 +0000 (15:52 +0100)
src/ircplom/client.py
src/ircplom/client_tui.py
src/ircplom/irc_conn.py
src/ircplom/testing.py

index 5d0f3a0087125bfff623f74f1279293fbe8f4430..b5b6343897c99881c07026e3f20bd4a09e6fcd59 100644 (file)
@@ -19,7 +19,7 @@ from ircplom.events import (
         AffectiveEvent, CrashingException, ExceptionEvent, QueueMixin)
 from ircplom.irc_conn import (
         BaseIrcConnection, IrcConnException, IrcMessage, NickUserHost,
-        ERR_STR_TIMEOUT, ILLEGAL_NICK_CHARS, ILLEGAL_NICK_FIRSTCHARS,
+        SendFail, ERR_STR_TIMEOUT, ILLEGAL_NICK_CHARS, ILLEGAL_NICK_FIRSTCHARS,
         ISUPPORT_DEFAULTS, PORT_SSL)
 from ircplom.msg_parse_expectations import MSG_EXPECTATIONS
 
@@ -45,10 +45,6 @@ def _tuple_key_val_from_eq_str(eq_str: str) -> tuple[str, str]:
     return toks[0], ('' if len(toks) == 1 else toks[1])
 
 
-class SendFail(Exception):
-    'When Client.send fails.'
-
-
 class TargetUserOffline(Exception):
     'When according to server our target user is not to be found.'
 
index c022cdcb5cce0d85c65c79028c9b356e6bc6e5ae..56f2ca72ce57950c3020d9457c22c49763238f3c 100644 (file)
@@ -7,11 +7,11 @@ from typing import Any, Callable, Optional, Sequence
 # ourselves
 from ircplom.client import (
         Channel, ChatMessage, Client, ClientQueueMixin, ImplementationFail,
-        IrcConnSetup, NewClientEvent, SendFail, ServerCapability,
-        SharedClientDbFields, TargetUserOffline, User)
+        IrcConnSetup, NewClientEvent, ServerCapability, SharedClientDbFields,
+        TargetUserOffline, User)
 from ircplom.db_primitives import (
         AutoAttrMixin, DbLinked, DbLinking, Dict, DictItem)
-from ircplom.irc_conn import IrcMessage, NickUserHost
+from ircplom.irc_conn import IrcMessage, NickUserHost, SendFail
 from ircplom.tui_base import (
         BaseTui, PromptWidget, StylingString, TuiEvent, Window,
         CMD_SHORTCUTS, LOG_FMT_ATTRS, LOG_FMT_TAG_ALERT, LOG_PREFIX_DEFAULT)
index 21a74e494b70ef1df463695f9fb7e1f8136300fd..cd6ac7846a0723c4233c6841e0ab9d89c89d58eb 100644 (file)
@@ -31,6 +31,11 @@ _IRCSPEC_TAG_ESCAPES = ((r'\:', ';'),
                         (r'\n', '\n'),
                         (r'\r', '\r'),
                         (r'\\', '\\'))
+_LEN_MAX_MESSAGE = 512
+
+
+class SendFail(Exception):
+    'Failure to send message.'
 
 
 @dataclass
@@ -203,9 +208,15 @@ class BaseIrcConnection(QueueMixin, ABC):
         self._recv_loop.stop()
         self._socket.close()
 
+    def _make_sendable(self, msg: IrcMessage) -> bytes:
+        to_send = msg.raw.encode('utf-8') + _IRCSPEC_LINE_SEPARATOR
+        if len(to_send) > _LEN_MAX_MESSAGE:
+            raise SendFail('message too long')
+        return to_send
+
     def send(self, msg: IrcMessage) -> None:
         'Send line-separator-delimited message over socket.'
-        self._socket.sendall(msg.raw.encode('utf-8') + _IRCSPEC_LINE_SEPARATOR)
+        self._socket.sendall(self._make_sendable(msg))
 
     @abstractmethod
     def _make_recv_event(self, msg: IrcMessage) -> Event:
index e855a12eb17f6dce46cdb0d068562cc62a160148..097d4a7bb1f1a203a0ce2f85f13fdf6fd12f0633 100644 (file)
@@ -143,7 +143,7 @@ class _FakeIrcConnection(IrcConnection):
         self._recv_loop.stop()
 
     def send(self, msg: IrcMessage) -> None:
-        pass
+        self._make_sendable(msg)
 
     def _read_lines(self) -> Iterator[Optional[Event]]:
         try: