diff --git a/src/IRCLine.py b/src/IRCLine.py index 71fc0512..88c1cfe4 100644 --- a/src/IRCLine.py +++ b/src/IRCLine.py @@ -2,9 +2,11 @@ import datetime, typing from src import IRCObject class Line(IRCObject.Object): - def __init__(self, send_time: datetime.datetime, data: bytes): + def __init__(self, send_time: datetime.datetime, data: bytes, + truncated: str): self.send_time = send_time self.data = data + self.truncated = truncated self._on_send = [] # type: typing.List[typing.Callable[[], None]] diff --git a/src/IRCServer.py b/src/IRCServer.py index 6ace0098..0b9f694c 100644 --- a/src/IRCServer.py +++ b/src/IRCServer.py @@ -315,10 +315,11 @@ class Server(IRCObject.Object): break line_stripped = line.split("\n", 1)[0].strip("\r") - encoded = utils.encode_truncate(line_stripped, "utf8", LINE_CUTOFF) + encoded, truncated = utils.encode_truncate( + line_stripped, "utf8", LINE_CUTOFF) encoded = b"%s\r\n" % encoded - line_obj = IRCLine.Line(datetime.datetime.utcnow(), encoded) + line_obj = IRCLine.Line(datetime.datetime.utcnow(), encoded, truncated) self.queued_lines.append(line_obj) self.bot.log.debug("%s (raw send) | %s", [str(self), line]) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 24b3d71d..738541d5 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -190,12 +190,15 @@ def is_ip(s: str) -> bool: return False return True -def encode_truncate(s: str, encoding: str, byte_max: int) -> bytes: +def encode_truncate(s: str, encoding: str, byte_max: int + ) -> typing.Tuple[bytes, str]: encoded = b"" - for character in s: + truncated = "" + for i, character in enumerate(s): encoded_character = character.encode(encoding) if len(encoded + encoded_character) > byte_max: + truncated = s[i:] break else: encoded += encoded_character - return encoded + return encoded, truncated