From: Christian Heller Date: Wed, 18 Feb 2026 14:52:43 +0000 (+0100) Subject: Refuse to send messages to server above a certain length. X-Git-Url: https://plomlompom.com/repos//%22https:/validator.w3.org/check?a=commitdiff_plain;h=b2e3149c6e375a090d89c3b97f8529a2053e0901;p=ircplom Refuse to send messages to server above a certain length. --- diff --git a/src/ircplom/client.py b/src/ircplom/client.py index 5d0f3a0..b5b6343 100644 --- a/src/ircplom/client.py +++ b/src/ircplom/client.py @@ -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.' diff --git a/src/ircplom/client_tui.py b/src/ircplom/client_tui.py index c022cdc..56f2ca7 100644 --- a/src/ircplom/client_tui.py +++ b/src/ircplom/client_tui.py @@ -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) diff --git a/src/ircplom/irc_conn.py b/src/ircplom/irc_conn.py index 21a74e4..cd6ac78 100644 --- a/src/ircplom/irc_conn.py +++ b/src/ircplom/irc_conn.py @@ -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: diff --git a/src/ircplom/testing.py b/src/ircplom/testing.py index e855a12..097d4a7 100644 --- a/src/ircplom/testing.py +++ b/src/ircplom/testing.py @@ -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: